KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.action.assignment;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import prefuse.action.EncoderAction;
6 import prefuse.data.expression.Predicate;
7 import prefuse.data.expression.parser.ExpressionParser;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * <p>Assignment Action that assigns size values to VisualItems.</p>
13  *
14  * <p>
15  * By default, a SizeAction simply assigns a single default size value to
16  * all items (the initial default size is 1.0). Clients can change this
17  * default value to achieve uniform size assignment, or can add any number
18  * of additional rules for size assignment. Rules are specified by a Predicate
19  * instance which, if returning true, will trigger that rule, causing either the
20  * provided size value or the result of a delegate SizeAction to be
21  * applied. Rules are evaluated in the order in which they are added to the
22  * SizeAction, so earlier rules will have precedence over rules added later.
23  * </p>
24  *
25  * <p>In addition, subclasses can simply override {@link #getSize(VisualItem)}
26  * to achieve custom size assignment. In some cases, this may be the simplest
27  * or most flexible approach.</p>
28  *
29  * <p>To automatically assign size values based on varying values of a
30  * particular data field, consider using the {@link DataSizeAction}.</p>
31  *
32  * @author <a HREF="http://jheer.org">jeffrey heer</a>
33  */

34 public class SizeAction extends EncoderAction {
35
36     protected double m_defaultSize = 1.0;
37     
38     /**
39      * Constructor. A default size value of 1.0 will be used.
40      */

41     public SizeAction() {
42         super();
43     }
44     
45     /**
46      * Constructor. A default size value of 1.0 will be used.
47      * @param group the data group processed by this Action.
48      */

49     public SizeAction(String JavaDoc group) {
50         super(group);
51     }
52
53     /**
54      * Constructor which specified a default size value.
55      * @param group the data group processed by this Action.
56      * @param size the default size to use
57      */

58     public SizeAction(String JavaDoc group, double size) {
59         super(group);
60         m_defaultSize = size;
61     }
62     
63     /**
64      * Returns the default size value assigned to items.
65      * @return the default size value
66      */

67     public double getDefaultSize() {
68         return m_defaultSize;
69     }
70     
71     /**
72      * Sets the default size value assigned to items. Items will be assigned
73      * the default size if they do not match any registered rules.
74      * @param defaultSize the new default size value
75      */

76     public void setDefaultSize(double defaultSize) {
77         m_defaultSize = defaultSize;
78     }
79     
80     /**
81      * Add a size mapping rule to this SizeAction. VisualItems that match
82      * the provided predicate will be assigned the given size value (assuming
83      * they do not match an earlier rule).
84      * @param p the rule Predicate
85      * @param size the size value
86      */

87     public void add(Predicate p, double size) {
88         super.add(p, new Double JavaDoc(size));
89     }
90
91     /**
92      * Add a size mapping rule to this SizeAction. VisualItems that match
93      * the provided expression will be assigned the given size value (assuming
94      * they do not match an earlier rule). The provided expression String will
95      * be parsed to generate the needed rule Predicate.
96      * @param expr the expression String, should parse to a Predicate.
97      * @param size the size value
98      * @throws RuntimeException if the expression does not parse correctly or
99      * does not result in a Predicate instance.
100      */

101     public void add(String JavaDoc expr, double size) {
102         Predicate p = (Predicate)ExpressionParser.parse(expr);
103         add(p, size);
104     }
105     
106     /**
107      * Add a size mapping rule to this SizeAction. VisualItems that match
108      * the provided predicate will be assigned the size value returned by
109      * the given SizeAction's getSize() method.
110      * @param p the rule Predicate
111      * @param f the delegate SizeAction to use
112      */

113     public void add(Predicate p, SizeAction f) {
114         super.add(p, f);
115     }
116
117     /**
118      * Add a size mapping rule to this SizeAction. VisualItems that match
119      * the provided expression will be assigned the given size value (assuming
120      * they do not match an earlier rule). The provided expression String will
121      * be parsed to generate the needed rule Predicate.
122      * @param expr the expression String, should parse to a Predicate.
123      * @param f the delegate SizeAction to use
124      * @throws RuntimeException if the expression does not parse correctly or
125      * does not result in a Predicate instance.
126      */

127     public void add(String JavaDoc expr, SizeAction f) {
128         Predicate p = (Predicate)ExpressionParser.parse(expr);
129         super.add(p, f);
130     }
131     
132     // ------------------------------------------------------------------------
133

134     /**
135      * @see prefuse.action.ItemAction#process(prefuse.visual.VisualItem, double)
136      */

137     public void process(VisualItem item, double frac) {
138         double size = getSize(item);
139         double old = item.getSize();
140         item.setStartSize(old);
141         item.setEndSize(size);
142         item.setSize(size);
143     }
144     
145     /**
146      * Returns a size value for the given item.
147      * @param item the item for which to get the size value
148      * @return the size value for the item
149      */

150     public double getSize(VisualItem item) {
151         Object JavaDoc o = lookup(item);
152         if ( o != null ) {
153             if ( o instanceof SizeAction ) {
154                 return ((SizeAction)o).getSize(item);
155             } else if ( o instanceof Number JavaDoc ) {
156                 return ((Number JavaDoc)o).doubleValue();
157             } else {
158                 Logger.getLogger(this.getClass().getName())
159                     .warning("Unrecognized Object from predicate chain.");
160             }
161         }
162         return m_defaultSize;
163     }
164
165 } // end of class SizeAction
166
Popular Tags