| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.Dimension; |
| 5 |
import javax.swing.JList; |
| 6 |
import javax.swing.JPanel; |
| 7 |
import javax.swing.JScrollPane; |
| 8 |
import javax.swing.JSplitPane; |
| 9 |
import javax.swing.ListModel; |
| 10 |
import javax.swing.ListSelectionModel; |
| 11 |
import javax.swing.event.ListDataEvent; |
| 12 |
import javax.swing.event.ListDataListener; |
| 13 |
import javax.swing.event.ListSelectionEvent; |
| 14 |
import javax.swing.event.ListSelectionListener; |
| 15 |
import org.astrogrid.samp.Client; |
| 16 |
import org.astrogrid.samp.Metadata; |
| 17 |
|
| 18 |
/** |
| 19 |
* Component displaying a list of SAMP {@link org.astrogrid.samp.Client}s, |
| 20 |
* usually those registered with a hub. |
| 21 |
* |
| 22 |
* @author Mark Taylor |
| 23 |
* @since 16 Jul 2008 |
| 24 |
*/ |
| 25 |
public class HubView extends JPanel { |
| 26 |
|
| 27 |
private final JList jlist_; |
| 28 |
private final ClientPanel clientPanel_; |
| 29 |
private final ListDataListener listListener_; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* @param hubLike true if this will be displaying clients implementing |
| 35 |
* the HubClient interface |
| 36 |
*/ |
| 37 |
public HubView( boolean hubLike ) { |
| 38 |
super( new BorderLayout() ); |
| 39 |
|
| 40 |
// Set up a JList to display the list of clients. |
| 41 |
// If a selection is made, update the client detail panel. |
| 42 |
jlist_ = new JList(); |
| 43 |
jlist_.setCellRenderer( new ClientListCellRenderer() ); |
| 44 |
ListSelectionModel selModel = jlist_.getSelectionModel(); |
| 45 |
selModel.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); |
| 46 |
selModel.addListSelectionListener( new ListSelectionListener() { |
| 47 |
public void valueChanged( ListSelectionEvent evt ) { |
| 48 |
if ( ! evt.getValueIsAdjusting() ) { |
| 49 |
updateClientView(); |
| 50 |
} |
| 51 |
} |
| 52 |
} ); |
| 53 |
|
| 54 |
// Watch the list; if any change occurs which may affect the currently- |
| 55 |
// selected client, update the client detail panel. |
| 56 |
listListener_ = new ListDataListener() { |
| 57 |
public void contentsChanged( ListDataEvent evt ) { |
| 58 |
preferSelection(); |
| 59 |
int isel = jlist_.getSelectedIndex(); |
| 60 |
int i0 = evt.getIndex0(); |
| 61 |
int i1 = evt.getIndex1(); |
| 62 |
if ( isel >= 0 && ( i0 < 0 || i1 < 0 || |
| 63 |
( i0 - isel ) * ( i1 - isel ) <= 0 ) ) { |
| 64 |
updateClientView(); |
| 65 |
} |
| 66 |
} |
| 67 |
public void intervalRemoved( ListDataEvent evt ) { |
| 68 |
if ( clientPanel_.getClient() != null && |
| 69 |
jlist_.getSelectedIndex() < 0 ) { |
| 70 |
updateClientView(); |
| 71 |
} |
| 72 |
} |
| 73 |
public void intervalAdded( ListDataEvent evt ) { |
| 74 |
preferSelection(); |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
// Construct and place subcomponents. |
| 79 |
clientPanel_ = new ClientPanel( hubLike ); |
| 80 |
JSplitPane splitter = new JSplitPane(); |
| 81 |
splitter.setOneTouchExpandable( true ); |
| 82 |
JScrollPane listScroller = new JScrollPane( jlist_ ); |
| 83 |
listScroller.setPreferredSize( new Dimension( 200, 400 ) ); |
| 84 |
listScroller.setBorder( ClientPanel.createTitledBorder( "Clients" ) ); |
| 85 |
splitter.setLeftComponent( listScroller ); |
| 86 |
splitter.setRightComponent( clientPanel_ ); |
| 87 |
add( splitter ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Sets the client list model which is displayed in this component. |
| 92 |
* |
| 93 |
* @param clientModel list model whose elements are |
| 94 |
* {@link org.astrogrid.samp.Client}s |
| 95 |
*/ |
| 96 |
public void setClientListModel( ListModel clientModel ) { |
| 97 |
ListModel oldModel = jlist_.getModel(); |
| 98 |
jlist_.getSelectionModel().clearSelection(); |
| 99 |
if ( oldModel != null ) { |
| 100 |
oldModel.removeListDataListener( listListener_ ); |
| 101 |
} |
| 102 |
jlist_.setModel( clientModel ); |
| 103 |
if ( clientModel != null ) { |
| 104 |
clientModel.addListDataListener( listListener_ ); |
| 105 |
preferSelection(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the JList component which houses the active list of clients. |
| 111 |
* This can be manipulated to, for instance, customise the renderer |
| 112 |
* if required. Its model should not be set directly however; use the |
| 113 |
* {@link #setClientListModel} method. |
| 114 |
* |
| 115 |
* @return client JList |
| 116 |
*/ |
| 117 |
public JList getClientList() { |
| 118 |
return jlist_; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Ensure that the client panel is up to date with respect to the currently |
| 123 |
* selected client. |
| 124 |
*/ |
| 125 |
private void updateClientView() { |
| 126 |
int isel = jlist_.getSelectedIndex(); |
| 127 |
clientPanel_.setClient( isel >= 0 |
| 128 |
? (Client) jlist_.getModel().getElementAt( isel ) |
| 129 |
: null ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Invoked when the list may have just acquired more than zero elements |
| 134 |
* to select one rather than none of them. |
| 135 |
*/ |
| 136 |
private void preferSelection() { |
| 137 |
if ( jlist_.getSelectedIndex() < 0 && |
| 138 |
jlist_.getModel().getSize() > 0 ) { |
| 139 |
jlist_.setSelectedIndex( 0 ); |
| 140 |
} |
| 141 |
} |
| 142 |
} |