- Timestamp:
- 10/02/18 10:28:51 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/gui/MessageBay.java
r1125 r1183 35 35 36 36 /** 37 * The bay at the bottom of the expeditee browser which displays messages. 38 * TODO:Make it thread safe!37 * The bay at the bottom of the expeditee browser which displays messages. TODO: 38 * Make it thread safe! 39 39 */ 40 40 public final class MessageBay { … … 54 54 /** The list of messages shown in the message bay. */ 55 55 private static List<Item> _messages = new LinkedList<Item>(); 56 57 /** Messages which were delayed because they couldn't be shown at time of creation. */ 56 57 /** 58 * Messages which were delayed because they couldn't be shown at time of 59 * creation. 60 */ 58 61 private static List<DelayedMessage> _delayedMessages = new LinkedList<DelayedMessage>(); 59 62 … … 86 89 87 90 /** Static-only class. */ 88 private MessageBay() 89 { 90 } 91 91 private MessageBay() { 92 } 93 92 94 /** Whether the message bay is ready to display messages. */ 93 public static boolean isReady() 94 { 95 public static boolean isReady() { 95 96 return Browser.isInitComplete(); 96 97 } 97 98 98 99 /** Syncs message bay size according to FrameGraphics max size. */ 99 static void updateSize() 100 { 101 for(Item i : _messages) { 102 if(i != null) { 100 private static void updateSize() { 101 for (Item i : _messages) { 102 if (i != null) { 103 103 i.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY()); 104 104 } … … 111 111 112 112 /** Whether the given item is an item in the message bay. */ 113 public static boolean isMessageItem(Item i) 114 { 113 public static boolean isMessageItem(Item i) { 115 114 return _messages.contains(i) || i == _messageLink || i == _status; 116 115 } 117 116 118 117 /** TODO: Comment. cts16 */ 119 public synchronized static Item getMessageLink() 120 { 118 public synchronized static Item getMessageLink() { 121 119 return _messageLink; 122 120 } 123 121 124 122 /** TODO: Comment. cts16 */ 125 public synchronized static List<Item> getMessages() 126 { 123 public synchronized static List<Item> getMessages() { 127 124 return _messages; 128 125 } 129 126 130 127 /** Causes the entire message bay area to be invalidated. */ 131 public synchronized static void invalidateFullBay() 132 { 128 public synchronized static void invalidateFullBay() { 133 129 DisplayController.invalidateArea(DisplayController.getMessageBayPaintArea()); 134 130 } 135 131 136 132 /** TODO: Comment. cts16 */ 137 private static void updateLink() 138 {139 if (!isLinkInitialized && DisplayController.getFramePaintArea() != null&& DisplayController.getFramePaintArea().getWidth() > 0) {133 private static void updateLink() { 134 if (!isLinkInitialized && DisplayController.getFramePaintArea() != null 135 && DisplayController.getFramePaintArea().getWidth() > 0) { 140 136 // set up 'Messages' link on the right hand side 141 _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET, MESSAGE_LINK_X); 137 _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET, 138 MESSAGE_LINK_X); 142 139 _messageLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY()); 143 140 isLinkInitialized = true; 144 141 } else { 145 _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET, MESSAGE_LINK_X);146 }147 }148 149 /** TODO: Comment. cts16 */ 150 public static Image getImage(Clip clip, Dimension size)151 {142 _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET, 143 MESSAGE_LINK_X); 144 } 145 } 146 147 /** TODO: Comment. cts16 */ 148 public static Image getImage(Clip clip, Dimension size) { 152 149 // Can't get an image with an invalid size 153 if (size == null || size.width <= 0 || size.height <= 0) return null; 150 if (size == null || size.width <= 0 || size.height <= 0) { 151 return null; 152 } 154 153 155 154 // Update the buffer 156 155 updateBuffer(Item.DEFAULT_BACKGROUND, clip, size); 157 156 158 157 // Return the image buffer 159 158 return _messageBuffer; … … 161 160 162 161 /** Updates the image buffer to reflect the current state of the message bay. */ 163 private static void updateBuffer(Colour background, Clip clip, Dimension size) 164 { 162 private synchronized static void updateBuffer(Colour background, Clip clip, Dimension size) { 165 163 // If the buffer doesn't exist or is the wrong size, recreate it 166 164 if (_messageBuffer == null || !_messageBuffer.getSize().equals(size)) { … … 173 171 g.pushDrawingSurface(_messageBuffer); 174 172 175 if (clip != null) g.pushClip(clip); 173 if (clip != null) { 174 g.pushClip(clip); 175 } 176 176 g.setAntialiasing(true); 177 177 178 178 g.clear(background); 179 179 180 180 g.setFont(_messageFont); 181 182 for (Item message : _messages) { //TODO: Solve occasional ConcurrentModificationException, possible culprit in overwriteMessage181 182 for (Item message : _messages) { 183 183 if (message != null) { 184 184 if (clip == null || clip.isNotClipped() || message.isInDrawingArea(clip.getBounds())) { … … 187 187 } 188 188 } 189 190 if (_status != null) FrameGraphics.PaintItem(_status); 189 190 if (_status != null) { 191 FrameGraphics.PaintItem(_status); 192 } 191 193 192 194 if (clip == null || clip.isNotClipped() || _messageLink.isInDrawingArea(clip.getBounds())) { 193 195 FrameGraphics.PaintItem(_messageLink); 194 196 } 195 197 196 198 g.popDrawingSurface(); 197 199 } 198 200 199 201 /** TODO: Comment. cts16 */ 200 private static Text displayMessage(String message, String link, List<String> actions, Colour color) 201 { 202 private static Text displayMessage(String message, String link, List<String> actions, Colour color) { 202 203 return displayMessage(message, link, actions, color, true); 203 204 } 204 205 205 206 /** TODO: Comment. cts16 */ 206 public synchronized static Text displayMessage(String message, String link, Colour color, boolean displayAlways, String action)207 {207 public synchronized static Text displayMessage(String message, String link, Colour color, boolean displayAlways, 208 String action) { 208 209 List<String> actions = new LinkedList<String>(); 209 if (action != null) actions.add(action); 210 if (action != null) { 211 actions.add(action); 212 } 210 213 return displayMessage(message, link, actions, color, displayAlways); 211 214 } 212 215 213 216 /** TODO: Comment. cts16 */ 214 private static Text newMessage(String message, String link, List<String> actions, Colour color) 215 { 217 private static Text newMessage(String message, String link, List<String> actions, Colour color) { 216 218 Text t = new Text(getMessagePrefix(true) + message); 217 219 t.setPosition(20, 15 + _messages.size() * 25); … … 222 224 t.setFont(_messageFont.clone()); 223 225 _creator.addItem(t.copy(), true); 224 if(link == null) t.setLink(_creator.getCurrent()); 226 if (link == null) { 227 t.setLink(_creator.getCurrent()); 228 } 225 229 return t; 226 230 } 227 231 228 232 /** TODO: Comment. cts16 */ 229 private synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways, boolean redraw)230 {233 private synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color, 234 boolean displayAlways, boolean redraw) { 231 235 assert (message != null); 232 236 233 237 if (!isReady()) { 234 238 delayMessage(message, link, actions, color, displayAlways, redraw); 235 239 return null; 236 240 } 237 241 238 242 System.out.println(message); 239 243 … … 241 245 invalidateFullBay(); 242 246 243 if (_suppressMessages) return null; 247 if (_suppressMessages) { 248 return null; 249 } 244 250 245 251 if (!displayAlways && message.equals(_lastMessage)) { … … 247 253 return null; 248 254 } 249 255 250 256 _lastMessage = message; 251 257 252 258 if (_creator == null) { 253 _creator = new FrameCreator(MESSAGES_FRAMESET_NAME, FrameIO.MESSAGES_PATH, MESSAGES_FRAMESET_NAME, true, false); 259 _creator = new FrameCreator(MESSAGES_FRAMESET_NAME, FrameIO.MESSAGES_PATH, MESSAGES_FRAMESET_NAME, true, 260 false); 254 261 } 255 262 … … 257 264 updateLink(); 258 265 259 if (_messages.size() >= 3) {266 if (_messages.size() >= 3) { 260 267 _messages.remove(0); 261 for (Item i : _messages) {268 for (Item i : _messages) { 262 269 i.setY(i.getY() - 25); 263 270 } 264 271 } 265 272 266 273 Text t = newMessage(message, link, actions, color); 267 274 268 275 _messages.add(t); 269 276 270 277 // update the link to the latest message frame 271 278 _messageLink.setLink(_creator.getCurrent()); 272 279 273 280 // TODO: Can we just make this DisplayController.requestRefresh()? cts16 274 if (redraw) {281 if (redraw) { 275 282 DisplayController.requestRefresh(true); 276 283 } … … 280 287 281 288 /** TODO: Comment. cts16 */ 282 public synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways)283 {289 public synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color, 290 boolean displayAlways) { 284 291 return displayMessage(message, link, actions, color, displayAlways, true); 285 292 } 286 293 287 294 /** TODO: Comment. cts16 */ 288 public synchronized static void overwriteMessage(String message) 289 { 295 public synchronized static void overwriteMessage(String message) { 290 296 overwriteMessage(message, null); 291 297 } 292 298 293 299 /** TODO: Comment. cts16 */ 294 public synchronized static void overwriteMessage(String message, Colour color) 295 { 300 public synchronized static void overwriteMessage(String message, Colour color) { 296 301 _messages.remove(_messages.size() - 1); 297 302 Text t = newMessage(message, null, null, color); … … 301 306 302 307 /** TODO: Comment. cts16 */ 303 private static String getMessagePrefix(int counter) 304 { 308 private static String getMessagePrefix(int counter) { 305 309 return "@" + counter + ": "; 306 310 } 307 311 308 312 /** TODO: Comment. cts16 */ 309 private static String getMessagePrefix(boolean incrementCounter) 310 { 311 if (incrementCounter) _messageCount++; 312 313 private static String getMessagePrefix(boolean incrementCounter) { 314 if (incrementCounter) { 315 _messageCount++; 316 } 317 313 318 return getMessagePrefix(_messageCount); 314 319 } … … 321 326 * the message to be displayed 322 327 */ 323 public synchronized static Text linkedErrorMessage(String message) 324 { 325 if (_suppressMessages) return null; 328 public synchronized static Text linkedErrorMessage(String message) { 329 if (_suppressMessages) { 330 return null; 331 } 326 332 Misc.beep(); 327 333 String[] tokens = message.split(Text.FRAME_NAME_SEPARATOR); 328 334 String link = null; 329 if (tokens.length > 1) link = tokens[tokens.length - 1]; 335 if (tokens.length > 1) { 336 link = tokens[tokens.length - 1]; 337 } 330 338 return displayMessage(message, link, null, ERROR_COLOR); 331 339 } 332 340 333 341 /** TODO: Comment. cts16 */ 334 public synchronized static Text errorMessage(String message) 335 { 336 if (_suppressMessages) return null; 342 public synchronized static Text errorMessage(String message) { 343 if (_suppressMessages) { 344 return null; 345 } 337 346 Misc.beep(); 338 347 return displayMessage(message, null, null, ERROR_COLOR, false); … … 346 355 * The message to display to the user in the message area 347 356 */ 348 public synchronized static Text displayMessage(String message) 349 { 357 public synchronized static Text displayMessage(String message) { 350 358 return displayMessageAlways(message); 351 359 } 352 360 353 361 /** TODO: Comment. cts16 */ 354 public synchronized static Text displayMessageOnce(String message) 355 { 362 public synchronized static Text displayMessageOnce(String message) { 356 363 return displayMessage(message, null, null, Colour.BLACK, false); 357 364 } 358 365 359 366 /** TODO: Comment. cts16 */ 360 public synchronized static Text displayMessage(String message, Colour textColor) 361 { 367 public synchronized static Text displayMessage(String message, Colour textColor) { 362 368 return displayMessage(message, null, null, textColor); 363 369 } 364 370 365 371 /** TODO: Comment. cts16 */ 366 public synchronized static Text displayMessage(Text message) 367 { 372 public synchronized static Text displayMessage(Text message) { 368 373 Text t = null; 369 374 String link = message.getLink(); … … 377 382 378 383 /** TODO: Comment. cts16 */ 379 public synchronized static Text displayMessageAlways(String message) 380 { 384 public synchronized static Text displayMessageAlways(String message) { 381 385 return displayMessage(message, null, null, Colour.BLACK); 382 386 // Misc.Beep(); … … 384 388 385 389 /** TODO: Comment. cts16 */ 386 public synchronized static Text warningMessage(String message) 387 { 390 public synchronized static Text warningMessage(String message) { 388 391 return displayMessage(message, null, null, Colour.MAGENTA); 389 392 } 390 393 391 394 /** TODO: Comment. cts16 */ 392 public synchronized static List<Text> warningMessages(List<String> messages) 393 { 394 if (messages == null) return null; 395 public synchronized static List<Text> warningMessages(List<String> messages) { 396 if (messages == null) { 397 return null; 398 } 395 399 List<Text> ret = new LinkedList<Text>(); 396 for (String message : messages) ret.add(warningMessage(message)); 400 for (String message : messages) { 401 ret.add(warningMessage(message)); 402 } 397 403 return ret; 398 404 } 399 405 400 406 /** TODO: Comment. cts16 */ 401 public synchronized static void suppressMessages(boolean val) 402 { 407 public synchronized static void suppressMessages(boolean val) { 403 408 _suppressMessages = val; 404 409 } 405 410 406 411 /** TODO: Comment. cts16 */ 407 public synchronized static void setStatus(String status) 408 { 412 public synchronized static void setStatus(String status) { 409 413 if (_status == null) { 410 414 _status = new Text(status); … … 417 421 } 418 422 419 // invalidateFullBay();423 // invalidateFullBay(); 420 424 DisplayController.requestRefresh(true); 421 425 } 422 426 423 427 /** TODO: Comment. cts16 */ 424 public static final class Progress 425 { 428 public static final class Progress { 426 429 /** The colour progress bars should be displayed in. */ 427 430 private static final Colour BAR_COLOUR = Colour.GREEN.darker(); 428 429 /** The character used to assemble the uncompleted portions of the progress bar. */ 431 432 /** 433 * The character used to assemble the uncompleted portions of the progress bar. 434 */ 430 435 private static final char UNCOMPLETED_CHARACTER = '\u2591'; // ░ 431 /** The character used to assemble the completed portions of the progress bar. */ 436 /** 437 * The character used to assemble the completed portions of the progress bar. 438 */ 432 439 private static final char COMPLETED_CHARACTER = '\u2592'; // ▒ 433 440 434 441 /** What the progress bar should look like when at 100% completion. */ 435 442 private static final String COMPLETED_BAR = Util.nCopiesOf(20, COMPLETED_CHARACTER); 436 443 /** What the progress bar should look like when at 0% completion. */ 437 444 private static final String UNCOMPLETED_BAR = Util.nCopiesOf(20, UNCOMPLETED_CHARACTER); 438 445 439 446 private String _message; 440 447 private Text _text; 441 442 protected Progress(String text) 443 { 448 449 protected Progress(String text) { 444 450 this._text = displayMessage(text, null, null, BAR_COLOUR, true, false); 445 451 this._message = this._text.getText(); … … 447 453 DisplayController.requestRefresh(true); 448 454 } 449 450 public void UpdateMessage(final String text, final int newProgress) throws Exception 451 { 455 456 public void UpdateMessage(final String text, final int newProgress) throws Exception { 452 457 this._message = text; 453 458 set(newProgress); 454 459 } 455 456 public String GetMessage() 457 { 460 461 public String GetMessage() { 458 462 return _message; 459 463 } 460 464 461 465 /** 462 466 * 463 * @param progress progress value from 0 to 100 464 * @return true if the progress was updated, false if the progress was off the screen 465 * @throws Exception if progress out of bounds 467 * @param progress 468 * progress value from 0 to 100 469 * @return true if the progress was updated, false if the progress was off the 470 * screen 471 * @throws Exception 472 * if progress out of bounds 466 473 */ 467 public boolean set(int progress) throws Exception 468 { 469 if(progress < 0 || progress > 100) throw new Exception("Progress value out of bounds"); 474 public boolean set(int progress) throws Exception { 475 if (progress < 0 || progress > 100) { 476 throw new Exception("Progress value out of bounds"); 477 } 470 478 int p = progress / 5; 471 if(isMessageItem(this._text)) { 472 this._text.setText(this._message + " [" + COMPLETED_BAR.substring(0, p) + UNCOMPLETED_BAR.substring(p) + "] " + progress + "%"); 479 if (isMessageItem(this._text)) { 480 this._text.setText(this._message + " [" + COMPLETED_BAR.substring(0, p) + UNCOMPLETED_BAR.substring(p) 481 + "] " + progress + "%"); 473 482 DisplayController.requestRefresh(true); 474 483 return true; 475 484 } 476 return false; 477 } 478 } 479 480 /** TODO: Comment. cts16 */ 481 public synchronized static Progress displayProgress(String message) 482 { 485 return false; 486 } 487 } 488 489 /** TODO: Comment. cts16 */ 490 public synchronized static Progress displayProgress(String message) { 483 491 return new Progress(message); 484 492 } 485 486 /** Remembers the arguments to a displayMessage call for later use. */ 493 494 /** Remembers the arguments to a displayMessage call for later use. */ 487 495 private static class DelayedMessage { 488 496 489 497 private String _message; 490 498 private String _link; … … 493 501 private boolean _displayAlways; 494 502 private boolean _redraw; 495 496 public DelayedMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways, boolean redraw)497 {503 504 public DelayedMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways, 505 boolean redraw) { 498 506 _message = message; 499 507 _link = link; … … 501 509 _actions = null; 502 510 } else { 503 _actions = 511 _actions = new LinkedList<String>(); 504 512 _actions.addAll(actions); 505 513 } … … 508 516 _redraw = redraw; 509 517 } 510 511 public void display() 512 { 518 519 public void display() { 513 520 displayMessage(_message, _link, _actions, _colour, _displayAlways, _redraw); 514 521 } 515 516 } 517 518 private static void delayMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways, boolean redraw)519 {522 523 } 524 525 private static void delayMessage(String message, String link, List<String> actions, Colour color, 526 boolean displayAlways, boolean redraw) { 520 527 _delayedMessages.add(new DelayedMessage(message, link, actions, color, displayAlways, redraw)); 521 528 } 522 523 public static void showDelayedMessages() 524 { 529 530 public static void showDelayedMessages(final boolean requestRefresh) { 525 531 if (isReady()) { 526 for (DelayedMessage message : _delayedMessages) message.display(); 532 for (DelayedMessage message : _delayedMessages) { 533 message.display(); 534 } 527 535 _delayedMessages.clear(); 528 536 invalidateFullBay(); 529 DisplayController.requestRefresh(true); 537 if(requestRefresh) { 538 DisplayController.requestRefresh(true); 539 } 530 540 } 531 541 }
Note:
See TracChangeset
for help on using the changeset viewer.