KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > meta > arranger > Arranger


1 /*
2  * Arranger.java
3  *
4  * Created on 7 July 2003, 13:37
5  */

6
7 package com.thoughtriver.open.vectorvisuals.meta.arranger;
8
9 import java.awt.geom.*;
10 import java.util.*;
11
12 import com.thoughtriver.open.vectorvisuals.*;
13 import com.thoughtriver.open.vectorvisuals.task.*;
14
15 /**
16  * Concrete subclasses of <CODE>Arranger</CODE> are used to lay out <CODE>VisualObject</CODE>s
17  * in particular ways, such as in a grid or in a circle. Any <CODE>VisualObject</CODE>
18  * can have an <CODE>Arranger</CODE> set on it, which causes the <CODE>VisualObject</CODE>s
19  * embedded within that parent <CODE>VisualObject</CODE> to be arranged in the
20  * appropriate way. <CODE>Arranger</CODE>s can be animated, allowing them to
21  * execute smooth "morphs" from one state to another.
22  *
23  * @author Brandon Franklin
24  * @version $Date: 2006/11/25 08:58:56 $
25  */

26 public abstract class Arranger {
27
28     /** The <CODE>List</CODE> of objects being arranged */
29     private final List<VisualObject> objects = Collections.synchronizedList(new LinkedList<VisualObject>());
30
31     /** A <CODE>Map</CODE> of the original positions of the objects */
32     private final Map<VisualObject, AffineTransform> unarrangedPositions = new HashMap<VisualObject, AffineTransform>();
33
34     /** Whether or not animated transitions should be used */
35     private boolean animationEnabled = false;
36
37     /**
38      * Adds a new <CODE>VisualObject</CODE> to the arrangement.
39      *
40      * @param obj the <CODE>VisualObject</CODE> to add to the arrangment
41      */

42     public void add(final VisualObject obj) {
43         objects.add(obj);
44         unarrangedPositions.put(obj, obj.getTransform());
45     }
46
47     /**
48      * Removes the supplied <CODE>VisualObject</CODE> removed from the
49      * arrangement.
50      *
51      * @param obj the <CODE>VisualObject</CODE> to remove from the arrangement
52      */

53     public void remove(final VisualObject obj) {
54         unarrangedPositions.remove(obj);
55         objects.remove(obj);
56     }
57
58     /**
59      * Returns the <CODE>List</CODE> of <CODE>VisualObject</CODE>s that are
60      * being arranged. This is a reference to the actual backing <CODE>List</CODE>,
61      * so any changes will have an effect on the arrangement itself.
62      *
63      * @return the <CODE>List</CODE> of <CODE>VisualObject</CODE>s that are
64      * being arranged
65      */

66     public List<VisualObject> getObjects() {
67         return objects;
68     }
69
70     /**
71      * Enables or disables animated arrangement transitions, depending upon the
72      * value provided.
73      *
74      * @param enabled true to enable animated transitions, false to disable them
75      */

76     public void setAnimationEnabled(final boolean enabled) {
77         animationEnabled = enabled;
78     }
79
80     /**
81      * Returns true if animated arrangement transitions are enabled, false
82      * otherwise. The default is false.
83      *
84      * @return true if animated arrangement transitions are enabled, false
85      * otherwise
86      */

87     public boolean isAnimationEnabled() {
88         return animationEnabled;
89     }
90
91     /**
92      * Causes this <CODE>Arranger</CODE> to transform all of its arranged
93      * <CODE>VisualObject</CODE>s so that they are organized in a way
94      * appropriate to the purpose of this implementation. If animations are
95      * enabled, the transformation will be a smooth "morph" effect.
96      */

97     public void arrange() {
98
99         Map<VisualObject, AffineTransform> positions = getArrangedPositions();
100
101         // Apply the positions, possibly using an animation
102
for (VisualObject obj : getObjects()) {
103             AffineTransform position = positions.get(obj);
104             VVDisplay vvDisplay = obj.getDisplay();
105             if (vvDisplay != null) {
106                 TaskManager taskManager = vvDisplay.getTaskManager();
107                 if (taskManager != null) {
108                     if (isAnimationEnabled()) {
109                         vvDisplay.getTaskManager().addTask(new TranslationTask(obj, position.getTranslateX(), position.getTranslateY(), 2000));
110                         continue;
111                     }
112                 }
113             }
114
115             // This is the default behavior if animation cannot be achieved
116
obj.setTransform(position);
117         }
118
119     }
120
121     /**
122      * Causes this <CODE>Arranger</CODE> to transform all of its arranged
123      * <CODE>VisualObject</CODE>s so that they are positioned the way they
124      * were when they were added to the arrangement.
125      */

126     public void unarrange() {
127         for (VisualObject obj : getObjects()) {
128             AffineTransform position = unarrangedPositions.get(obj);
129             obj.setTransform(position);
130         }
131     }
132
133     /**
134      * Calculates and returns all of the transforms that this <CODE>Arranger</CODE>
135      * would apply to the arranged objects. This method does not cause them to
136      * be applied, however.
137      *
138      * @return all of the transforms that this <CODE>Arranger</CODE> would
139      * apply to the arranged objects
140      */

141     abstract protected Map<VisualObject, AffineTransform> getArrangedPositions();
142 }
143
Popular Tags