KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > assignment > ShapeAction


1 package prefuse.action.assignment;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import prefuse.Constants;
6 import prefuse.action.EncoderAction;
7 import prefuse.data.expression.Predicate;
8 import prefuse.data.expression.parser.ExpressionParser;
9 import prefuse.visual.VisualItem;
10
11
12 /**
13  * <p>Assignment Action that assigns shape values to VisualItems.
14  * Shape values are simple integer codes that indicate to
15  * appropriate renderer instances what shape should be drawn. The default
16  * list of shape values is included in the {@link prefuse.Constants} class,
17  * all beginning with the prefix <code>SHAPE</code>. Of course, clients can
18  * always create their own shape codes that are handled by a custom Renderer.
19  * </p>
20  *
21  * <p>
22  * By default, a ShapeAction simply sets each VisualItem to be a
23  * rectangle. Clients can change this default value to achieve uniform shape
24  * assignment, or can add any number of additional rules for shape assignment.
25  * Rules are specified by a Predicate instance which, if returning true, will
26  * trigger that rule, causing either the provided shape value or the result of
27  * a delegate ShapeAction to be applied. Rules are evaluated in the order in
28  * which they are added to the ShapeAction, so earlier rules will have
29  * precedence over rules added later.
30  * </p>
31  *
32  * <p>In addition, subclasses can simply override {@link #getShape(VisualItem)}
33  * to achieve custom shape assignment. In some cases, this may be the simplest
34  * or most flexible approach.</p>
35  *
36  * <p>This Action only sets the shape field of the VisualItem. For this value
37  * to have an effect, a renderer instance that takes this shape value
38  * into account must be used (e.g., {@link prefuse.render.ShapeRenderer}).
39  * </p>
40  *
41  * <p>To automatically assign shape values based on varying values of a
42  * particular data field, consider using the {@link DataShapeAction}.</p>
43  *
44  * @author <a HREF="http://jheer.org">jeffrey heer</a>
45  */

46 public class ShapeAction extends EncoderAction {
47
48     protected int m_defaultShape = Constants.SHAPE_RECTANGLE;
49     
50     /**
51      * Constructor. A default rectangle shape will be used.
52      */

53     public ShapeAction() {
54         super();
55     }
56     
57     /**
58      * Constructor. A default rectangle shape will be used.
59      * @param group the data group processed by this Action.
60      */

61     public ShapeAction(String JavaDoc group) {
62         super(group);
63     }
64
65     /**
66      * Constructor with a specified a default shape value.
67      * @param group the data group processed by this Action.
68      * @param shape the default shape value to use
69      */

70     public ShapeAction(String JavaDoc group, int shape) {
71         super(group);
72         m_defaultShape = shape;
73     }
74     
75     /**
76      * Returns the default shape value assigned to items.
77      * @return the default shape value
78      */

79     public int getDefaultSize() {
80         return m_defaultShape;
81     }
82     
83     /**
84      * Sets the default shape value assigned to items. Items will be assigned
85      * the default shape if they do not match any registered rules.
86      * @param defaultShape the new default shape value
87      */

88     public void setDefaultShape(int defaultShape) {
89         m_defaultShape = defaultShape;
90     }
91     
92     /**
93      * Add a shape mapping rule to this ShapeAction. VisualItems that match
94      * the provided predicate will be assigned the given shape value (assuming
95      * they do not match an earlier rule).
96      * @param p the rule Predicate
97      * @param shape the shape value
98      */

99     public void add(Predicate p, int shape) {
100         super.add(p, new Integer JavaDoc(shape));
101     }
102
103     /**
104      * Add a shape mapping rule to this ShapeAction. VisualItems that match
105      * the provided expression will be assigned the given shape value (assuming
106      * they do not match an earlier rule). The provided expression String will
107      * be parsed to generate the needed rule Predicate.
108      * @param expr the expression String, should parse to a Predicate.
109      * @param shape the shape value
110      * @throws RuntimeException if the expression does not parse correctly or
111      * does not result in a Predicate instance.
112      */

113     public void add(String JavaDoc expr, int shape) {
114         Predicate p = (Predicate)ExpressionParser.parse(expr);
115         add(p, shape);
116     }
117     
118     /**
119      * Add a size mapping rule to this ShapeAction. VisualItems that match
120      * the provided predicate will be assigned the shape value returned by
121      * the given ShapeAction's getSize() method.
122      * @param p the rule Predicate
123      * @param f the delegate ShapeAction to use
124      */

125     public void add(Predicate p, ShapeAction f) {
126         super.add(p, f);
127     }
128
129     /**
130      * Add a shape mapping rule to this ShapeAction. VisualItems that match
131      * the provided expression will be assigned the given shape value (assuming
132      * they do not match an earlier rule). The provided expression String will
133      * be parsed to generate the needed rule Predicate.
134      * @param expr the expression String, should parse to a Predicate.
135      * @param f the delegate ShapeAction to use
136      * @throws RuntimeException if the expression does not parse correctly or
137      * does not result in a Predicate instance.
138      */

139     public void add(String JavaDoc expr, ShapeAction f) {
140         Predicate p = (Predicate)ExpressionParser.parse(expr);
141         super.add(p, f);
142     }
143     
144     // ------------------------------------------------------------------------
145

146     /**
147      * @see prefuse.action.ItemAction#process(prefuse.visual.VisualItem, double)
148      */

149     public void process(VisualItem item, double frac) {
150         item.setShape(getShape(item));
151     }
152     
153     /**
154      * Returns a shape value for the given item.
155      * @param item the item for which to get the shape value
156      * @return the shape value for the item
157      */

158     public int getShape(VisualItem item) {
159         Object JavaDoc o = lookup(item);
160         if ( o != null ) {
161             if ( o instanceof ShapeAction ) {
162                 return ((ShapeAction)o).getShape(item);
163             } else if ( o instanceof Number JavaDoc ) {
164                 return ((Number JavaDoc)o).intValue();
165             } else {
166                 Logger.getLogger(this.getClass().getName())
167                     .warning("Unrecognized Object from predicate chain.");
168             }
169         }
170         return m_defaultShape;
171     }
172
173 } // end of class ShapeAction
174
Popular Tags