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

Last change on this file since 184 was 184, checked in by bjn8, 16 years ago

Widgets are now linkable.
Also made some improvements for popups - improved interaction with free items.

File size: 27.2 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 // 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 // Paint borders
341 for (Item i : iw.getItems()) {
342 if (clip == null
343 || i.isInDrawingArea(clip))
344 i.paint(bg);
345 }
346
347 }
348
349 // Filter out items that do not need to be painted
350 List<Item> paintItems;
351 HashSet<Item> fillOnlyItems = null; // only contains items that do
352 // not need drawing but fills
353 // might
354
355 if (clip == null) {
356 paintItems = visibleItems;
357 } else {
358 fillOnlyItems = new HashSet<Item>();
359 paintItems = new LinkedList<Item>();
360 for (Item i : visibleItems) {
361 if (i.isInDrawingArea(clip)) {
362 paintItems.add(i);
363 } else if (i.isEnclosed()) {
364 // just add all fill items despite possibility of fills
365 // not being in clip
366 // because it will be faster than having to test twice
367 // for fills that do need
368 // repainting.
369 fillOnlyItems.add(i);
370 }
371 }
372 }
373 // Only paint files and lines once ... between anchored AND free
374 // items
375 HashSet<Item> paintedFillsAndLines = new HashSet<Item>();
376 PaintPictures(bg, paintItems, fillOnlyItems, paintedFillsAndLines);
377 PaintLines(bg, visibleItems);
378
379 // Filter out free items that do not need to be painted
380 // This is efficient in cases with animation while free items exist
381
382 List<Item> freeItemsToPaint = new LinkedList<Item>();
383 // Dont paint the free items for the other frame in twin frames mode
384 // if (toPaint == DisplayIO.getCurrentFrame()) {
385 if (clip == null) {
386 freeItemsToPaint = FreeItems.getInstance();
387 } else {
388 freeItemsToPaint = new LinkedList<Item>();
389 fillOnlyItems.clear();
390 for (Item i : FreeItems.getInstance()) {
391 if (i.isInDrawingArea(clip)) {
392 freeItemsToPaint.add(i);
393 } else if (i.isEnclosed()) {
394 fillOnlyItems.add(i);
395 }
396 }
397 }
398 // }
399
400 if (isActualFrame && toPaint == DisplayIO.getCurrentFrame())
401 PaintPictures(bg, freeItemsToPaint, fillOnlyItems,
402 paintedFillsAndLines);
403 // TODO if we can get transparency with FreeItems.getInstance()...
404 // then text can be done before freeItems
405 PaintNonLinesNonPicture(bg, paintItems);
406
407 // toPaint.setBufferValid(true);
408
409 if (isActualFrame && !isAudienceMode()) {
410 PaintItem(bg, toPaint.getNameItem());
411 }
412
413 if (DisplayIO.isTwinFramesOn()) {
414 List<Item> lines = new LinkedList<Item>();
415 for (Item i : freeItemsToPaint) {
416 if (i instanceof Line) {
417 Line line = (Line) i;
418
419 if (toPaint == DisplayIO.getCurrentFrame()) {
420 // If exactly one end of the line is floating...
421
422 if (line.getEndItem().isFloating()
423 ^ line.getStartItem().isFloating()) {
424 // Line l = TransposeLine(line,
425 // line.getEndItem(),
426 // toPaint, 0, 0);
427 // if (l == null)
428 // l = TransposeLine(line,
429 // line.getStartItem(), toPaint, 0, 0);
430 // if (l == null)
431 // l = line;
432 // lines.add(l);
433 } else {
434 // lines.add(line);
435 }
436 } else {
437 // if (line.getEndItem().isFloating()
438 // ^ line.getStartItem().isFloating()) {
439 // lines.add(TransposeLine(line,
440 // line.getEndItem(), toPaint,
441 // FrameMouseActions.getY(), -DisplayIO
442 // .getMiddle()));
443 // lines.add(TransposeLine(line, line
444 // .getStartItem(), toPaint,
445 // FrameMouseActions.getY(), -DisplayIO
446 // .getMiddle()));
447 // }
448 }
449 }
450 }
451 if (isActualFrame)
452 PaintLines(bg, lines);
453 } else {
454 // Dont paint the
455 if (isActualFrame)
456 PaintLines(bg, freeItemsToPaint);
457 }
458
459 if (isActualFrame && toPaint == DisplayIO.getCurrentFrame())
460 PaintNonLinesNonPicture(bg, freeItemsToPaint);
461
462 // Repaint popups / drags... As well as final passes
463 if (isActualFrame) {
464 PopupManager.getInstance().paintLayeredPane(bg, clip);
465 for (FinalFrameRenderPass pass : _finalPasses) {
466 pass.paint(bg);
467 }
468 }
469
470 bg.dispose();
471 }
472
473 return toPaint.getBuffer();
474 }
475
476 // creates a new line so that lines are shown correctly when spanning
477 // across frames in TwinFrames mode
478 private static Line TransposeLine(Line line, Item d, Frame toPaint,
479 int base, int adj) {
480 Line nl = null;
481
482 if (toPaint != DisplayIO.getCurrentFrame() && d.getParent() == null
483 && line.getOppositeEnd(d).getParent() == toPaint) {
484 nl = line.copy();
485 if (d == line.getStartItem())
486 d = nl.getStartItem();
487 else
488 d = nl.getEndItem();
489
490 if (DisplayIO.FrameOnSide(toPaint) == 0)
491 d.setX(base);
492 else
493 d.setX(base + adj);
494
495 } else if (toPaint == DisplayIO.getCurrentFrame()
496 && d.getParent() == null
497 && line.getOppositeEnd(d).getParent() != toPaint) {
498 nl = line.copy();
499
500 if (d == line.getStartItem())
501 d = nl.getEndItem();
502 else
503 d = nl.getStartItem();
504
505 if (DisplayIO.FrameOnSide(toPaint) == 1)
506 d.setX(d.getX() - DisplayIO.getMiddle());
507 else
508 d.setX(d.getX() + DisplayIO.getMiddle());
509 }
510 if (nl != null) {
511 nl.invalidateAll();
512 FrameGraphics.requestRefresh(true);
513 }
514 return nl;
515 }
516
517 private static void Paint(Graphics g, Image left, Image right,
518 Color background) {
519
520 // if TwinFrames mode is on, then clipping etc has to be set
521 if (DisplayIO.isTwinFramesOn()) {
522 // draw the two lines down the middle of the window
523 if (left != null)
524 left.getGraphics().drawLine(DisplayIO.getMiddle() - 2, 0,
525 DisplayIO.getMiddle() - 2, _MaxSize.height);
526
527 if (right != null)
528 right.getGraphics().drawLine(0, 0, 0, _MaxSize.height);
529
530 // set the clipping area
531 ((Graphics2D) g).setClip(0, 0, DisplayIO.getMiddle() - 1,
532 _MaxSize.height);
533 g.drawImage(left, 0, 0, Item.DEFAULT_BACKGROUND, null);
534 ((Graphics2D) g).setClip(null);
535 g.drawImage(right, DisplayIO.getMiddle() + 1, 0,
536 Item.DEFAULT_BACKGROUND, null);
537
538 // otherwise, just draw whichever side is active
539 } else {
540 if (DisplayIO.getCurrentSide() == 0)
541 g.drawImage(left, 0, 0, Item.DEFAULT_BACKGROUND, null);
542 else
543 g.drawImage(right, 0, 0, Item.DEFAULT_BACKGROUND, null);
544 }
545
546 }
547
548 public static void Clear() {
549 Graphics g = _DisplayGraphics.create();
550 g.setColor(Color.WHITE);
551 g.fillRect(0, 0, _MaxSize.width, _MaxSize.height);
552 g.dispose();
553 }
554
555 private static void PaintNonLinesNonPicture(Graphics2D g, List<Item> toPaint) {
556 for (Item i : toPaint)
557 if (!(i instanceof Line) && !(i instanceof XRayable))
558 PaintItem(g, i);
559 }
560
561 /**
562 * Paint the lines that are not part of an enclosure.
563 *
564 * @param g
565 * @param toPaint
566 */
567 private static void PaintLines(Graphics2D g, List<Item> toPaint) {
568 // Use this set to keep track of the items that have been painted
569 Collection<Item> done = new HashSet<Item>();
570 for (Item i : toPaint)
571 if (i instanceof Line) {
572 Line l = (Line) i;
573 if (done.contains(l)) {
574 l.paintArrows(g);
575 } else {
576 // When painting a line all connected lines are painted too
577 done.addAll(l.getAllConnected());
578 if (l.getStartItem().getEnclosedArea() == 0)
579 PaintItem(g, i);
580 }
581 }
582 }
583
584 /**
585 * Paint filled areas and their surrounding lines as well as pictures. Note:
586 * floating widgets are painted as fills
587 *
588 * @param g
589 * @param toPaint
590 */
591 private static void PaintPictures(Graphics2D g, List<Item> toPaint,
592 HashSet<Item> fillOnlyItems, HashSet<Item> done) {
593
594 List<Item> toFill = new LinkedList<Item>();
595 for (Item i : toPaint) {
596 // Ignore items that have already been done!
597 // Also ignore invisible items..
598 // TODO possibly ignore invisible items before coming to this
599 // method?
600 if (done.contains(i))
601 continue;
602 if (i instanceof XRayable) {
603 toFill.add(i);
604 done.addAll(i.getConnected());
605 } else if (i.hasEnclosures()) {
606 for (Item enclosure : i.getEnclosures()) {
607 if (!toFill.contains(enclosure))
608 toFill.add(enclosure);
609 }
610 done.addAll(i.getConnected());
611 } else if (i.isLineEnd()
612 && (!isAudienceMode() || !i.isConnectedToAnnotation())) {
613 toFill.add(i);
614 done.addAll(i.getAllConnected());
615 }
616 }
617
618 if (fillOnlyItems != null) {
619 for (Item i : fillOnlyItems) {
620 if (done.contains(i))
621 continue;
622 else if (!isAudienceMode() || !i.isConnectedToAnnotation()) {
623 toFill.add(i);
624 }
625 done.addAll(i.getAllConnected());
626 }
627 }
628
629 // Sort the items to fill
630 Collections.sort(toFill, new Comparator<Item>() {
631 public int compare(Item a, Item b) {
632 Double aArea = a.getEnclosedArea();
633 Double bArea = b.getEnclosedArea();
634 int cmp = aArea.compareTo(bArea);
635 if (cmp == 0) {
636 // System.out.println(a.getEnclosureID() + " " + b.getID());
637 return new Integer(a.getEnclosureID()).compareTo(b
638 .getEnclosureID());
639 }
640 return cmp * -1;
641 }
642 });
643 for (Item i : toFill) {
644 if (i instanceof XRayable) {
645 PaintItem(g, i);
646 } else {
647 // Paint the fill and lines
648 i.paintFill(g);
649 List<Line> lines = i.getLines();
650 if (lines.size() > 0)
651 PaintItem(g, lines.get(0));
652 }
653 }
654 }
655
656 /**
657 * Highlights an item on the screen Note: All graphics are handled by the
658 * Item itself.
659 *
660 * @param i
661 * The Item to highlight.
662 * @param val
663 * True if the highlighting is being shown, false if it is being
664 * erased.
665 * @return the item that was highlighted
666 */
667 public static Item Highlight(Item i) {
668 if ((i instanceof Line)) {
669 // Check if within 20% of the end of the line
670 Line l = (Line) i;
671 Item toDisconnect = l.getEndPointToDisconnect(Math
672 .round(FrameMouseActions.MouseX), Math
673 .round(FrameMouseActions.MouseY));
674
675 // Brook: Widget Edges do not have such a context
676 if (toDisconnect != null && !(i instanceof WidgetEdge)) {
677 Item.HighlightMode newMode = toDisconnect.getHighlightMode();
678 if (FreeItems.itemAttachedToCursor())
679 newMode = Item.HighlightMode.Normal;
680 // unhighlight all the other dots
681 for (Item conn : toDisconnect.getAllConnected()) {
682 conn.setHighlightMode(Item.HighlightMode.None);
683 }
684 l.setHighlightMode(newMode);
685 // highlight the dot that will be in disconnect mode
686 toDisconnect.setHighlightMode(newMode);
687 i = toDisconnect;
688 } else {
689 Collection<Item> connected = i.getAllConnected();
690 for (Item conn : connected) {
691 conn.setHighlightMode(Item.HighlightMode.Connected);
692 }
693 }
694 } else if (i instanceof Circle) {
695 i.setHighlightMode(Item.HighlightMode.Connected);
696 } else {
697 // FrameGraphics.ChangeSelectionMode(i,
698 // Item.SelectedMode.Normal);
699 // For polygons need to make sure all other endpoints are
700 // unHighlighted
701 if (i.hasPermission(Permission.full))
702 changeHighlightMode(i, Item.HighlightMode.Normal,
703 Item.HighlightMode.None);
704 else
705 changeHighlightMode(i, Item.HighlightMode.Connected,
706 Item.HighlightMode.Connected);
707 }
708 Repaint();
709 return i;
710 }
711
712 public static void changeHighlightMode(Item item, Item.HighlightMode newMode) {
713 changeHighlightMode(item, newMode, newMode);
714 }
715
716 public static void changeHighlightMode(Item item,
717 Item.HighlightMode newMode, Item.HighlightMode connectedNewMode) {
718 if (item == null)
719 return;
720 // Mike: TODO comment on why the line below is used!!
721 // I forgot already!!Opps
722 boolean freeItem = FreeItems.getInstance().contains(item);
723 for (Item i : item.getAllConnected()) {
724 if (/* freeItem || */!FreeItems.getInstance().contains(i)) {
725 i.setHighlightMode(connectedNewMode);
726 }
727 }
728 if (!freeItem && newMode != connectedNewMode)
729 item.setHighlightMode(newMode);
730 Repaint();
731 }
732
733 /**
734 * Repaints the buffer of the given Frame.
735 *
736 * @param toUpdate
737 * The Frame whose buffer is to be repainted.
738 */
739
740 public static void UpdateBuffer(Frame toUpdate, boolean paintOverlays, boolean useVolitile) {
741 toUpdate.setBuffer(getBuffer(toUpdate, paintOverlays, useVolitile));
742 }
743
744 public static Image getBuffer(Frame toUpdate, boolean paintOverlays, boolean useVolitile){
745 if (toUpdate == null)
746 return null;
747
748 return Paint(toUpdate, null, paintOverlays, useVolitile);
749 }
750
751 public static int getMode() {
752 return _Mode;
753 }
754
755 public static Graphics createGraphics() {
756 // Error messages on start up will call this message before
757 // _DisplayGraphics has been initialised
758 if (_DisplayGraphics == null)
759 return null;
760 return _DisplayGraphics.create();
761 }
762
763 // Damaged areas pending to render. Accessessed by multiple threads
764 private static HashSet<Rectangle> damagedAreas = new HashSet<Rectangle>();
765
766 /**
767 * Checks that the item is visible (on current frame && overlays) - if
768 * visible then damaged area will be re-rendered on the next refresh.
769 *
770 * @param damagedItem
771 * @param toRepaint
772 */
773 public static void invalidateItem(Item damagedItem, Rectangle toRepaint) {
774 // Only add area to repaint if item is visible...
775 if (ItemUtils.isVisible(damagedItem)) {
776 synchronized (damagedAreas) {
777 damagedAreas.add(toRepaint);
778 }
779 } else if (MessageBay.isMessageItem(damagedItem)) {
780 MessageBay.addDirtyArea(toRepaint);
781 }
782 }
783
784 /**
785 * The given area will be re-rendered in the next refresh. This is the
786 * quicker version and is more useful for re-rendering animated areas.
787 *
788 * @param toRepaint
789 */
790 public static void invalidateArea(Rectangle toRepaint) {
791 synchronized (damagedAreas) {
792 damagedAreas.add(toRepaint);
793 }
794 }
795
796 public static void clearInvalidAreas() {
797 synchronized (damagedAreas) {
798 damagedAreas.clear();
799 }
800 }
801
802 /**
803 * Invalidates the buffered image of the current Frame and forces it to be
804 * repainted on to the screen. Repaints all items. This is more expensive
805 * than refresh.
806 */
807 public static void ForceRepaint() { // TODO: TEMP: Use refresh
808 Frame current = DisplayIO.getCurrentFrame();
809
810 if (current == null)
811 return;
812 refresh(false);
813 }
814
815 public static void Repaint() { // TODO: TEMP: Use refresh
816 refresh(true);
817 }
818
819 /**
820 * Called to refresh the display screen. Thread safe.
821 */
822 public static void refresh(boolean useInvalidation) {
823
824 if (_DisplayGraphics == null || _MaxSize.width <= 0
825 || _MaxSize.height <= 0)
826 return;
827
828 Area clip = null;
829 if (useInvalidation) { // build clip
830
831 synchronized (damagedAreas) {
832 if (!damagedAreas.isEmpty()) {
833
834 for (Rectangle r : damagedAreas) {
835 if (clip == null)
836 clip = new Area(r);
837 else
838 clip.add(new Area(r));
839 }
840 damagedAreas.clear();
841
842 } else if (MessageBay.isDirty()) {
843 // Paint dirty message bay
844 Graphics dg = _DisplayGraphics.create();
845 MessageBay.refresh(true, dg, Item.DEFAULT_BACKGROUND);
846 return;
847
848 } else return; // nothing to render
849 }
850
851 } else {
852 synchronized (damagedAreas) {
853 damagedAreas.clear();
854 }
855 // System.out.println("FULLSCREEN REFRESH"); // TODO: REMOVE
856 }
857
858 Frame[] toPaint = DisplayIO.getFrames();
859 Image left = Paint(toPaint[0], clip);
860 Image right = Paint(toPaint[1], clip);
861
862 Graphics dg = _DisplayGraphics.create();
863
864 // Paint frame to window
865 Paint(dg, left, right, Item.DEFAULT_BACKGROUND);
866
867 // Paint any animations
868 PopupManager.getInstance().paintAnimations();
869
870 // Paint message bay
871 MessageBay.refresh(useInvalidation, dg, Item.DEFAULT_BACKGROUND);
872
873 dg.dispose();
874 }
875
876 /**
877 * If wanting to refresh from another thread - other than the main thread
878 * that handles the expeditee datamodel (modifying / accessing / rendering).
879 * Use this method for thread safety.
880 */
881 public static synchronized void requestRefresh(boolean useInvalidation) {
882
883 _requestMarsheller._useInvalidation = useInvalidation;
884
885 if (_requestMarsheller._isQueued) {
886 return;
887 }
888
889 _requestMarsheller._isQueued = true;
890 EventQueue.invokeLater(_requestMarsheller); // Render on AWT thread
891 }
892
893 private static RenderRequestMarsheller _requestMarsheller = new FrameGraphics().new RenderRequestMarsheller();
894
895 /**
896 * Used for marshelling render requests from foreign threads to the event
897 * dispatcher thread... (AWT)
898 *
899 * @author Brook Novak
900 */
901 private class RenderRequestMarsheller implements Runnable {
902
903 boolean _useInvalidation = true;
904
905 boolean _isQueued = false;
906
907 public void run() {
908 refresh(_useInvalidation);
909 _isQueued = false;
910 _useInvalidation = true;
911 }
912
913 }
914
915 /**
916 * Adds a FinalFrameRenderPass to the frame-render pipeline...
917 *
918 * Note that the last added FinalFrameRenderPass will be rendered at the
919 * very top.
920 *
921 * @param pass
922 * The pass to add. If already added then nothing results in the call.
923 *
924 * @throws NullPointerException
925 * If pass is null.
926 */
927 public static void addFinalFrameRenderPass(FinalFrameRenderPass pass) {
928 if (pass == null) throw new NullPointerException("pass");
929
930 if (!_finalPasses.contains(pass))
931 _finalPasses.add(pass);
932 }
933
934 /**
935 * Adds a FinalFrameRenderPass to the frame-render pipeline...
936 *
937 * Note that the last added FinalFrameRenderPass will be rendered at the
938 * very top.
939 *
940 * @param pass
941 * The pass to remove
942 *
943 */
944 public static void removeFinalFrameRenderPass(FinalFrameRenderPass pass) {
945 _finalPasses.remove(pass);
946 }
947
948 /**
949 * A FinalFrameRenderPass is invoked at the very final stages for rendering a frame:
950 * that is, after the popups are drawn.
951 *
952 * There can be many applications for FinalFrameRenderPass. Such as tool tips, balloons,
953 * or drawing items at the highest Z-order in special situations.
954 *
955 * Although if there are multiples FinalFrameRenderPasses attatach to the
956 * frame painter then it is not garaunteed to be rendered very last.
957 *
958 * @see FrameGraphics#addFinalFrameRenderPass(org.expeditee.gui.FrameGraphics.FinalFrameRenderPass)
959 * @see FrameGraphics#removeFinalFrameRenderPass(org.expeditee.gui.FrameGraphics.FinalFrameRenderPass)
960 *
961 * @author Brook Novak
962 */
963 public interface FinalFrameRenderPass {
964 void paint(Graphics g);
965 }
966
967}
Note: See TracBrowser for help on using the repository browser.