KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > VisualObjectManager


1 /*
2  * VisualObjectManager.java
3  *
4  * Created on 2 June 2003, 23:35
5  */

6
7 package com.thoughtriver.open.vectorvisuals;
8
9 import java.awt.*;
10 import java.util.*;
11 import java.util.List JavaDoc;
12
13 /**
14  * This class keeps track of all of the <CODE>VisualObject</CODE> instances
15  * that have been registered for rendering. The objects are kept in a sorted
16  * <CODE>List</CODE>, ordered by their layer depth. This is the class that
17  * actually calls each object's <CODE>render</CODE> method at the appropriate
18  * time.
19  * <P>
20  * An instance of this class is kept by every object that wishes to perform
21  * layer-based renders of <CODE>VisualObject</CODE>s.
22  *
23  * @author Brandon Franklin
24  * @version $Date: 2006/11/25 09:18:36 $
25  */

26 public class VisualObjectManager {
27
28     /** Sorts the <CODE>VisualObject</CODE>s by layer depth */
29     private LayerComparator layerComparator = null;
30
31     /**
32      * All of the objects currently being drawn in the vector world, sorted by
33      * layer
34      */

35     private List JavaDoc<VisualObject> objects = null;
36
37     /**
38      * Creates a new instance of <CODE>VisualObjectManager</CODE>.
39      */

40     public VisualObjectManager() {
41         layerComparator = new LayerComparator();
42         objects = new LinkedList<VisualObject>();
43     }
44
45     /**
46      * Adds the supplied <CODE>VisualObject</CODE> for rendering in the vector
47      * world.
48      *
49      * @param vo the new <CODE>VisualObject</CODE> to be rendered
50      */

51     public void addObject(final VisualObject vo) {
52         objects.add(vo);
53         Collections.sort(objects, layerComparator);
54     }
55
56     /**
57      * Removes the supplied <CODE>VisualObject</CODE> from rendering in the
58      * vector world.
59      *
60      * @param vo the <CODE>VisualObject</CODE> to be removed
61      */

62     public void removeObject(final VisualObject vo) {
63         objects.remove(vo);
64     }
65
66     /**
67      * Tells the manager to render all known <CODE>VisualObject</CODE>s that
68      * exist on negative layers. There are some guarantees about the rendering
69      * of objects:
70      * <OL>
71      * <LI>They will be rendered in ascending order by layer.</LI>
72      * <LI>An embedded object will be rendered following its parent's <CODE>renderObject</CODE>
73      * method being invoked.</LI>
74      * <LI>An embedded object with a negative layer will be rendered prior to
75      * the invocation of its parent's <CODE>renderOutline</CODE> method.</LI>
76      * <LI>An embedded object with a non-negative layer will be rendered after
77      * the invocation of its parent's <CODE>renderOutline</CODE> method.</LI>
78      * </OL>
79      * When this method is invoked, <CODE>prepare</CODE> is invoked on every
80      * directly contained <CODE>VisualObject</CODE> in this manager before any
81      * <CODE>render</CODE> method is called.
82      *
83      * @param g the <CODE>Graphics2D</CODE> context onto which to render the
84      * objects
85      */

86     public void renderNegativeLayers(final Graphics2D g) {
87
88         // Prepare every object
89
for (VisualObject obj : objects) {
90             obj.prepare();
91         }
92
93         // Check every object
94
for (VisualObject obj : objects) {
95             Graphics2D g2 = null;
96
97             // Negative layer means "down in", so clipping gets applied
98
if (obj.getLayer() < 0) {
99                 g2 = (Graphics2D) g.create();
100                 if (obj.getParent() != null) {
101                     g2.setClip(obj.getParent().getShape());
102                 }
103                 else {
104                     g2.setClip(obj.getShape());
105                 }
106             }
107             else {
108                 // Skip non-negatives for now
109
continue;
110             }
111
112             // Only render objects that are within the clip
113
Rectangle clip = g2.getClipBounds();
114             Rectangle bounds = obj.getTransform().createTransformedShape(obj.getShape()).getBounds();
115             if (!clip.createIntersection(bounds).isEmpty()) {
116                 obj.render(g2);
117             }
118
119         }
120
121     }
122
123     /**
124      * Tells the manager to render all known <CODE>VisualObject</CODE>s that
125      * exist on non-negative layers. There are some guarantees about the
126      * rendering of objects:
127      * <OL>
128      * <LI>They will be rendered in ascending order by layer.</LI>
129      * <LI>An embedded object will be rendered following its parent's <CODE>renderObject</CODE>
130      * method being invoked.</LI>
131      * <LI>An embedded object with a negative layer will be rendered prior to
132      * the invocation of its parent's <CODE>renderOutline</CODE> method.</LI>
133      * <LI>An embedded object with a non-negative layer will be rendered after
134      * the invocation of its parent's <CODE>renderOutline</CODE> method.</LI>
135      * </OL>
136      *
137      * @param g the <CODE>Graphics2D</CODE> context onto which to render the
138      * objects
139      */

140     public void renderNonNegativeLayers(final Graphics2D g) {
141
142         // Check every object
143
for (VisualObject obj : objects) {
144
145             // Only render objects on non-negative layers
146
if (obj.getLayer() >= 0) {
147                 obj.render(g);
148             }
149         }
150
151     }
152
153     /**
154      * Returns a <CODE>List</CODE> containing references to all of the <CODE>VisualObject</CODE>s
155      * registered with this manager. The references will be in reverse order
156      * with regard to rendering layer. In other words, the "topmost" objects
157      * will appear first in the returned <CODE>List</CODE>.
158      *
159      * @return a <CODE>List</CODE> containing references to all of the <CODE>VisualObject</CODE>s
160      * registered with this manager
161      */

162     public List JavaDoc<VisualObject> getAllObjects() {
163         List JavaDoc<VisualObject> objList = new LinkedList<VisualObject>(objects);
164         Collections.reverse(objList);
165         return objList;
166     }
167
168     /**
169      * This is the <CODE>Comparator</CODE> class that is used to organize the
170      * <CODE>VisualObject</CODE>s by layer depth.
171      */

172     private class LayerComparator implements Comparator<VisualObject> {
173
174         /** Creates a new instance of <CODE>LayerComparator</CODE>. */
175         public LayerComparator() {
176         }
177
178         /**
179          * Compares its two arguments for order. Returns a negative integer,
180          * zero, or a positive integer as the first argument is less than, equal
181          * to, or greater than the second.
182          *
183          * @param obj1 the first <CODE>VisualObject</CODE> to be compared
184          * @param obj2 the second <CODE>VisualObject</CODE> to be compared
185          * @return a negative integer, zero, or a positive integer as the first
186          * argument is less than, equal to, or greater than the second
187          */

188         public int compare(final VisualObject obj1, final VisualObject obj2) {
189             return obj1.getLayer() - obj2.getLayer();
190         }
191
192     }
193
194 }
195
Popular Tags