| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.Component; |
| 6 |
import java.awt.Dimension; |
| 7 |
import java.awt.Graphics; |
| 8 |
import java.awt.Insets; |
| 9 |
import java.awt.Toolkit; |
| 10 |
import java.awt.event.ActionEvent; |
| 11 |
import java.awt.event.ActionListener; |
| 12 |
import java.io.IOException; |
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Arrays; |
| 15 |
import java.util.HashMap; |
| 16 |
import java.util.Iterator; |
| 17 |
import java.util.List; |
| 18 |
import java.util.Map; |
| 19 |
import java.util.WeakHashMap; |
| 20 |
import javax.swing.AbstractAction; |
| 21 |
import javax.swing.Action; |
| 22 |
import javax.swing.BorderFactory; |
| 23 |
import javax.swing.Icon; |
| 24 |
import javax.swing.ImageIcon; |
| 25 |
import javax.swing.JButton; |
| 26 |
import javax.swing.JComponent; |
| 27 |
import javax.swing.JDialog; |
| 28 |
import javax.swing.JFrame; |
| 29 |
import javax.swing.JLabel; |
| 30 |
import javax.swing.JOptionPane; |
| 31 |
import javax.swing.JTextField; |
| 32 |
import javax.swing.ListCellRenderer; |
| 33 |
import javax.swing.ListModel; |
| 34 |
import javax.swing.SwingUtilities; |
| 35 |
import javax.swing.border.Border; |
| 36 |
import javax.swing.event.ChangeEvent; |
| 37 |
import javax.swing.event.ChangeListener; |
| 38 |
import javax.swing.event.ListDataEvent; |
| 39 |
import javax.swing.event.ListDataListener; |
| 40 |
import org.astrogrid.samp.Client; |
| 41 |
import org.astrogrid.samp.client.ClientProfile; |
| 42 |
import org.astrogrid.samp.client.HubConnection; |
| 43 |
import org.astrogrid.samp.client.HubConnector; |
| 44 |
import org.astrogrid.samp.client.SampException; |
| 45 |
import org.astrogrid.samp.client.TrackedClientSet; |
| 46 |
import org.astrogrid.samp.hub.Hub; |
| 47 |
import org.astrogrid.samp.hub.HubServiceMode; |
| 48 |
import org.astrogrid.samp.xmlrpc.XmlRpcKit; |
| 49 |
|
| 50 |
/** |
| 51 |
* Extends HubConnector to provide additional graphical functionality. |
| 52 |
* In particular Swing {@link javax.swing.Action}s are provided for |
| 53 |
* hub connection/disconnection |
| 54 |
* and the client list is made available as a {@link javax.swing.ListModel}. |
| 55 |
* See the {@link org.astrogrid.samp.client.HubConnector superclass} |
| 56 |
* documentation for details of how to use this class. |
| 57 |
* A number of utility methods build on these features to provide |
| 58 |
* Swing components and Actions which can be used directly to populate |
| 59 |
* application menus etc. |
| 60 |
* |
| 61 |
* @author Mark Taylor |
| 62 |
* @since 25 Nov 2008 |
| 63 |
*/ |
| 64 |
public class GuiHubConnector extends HubConnector { |
| 65 |
|
| 66 |
private final ListModel clientListModel_; |
| 67 |
private final List connectionListenerList_; |
| 68 |
private final Map updateMap_; |
| 69 |
private boolean wasConnected_; |
| 70 |
static ConnectionUpdate ENABLE_ACTION = new ConnectionUpdate() { |
| 71 |
public void setConnected( Object action, boolean isConnected ) { |
| 72 |
((Action) action).setEnabled( isConnected ); |
| 73 |
} |
| 74 |
}; |
| 75 |
static ConnectionUpdate DISABLE_ACTION = new ConnectionUpdate() { |
| 76 |
public void setConnected( Object action, boolean isConnected ) { |
| 77 |
((Action) action).setEnabled( ! isConnected ); |
| 78 |
} |
| 79 |
}; |
| 80 |
static ConnectionUpdate REPAINT_COMPONENT = new ConnectionUpdate() { |
| 81 |
public void setConnected( Object comp, boolean isConnected ) { |
| 82 |
((Component) comp).repaint(); |
| 83 |
} |
| 84 |
}; |
| 85 |
static ConnectionUpdate ENABLE_COMPONENT = new ConnectionUpdate() { |
| 86 |
public void setConnected( Object comp, boolean isConnected ) { |
| 87 |
((Component) comp).setEnabled( isConnected ); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
/** |
| 92 |
* Constructs a hub connector based on a given profile instance. |
| 93 |
* |
| 94 |
* @param profile profile implementation |
| 95 |
*/ |
| 96 |
public GuiHubConnector( ClientProfile profile ) { |
| 97 |
super( profile, new ListModelTrackedClientSet() ); |
| 98 |
clientListModel_ = (ListModelTrackedClientSet) getClientSet(); |
| 99 |
connectionListenerList_ = new ArrayList(); |
| 100 |
updateMap_ = new WeakHashMap(); |
| 101 |
|
| 102 |
// Update state when hub connection starts/stops. |
| 103 |
addConnectionListener( new ChangeListener() { |
| 104 |
public void stateChanged( ChangeEvent evt ) { |
| 105 |
updateConnectionState(); |
| 106 |
} |
| 107 |
} ); |
| 108 |
updateConnectionState(); |
| 109 |
} |
| 110 |
|
| 111 |
protected void connectionChanged( boolean isConnected ) { |
| 112 |
super.connectionChanged( isConnected ); |
| 113 |
SwingUtilities.invokeLater( new Runnable() { |
| 114 |
public void run() { |
| 115 |
ChangeEvent evt = new ChangeEvent( GuiHubConnector.this ); |
| 116 |
for ( Iterator it = connectionListenerList_.iterator(); |
| 117 |
it.hasNext(); ) { |
| 118 |
((ChangeListener) it.next()).stateChanged( evt ); |
| 119 |
} |
| 120 |
} |
| 121 |
} ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Adds a listener which will be notified when this connector |
| 126 |
* registers or unregisters with a hub. |
| 127 |
* |
| 128 |
* @param listener listener to add |
| 129 |
*/ |
| 130 |
public void addConnectionListener( ChangeListener listener ) { |
| 131 |
connectionListenerList_.add( listener ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Removes a listener previously added by |
| 136 |
* {@link #addConnectionListener addConnectionListener}. |
| 137 |
* |
| 138 |
* @param listener listener to remove |
| 139 |
*/ |
| 140 |
public void removeConnectionListener( ChangeListener listener ) { |
| 141 |
connectionListenerList_.remove( listener ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns a ListModel containing the registered clients. |
| 146 |
* Listeners to this model are correctly notified whenever any change |
| 147 |
* in its contents takes place. |
| 148 |
* |
| 149 |
* @return list model containing {@link Client} objects |
| 150 |
*/ |
| 151 |
public ListModel getClientListModel() { |
| 152 |
return clientListModel_; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Returns a list cell renderer suitable for use with the |
| 157 |
* client list model returned by {@link #getClientListModel}. |
| 158 |
* |
| 159 |
* @return list cell renderer for Client objects |
| 160 |
*/ |
| 161 |
public ListCellRenderer createClientListCellRenderer() { |
| 162 |
return new ClientListCellRenderer(); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns an action which attempts to register with the hub. |
| 167 |
* Disabled when already registered. |
| 168 |
* |
| 169 |
* @return registration action |
| 170 |
*/ |
| 171 |
public Action createRegisterAction() { |
| 172 |
Action regAct = new RegisterAction( true ); |
| 173 |
registerUpdater( regAct, DISABLE_ACTION ); |
| 174 |
return regAct; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns an action which attempts to unregister from the hub. |
| 179 |
* Disabled when already unregistered. |
| 180 |
* |
| 181 |
* @return unregistration action |
| 182 |
*/ |
| 183 |
public Action createUnregisterAction() { |
| 184 |
Action unregAct = new RegisterAction( false ); |
| 185 |
registerUpdater( unregAct, ENABLE_ACTION ); |
| 186 |
return unregAct; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Returns an action which toggles hub registration. |
| 191 |
* |
| 192 |
* @return registration toggler action |
| 193 |
*/ |
| 194 |
public Action createToggleRegisterAction() { |
| 195 |
RegisterAction toggleRegAct = new RegisterAction(); |
| 196 |
registerUpdater( toggleRegAct, new ConnectionUpdate() { |
| 197 |
public void setConnected( Object item, boolean isConnected ) { |
| 198 |
((RegisterAction) item).setSense( ! isConnected ); |
| 199 |
} |
| 200 |
} ); |
| 201 |
return toggleRegAct; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns a new action which will register with a hub if one is running, |
| 206 |
* and if not, will offer to start a hub. |
| 207 |
* The exact options for starting a hub are given by the |
| 208 |
* <code>hubStartActions</code> parameter - the elements of this array |
| 209 |
* will normally be generated by calling the |
| 210 |
* {@link #createHubAction createHubAction} method. |
| 211 |
* |
| 212 |
* @param parent parent component, used for placing dialogue |
| 213 |
* @param hubStartActions actions which start a hub, |
| 214 |
* or null for a default list |
| 215 |
*/ |
| 216 |
public Action createRegisterOrHubAction( final Component parent, |
| 217 |
Action[] hubStartActions ) { |
| 218 |
final Action[] hubActs; |
| 219 |
if ( hubStartActions != null ) { |
| 220 |
hubActs = hubStartActions; |
| 221 |
} |
| 222 |
else { |
| 223 |
HubServiceMode internalMode = SysTray.getInstance().isSupported() |
| 224 |
? HubServiceMode.CLIENT_GUI |
| 225 |
: HubServiceMode.NO_GUI; |
| 226 |
hubActs = new Action[] { |
| 227 |
createHubAction( false, internalMode ), |
| 228 |
createHubAction( true, HubServiceMode.MESSAGE_GUI ), |
| 229 |
}; |
| 230 |
} |
| 231 |
Action regAct = new RegisterAction() { |
| 232 |
protected void registerFailed() { |
| 233 |
Object msg = new String[] { |
| 234 |
"No SAMP hub is running.", |
| 235 |
"You may start a hub if you wish.", |
| 236 |
}; |
| 237 |
List buttList = new ArrayList(); |
| 238 |
JButton[] options = new JButton[ hubActs.length + 1 ]; |
| 239 |
for ( int i = 0; i < hubActs.length; i++ ) { |
| 240 |
options[ i ] = new JButton( hubActs[ i ] ); |
| 241 |
} |
| 242 |
options[ hubActs.length ] = new JButton( "Cancel" ); |
| 243 |
final JDialog dialog = |
| 244 |
new JOptionPane( msg, JOptionPane.WARNING_MESSAGE, |
| 245 |
JOptionPane.DEFAULT_OPTION, |
| 246 |
null, options, null ) |
| 247 |
.createDialog( parent, "No Hub" ); |
| 248 |
ActionListener closeListener = new ActionListener() { |
| 249 |
public void actionPerformed( ActionEvent evt ) { |
| 250 |
dialog.dispose(); |
| 251 |
} |
| 252 |
}; |
| 253 |
for ( int iopt = 0; iopt < options.length; iopt++ ) { |
| 254 |
options[ iopt ].addActionListener( closeListener ); |
| 255 |
} |
| 256 |
dialog.setVisible( true ); |
| 257 |
} |
| 258 |
}; |
| 259 |
registerUpdater( regAct, new ConnectionUpdate() { |
| 260 |
public void setConnected( Object item, boolean isConnected ) { |
| 261 |
((RegisterAction) item).setSense( ! isConnected ); |
| 262 |
} |
| 263 |
} ); |
| 264 |
return regAct; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Returns an action which will display a SAMP hub monitor window. |
| 269 |
* |
| 270 |
* @return monitor window action |
| 271 |
*/ |
| 272 |
public Action createShowMonitorAction() { |
| 273 |
return new MonitorAction(); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Returns an action which will start up a SAMP hub. |
| 278 |
* You can specify whether it runs in the current JVM or a newly |
| 279 |
* created one; in the former case, it will shut down when the |
| 280 |
* current application does. |
| 281 |
* |
| 282 |
* @param external false to run in the current JVM, |
| 283 |
* true to run in a new one |
| 284 |
* @param hubMode hub mode |
| 285 |
*/ |
| 286 |
public Action createHubAction( boolean external, HubServiceMode hubMode ) { |
| 287 |
return new HubAction( external, hubMode ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Creates a component which indicates whether this connector is currently |
| 292 |
* connected or not, using supplied icons. |
| 293 |
* |
| 294 |
* @param onIcon icon indicating connection |
| 295 |
* @param offIcon icon indicating no connection |
| 296 |
* @return connection indicator |
| 297 |
*/ |
| 298 |
public JComponent createConnectionIndicator( final Icon onIcon, |
| 299 |
final Icon offIcon ) { |
| 300 |
JLabel label = new JLabel( new Icon() { |
| 301 |
private Icon effIcon() { |
| 302 |
return isConnected() ? onIcon : offIcon; |
| 303 |
} |
| 304 |
public int getIconWidth() { |
| 305 |
return effIcon().getIconWidth(); |
| 306 |
} |
| 307 |
public int getIconHeight() { |
| 308 |
return effIcon().getIconHeight(); |
| 309 |
} |
| 310 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 311 |
effIcon().paintIcon( c, g, x, y ); |
| 312 |
} |
| 313 |
} ); |
| 314 |
registerUpdater( label, REPAINT_COMPONENT ); |
| 315 |
return label; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Creates a component which indicates whether this connector is currently |
| 320 |
* connected or not, using default icons. |
| 321 |
* |
| 322 |
* @return connection indicator |
| 323 |
*/ |
| 324 |
public JComponent createConnectionIndicator() { |
| 325 |
return createConnectionIndicator( |
| 326 |
new ImageIcon( Client.class |
| 327 |
.getResource( "images/connected-24.gif" ) ), |
| 328 |
new ImageIcon( Client.class |
| 329 |
.getResource( "images/disconnected-24.gif" ) ) |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Creates a component which shows an icon for each registered client. |
| 335 |
* |
| 336 |
* @param vertical true for vertical box, false for horizontal |
| 337 |
* @param iconSize dimension in pixel of each icon (square) |
| 338 |
*/ |
| 339 |
public JComponent createClientBox( final boolean vertical, int iconSize ) { |
| 340 |
final IconStore iconStore = |
| 341 |
new IconStore( IconStore.createMinimalIcon( iconSize ) ); |
| 342 |
IconBox box = new IconBox( iconSize ); |
| 343 |
box.setVertical( vertical ); |
| 344 |
box.setBorder( createBoxBorder() ); |
| 345 |
box.setModel( clientListModel_ ); |
| 346 |
box.setRenderer( new IconBox.CellRenderer() { |
| 347 |
public Icon getIcon( IconBox iconBox, Object value, int index ) { |
| 348 |
return IconStore.scaleIcon( iconStore.getIcon( (Client) value ), |
| 349 |
iconBox.getTransverseSize(), |
| 350 |
2.0, ! vertical ); |
| 351 |
} |
| 352 |
public String getToolTipText( IconBox iconBox, Object value, |
| 353 |
int index ) { |
| 354 |
return ((Client) value).toString(); |
| 355 |
} |
| 356 |
} ); |
| 357 |
Dimension boxSize = box.getPreferredSize(); |
| 358 |
boxSize.width = 128; |
| 359 |
box.setPreferredSize( boxSize ); |
| 360 |
registerUpdater( box, ENABLE_COMPONENT ); |
| 361 |
return box; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Returns a new component which displays status for this connector. |
| 366 |
* |
| 367 |
* @return new hub connection monitor component |
| 368 |
*/ |
| 369 |
public JComponent createMonitorPanel() { |
| 370 |
HubView view = new HubView( false ); |
| 371 |
view.setClientListModel( getClientListModel() ); |
| 372 |
view.getClientList().setCellRenderer( createClientListCellRenderer() ); |
| 373 |
return view; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Called when the connection status (registered/unregistered) may have |
| 378 |
* changed. May be called from any thread. |
| 379 |
*/ |
| 380 |
private void scheduleConnectionChange() { |
| 381 |
SwingUtilities.invokeLater( new Runnable() { |
| 382 |
public void run() { |
| 383 |
boolean isConnected = isConnected(); |
| 384 |
if ( isConnected != wasConnected_ ) { |
| 385 |
wasConnected_ = isConnected; |
| 386 |
ChangeEvent evt = new ChangeEvent( GuiHubConnector.this ); |
| 387 |
for ( Iterator it = connectionListenerList_.iterator(); |
| 388 |
it.hasNext(); ) { |
| 389 |
((ChangeListener) it.next()).stateChanged( evt ); |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} ); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Called when the connection status has changed, or may have changed. |
| 398 |
*/ |
| 399 |
private void updateConnectionState() { |
| 400 |
boolean isConn = isConnected(); |
| 401 |
for ( Iterator it = updateMap_.entrySet().iterator(); it.hasNext(); ) { |
| 402 |
Map.Entry entry = (Map.Entry) it.next(); |
| 403 |
Object item = entry.getKey(); |
| 404 |
ConnectionUpdate update = (ConnectionUpdate) entry.getValue(); |
| 405 |
update.setConnected( item, isConn ); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Returns a border suitable for icon boxes. |
| 411 |
* |
| 412 |
* @return border |
| 413 |
*/ |
| 414 |
static Border createBoxBorder() { |
| 415 |
return BorderFactory.createCompoundBorder( |
| 416 |
new JTextField().getBorder(), |
| 417 |
BorderFactory.createEmptyBorder( 1, 1, 1, 1 ) ); |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Adds a given item to the list of objects which will be notified |
| 422 |
* when the hub is connected/disconnected. By doing it like this |
| 423 |
* rather than with the usual listener mechanism the problem of |
| 424 |
* retaining references to otherwise unused listeners is circumvented. |
| 425 |
* |
| 426 |
* @param item object to be notified |
| 427 |
* @param updater object which performs the notification on hub |
| 428 |
* connect/disconnect |
| 429 |
*/ |
| 430 |
void registerUpdater( Object item, ConnectionUpdate updater ) { |
| 431 |
updater.setConnected( item, isConnected() ); |
| 432 |
updateMap_.put( item, updater ); |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Interface defining how an object is to be notified when the hub |
| 437 |
* connection status changes. |
| 438 |
*/ |
| 439 |
interface ConnectionUpdate { |
| 440 |
|
| 441 |
/** |
| 442 |
* Invoked when hub connection status changes. |
| 443 |
* |
| 444 |
* @param item which is being notified |
| 445 |
* @param isConnected whether the hub is now connected or not |
| 446 |
*/ |
| 447 |
void setConnected( Object item, boolean isConnected ); |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* TrackedClientSet implementation used by this class. |
| 452 |
* Implements ListModel as well. |
| 453 |
*/ |
| 454 |
private static class ListModelTrackedClientSet extends TrackedClientSet |
| 455 |
implements ListModel { |
| 456 |
|
| 457 |
private final List clientList_; |
| 458 |
private final List listenerList_; |
| 459 |
|
| 460 |
/** |
| 461 |
* Constructor. |
| 462 |
*/ |
| 463 |
ListModelTrackedClientSet() { |
| 464 |
clientList_ = new ArrayList(); |
| 465 |
listenerList_ = new ArrayList(); |
| 466 |
} |
| 467 |
|
| 468 |
public int getSize() { |
| 469 |
return clientList_.size(); |
| 470 |
} |
| 471 |
|
| 472 |
public Object getElementAt( int index ) { |
| 473 |
return clientList_.get( index ); |
| 474 |
} |
| 475 |
|
| 476 |
public void addListDataListener( ListDataListener listener ) { |
| 477 |
listenerList_.add( listener ); |
| 478 |
} |
| 479 |
|
| 480 |
public void removeListDataListener( ListDataListener listener ) { |
| 481 |
listenerList_.remove( listener ); |
| 482 |
} |
| 483 |
|
| 484 |
public void addClient( final Client client ) { |
| 485 |
super.addClient( client ); |
| 486 |
SwingUtilities.invokeLater( new Runnable() { |
| 487 |
public void run() { |
| 488 |
int index = clientList_.size(); |
| 489 |
clientList_.add( client ); |
| 490 |
ListDataEvent evt = |
| 491 |
new ListDataEvent( ListModelTrackedClientSet.this, |
| 492 |
ListDataEvent.INTERVAL_ADDED, |
| 493 |
index, index ); |
| 494 |
for ( Iterator it = listenerList_.iterator(); |
| 495 |
it.hasNext(); ) { |
| 496 |
((ListDataListener) it.next()).intervalAdded( evt ); |
| 497 |
} |
| 498 |
} |
| 499 |
} ); |
| 500 |
} |
| 501 |
|
| 502 |
public void removeClient( final Client client ) { |
| 503 |
super.removeClient( client ); |
| 504 |
SwingUtilities.invokeLater( new Runnable() { |
| 505 |
public void run() { |
| 506 |
int index = clientList_.indexOf( client ); |
| 507 |
assert index >= 0; |
| 508 |
if ( index >= 0 ) { |
| 509 |
clientList_.remove( index ); |
| 510 |
ListDataEvent evt = |
| 511 |
new ListDataEvent( ListModelTrackedClientSet.this, |
| 512 |
ListDataEvent.INTERVAL_REMOVED, |
| 513 |
index, index ); |
| 514 |
for ( Iterator it = listenerList_.iterator(); |
| 515 |
it.hasNext(); ) { |
| 516 |
((ListDataListener) it.next()) |
| 517 |
.intervalRemoved( evt ); |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
} ); |
| 522 |
} |
| 523 |
|
| 524 |
public void setClients( final Client[] clients ) { |
| 525 |
super.setClients( clients ); |
| 526 |
SwingUtilities.invokeLater( new Runnable() { |
| 527 |
public void run() { |
| 528 |
int oldSize = clientList_.size(); |
| 529 |
if ( oldSize > 0 ) { |
| 530 |
clientList_.clear(); |
| 531 |
ListDataEvent removeEvt = |
| 532 |
new ListDataEvent( ListModelTrackedClientSet.this, |
| 533 |
ListDataEvent.INTERVAL_REMOVED, |
| 534 |
0, oldSize - 1); |
| 535 |
for ( Iterator it = listenerList_.iterator(); |
| 536 |
it.hasNext(); ) { |
| 537 |
((ListDataListener) it.next()) |
| 538 |
.intervalRemoved( removeEvt ); |
| 539 |
} |
| 540 |
} |
| 541 |
if ( clients.length > 0 ) { |
| 542 |
clientList_.addAll( Arrays.asList( clients ) ); |
| 543 |
int newSize = clientList_.size(); |
| 544 |
ListDataEvent addEvt = |
| 545 |
new ListDataEvent( ListModelTrackedClientSet.this, |
| 546 |
ListDataEvent.INTERVAL_ADDED, |
| 547 |
0, newSize - 1); |
| 548 |
for ( Iterator it = listenerList_.iterator(); |
| 549 |
it.hasNext(); ) { |
| 550 |
((ListDataListener) it.next()) |
| 551 |
.intervalAdded( addEvt ); |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
} ); |
| 556 |
} |
| 557 |
|
| 558 |
public void updateClient( final Client client, |
| 559 |
boolean metaChanged, boolean subsChanged ) { |
| 560 |
super.updateClient( client, metaChanged, subsChanged ); |
| 561 |
SwingUtilities.invokeLater( new Runnable() { |
| 562 |
public void run() { |
| 563 |
int index = clientList_.indexOf( client ); |
| 564 |
if ( index >= 0 ) { |
| 565 |
ListDataEvent evt = |
| 566 |
new ListDataEvent( ListModelTrackedClientSet.this, |
| 567 |
ListDataEvent.CONTENTS_CHANGED, |
| 568 |
index, index ); |
| 569 |
for ( Iterator it = listenerList_.iterator(); |
| 570 |
it.hasNext(); ) { |
| 571 |
((ListDataListener) it.next()) |
| 572 |
.contentsChanged( evt ); |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
} ); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Action which registers and unregisters with the hub. |
| 582 |
*/ |
| 583 |
private class RegisterAction extends AbstractAction { |
| 584 |
|
| 585 |
/** |
| 586 |
* Constructs in an unarmed state. |
| 587 |
*/ |
| 588 |
public RegisterAction() { |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* Constructs with a given (initial) sense. |
| 593 |
* |
| 594 |
* @param active true to register, false to unregister |
| 595 |
*/ |
| 596 |
public RegisterAction( boolean active ) { |
| 597 |
this(); |
| 598 |
setSense( active ); |
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
* Sets whether this action registers or unregisters. |
| 603 |
* |
| 604 |
* @param active true to register, false to unregister |
| 605 |
*/ |
| 606 |
public void setSense( boolean active ) { |
| 607 |
putValue( ACTION_COMMAND_KEY, active ? "REGISTER" |
| 608 |
: "UNREGISTER" ); |
| 609 |
putValue( NAME, active ? "Register with Hub" |
| 610 |
: "Unregister from Hub" ); |
| 611 |
putValue( SHORT_DESCRIPTION, |
| 612 |
active ? "Attempt to connect to SAMP hub" |
| 613 |
: "Disconnect from SAMP hub" ); |
| 614 |
} |
| 615 |
|
| 616 |
public void actionPerformed( ActionEvent evt ) { |
| 617 |
String cmd = evt.getActionCommand(); |
| 618 |
if ( "REGISTER".equals( cmd ) ) { |
| 619 |
setActive( true ); |
| 620 |
if ( ! isConnected() ) { |
| 621 |
registerFailed(); |
| 622 |
} |
| 623 |
} |
| 624 |
else if ( "UNREGISTER".equals( cmd ) ) { |
| 625 |
setActive( false ); |
| 626 |
} |
| 627 |
else { |
| 628 |
throw new UnsupportedOperationException( "Unknown action " |
| 629 |
+ cmd ); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
protected void registerFailed() { |
| 634 |
Toolkit.getDefaultToolkit().beep(); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
/** |
| 639 |
* Action subclass for popping up a monitor window. |
| 640 |
*/ |
| 641 |
private class MonitorAction extends AbstractAction { |
| 642 |
private JFrame monitorWindow_; |
| 643 |
|
| 644 |
/** |
| 645 |
* Constructor. |
| 646 |
*/ |
| 647 |
MonitorAction() { |
| 648 |
super( "Show Hub Status" ); |
| 649 |
putValue( SHORT_DESCRIPTION, |
| 650 |
"Display a window showing client applications" |
| 651 |
+ " registered with the SAMP hub" ); |
| 652 |
} |
| 653 |
|
| 654 |
public void actionPerformed( ActionEvent evt ) { |
| 655 |
if ( monitorWindow_ == null ) { |
| 656 |
monitorWindow_ = new JFrame( "SAMP Status" ); |
| 657 |
monitorWindow_.getContentPane() |
| 658 |
.add( createMonitorPanel(), BorderLayout.CENTER ); |
| 659 |
monitorWindow_.pack(); |
| 660 |
} |
| 661 |
monitorWindow_.setVisible( true ); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
* Action subclass for running a hub. |
| 667 |
*/ |
| 668 |
private class HubAction extends AbstractAction { |
| 669 |
private final boolean external_; |
| 670 |
private final HubServiceMode hubMode_; |
| 671 |
private final boolean isAvailable_; |
| 672 |
|
| 673 |
/** |
| 674 |
* Constructor. |
| 675 |
* |
| 676 |
* @param external false to run in the current JVM, |
| 677 |
* true to run in a new one |
| 678 |
* @param hubMode hub mode |
| 679 |
*/ |
| 680 |
HubAction( boolean external, HubServiceMode hubMode ) { |
| 681 |
external_ = external; |
| 682 |
hubMode_ = hubMode; |
| 683 |
putValue( NAME, |
| 684 |
"Start " + ( external ? "external" : "internal" ) |
| 685 |
+ " hub" ); |
| 686 |
putValue( SHORT_DESCRIPTION, |
| 687 |
"Attempts to start up a SAMP hub" |
| 688 |
+ ( external ? " running independently of this application" |
| 689 |
: " running within this application" ) ); |
| 690 |
setEnabled( ! isConnected() ); |
| 691 |
registerUpdater( this, DISABLE_ACTION ); |
| 692 |
boolean isAvailable = true; |
| 693 |
if ( external ) { |
| 694 |
try { |
| 695 |
Hub.checkExternalHubAvailability(); |
| 696 |
} |
| 697 |
catch ( Exception e ) { |
| 698 |
isAvailable = false; |
| 699 |
} |
| 700 |
} |
| 701 |
isAvailable_ = isAvailable; |
| 702 |
} |
| 703 |
|
| 704 |
public void actionPerformed( ActionEvent evt ) { |
| 705 |
try { |
| 706 |
attemptRunHub(); |
| 707 |
} |
| 708 |
catch ( Exception e ) { |
| 709 |
ErrorDialog.showError( null, "Hub Start Failed", |
| 710 |
e.getMessage(), e ); |
| 711 |
} |
| 712 |
setActive( true ); |
| 713 |
} |
| 714 |
|
| 715 |
public boolean isEnabled() { |
| 716 |
return isAvailable_ && super.isEnabled(); |
| 717 |
} |
| 718 |
|
| 719 |
/** |
| 720 |
* Tries to start a hub, but may throw an exception. |
| 721 |
*/ |
| 722 |
private void attemptRunHub() throws IOException { |
| 723 |
if ( external_ ) { |
| 724 |
Hub.runExternalHub( hubMode_ ); |
| 725 |
} |
| 726 |
else { |
| 727 |
Hub.runHub( hubMode_ ); |
| 728 |
} |
| 729 |
setActive( true ); |
| 730 |
} |
| 731 |
} |
| 732 |
} |