| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.GridBagConstraints; |
| 5 |
import java.awt.GridBagLayout; |
| 6 |
import java.awt.Insets; |
| 7 |
import java.io.StringWriter; |
| 8 |
import java.io.PrintWriter; |
| 9 |
import javax.swing.BorderFactory; |
| 10 |
import javax.swing.Box; |
| 11 |
import javax.swing.JComponent; |
| 12 |
import javax.swing.JLabel; |
| 13 |
import javax.swing.JPanel; |
| 14 |
import javax.swing.JScrollPane; |
| 15 |
import javax.swing.JSplitPane; |
| 16 |
import javax.swing.JTextField; |
| 17 |
import javax.swing.JTextArea; |
| 18 |
import javax.swing.event.ChangeEvent; |
| 19 |
import javax.swing.event.ChangeListener; |
| 20 |
import org.astrogrid.samp.Client; |
| 21 |
import org.astrogrid.samp.Message; |
| 22 |
import org.astrogrid.samp.Metadata; |
| 23 |
import org.astrogrid.samp.Response; |
| 24 |
import org.astrogrid.samp.SampMap; |
| 25 |
import org.astrogrid.samp.SampUtils; |
| 26 |
|
| 27 |
/** |
| 28 |
* Component which displays the details of a given Transmission object. |
| 29 |
* |
| 30 |
* @author Mark Taylor |
| 31 |
* @since 5 Dec 2008 |
| 32 |
*/ |
| 33 |
public class TransmissionPanel extends JPanel { |
| 34 |
|
| 35 |
private final JTextField mtypeField_; |
| 36 |
private final JTextField idField_; |
| 37 |
private final JTextField senderField_; |
| 38 |
private final JTextField receiverField_; |
| 39 |
private final JTextField statusField_; |
| 40 |
private final JTextArea messageField_; |
| 41 |
private final JTextArea responseField_; |
| 42 |
private final ChangeListener changeListener_; |
| 43 |
private Transmission trans_; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
*/ |
| 48 |
public TransmissionPanel() { |
| 49 |
super( new BorderLayout() ); |
| 50 |
|
| 51 |
// Panel displaying one-line metadata items. |
| 52 |
Stack metaPanel = new Stack(); |
| 53 |
metaPanel.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) ); |
| 54 |
mtypeField_ = metaPanel.addField( "MType" ); |
| 55 |
idField_ = metaPanel.addField( "Message ID" ); |
| 56 |
senderField_ = metaPanel.addField( "Sender" ); |
| 57 |
receiverField_ = metaPanel.addField( "Receiver" ); |
| 58 |
statusField_ = metaPanel.addField( "Status" ); |
| 59 |
|
| 60 |
// Panel displaying Message content. |
| 61 |
messageField_ = new JTextArea(); |
| 62 |
messageField_.setEditable( false ); |
| 63 |
messageField_.setLineWrap( false ); |
| 64 |
JComponent messagePanel = new JPanel( new BorderLayout() ); |
| 65 |
JComponent messageHeadBox = Box.createHorizontalBox(); |
| 66 |
messageHeadBox.add( new JLabel( "Message" ) ); |
| 67 |
messageHeadBox.add( Box.createHorizontalGlue() ); |
| 68 |
messagePanel.add( messageHeadBox, BorderLayout.NORTH ); |
| 69 |
messagePanel.add( new JScrollPane( messageField_ ), |
| 70 |
BorderLayout.CENTER ); |
| 71 |
|
| 72 |
// Panel displaying Response content. |
| 73 |
responseField_ = new JTextArea(); |
| 74 |
responseField_.setEditable( false ); |
| 75 |
responseField_.setLineWrap( false ); |
| 76 |
JComponent responsePanel = new JPanel( new BorderLayout() ); |
| 77 |
JComponent responseHeadBox = Box.createHorizontalBox(); |
| 78 |
responseHeadBox.add( new JLabel( "Response" ) ); |
| 79 |
responseHeadBox.add( Box.createHorizontalGlue() ); |
| 80 |
responsePanel.add( responseHeadBox, BorderLayout.NORTH ); |
| 81 |
responsePanel.add( new JScrollPane( responseField_ ), |
| 82 |
BorderLayout.CENTER ); |
| 83 |
|
| 84 |
// Place panels. |
| 85 |
JSplitPane splitter = new JSplitPane( JSplitPane.VERTICAL_SPLIT ); |
| 86 |
splitter.setTopComponent( messagePanel ); |
| 87 |
splitter.setBottomComponent( responsePanel ); |
| 88 |
splitter.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) ); |
| 89 |
splitter.setResizeWeight( 0.5 ); |
| 90 |
add( metaPanel, BorderLayout.NORTH ); |
| 91 |
add( splitter, BorderLayout.CENTER ); |
| 92 |
|
| 93 |
// Prepare a listener to react to changes in the displayed |
| 94 |
// transmission object. |
| 95 |
changeListener_ = new ChangeListener() { |
| 96 |
public void stateChanged( ChangeEvent evt ) { |
| 97 |
updateState(); |
| 98 |
} |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Sets the transmission object being displayed. |
| 104 |
* |
| 105 |
* @param trans transmission object to display |
| 106 |
*/ |
| 107 |
public void setTransmission( Transmission trans ) { |
| 108 |
if ( trans_ != null ) { |
| 109 |
trans_.removeChangeListener( changeListener_ ); |
| 110 |
} |
| 111 |
trans_ = trans; |
| 112 |
if ( trans_ != null ) { |
| 113 |
trans_.addChangeListener( changeListener_ ); |
| 114 |
} |
| 115 |
updateState(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the transmission object currently being displayed. |
| 120 |
* |
| 121 |
* @return transmission |
| 122 |
*/ |
| 123 |
public Transmission getTransmission() { |
| 124 |
return trans_; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Invoked whenever the displayed transmission, or its characteristics, |
| 129 |
* change. |
| 130 |
*/ |
| 131 |
private void updateState() { |
| 132 |
if ( trans_ == null ) { |
| 133 |
mtypeField_.setText( null ); |
| 134 |
idField_.setText( null ); |
| 135 |
senderField_.setText( null ); |
| 136 |
receiverField_.setText( null ); |
| 137 |
statusField_.setText( null ); |
| 138 |
messageField_.setText( null ); |
| 139 |
responseField_.setText( null ); |
| 140 |
} |
| 141 |
else { |
| 142 |
Message msg = trans_.getMessage(); |
| 143 |
Response response = trans_.getResponse(); |
| 144 |
Throwable error = trans_.getError(); |
| 145 |
mtypeField_.setText( msg.getMType() ); |
| 146 |
mtypeField_.setCaretPosition( 0 ); |
| 147 |
idField_.setText( formatId( trans_ ) ); |
| 148 |
idField_.setCaretPosition( 0 ); |
| 149 |
senderField_.setText( formatClient( trans_.getSender() ) ); |
| 150 |
senderField_.setCaretPosition( 0 ); |
| 151 |
receiverField_.setText( formatClient( trans_.getReceiver() ) ); |
| 152 |
receiverField_.setCaretPosition( 0 ); |
| 153 |
statusField_.setText( trans_.getStatus().getText() ); |
| 154 |
statusField_.setCaretPosition( 0 ); |
| 155 |
messageField_.setText( SampUtils.formatObject( msg, 2 ) ); |
| 156 |
messageField_.setCaretPosition( 0 ); |
| 157 |
String responseText = response == null |
| 158 |
? null |
| 159 |
: SampUtils.formatObject( response, 2 ); |
| 160 |
final String errorText; |
| 161 |
if ( error == null ) { |
| 162 |
errorText = null; |
| 163 |
} |
| 164 |
else { |
| 165 |
StringWriter traceWriter = new StringWriter(); |
| 166 |
error.printStackTrace( new PrintWriter( traceWriter ) ); |
| 167 |
errorText = traceWriter.toString(); |
| 168 |
} |
| 169 |
StringBuffer rbuf = new StringBuffer(); |
| 170 |
if ( responseText != null ) { |
| 171 |
rbuf.append( responseText ); |
| 172 |
} |
| 173 |
if ( errorText != null ) { |
| 174 |
if ( rbuf.length() > 0 ) { |
| 175 |
rbuf.append( "\n\n" ); |
| 176 |
} |
| 177 |
rbuf.append( errorText ); |
| 178 |
} |
| 179 |
responseField_.setText( rbuf.toString() ); |
| 180 |
responseField_.setCaretPosition( 0 ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Formats the identifier of a transmission as a string. |
| 186 |
* |
| 187 |
* @param trans transmission |
| 188 |
* @return id string |
| 189 |
*/ |
| 190 |
private static String formatId( Transmission trans ) { |
| 191 |
String msgTag = trans.getMessageTag(); |
| 192 |
String msgId = trans.getMessageId(); |
| 193 |
StringBuffer idBuf = new StringBuffer(); |
| 194 |
if ( msgTag != null ) { |
| 195 |
idBuf.append( "Tag: " ) |
| 196 |
.append( msgTag ); |
| 197 |
} |
| 198 |
if ( msgId != null ) { |
| 199 |
if ( idBuf.length() > 0 ) { |
| 200 |
idBuf.append( "; " ); |
| 201 |
} |
| 202 |
idBuf.append( "ID: " ) |
| 203 |
.append( msgId ); |
| 204 |
} |
| 205 |
return idBuf.toString(); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Formats a client as a string. |
| 210 |
* |
| 211 |
* @param client client |
| 212 |
* @return string |
| 213 |
*/ |
| 214 |
private static String formatClient( Client client ) { |
| 215 |
StringBuffer sbuf = new StringBuffer(); |
| 216 |
sbuf.append( client.getId() ); |
| 217 |
Metadata meta = client.getMetadata(); |
| 218 |
if ( meta != null ) { |
| 219 |
String name = meta.getName(); |
| 220 |
if ( name != null ) { |
| 221 |
sbuf.append( ' ' ) |
| 222 |
.append( '(' ) |
| 223 |
.append( name ) |
| 224 |
.append( ')' ); |
| 225 |
} |
| 226 |
} |
| 227 |
return sbuf.toString(); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Component for aligning headings and text fields for metadata display. |
| 232 |
*/ |
| 233 |
private class Stack extends JPanel { |
| 234 |
private final GridBagLayout layer_; |
| 235 |
private int line_; |
| 236 |
|
| 237 |
/** |
| 238 |
* Constructor. |
| 239 |
*/ |
| 240 |
Stack() { |
| 241 |
layer_ = new GridBagLayout(); |
| 242 |
setLayout( layer_ ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Adds an item. |
| 247 |
* |
| 248 |
* @param heading text heading for item |
| 249 |
* @return text field associated with heading |
| 250 |
*/ |
| 251 |
JTextField addField( String heading ) { |
| 252 |
GridBagConstraints cons = new GridBagConstraints(); |
| 253 |
cons.gridy = line_++; |
| 254 |
cons.weighty = 1; |
| 255 |
cons.insets = new Insets( 2, 2, 2, 2 ); |
| 256 |
cons.gridx = 0; |
| 257 |
cons.fill = GridBagConstraints.HORIZONTAL; |
| 258 |
cons.anchor = GridBagConstraints.WEST; |
| 259 |
cons.weightx = 0; |
| 260 |
JLabel label = new JLabel( heading + ":" ); |
| 261 |
JTextField field = new JTextField(); |
| 262 |
field.setEditable( false ); |
| 263 |
layer_.setConstraints( label, cons ); |
| 264 |
add( label ); |
| 265 |
cons.gridx++; |
| 266 |
cons.weightx = 1; |
| 267 |
layer_.setConstraints( field, cons ); |
| 268 |
add( field ); |
| 269 |
return field; |
| 270 |
} |
| 271 |
} |
| 272 |
} |