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

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

added new package to separate widget classes from item classes

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