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

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

Fixed some bugs...
AbstractCharts can not be copied, resized, etc

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