| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.util.Map; |
| 4 |
import java.util.Random; |
| 5 |
import java.awt.event.ActionEvent; |
| 6 |
import java.awt.event.KeyEvent; |
| 7 |
import javax.swing.AbstractAction; |
| 8 |
import javax.swing.Action; |
| 9 |
import javax.swing.ImageIcon; |
| 10 |
import javax.swing.JComponent; |
| 11 |
import javax.swing.JFrame; |
| 12 |
import javax.swing.JList; |
| 13 |
import javax.swing.JMenu; |
| 14 |
import javax.swing.JMenuItem; |
| 15 |
import javax.swing.ListModel; |
| 16 |
import javax.swing.ListSelectionModel; |
| 17 |
import javax.swing.event.ListSelectionEvent; |
| 18 |
import javax.swing.event.ListSelectionListener; |
| 19 |
import org.astrogrid.samp.Client; |
| 20 |
import org.astrogrid.samp.Message; |
| 21 |
import org.astrogrid.samp.client.HubConnection; |
| 22 |
import org.astrogrid.samp.client.SampException; |
| 23 |
import org.astrogrid.samp.hub.BasicHubService; |
| 24 |
import org.astrogrid.samp.hub.ClientSet; |
| 25 |
import org.astrogrid.samp.hub.HubClient; |
| 26 |
|
| 27 |
/** |
| 28 |
* BasicHubService subclass which provides a GUI window displaying hub |
| 29 |
* status as well as the basic hub services. |
| 30 |
* |
| 31 |
* @author Mark Taylor |
| 32 |
* @since 16 Jul 2008 |
| 33 |
*/ |
| 34 |
public class GuiHubService extends BasicHubService { |
| 35 |
|
| 36 |
private GuiClientSet clientSet_; |
| 37 |
private ListSelectionModel selectionModel_; |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. |
| 41 |
* |
| 42 |
* @param random random number generator used for message tags etc |
| 43 |
*/ |
| 44 |
public GuiHubService( Random random ) { |
| 45 |
super( random ); |
| 46 |
} |
| 47 |
|
| 48 |
public void start() { |
| 49 |
super.start(); |
| 50 |
clientSet_ = (GuiClientSet) getClientSet(); |
| 51 |
} |
| 52 |
|
| 53 |
protected ClientSet createClientSet() { |
| 54 |
return new GuiClientSet( getIdComparator() ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Creates a new component containing a display of the current hub |
| 59 |
* internal state. |
| 60 |
* |
| 61 |
* @return new hub viewer panel |
| 62 |
*/ |
| 63 |
public JComponent createHubPanel() { |
| 64 |
HubView hubView = new HubView( true ); |
| 65 |
hubView.setClientListModel( getClientListModel() ); |
| 66 |
JList jlist = hubView.getClientList(); |
| 67 |
jlist.setCellRenderer( new ClientListCellRenderer() ); |
| 68 |
jlist.addMouseListener( new HubClientPopupListener( this ) ); |
| 69 |
selectionModel_ = jlist.getSelectionModel(); |
| 70 |
return hubView; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Creates a new window which maintains a display of the current hub |
| 75 |
* internal state. |
| 76 |
* |
| 77 |
* @return new hub viewer window |
| 78 |
*/ |
| 79 |
public JFrame createHubWindow() { |
| 80 |
JFrame frame = new JFrame( "SAMP Hub" ); |
| 81 |
frame.getContentPane().add( createHubPanel() ); |
| 82 |
frame.setIconImage( new ImageIcon( Client.class |
| 83 |
.getResource( "images/hub.png" ) ) |
| 84 |
.getImage() ); |
| 85 |
frame.pack(); |
| 86 |
return frame; |
| 87 |
} |
| 88 |
|
| 89 |
protected void declareMetadata( HubClient caller, Map meta ) |
| 90 |
throws SampException { |
| 91 |
super.declareMetadata( caller, meta ); |
| 92 |
clientSet_.scheduleClientChanged( caller ); |
| 93 |
} |
| 94 |
|
| 95 |
protected void declareSubscriptions( HubClient caller, Map subscriptions ) |
| 96 |
throws SampException { |
| 97 |
super.declareSubscriptions( caller, subscriptions ); |
| 98 |
clientSet_.scheduleClientChanged( caller ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns a ListModel containing information about clients currently |
| 103 |
* registered with this hub. |
| 104 |
* |
| 105 |
* @return list model in which each element is a |
| 106 |
* {@link org.astrogrid.samp.Client} |
| 107 |
*/ |
| 108 |
public ListModel getClientListModel() { |
| 109 |
return clientSet_; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns the selection model corresponding to this service's client |
| 114 |
* list model. |
| 115 |
* |
| 116 |
* @return list selection model for client selection |
| 117 |
*/ |
| 118 |
public ListSelectionModel getClientSelectionModel() { |
| 119 |
return selectionModel_; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the client object currently selected in the GUI, if any. |
| 124 |
* |
| 125 |
* @return currently selected client, or null |
| 126 |
*/ |
| 127 |
private Client getSelectedClient() { |
| 128 |
ListSelectionModel selModel = getClientSelectionModel(); |
| 129 |
int isel = selModel.getMinSelectionIndex(); |
| 130 |
Object selected = isel >= 0 ? getClientListModel().getElementAt( isel ) |
| 131 |
: null; |
| 132 |
return selected instanceof Client ? (Client) selected : null; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns an array of menus which may be added to a window |
| 137 |
* containing this service's window. |
| 138 |
* |
| 139 |
* @return menu array |
| 140 |
*/ |
| 141 |
public JMenu[] createMenus() { |
| 142 |
final HubConnection serviceConnection = getServiceConnection(); |
| 143 |
final String hubId = serviceConnection.getRegInfo().getSelfId(); |
| 144 |
|
| 145 |
/* Broadcast ping action. */ |
| 146 |
final Message pingMessage = new Message( "samp.app.ping" ); |
| 147 |
final Action pingAllAction = new AbstractAction( "Ping all" ) { |
| 148 |
public void actionPerformed( ActionEvent evt ) { |
| 149 |
new SampThread( evt, "Ping Error", "Error broadcasting ping" ) { |
| 150 |
protected void sampRun() throws SampException { |
| 151 |
serviceConnection.callAll( "ping-tag", pingMessage ); |
| 152 |
} |
| 153 |
}.start(); |
| 154 |
} |
| 155 |
}; |
| 156 |
pingAllAction.putValue( Action.SHORT_DESCRIPTION, |
| 157 |
"Send ping message to all clients" ); |
| 158 |
|
| 159 |
/* Single client ping action. */ |
| 160 |
final String pingSelectedName = "Ping selected client"; |
| 161 |
final Action pingSelectedAction = |
| 162 |
new AbstractAction( pingSelectedName ) { |
| 163 |
public void actionPerformed( ActionEvent evt ) { |
| 164 |
final Client client = getSelectedClient(); |
| 165 |
if ( client != null ) { |
| 166 |
new SampThread( evt, "Ping Error", |
| 167 |
"Error sending ping to " + client ) { |
| 168 |
protected void sampRun() throws SampException { |
| 169 |
serviceConnection.call( client.getId(), "ping-tag", |
| 170 |
pingMessage ); |
| 171 |
} |
| 172 |
}.start(); |
| 173 |
} |
| 174 |
} |
| 175 |
}; |
| 176 |
pingSelectedAction.putValue( Action.SHORT_DESCRIPTION, |
| 177 |
"Send ping message to selected client" ); |
| 178 |
|
| 179 |
/* Single client disconnect action. */ |
| 180 |
final String disconnectSelectedName = "Disconnect selected client"; |
| 181 |
final Action disconnectSelectedAction = |
| 182 |
new AbstractAction( disconnectSelectedName ) { |
| 183 |
public void actionPerformed( ActionEvent evt ) { |
| 184 |
final Client client = getSelectedClient(); |
| 185 |
if ( client != null ) { |
| 186 |
new SampThread( evt, "Disconnect Error", |
| 187 |
"Error disconnecting " + client ) { |
| 188 |
protected void sampRun() throws SampException { |
| 189 |
disconnect( client.getId(), |
| 190 |
"GUI hub user requested ejection" ); |
| 191 |
} |
| 192 |
}.start(); |
| 193 |
} |
| 194 |
} |
| 195 |
}; |
| 196 |
disconnectSelectedAction.putValue( Action.SHORT_DESCRIPTION, |
| 197 |
"Forcibly disconnect selected client" |
| 198 |
+ " from the hub" ); |
| 199 |
|
| 200 |
/* Ensure that actions are kept up to date. */ |
| 201 |
ListSelectionListener selListener = new ListSelectionListener() { |
| 202 |
public void valueChanged( ListSelectionEvent evt ) { |
| 203 |
Client client = getSelectedClient(); |
| 204 |
boolean isSel = client != null; |
| 205 |
boolean canPing = isSel |
| 206 |
&& client.getSubscriptions() |
| 207 |
.isSubscribed( pingMessage.getMType() ); |
| 208 |
boolean canDisco = isSel |
| 209 |
&& ! hubId.equals( client.getId() ); |
| 210 |
pingSelectedAction.setEnabled( canPing ); |
| 211 |
disconnectSelectedAction.setEnabled( canDisco ); |
| 212 |
String clientDesignation = client == null |
| 213 |
? "" |
| 214 |
: ( " (" + client + ")" ); |
| 215 |
pingSelectedAction.putValue( Action.NAME, |
| 216 |
pingSelectedName |
| 217 |
+ clientDesignation ); |
| 218 |
disconnectSelectedAction.putValue( Action.NAME, |
| 219 |
disconnectSelectedName |
| 220 |
+ clientDesignation ); |
| 221 |
} |
| 222 |
}; |
| 223 |
getClientSelectionModel().addListSelectionListener( selListener ); |
| 224 |
selListener.valueChanged( null ); |
| 225 |
|
| 226 |
/* Prepare and return menus containing the actions. */ |
| 227 |
JMenu clientMenu = new JMenu( "Clients" ); |
| 228 |
clientMenu.setMnemonic( KeyEvent.VK_C ); |
| 229 |
clientMenu.add( new JMenuItem( pingAllAction ) ); |
| 230 |
clientMenu.add( new JMenuItem( pingSelectedAction ) ); |
| 231 |
clientMenu.add( new JMenuItem( disconnectSelectedAction ) ); |
| 232 |
return new JMenu[] { clientMenu }; |
| 233 |
} |
| 234 |
} |