| 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.Container; |
| 7 |
import java.awt.Dimension; |
| 8 |
import java.awt.GridBagConstraints; |
| 9 |
import java.awt.GridBagLayout; |
| 10 |
import java.awt.Insets; |
| 11 |
import java.awt.event.ActionEvent; |
| 12 |
import java.awt.event.ActionListener; |
| 13 |
import java.util.HashMap; |
| 14 |
import java.util.Iterator; |
| 15 |
import java.util.Map; |
| 16 |
import javax.swing.AbstractAction; |
| 17 |
import javax.swing.Action; |
| 18 |
import javax.swing.BorderFactory; |
| 19 |
import javax.swing.Box; |
| 20 |
import javax.swing.JButton; |
| 21 |
import javax.swing.JCheckBox; |
| 22 |
import javax.swing.JComponent; |
| 23 |
import javax.swing.JDialog; |
| 24 |
import javax.swing.JFrame; |
| 25 |
import javax.swing.JLabel; |
| 26 |
import javax.swing.JOptionPane; |
| 27 |
import javax.swing.JPanel; |
| 28 |
import javax.swing.JScrollPane; |
| 29 |
import javax.swing.JTextArea; |
| 30 |
import javax.swing.Timer; |
| 31 |
import org.astrogrid.samp.Client; |
| 32 |
import org.astrogrid.samp.ErrInfo; |
| 33 |
import org.astrogrid.samp.Message; |
| 34 |
import org.astrogrid.samp.Response; |
| 35 |
import org.astrogrid.samp.SampUtils; |
| 36 |
import org.astrogrid.samp.client.ResultHandler; |
| 37 |
|
| 38 |
/** |
| 39 |
* ResultHandler which pops up a window displaying progress and success |
| 40 |
* of a message sent to one or many recipients. |
| 41 |
* |
| 42 |
* @author Mark Taylor |
| 43 |
* @since 12 Nov 2008 |
| 44 |
*/ |
| 45 |
public class PopupResultHandler extends JFrame implements ResultHandler { |
| 46 |
|
| 47 |
private final Map clientMap_; |
| 48 |
private final int closeDelayMillis_; |
| 49 |
private final JCheckBox autoCloser_; |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructor. |
| 53 |
* |
| 54 |
* @param parent parent component, used for window placement; |
| 55 |
* may be null |
| 56 |
* @param title window title |
| 57 |
* @param msg message which was sent |
| 58 |
* @param recipients clients from which responses are expected |
| 59 |
* @param closeDelay number of seconds after final response received |
| 60 |
* that window closes automatically; if <0, no auto close |
| 61 |
*/ |
| 62 |
public PopupResultHandler( Component parent, String title, |
| 63 |
final Message msg, Client[] recipients, |
| 64 |
int closeDelay ) { |
| 65 |
super( title ); |
| 66 |
closeDelayMillis_ = closeDelay * 1000; |
| 67 |
|
| 68 |
// Set up manual window close action. |
| 69 |
Action closeAction = new AbstractAction( "Close" ) { |
| 70 |
public void actionPerformed( ActionEvent evt ) { |
| 71 |
PopupResultHandler.this.dispose(); |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
// Set up check box for configuring auto close |
| 76 |
autoCloser_ = closeDelayMillis_ >= 0 |
| 77 |
? new JCheckBox( "Auto Close", true ) |
| 78 |
: null; |
| 79 |
|
| 80 |
// Set up per-client response handler objects |
| 81 |
clientMap_ = new HashMap(); |
| 82 |
for ( int i = 0; i < recipients.length; i++ ) { |
| 83 |
Client client = recipients[ i ]; |
| 84 |
clientMap_.put( client, new ClientHandler( client ) ); |
| 85 |
} |
| 86 |
|
| 87 |
// Prepare component layout. |
| 88 |
Container content = getContentPane(); |
| 89 |
content.setLayout( new BorderLayout() ); |
| 90 |
JComponent main = new JPanel( new BorderLayout() ); |
| 91 |
content.add( main ); |
| 92 |
|
| 93 |
// Panel containing control buttons (window close) |
| 94 |
Box buttonBox = Box.createHorizontalBox(); |
| 95 |
buttonBox.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); |
| 96 |
if ( closeDelayMillis_ >= 0 ) { |
| 97 |
buttonBox.add( Box.createHorizontalGlue() ); |
| 98 |
buttonBox.add( autoCloser_ ); |
| 99 |
} |
| 100 |
buttonBox.add( Box.createHorizontalGlue() ); |
| 101 |
buttonBox.add( new JButton( closeAction ) ); |
| 102 |
buttonBox.add( Box.createHorizontalGlue() ); |
| 103 |
main.add( buttonBox, BorderLayout.SOUTH ); |
| 104 |
|
| 105 |
// Panel containing information about sent message. |
| 106 |
Box msgBox = Box.createVerticalBox(); |
| 107 |
msgBox.setBorder( ClientPanel.createTitledBorder( "Message" ) ); |
| 108 |
Box mtypeLine = Box.createHorizontalBox(); |
| 109 |
mtypeLine.add( new JLabel( msg.getMType() ) ); |
| 110 |
mtypeLine.add( Box.createHorizontalStrut( 10 ) ); |
| 111 |
mtypeLine.add( Box.createHorizontalGlue() ); |
| 112 |
Action msgDetailAction = |
| 113 |
new DetailAction( "Detail", "Show details of message sent", |
| 114 |
"Message Sent", "SAMP message sent:" ) { |
| 115 |
protected Map getSampMap() { |
| 116 |
return msg; |
| 117 |
} |
| 118 |
}; |
| 119 |
mtypeLine.add( new JButton( msgDetailAction ) ); |
| 120 |
msgBox.add( mtypeLine ); |
| 121 |
main.add( msgBox, BorderLayout.NORTH ); |
| 122 |
|
| 123 |
// Panel containing per-client response information. |
| 124 |
GridBagLayout layer = new GridBagLayout(); |
| 125 |
GridBagConstraints cons = new GridBagConstraints(); |
| 126 |
cons.weighty = 1; |
| 127 |
cons.gridx = 0; |
| 128 |
cons.gridy = 0; |
| 129 |
cons.insets = new Insets( 0, 2, 0, 2 ); |
| 130 |
JComponent resultBox = new JPanel( layer ); |
| 131 |
resultBox.setBorder( ClientPanel.createTitledBorder( "Responses" ) ); |
| 132 |
for ( Iterator it = clientMap_.entrySet().iterator(); it.hasNext(); ) { |
| 133 |
Map.Entry entry = (Map.Entry) it.next(); |
| 134 |
ClientHandler ch = (ClientHandler) entry.getValue(); |
| 135 |
|
| 136 |
// Client name. |
| 137 |
cons.anchor = GridBagConstraints.WEST; |
| 138 |
cons.weightx = 0; |
| 139 |
cons.fill = GridBagConstraints.NONE; |
| 140 |
Component nameLabel = ch.nameLabel_; |
| 141 |
layer.setConstraints( nameLabel, cons ); |
| 142 |
resultBox.add( nameLabel ); |
| 143 |
|
| 144 |
// Response status. |
| 145 |
cons.gridx++; |
| 146 |
cons.anchor = GridBagConstraints.CENTER; |
| 147 |
cons.weightx = 1; |
| 148 |
cons.fill = GridBagConstraints.NONE; |
| 149 |
Component statusLabel = ch.statusLabel_; |
| 150 |
layer.setConstraints( statusLabel, cons ); |
| 151 |
resultBox.add( statusLabel ); |
| 152 |
|
| 153 |
// Detail button. |
| 154 |
cons.gridx++; |
| 155 |
cons.anchor = GridBagConstraints.WEST; |
| 156 |
cons.weightx = 0; |
| 157 |
cons.fill = GridBagConstraints.NONE; |
| 158 |
Component detailButton = new JButton( ch.responseDetailAction_ ); |
| 159 |
layer.setConstraints( detailButton, cons ); |
| 160 |
resultBox.add( detailButton ); |
| 161 |
|
| 162 |
// Prepare for next row. |
| 163 |
cons.gridy++; |
| 164 |
cons.gridx = 0; |
| 165 |
} |
| 166 |
main.add( resultBox, BorderLayout.CENTER ); |
| 167 |
|
| 168 |
// Display popup window. |
| 169 |
pack(); |
| 170 |
if ( parent != null ) { |
| 171 |
setLocationRelativeTo( parent ); |
| 172 |
} |
| 173 |
setVisible( true ); |
| 174 |
} |
| 175 |
|
| 176 |
public void result( Client client, Response response ) { |
| 177 |
ClientHandler ch = (ClientHandler) clientMap_.get( client ); |
| 178 |
if ( ch == null ) { |
| 179 |
throw new IllegalArgumentException( "Shouldn't happen" ); |
| 180 |
} |
| 181 |
else { |
| 182 |
ch.result( response ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
public void done() { |
| 187 |
|
| 188 |
// Any clients which have not received responses yet, will not do so. |
| 189 |
// Make this clear in the status labels. |
| 190 |
for ( Iterator it = clientMap_.entrySet().iterator(); it.hasNext(); ) { |
| 191 |
ClientHandler ch = |
| 192 |
(ClientHandler) ((Map.Entry) it.next()).getValue(); |
| 193 |
if ( ch.response_ == null ) { |
| 194 |
ch.result( null ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// Arrange for auto close of window if required. |
| 199 |
if ( isVisible() && closeDelayMillis_ >= 0 && |
| 200 |
autoCloser_.isSelected() ) { |
| 201 |
if ( closeDelayMillis_ == 0 ) { |
| 202 |
dispose(); |
| 203 |
} |
| 204 |
else { |
| 205 |
new Timer( closeDelayMillis_, new ActionListener() { |
| 206 |
public void actionPerformed( ActionEvent evt ) { |
| 207 |
if ( autoCloser_.isSelected() ) { |
| 208 |
dispose(); |
| 209 |
} |
| 210 |
} |
| 211 |
} ).start(); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Handles per-client state for message wait/response information. |
| 218 |
*/ |
| 219 |
private class ClientHandler { |
| 220 |
final JLabel nameLabel_; |
| 221 |
final JLabel statusLabel_; |
| 222 |
final Action responseDetailAction_; |
| 223 |
Response response_; |
| 224 |
|
| 225 |
/** |
| 226 |
* Constructor. |
| 227 |
* |
| 228 |
* @param client client |
| 229 |
*/ |
| 230 |
ClientHandler( Client client ) { |
| 231 |
final String cName = client.toString(); |
| 232 |
nameLabel_ = new JLabel( cName ); |
| 233 |
statusLabel_ = new JLabel( "... Waiting ..." ); |
| 234 |
statusLabel_.setForeground( new Color( 0x40, 0x40, 0x40 ) ); |
| 235 |
responseDetailAction_ = |
| 236 |
new DetailAction( "Detail", "Show details of response", |
| 237 |
cName + " Response", |
| 238 |
"SAMP Response from client " + cName |
| 239 |
+ ":" ) { |
| 240 |
protected Map getSampMap() { |
| 241 |
return response_; |
| 242 |
} |
| 243 |
}; |
| 244 |
responseDetailAction_.setEnabled( response_ != null ); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Updates status according to a received response. |
| 249 |
* If the response is null, it indicates that no response is expected |
| 250 |
* in the future. |
| 251 |
* |
| 252 |
* @param response received response, or null |
| 253 |
*/ |
| 254 |
public void result( Response response ) { |
| 255 |
response_ = response; |
| 256 |
String status; |
| 257 |
String tip; |
| 258 |
boolean success; |
| 259 |
if ( response_ == null ) { |
| 260 |
success = false; |
| 261 |
status = "Aborted"; |
| 262 |
tip = "Interruption in messaging system prevented " |
| 263 |
+ "receipt of response?"; |
| 264 |
} |
| 265 |
else if ( response.isOK() ) { |
| 266 |
success = true; |
| 267 |
status = "OK"; |
| 268 |
tip = "Message processed successfully"; |
| 269 |
} |
| 270 |
else { |
| 271 |
success = false; |
| 272 |
status = "Fail"; |
| 273 |
ErrInfo errInfo = response.getErrInfo(); |
| 274 |
String errtxt = errInfo == null ? null : errInfo.getErrortxt(); |
| 275 |
tip = errtxt; |
| 276 |
if ( errtxt != null ) { |
| 277 |
status += " (" + errtxt + ")"; |
| 278 |
} |
| 279 |
} |
| 280 |
statusLabel_.setText( status ); |
| 281 |
statusLabel_.setForeground( success ? new Color( 0, 0x80, 0 ) |
| 282 |
: new Color( 0x80, 0, 0 ) ); |
| 283 |
statusLabel_.setToolTipText( tip ); |
| 284 |
responseDetailAction_.setEnabled( response_ != null ); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Action which will display a SampMap in a popup window. |
| 290 |
*/ |
| 291 |
private abstract class DetailAction extends AbstractAction { |
| 292 |
|
| 293 |
private final String popupTitle_; |
| 294 |
private final String heading_; |
| 295 |
|
| 296 |
/** |
| 297 |
* Constructor. |
| 298 |
* |
| 299 |
* @param name action name |
| 300 |
* @param shortDesc action short description (tool tip) |
| 301 |
* @param popupTitle title of popup window |
| 302 |
*/ |
| 303 |
DetailAction( String name, String shortDesc, String popupTitle, |
| 304 |
String heading ) { |
| 305 |
super( name ); |
| 306 |
putValue( SHORT_DESCRIPTION, shortDesc ); |
| 307 |
popupTitle_ = popupTitle; |
| 308 |
heading_ = heading; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Returns the map object which is to be displayed. |
| 313 |
* Invoked by {@link #actionPerformed}. |
| 314 |
* |
| 315 |
* @return object to display |
| 316 |
*/ |
| 317 |
protected abstract Map getSampMap(); |
| 318 |
|
| 319 |
public void actionPerformed( ActionEvent evt ) { |
| 320 |
|
| 321 |
// Make sure that autoclose is off, since otherwise these dialogues |
| 322 |
// will get disappeared at the same time as the owner window, |
| 323 |
// which is probably not what the user would expect. |
| 324 |
autoCloser_.setSelected( false ); |
| 325 |
|
| 326 |
// Set up a text component containing the full map serialization. |
| 327 |
JTextArea ta = new JTextArea(); |
| 328 |
ta.setLineWrap( false ); |
| 329 |
ta.setEditable( false ); |
| 330 |
ta.append( SampUtils.formatObject( getSampMap(), 3 ) ); |
| 331 |
ta.setCaretPosition( 0 ); |
| 332 |
|
| 333 |
// Wrap it in a scroll pane and size appropriately. |
| 334 |
Dimension size = ta.getPreferredSize(); |
| 335 |
size.height = Math.min( size.height + 20, 200 ); |
| 336 |
size.width = Math.min( size.width + 20, 360 ); |
| 337 |
JScrollPane scroller = new JScrollPane( ta ); |
| 338 |
scroller.setPreferredSize( size ); |
| 339 |
|
| 340 |
// Put into a non-modal dialogue with decorations. |
| 341 |
final JDialog dialog = |
| 342 |
new JDialog( PopupResultHandler.this, popupTitle_, false ); |
| 343 |
dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE ); |
| 344 |
JComponent main = new JPanel( new BorderLayout() ); |
| 345 |
main.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); |
| 346 |
dialog.getContentPane().add( main ); |
| 347 |
Action closeAction = new AbstractAction( "Close" ) { |
| 348 |
public void actionPerformed( ActionEvent evt ) { |
| 349 |
dialog.dispose(); |
| 350 |
} |
| 351 |
}; |
| 352 |
Box buttonBox = Box.createHorizontalBox(); |
| 353 |
buttonBox.add( Box.createHorizontalGlue() ); |
| 354 |
buttonBox.add( new JButton( closeAction ) ); |
| 355 |
buttonBox.add( Box.createHorizontalGlue() ); |
| 356 |
Box headBox = Box.createHorizontalBox(); |
| 357 |
headBox.add( new JLabel( heading_ ) ); |
| 358 |
headBox.add( Box.createHorizontalGlue() ); |
| 359 |
headBox.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 0 ) ); |
| 360 |
buttonBox.setBorder( BorderFactory |
| 361 |
.createEmptyBorder( 5, 0, 0, 0 ) ); |
| 362 |
main.add( headBox, BorderLayout.NORTH ); |
| 363 |
main.add( buttonBox, BorderLayout.SOUTH ); |
| 364 |
main.add( scroller, BorderLayout.CENTER ); |
| 365 |
|
| 366 |
// Post the dialogue. |
| 367 |
dialog.setLocationRelativeTo( PopupResultHandler.this ); |
| 368 |
dialog.pack(); |
| 369 |
dialog.setVisible( true ); |
| 370 |
} |
| 371 |
} |
| 372 |
} |