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

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

Added PDF writers

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