| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.Color; |
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Graphics; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.Iterator; |
| 8 |
import java.util.List; |
| 9 |
import java.util.logging.Level; |
| 10 |
import java.util.logging.Logger; |
| 11 |
import javax.swing.Icon; |
| 12 |
import javax.swing.SwingUtilities; |
| 13 |
import javax.swing.event.ChangeEvent; |
| 14 |
import javax.swing.event.ChangeListener; |
| 15 |
import org.astrogrid.samp.Client; |
| 16 |
import org.astrogrid.samp.Message; |
| 17 |
import org.astrogrid.samp.Response; |
| 18 |
|
| 19 |
/** |
| 20 |
* Describes the properties of a message which has been sent from one |
| 21 |
* client to another. Methods which might change the state of instances |
| 22 |
* of this class should be invoked only from the AWT event dispatch thread. |
| 23 |
* |
| 24 |
* @author Mark Taylor |
| 25 |
* @since 20 Nov 2008 |
| 26 |
*/ |
| 27 |
public class Transmission { |
| 28 |
|
| 29 |
private final Client sender_; |
| 30 |
private final Client receiver_; |
| 31 |
private final Message msg_; |
| 32 |
private final String msgId_; |
| 33 |
private final String msgTag_; |
| 34 |
private final List listenerList_; |
| 35 |
private final ChangeEvent evt_; |
| 36 |
private Response response_; |
| 37 |
private Throwable error_; |
| 38 |
private boolean senderUnreg_; |
| 39 |
private boolean receiverUnreg_; |
| 40 |
private long doneTime_; |
| 41 |
private static final Logger logger_ = |
| 42 |
Logger.getLogger( Transmission.class.getName() ); |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor. |
| 46 |
* |
| 47 |
* @param sender sender |
| 48 |
* @param receiver receiver |
| 49 |
* @param msg message |
| 50 |
* @param msgTag message tag |
| 51 |
* @param msgId message ID |
| 52 |
*/ |
| 53 |
public Transmission( Client sender, Client receiver, Message msg, |
| 54 |
String msgTag, String msgId ) { |
| 55 |
sender_ = sender; |
| 56 |
receiver_ = receiver; |
| 57 |
msg_ = msg; |
| 58 |
msgTag_ = msgTag; |
| 59 |
msgId_ = msgId; |
| 60 |
listenerList_ = new ArrayList(); |
| 61 |
evt_ = new ChangeEvent( this ); |
| 62 |
doneTime_ = Long.MAX_VALUE; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the client which sent this transmission. |
| 67 |
* |
| 68 |
* @return sender |
| 69 |
*/ |
| 70 |
public Client getSender() { |
| 71 |
return sender_; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns the client to which this transmission was sent. |
| 76 |
* |
| 77 |
* @return receiver |
| 78 |
*/ |
| 79 |
public Client getReceiver() { |
| 80 |
return receiver_; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the message which was sent. |
| 85 |
* |
| 86 |
* @return message |
| 87 |
*/ |
| 88 |
public Message getMessage() { |
| 89 |
return msg_; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the message tag corresponding to this transmission. |
| 94 |
* Will be null for notify-type sends. |
| 95 |
* |
| 96 |
* @return msg tag |
| 97 |
*/ |
| 98 |
public String getMessageTag() { |
| 99 |
return msgTag_; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the message ID associated with this message. |
| 104 |
* This is the identifier passed to the receiver which it uses to |
| 105 |
* match messages with responses; it will be null iff the transmission |
| 106 |
* used the <em>notify</em> delivery pattern (no response expected). |
| 107 |
* |
| 108 |
* @return msgId; possibly null |
| 109 |
*/ |
| 110 |
public String getMessageId() { |
| 111 |
return msgId_; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Sets the response for this transmission. |
| 116 |
* |
| 117 |
* @param response response |
| 118 |
*/ |
| 119 |
public void setResponse( Response response ) { |
| 120 |
response_ = response; |
| 121 |
fireChange(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the response for this transmission. |
| 126 |
* Will be null if no response has (yet) arrived. |
| 127 |
* |
| 128 |
* @return response |
| 129 |
*/ |
| 130 |
public Response getResponse() { |
| 131 |
return response_; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Associates an error with this transmission. |
| 136 |
* This is probably an indication that the send failed or some other |
| 137 |
* non-SAMP event intervened to prevent normal resolution. |
| 138 |
* |
| 139 |
* @param error throwable causing the failure |
| 140 |
*/ |
| 141 |
public void setError( Throwable error ) { |
| 142 |
error_ = error; |
| 143 |
logger_.log( Level.WARNING, |
| 144 |
"Error in hub operation: " + error.getMessage(), error ); |
| 145 |
fireChange(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns a Throwable which prevented normal resolution of this |
| 150 |
* transmission. |
| 151 |
* |
| 152 |
* @return error |
| 153 |
*/ |
| 154 |
public Throwable getError() { |
| 155 |
return error_; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Indicates that the sender of this transmission has unregistered. |
| 160 |
*/ |
| 161 |
public void setSenderUnregistered() { |
| 162 |
senderUnreg_ = true; |
| 163 |
fireChange(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Indicates that the receiver of this transmission has unregistered. |
| 168 |
*/ |
| 169 |
public void setReceiverUnregistered() { |
| 170 |
receiverUnreg_ = true; |
| 171 |
fireChange(); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Returns the epoch at which this transmission was completed. |
| 176 |
* If it is still pending ({@link #isDone()}==false), |
| 177 |
* the returned value will be (way) in the future. |
| 178 |
* |
| 179 |
* @return value of <code>System.currentTimeMillis()</code> at which |
| 180 |
* {@link #isDone} first returned true |
| 181 |
*/ |
| 182 |
public long getDoneTime() { |
| 183 |
return doneTime_; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Indicates whether further changes to the state of this object |
| 188 |
* are expected, that is if a response/failure is yet to be received. |
| 189 |
* |
| 190 |
* @return true iff no further changes are expected |
| 191 |
*/ |
| 192 |
public boolean isDone() { |
| 193 |
return error_ != null |
| 194 |
|| response_ != null |
| 195 |
|| ( msgId_ == null && msgTag_ == null ) |
| 196 |
|| receiverUnreg_; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Returns an object which describes the current status of this |
| 201 |
* transmission in terms which can be presented to the GUI. |
| 202 |
*/ |
| 203 |
public Status getStatus() { |
| 204 |
if ( error_ != null ) { |
| 205 |
return Status.EXCEPTION; |
| 206 |
} |
| 207 |
else if ( response_ != null ) { |
| 208 |
String status = response_.getStatus(); |
| 209 |
if ( Response.OK_STATUS.equals( status ) ) { |
| 210 |
return Status.OK; |
| 211 |
} |
| 212 |
else if ( Response.WARNING_STATUS.equals( status ) ) { |
| 213 |
return Status.WARNING; |
| 214 |
} |
| 215 |
else if ( Response.ERROR_STATUS.equals( status ) ) { |
| 216 |
return Status.ERROR; |
| 217 |
} |
| 218 |
else if ( status == null ) { |
| 219 |
return Status.NONE; |
| 220 |
} |
| 221 |
else { |
| 222 |
return new Status( "Completed (" + status + ")", |
| 223 |
Status.WARNING_COLOR, true ); |
| 224 |
} |
| 225 |
} |
| 226 |
else if ( msgId_ == null && msgTag_ == null ) { |
| 227 |
return Status.NOTIFIED; |
| 228 |
} |
| 229 |
else if ( receiverUnreg_ ) { |
| 230 |
return Status.ORPHANED; |
| 231 |
} |
| 232 |
else { |
| 233 |
assert ! isDone(); |
| 234 |
return Status.PENDING; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Adds a listener which will be notified if the state of this transmission |
| 240 |
* changes (if a response or failure is signalled). |
| 241 |
* The {@link javax.swing.event.ChangeEvent}s sent to these listeners |
| 242 |
* will have a source which is this Transmission. |
| 243 |
* |
| 244 |
* @param listener listener to add |
| 245 |
*/ |
| 246 |
public void addChangeListener( ChangeListener listener ) { |
| 247 |
listenerList_.add( listener ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Removes a listener previously added by {@link #addChangeListener}. |
| 252 |
* |
| 253 |
* @param listener listener to remove |
| 254 |
*/ |
| 255 |
public void removeChangeListener( ChangeListener listener ) { |
| 256 |
listenerList_.remove( listener ); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Notifies listeners of a state change. |
| 261 |
*/ |
| 262 |
private void fireChange() { |
| 263 |
if ( doneTime_ == Long.MAX_VALUE && isDone() ) { |
| 264 |
doneTime_ = System.currentTimeMillis(); |
| 265 |
} |
| 266 |
for ( Iterator it = listenerList_.iterator(); |
| 267 |
it.hasNext(); ) { |
| 268 |
ChangeListener listener = (ChangeListener) it.next(); |
| 269 |
listener.stateChanged( evt_ ); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Describes the status of a transmission in terms that can be |
| 275 |
* presented in the GUI. |
| 276 |
*/ |
| 277 |
public static class Status { |
| 278 |
|
| 279 |
private final String text_; |
| 280 |
private final Color iconColor_; |
| 281 |
private final boolean isDone_; |
| 282 |
|
| 283 |
private final static Color PENDING_COLOR = Color.BLACK; |
| 284 |
private final static Color OK_COLOR = new Color( 0x00c000 ); |
| 285 |
private final static Color ERROR_COLOR = new Color( 0xc00000 ); |
| 286 |
private final static Color WARNING_COLOR = new Color( 0x806030 ); |
| 287 |
private final static Color NOTIFY_COLOR = new Color( 0x808080 ); |
| 288 |
|
| 289 |
private final static Status OK = |
| 290 |
new Status( "Success", OK_COLOR, true ); |
| 291 |
private final static Status WARNING = |
| 292 |
new Status( "Warning", WARNING_COLOR, true ); |
| 293 |
private final static Status ERROR = |
| 294 |
new Status( "Error", ERROR_COLOR, true ); |
| 295 |
private final static Status NONE = |
| 296 |
new Status( "Completed (??)", WARNING_COLOR, true ); |
| 297 |
private final static Status NOTIFIED = |
| 298 |
new Status( "Notified", NOTIFY_COLOR, true ); |
| 299 |
private final static Status EXCEPTION = |
| 300 |
new Status( "Exception", ERROR_COLOR, true ); |
| 301 |
private final static Status ORPHANED = |
| 302 |
new Status( "Orphaned", WARNING_COLOR, true ); |
| 303 |
private final static Status PENDING = |
| 304 |
new Status( "...pending...", PENDING_COLOR, false ); |
| 305 |
|
| 306 |
/** |
| 307 |
* Constructor. |
| 308 |
* |
| 309 |
* @param text short status summary |
| 310 |
* @param iconColor colour to plot icon |
| 311 |
* @param isDone whether status represents completed processing |
| 312 |
*/ |
| 313 |
Status( String text, Color iconColor, boolean isDone ) { |
| 314 |
text_ = text; |
| 315 |
iconColor_ = iconColor; |
| 316 |
isDone_ = isDone; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Returns the text for this status. |
| 321 |
* |
| 322 |
* @return short summmary |
| 323 |
*/ |
| 324 |
public String getText() { |
| 325 |
return text_; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Returns a little icon representing status. |
| 330 |
* |
| 331 |
* @param height required height of icon |
| 332 |
* @return icon |
| 333 |
*/ |
| 334 |
public Icon getIcon( final int height ) { |
| 335 |
final int width = (int) Math.floor( 0.866 * height ); |
| 336 |
return new Icon() { |
| 337 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 338 |
int[] xs = new int[] { x, x + width, x, }; |
| 339 |
int[] ys = new int[] { y, y + height / 2, y + height, }; |
| 340 |
Color gcolor = g.getColor(); |
| 341 |
g.setColor( iconColor_ ); |
| 342 |
if ( isDone_ ) { |
| 343 |
g.drawPolygon( xs, ys, 3 ); |
| 344 |
} |
| 345 |
else { |
| 346 |
g.fillPolygon( xs, ys, 3 ); |
| 347 |
} |
| 348 |
g.setColor( gcolor ); |
| 349 |
} |
| 350 |
public int getIconWidth() { |
| 351 |
return width; |
| 352 |
} |
| 353 |
public int getIconHeight() { |
| 354 |
return height; |
| 355 |
} |
| 356 |
}; |
| 357 |
} |
| 358 |
|
| 359 |
public String toString() { |
| 360 |
return text_; |
| 361 |
} |
| 362 |
} |
| 363 |
} |