source: trunk/src/org/expeditee/gui/FrameGraphics.java@ 181

Last change on this file since 181 was 181, checked in by ra33, 16 years ago

Work on Bridging the Gap between SimpleStatements and Actions/Agents

File size: 25.4 KB
Line 
1package org.expeditee.gui;
2
3import java.awt.Color;
4import java.awt.Dimension;
5import java.awt.EventQueue;
6import java.awt.Graphics;
7import java.awt.Graphics2D;
8import java.awt.GraphicsEnvironment;
9import java.awt.Image;
10import java.awt.Rectangle;
11import java.awt.RenderingHints;
12import java.awt.geom.Area;
13import java.awt.image.BufferedImage;
14import java.util.Collection;
15import java.util.Collections;
16import java.util.Comparator;
17import java.util.HashSet;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.ListIterator;
21
22import org.expeditee.items.Circle;
23import org.expeditee.items.InteractiveWidget;
24import org.expeditee.items.Item;
25import org.expeditee.items.ItemUtils;
26import org.expeditee.items.Line;
27import org.expeditee.items.Permission;
28import org.expeditee.items.WidgetEdge;
29import org.expeditee.items.XRayable;
30
31public class FrameGraphics {
32
33 // the graphics used to paint with
34 private static Graphics2D _DisplayGraphics;
35
36 // the maximum size that can be used to paint on
37 private static Dimension _MaxSize = new Dimension(1000, 1000);
38
39 // modes
40 public static final int MODE_NORMAL = 0;
41
42 public static final int MODE_AUDIENCE = 1;
43
44 public static final int MODE_XRAY = 2;
45
46 // Start in XRay mode so that errors arnt thrown when parsing the profile
47 // frame if it has images on it
48 private static int _Mode = MODE_XRAY;
49
50 private FrameGraphics() {
51 // util constructor
52 }
53
54 /**
55 * If Audience Mode is on this method will toggle it to be off, or
56 * vice-versa. This results in the Frame being re-parsed and repainted.
57 */
58 public static void ToggleAudienceMode() {
59 Frame current = DisplayIO.getCurrentFrame();
60 if (_Mode == MODE_AUDIENCE)
61 _Mode = MODE_NORMAL;
62 else {
63 _Mode = MODE_AUDIENCE;
64 ItemUtils.UpdateConnectedToAnnotations(current.getItems());
65 for (Overlay o : current.getOverlays()) {
66 ItemUtils.UpdateConnectedToAnnotations(o.Frame.getItems());
67 }
68 for (Vector v : current.getVectorsDeep()) {
69 ItemUtils.UpdateConnectedToAnnotations(v.Frame.getItems());
70 }
71 }
72 FrameUtils.Parse(current);
73 DisplayIO.UpdateTitle();
74 setMaxSize(new Dimension(_MaxSize.width, MessageBay
75 .getMessageBufferHeight()
76 + _MaxSize.height));
77 refresh(false);
78 }
79
80 /**
81 * If X-Ray Mode is on this method will toggle it to be off, or vice-versa.
82 * This results in the Frame being re-parsed and repainted.
83 */
84 public static void ToggleXRayMode() {
85 if (_Mode == MODE_XRAY)
86 setMode(MODE_NORMAL, true);
87 else
88 setMode(MODE_XRAY, true);
89 DisplayIO.UpdateTitle();
90 FrameMouseActions.getInstance().refreshHighlights();
91 FrameMouseActions.updateCursor();
92 refresh(false);
93 }
94
95 public static void setMode(int mode, boolean parse) {
96 if (_Mode == mode)
97 return;
98 _Mode = mode;
99 if (parse)
100 FrameUtils.Parse(DisplayIO.getCurrentFrame());
101 }
102
103 /**
104 * @return True if Audience Mode is currently on, False otherwise.
105 */
106 public static boolean isAudienceMode() {
107 return _Mode == MODE_AUDIENCE;
108 }
109
110 /**
111 * @return True if X-Ray Mode is currently on, False otherwise.
112 */
113 public static boolean isXRayMode() {
114 return _Mode == MODE_XRAY;
115 }
116
117 public static void setMaxSize(Dimension max) {
118 if (_MaxSize == null)
119 _MaxSize = max;
120
121 // Hide the message buffer if in audience mode
122 int newMaxHeight = max.height
123 - (isAudienceMode() ? 0 : MessageBay.MESSAGE_BUFFER_HEIGHT);
124 if (newMaxHeight > 0) {
125 _MaxSize.setSize(max.width, newMaxHeight);
126 }
127 Frame current = DisplayIO.getCurrentFrame();
128 if (current != null) {
129 current.setBuffer(null);
130 current.refreshSize(getMaxFrameSize());
131 if (DisplayIO.isTwinFramesOn()) {
132 Frame opposite = DisplayIO.getOppositeFrame();
133 //if (opposite != null) {
134 opposite.setBuffer(null);
135 opposite.refreshSize(getMaxFrameSize());
136 //}
137 }
138 }
139
140 if (newMaxHeight > 0) {
141 MessageBay.updateSize();
142 }
143 }
144
145 public static Dimension getMaxSize() {
146 return _MaxSize;
147 }
148
149 public static Dimension getMaxFrameSize() {
150 if (DisplayIO.isTwinFramesOn()) {
151 return new Dimension((_MaxSize.width / 2), _MaxSize.height);
152 } else
153 return _MaxSize;
154 }
155
156 /**
157 * Sets the Graphics2D object that should be used for all painting tasks.
158 * Note: Actual painting is done by using g.create() to create temporary
159 * instances that are then disposed of using g.dispose().
160 *
161 * @param g
162 * The Graphics2D object to use for all painting
163 */
164 public static void setDisplayGraphics(Graphics2D g) {
165 _DisplayGraphics = g;
166 }
167
168 /*
169 * Displays the given Item on the screen
170 */
171 static void PaintItem(Graphics2D g, Item i) {
172 if (i == null || g == null)
173 return;
174
175 // do not paint annotation items in audience mode
176 if (!isAudienceMode()
177 || (isAudienceMode() && !i.isConnectedToAnnotation() && !i
178 .isAnnotation()) || i == FrameUtils.getLastEdited()) {
179
180 Graphics2D tg = (Graphics2D) g.create();
181 i.paint(tg);
182 tg.dispose();
183 }
184 }
185
186 /**
187 * Adds all the scaled vector items for a frame into a list. It uses
188 * recursion to get the items from nested vector frames.
189 *
190 * @param items
191 * the list into which the vector items will be placed.
192 * @param vector
193 * the frame containing vecor items.
194 * @param seenVectors
195 * the vectors which should be ignored to prevent infinate loops.
196 * @param origin
197 * start point for this frame or null if it is a top level frame.
198 * @param scale
199 * the factor by which the item on the vector frame are to be
200 * scaled.
201 */
202 // public static void AddAllVectorItems(List<Item> items, Vector vector,
203 // Collection<Frame> seenVectors) {
204 // // Check all the vector items and add the items on the vectors
205 // if (seenVectors.contains(vector))
206 // return;
207 // seenVectors.add(vector);
208 //
209 // float originX = origin == null ? 0 : origin.x;
210 // float originY = origin == null ? 0 : origin.y;
211 //
212 // for (Vector o : vector.getVectors())
213 // AddAllVectorItems(items, o.Frame, new HashSet<Frame>(seenVectors),
214 // new Point2D.Float(originX + o.Origin.x * scale, originY
215 // + o.Origin.y * scale), o.Scale * scale,
216 // o.Foreground, o.Background);
217 // // if its the original frame then were done
218 // if (origin == null) {
219 // ItemUtils.EnclosedCheck(items);
220 // return;
221 // }
222 // // Put copies of the items shifted to the origin of the VectorTag
223 // items.addAll(ItemUtils
224 // .CopyItems(vector.getNonAnnotationItems(), vector));
225 //
226 // }
227 /**
228 * Recursive function similar to AddAllOverlayItems.
229 *
230 * @param widgets
231 * The collection the widgets will be added to
232 * @param overlay
233 * An "overlay" frame - this intially will be the parent frame
234 * @param seenOverlays
235 * Used for state in the recursion stack. Pass as an empty
236 * (non-null) list.
237 */
238 public static void AddAllOverlayWidgets(List<InteractiveWidget> widgets,
239 Frame overlay, List<Frame> seenOverlays) {
240 if (seenOverlays.contains(overlay))
241 return;
242
243 seenOverlays.add(overlay);
244
245 for (Overlay o : overlay.getOverlays())
246 AddAllOverlayWidgets(widgets, o.Frame, seenOverlays);
247
248 widgets.addAll(overlay.getInteractiveWidgets());
249 }
250
251 private static Image Paint(Frame toPaint, Area clip) {
252 return Paint(toPaint, clip, true, true);
253 }
254
255 /**
256 *
257 * @param toPaint
258 * @param clip
259 * If null, then no clip applied.
260 * @param isActualFrame
261 * @return
262 */
263 private static Image Paint(Frame toPaint, Area clip, boolean isActualFrame, boolean createVolitile) {
264 if (toPaint == null)
265 return null;
266
267 // the buffer is not valid, so it must be recreated
268 if (!toPaint.isBufferValid()) {
269 Image buffer = toPaint.getBuffer();
270 if (buffer == null) {
271 GraphicsEnvironment ge = GraphicsEnvironment
272 .getLocalGraphicsEnvironment();
273 if (createVolitile) {
274 buffer = ge.getDefaultScreenDevice()
275 .getDefaultConfiguration()
276 .createCompatibleVolatileImage(_MaxSize.width,
277 _MaxSize.height);
278 } else {
279 buffer = new BufferedImage(_MaxSize.width, _MaxSize.height,
280 BufferedImage.TYPE_INT_ARGB);
281 }
282 toPaint.setBuffer(buffer);
283 }
284
285 Graphics2D bg = (Graphics2D) buffer.getGraphics();
286 bg.setClip(clip);
287
288 // TODO: Revise images and clip - VERY IMPORTANT
289
290 // Nicer looking lines, but may be too jerky while
291 // rubber-banding on older machines
292 if (UserSettings.AntiAlias)
293 bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
294 RenderingHints.VALUE_ANTIALIAS_ON);
295 // If we are doing @f etc... then have a clear background if its the
296 // default background color
297 Color backgroundColor = null;
298 // Need to allow transparency for frameImages
299 if (createVolitile) {
300 backgroundColor = toPaint.getPaintBackgroundColor();
301 }else{
302 backgroundColor = toPaint.getBackgroundColor();
303 if (backgroundColor == null)
304 backgroundColor = Item.TRANSPARENT;
305 }
306
307 bg.setColor(backgroundColor);
308 // bg.setColor(Color.pink); // TODO: TEMP FOR DEBUGGING
309
310 bg.fillRect(0, 0, _MaxSize.width, _MaxSize.height);
311
312 List<Item> visibleItems = new LinkedList<Item>();
313 List<InteractiveWidget> paintWidgets;
314
315 if (isActualFrame) {
316 // Add all the items for this frame and any other from other
317 // frames
318 visibleItems.addAll(toPaint.getAllItems());
319 paintWidgets = new LinkedList<InteractiveWidget>();
320 AddAllOverlayWidgets(paintWidgets, toPaint,
321 new LinkedList<Frame>());
322 } else {
323 visibleItems.addAll(toPaint.getVisibleItems());
324 visibleItems.addAll(toPaint.getVectorItems());
325 paintWidgets = toPaint.getInteractiveWidgets();
326 }
327
328 // FIRST: Paint widgets swing gui (not expeditee gui) .
329 // Note that these are the anchored widgets
330 ListIterator<InteractiveWidget> widgetItor = paintWidgets.listIterator(paintWidgets.size());
331 while (widgetItor.hasPrevious()) { // Paint first-in-last-serve ordering - like swing
332 InteractiveWidget iw = widgetItor.previous();
333 if (clip == null
334 || clip.intersects(iw.getComponant().getBounds()))
335 iw.paint(bg);
336
337 // Paint borders
338 for (Item i : iw.getItems()) {
339 if (clip == null
340 || i.isInDrawingArea(clip))
341 i.paint(bg);
342 }
343
344 }
345
346 // Filter out items that do not need to be painted
347 List<Item> paintItems;
348 HashSet<Item> fillOnlyItems = null; // only contains items that do
349 // not need drawing but fills
350 // might
351
352 if (clip == null) {
353 paintItems = visibleItems;
354 } else {
355 fillOnlyItems = new HashSet<Item>();
356 paintItems = new LinkedList<Item>();
357 for (Item i : visibleItems) {
358 if (i.isInDrawingArea(clip)) {
359 paintItems.add(i);
360 } else if (i.isEnclosed()) {
361 // just add all fill items despite possibility of fills
362 // not being in clip
363 // because it will be faster than having to test twice
364 // for fills that do need
365 // repainting.
366 fillOnlyItems.add(i);
367 }
368 }
369 }
370 // Only paint files and lines once ... between anchored AND free
371 // items
372 HashSet<Item> paintedFillsAndLines = new HashSet<Item>();
373 PaintPictures(bg, paintItems, fillOnlyItems, paintedFillsAndLines);
374 PaintLines(bg, visibleItems);
375
376 // Filter out free items that do not need to be painted
377 // This is efficient in cases with animation while free items exist
378
379 List<Item> freeItemsToPaint = new LinkedList<Item>();
380 // Dont paint the free items for the other frame in twin frames mode
381 // if (toPaint == DisplayIO.getCurrentFrame()) {
382 if (clip == null) {
383 freeItemsToPaint = FreeItems.getInstance();
384 } else {
385 freeItemsToPaint = new LinkedList<Item>();
386 fillOnlyItems.clear();
387 for (Item i : FreeItems.getInstance()) {
388 if (i.isInDrawingArea(clip)) {
389 freeItemsToPaint.add(i);
390 } else if (i.isEnclosed()) {
391 fillOnlyItems.add(i);
392 }
393 }
394 }
395 // }
396
397 if (isActualFrame && toPaint == DisplayIO.getCurrentFrame())
398 PaintPictures(bg, freeItemsToPaint, fillOnlyItems,
399 paintedFillsAndLines);
400 // TODO if we can get transparency with FreeItems.getInstance()...
401 // then text can be done before freeItems
402 PaintNonLinesNonPicture(bg, paintItems);
403
404 // toPaint.setBufferValid(true);
405
406 if (isActualFrame && !isAudienceMode()) {
407 PaintItem(bg, toPaint.getNameItem());
408 }
409
410 if (DisplayIO.isTwinFramesOn()) {
411 List<Item> lines = new LinkedList<Item>();
412 for (Item i : freeItemsToPaint) {
413 if (i instanceof Line) {
414 Line line = (Line) i;
415
416 if (toPaint == DisplayIO.getCurrentFrame()) {
417 // If exactly one end of the line is floating...
418
419 if (line.getEndItem().isFloating()
420 ^ line.getStartItem().isFloating()) {
421 // Line l = TransposeLine(line,
422 // line.getEndItem(),
423 // toPaint, 0, 0);
424 // if (l == null)
425 // l = TransposeLine(line,
426 // line.getStartItem(), toPaint, 0, 0);
427 // if (l == null)
428 // l = line;
429 // lines.add(l);
430 } else {
431 // lines.add(line);
432 }
433 } else {
434 // if (line.getEndItem().isFloating()
435 // ^ line.getStartItem().isFloating()) {
436 // lines.add(TransposeLine(line,
437 // line.getEndItem(), toPaint,
438 // FrameMouseActions.getY(), -DisplayIO
439 // .getMiddle()));
440 // lines.add(TransposeLine(line, line
441 // .getStartItem(), toPaint,
442 // FrameMouseActions.getY(), -DisplayIO
443 // .getMiddle()));
444 // }
445 }
446 }
447 }
448 if (isActualFrame)
449 PaintLines(bg, lines);
450 } else {
451 // Dont paint the
452 if (isActualFrame)
453 PaintLines(bg, freeItemsToPaint);
454 }
455
456 if (isActualFrame && toPaint == DisplayIO.getCurrentFrame())
457 PaintNonLinesNonPicture(bg, freeItemsToPaint);
458
459 // Repaint popups / drags...
460 if (isActualFrame)
461 PopupManager.getInstance().paintLayeredPane(bg, clip);
462
463 bg.dispose();
464 }
465
466 return toPaint.getBuffer();
467 }
468
469 // creates a new line so that lines are shown correctly when spanning
470 // across frames in TwinFrames mode
471 private static Line TransposeLine(Line line, Item d, Frame toPaint,
472 int base, int adj) {
473 Line nl = null;
474
475 if (toPaint != DisplayIO.getCurrentFrame() && d.getParent() == null
476 && line.getOppositeEnd(d).getParent() == toPaint) {
477 nl = line.copy();
478 if (d == line.getStartItem())
479 d = nl.getStartItem();
480 else
481 d = nl.getEndItem();
482
483 if (DisplayIO.FrameOnSide(toPaint) == 0)
484 d.setX(base);
485 else
486 d.setX(base + adj);
487
488 } else if (toPaint == DisplayIO.getCurrentFrame()
489 && d.getParent() == null
490 && line.getOppositeEnd(d).getParent() != toPaint) {
491 nl = line.copy();
492
493 if (d == line.getStartItem())
494 d = nl.getEndItem();
495 else
496 d = nl.getStartItem();
497
498 if (DisplayIO.FrameOnSide(toPaint) == 1)
499 d.setX(d.getX() - DisplayIO.getMiddle());
500 else
501 d.setX(d.getX() + DisplayIO.getMiddle());
502 }
503 if (nl != null) {
504 nl.invalidateAll();
505 FrameGraphics.requestRefresh(true);
506 }
507 return nl;
508 }
509
510 private static void Paint(Graphics g, Image left, Image right,
511 Color background) {
512
513 // if TwinFrames mode is on, then clipping etc has to be set
514 if (DisplayIO.isTwinFramesOn()) {
515 // draw the two lines down the middle of the window
516 if (left != null)
517 left.getGraphics().drawLine(DisplayIO.getMiddle() - 2, 0,
518 DisplayIO.getMiddle() - 2, _MaxSize.height);
519
520 if (right != null)
521 right.getGraphics().drawLine(0, 0, 0, _MaxSize.height);
522
523 // set the clipping area
524 ((Graphics2D) g).setClip(0, 0, DisplayIO.getMiddle() - 1,
525 _MaxSize.height);
526 g.drawImage(left, 0, 0, Item.DEFAULT_BACKGROUND, null);
527 ((Graphics2D) g).setClip(null);
528 g.drawImage(right, DisplayIO.getMiddle() + 1, 0,
529 Item.DEFAULT_BACKGROUND, null);
530
531 // otherwise, just draw whichever side is active
532 } else {
533 if (DisplayIO.getCurrentSide() == 0)
534 g.drawImage(left, 0, 0, Item.DEFAULT_BACKGROUND, null);
535 else
536 g.drawImage(right, 0, 0, Item.DEFAULT_BACKGROUND, null);
537 }
538
539 }
540
541 public static void Clear() {
542 Graphics g = _DisplayGraphics.create();
543 g.setColor(Color.WHITE);
544 g.fillRect(0, 0, _MaxSize.width, _MaxSize.height);
545 g.dispose();
546 }
547
548 private static void PaintNonLinesNonPicture(Graphics2D g, List<Item> toPaint) {
549 for (Item i : toPaint)
550 if (!(i instanceof Line) && !(i instanceof XRayable))
551 PaintItem(g, i);
552 }
553
554 /**
555 * Paint the lines that are not part of an enclosure.
556 *
557 * @param g
558 * @param toPaint
559 */
560 private static void PaintLines(Graphics2D g, List<Item> toPaint) {
561 // Use this set to keep track of the items that have been painted
562 Collection<Item> done = new HashSet<Item>();
563 for (Item i : toPaint)
564 if (i instanceof Line) {
565 Line l = (Line) i;
566 if (done.contains(l)) {
567 l.paintArrows(g);
568 } else {
569 // When painting a line all connected lines are painted too
570 done.addAll(l.getAllConnected());
571 if (l.getStartItem().getEnclosedArea() == 0)
572 PaintItem(g, i);
573 }
574 }
575 }
576
577 /**
578 * Paint filled areas and their surrounding lines as well as pictures. Note:
579 * floating widgets are painted as fills
580 *
581 * @param g
582 * @param toPaint
583 */
584 private static void PaintPictures(Graphics2D g, List<Item> toPaint,
585 HashSet<Item> fillOnlyItems, HashSet<Item> done) {
586
587 List<Item> toFill = new LinkedList<Item>();
588 for (Item i : toPaint) {
589 // Ignore items that have already been done!
590 // Also ignore invisible items..
591 // TODO possibly ignore invisible items before coming to this
592 // method?
593 if (done.contains(i))
594 continue;
595 if (i instanceof XRayable) {
596 toFill.add(i);
597 done.addAll(i.getConnected());
598 } else if (i.hasEnclosures()) {
599 for (Item enclosure : i.getEnclosures()) {
600 if (!toFill.contains(enclosure))
601 toFill.add(enclosure);
602 }
603 done.addAll(i.getConnected());
604 } else if (i.isLineEnd()
605 && (!isAudienceMode() || !i.isConnectedToAnnotation())) {
606 toFill.add(i);
607 done.addAll(i.getAllConnected());
608 }
609 }
610
611 if (fillOnlyItems != null) {
612 for (Item i : fillOnlyItems) {
613 if (done.contains(i))
614 continue;
615 else if (!isAudienceMode() || !i.isConnectedToAnnotation()) {
616 toFill.add(i);
617 }
618 done.addAll(i.getAllConnected());
619 }
620 }
621
622 // Sort the items to fill
623 Collections.sort(toFill, new Comparator<Item>() {
624 public int compare(Item a, Item b) {
625 Double aArea = a.getEnclosedArea();
626 Double bArea = b.getEnclosedArea();
627 int cmp = aArea.compareTo(bArea);
628 if (cmp == 0) {
629 // System.out.println(a.getEnclosureID() + " " + b.getID());
630 return new Integer(a.getEnclosureID()).compareTo(b
631 .getEnclosureID());
632 }
633 return cmp * -1;
634 }
635 });
636 for (Item i : toFill) {
637 if (i instanceof XRayable) {
638 PaintItem(g, i);
639 } else {
640 // Paint the fill and lines
641 i.paintFill(g);
642 List<Line> lines = i.getLines();
643 if (lines.size() > 0)
644 PaintItem(g, lines.get(0));
645 }
646 }
647 }
648
649 /**
650 * Highlights an item on the screen Note: All graphics are handled by the
651 * Item itself.
652 *
653 * @param i
654 * The Item to highlight.
655 * @param val
656 * True if the highlighting is being shown, false if it is being
657 * erased.
658 * @return the item that was highlighted
659 */
660 public static Item Highlight(Item i) {
661 if ((i instanceof Line)) {
662 // Check if within 20% of the end of the line
663 Line l = (Line) i;
664 Item toDisconnect = l.getEndPointToDisconnect(Math
665 .round(FrameMouseActions.MouseX), Math
666 .round(FrameMouseActions.MouseY));
667
668 // Brook: Widget Edges do not have such a context
669 if (toDisconnect != null && !(i instanceof WidgetEdge)) {
670 Item.HighlightMode newMode = toDisconnect.getHighlightMode();
671 if (FreeItems.itemAttachedToCursor())
672 newMode = Item.HighlightMode.Normal;
673 // unhighlight all the other dots
674 for (Item conn : toDisconnect.getAllConnected()) {
675 conn.setHighlightMode(Item.HighlightMode.None);
676 }
677 l.setHighlightMode(newMode);
678 // highlight the dot that will be in disconnect mode
679 toDisconnect.setHighlightMode(newMode);
680 i = toDisconnect;
681 } else {
682 Collection<Item> connected = i.getAllConnected();
683 for (Item conn : connected) {
684 conn.setHighlightMode(Item.HighlightMode.Connected);
685 }
686 }
687 } else if (i instanceof Circle) {
688 i.setHighlightMode(Item.HighlightMode.Connected);
689 } else {
690 // FrameGraphics.ChangeSelectionMode(i,
691 // Item.SelectedMode.Normal);
692 // For polygons need to make sure all other endpoints are
693 // unHighlighted
694 if (i.hasPermission(Permission.full))
695 changeHighlightMode(i, Item.HighlightMode.Normal,
696 Item.HighlightMode.None);
697 else
698 changeHighlightMode(i, Item.HighlightMode.Connected,
699 Item.HighlightMode.Connected);
700 }
701 Repaint();
702 return i;
703 }
704
705 public static void changeHighlightMode(Item item, Item.HighlightMode newMode) {
706 changeHighlightMode(item, newMode, newMode);
707 }
708
709 public static void changeHighlightMode(Item item,
710 Item.HighlightMode newMode, Item.HighlightMode connectedNewMode) {
711 if (item == null)
712 return;
713 // Mike: TODO comment on why the line below is used!!
714 // I forgot already!!Opps
715 boolean freeItem = FreeItems.getInstance().contains(item);
716 for (Item i : item.getAllConnected()) {
717 if (/* freeItem || */!FreeItems.getInstance().contains(i)) {
718 i.setHighlightMode(connectedNewMode);
719 }
720 }
721 if (!freeItem && newMode != connectedNewMode)
722 item.setHighlightMode(newMode);
723 Repaint();
724 }
725
726 /**
727 * Repaints the buffer of the given Frame.
728 *
729 * @param toUpdate
730 * The Frame whose buffer is to be repainted.
731 */
732
733 public static void UpdateBuffer(Frame toUpdate, boolean paintOverlays, boolean useVolitile) {
734 toUpdate.setBuffer(getBuffer(toUpdate, paintOverlays, useVolitile));
735 }
736
737 public static Image getBuffer(Frame toUpdate, boolean paintOverlays, boolean useVolitile){
738 if (toUpdate == null)
739 return null;
740
741 return Paint(toUpdate, null, paintOverlays, useVolitile);
742 }
743
744 public static int getMode() {
745 return _Mode;
746 }
747
748 public static Graphics createGraphics() {
749 // Error messages on start up will call this message before
750 // _DisplayGraphics has been initialised
751 if (_DisplayGraphics == null)
752 return null;
753 return _DisplayGraphics.create();
754 }
755
756 // Damaged areas pending to render. Accessessed by multiple threads
757 private static HashSet<Rectangle> damagedAreas = new HashSet<Rectangle>();
758
759 /**
760 * Checks that the item is visible (on current frame && overlays) - if
761 * visible then damaged area will be re-rendered on the next refresh.
762 *
763 * @param damagedItem
764 * @param toRepaint
765 */
766 public static void invalidateItem(Item damagedItem, Rectangle toRepaint) {
767 // Only add area to repaint if item is visible...
768 if (ItemUtils.isVisible(damagedItem)) {
769 synchronized (damagedAreas) {
770 damagedAreas.add(toRepaint);
771 }
772 } else if (MessageBay.isMessageItem(damagedItem)) {
773 MessageBay.addDirtyArea(toRepaint);
774 }
775 }
776
777 /**
778 * The given area will be re-rendered in the next refresh. This is the
779 * quicker version and is more useful for re-rendering animated areas.
780 *
781 * @param toRepaint
782 */
783 public static void invalidateArea(Rectangle toRepaint) {
784 synchronized (damagedAreas) {
785 damagedAreas.add(toRepaint);
786 }
787 }
788
789 public static void clearInvalidAreas() {
790 synchronized (damagedAreas) {
791 damagedAreas.clear();
792 }
793 }
794
795 /**
796 * Invalidates the buffered image of the current Frame and forces it to be
797 * repainted on to the screen. Repaints all items. This is more expensive
798 * than refresh.
799 */
800 public static void ForceRepaint() { // TODO: TEMP: Use refresh
801 Frame current = DisplayIO.getCurrentFrame();
802
803 if (current == null)
804 return;
805 refresh(false);
806 }
807
808 public static void Repaint() { // TODO: TEMP: Use refresh
809 refresh(true);
810 }
811
812 /**
813 * Called to refresh the display screen. Thread safe.
814 */
815 public static void refresh(boolean useInvalidation) {
816
817 if (_DisplayGraphics == null || _MaxSize.width <= 0
818 || _MaxSize.height <= 0)
819 return;
820
821 Area clip = null;
822 if (useInvalidation) { // build clip
823
824 synchronized (damagedAreas) {
825 if (!damagedAreas.isEmpty()) {
826
827 for (Rectangle r : damagedAreas) {
828 if (clip == null)
829 clip = new Area(r);
830 else
831 clip.add(new Area(r));
832 }
833 damagedAreas.clear();
834
835 } else if (MessageBay.isDirty()) {
836 // Paint dirty message bay
837 Graphics dg = _DisplayGraphics.create();
838 MessageBay.refresh(true, dg, Item.DEFAULT_BACKGROUND);
839 return;
840
841 } else return; // nothing to render
842 }
843
844 } else {
845 synchronized (damagedAreas) {
846 damagedAreas.clear();
847 }
848 // System.out.println("FULLSCREEN REFRESH"); // TODO: REMOVE
849 }
850
851 Frame[] toPaint = DisplayIO.getFrames();
852 Image left = Paint(toPaint[0], clip);
853 Image right = Paint(toPaint[1], clip);
854
855 Graphics dg = _DisplayGraphics.create();
856
857 // Paint frame to window
858 Paint(dg, left, right, Item.DEFAULT_BACKGROUND);
859
860 // Paint any animations
861 PopupManager.getInstance().paintAnimations();
862
863 // Paint message bay
864 MessageBay.refresh(useInvalidation, dg, Item.DEFAULT_BACKGROUND);
865
866 dg.dispose();
867 }
868
869 /**
870 * If wanting to refresh from another thread - other than the main thread
871 * that handles the expeditee datamodel (modifying / accessing / rendering).
872 * Use this method for thread safety.
873 */
874 public static synchronized void requestRefresh(boolean useInvalidation) {
875
876 _requestMarsheller._useInvalidation = useInvalidation;
877
878 if (_requestMarsheller._isQueued) {
879 return;
880 }
881
882 _requestMarsheller._isQueued = true;
883 EventQueue.invokeLater(_requestMarsheller); // Render on AWT thread
884 }
885
886 private static RenderRequestMarsheller _requestMarsheller = new FrameGraphics().new RenderRequestMarsheller();
887
888 /**
889 * Used for marshelling render requests from foreign threads to the event
890 * dispatcher thread... (AWT)
891 *
892 * @author Brook Novak
893 */
894 private class RenderRequestMarsheller implements Runnable {
895
896 boolean _useInvalidation = true;
897
898 boolean _isQueued = false;
899
900 public void run() {
901 refresh(_useInvalidation);
902 _isQueued = false;
903 _useInvalidation = true;
904 }
905
906 }
907
908}
Note: See TracBrowser for help on using the repository browser.