KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.action.assignment;
2
3 import java.awt.BasicStroke JavaDoc;
4 import java.awt.Stroke JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import prefuse.action.EncoderAction;
8 import prefuse.data.expression.Predicate;
9 import prefuse.data.expression.parser.ExpressionParser;
10 import prefuse.util.StrokeLib;
11 import prefuse.visual.VisualItem;
12
13
14 /**
15  * <p>Assignment Action that assigns <code>Stroke</code> values to VisualItems.
16  * The Stroke instance determines how lines and shape outlines are drawn,
17  * including the base size of the line, the line endings and line join types,
18  * and whether the line is solid or dashed. By default, a StrokeAction simply
19  * sets each VisualItem to use a default 1-pixel wide solid line. Clients can
20  * change this default value to achieve uniform Stroke assignment, or can add
21  * any number of additional rules for Stroke assignment.
22  * Rules are specified by a Predicate instance which, if returning true, will
23  * trigger that rule, causing either the provided Stroke value or the result of
24  * a delegate StrokeAction to be applied. Rules are evaluated in the order in
25  * which they are added to the StrokeAction, so earlier rules will have
26  * precedence over rules added later.
27  * </p>
28  *
29  * <p>In addition, subclasses can simply override
30  * {@link #getStroke(VisualItem)} to achieve custom Stroke assignment. In some
31  * cases, this may be the simplest or most flexible approach.</p>
32  *
33  * @author <a HREF="http://jheer.org">jeffrey heer</a>
34  */

35 public class StrokeAction extends EncoderAction {
36
37     protected BasicStroke JavaDoc defaultStroke = StrokeLib.getStroke(1.0f);
38     
39     /**
40      * Create a new StrokeAction that processes all data groups.
41      */

42     public StrokeAction() {
43         super();
44     }
45     
46     /**
47      * Create a new StrokeAction that processes the specified group.
48      * @param group the data group to process
49      */

50     public StrokeAction(String JavaDoc group) {
51         super(group);
52     }
53     
54     /**
55      * Create a new StrokeAction that processes the specified group.
56      * @param group the data group to process
57      * @param defaultStroke the default Stroke to assign
58      */

59     public StrokeAction(String JavaDoc group, BasicStroke JavaDoc defaultStroke) {
60         super(group);
61         this.defaultStroke = defaultStroke;
62     }
63     
64     // ------------------------------------------------------------------------
65

66     /**
67      * Set the default BasicStroke to be assigned to items. Items will be
68      * assigned the default Stroke if they do not match any registered rules.
69      * @param f the default BasicStroke to use
70      */

71     public void setDefaultStroke(BasicStroke JavaDoc f) {
72         defaultStroke = f;
73     }
74     
75     /**
76      * Get the default BasicStroke assigned to items.
77      * @return the default BasicStroke
78      */

79     public BasicStroke JavaDoc getDefaultStroke() {
80         return defaultStroke;
81     }
82     
83     /**
84      * Add a mapping rule to this StrokeAction. VisualItems that match
85      * the provided predicate will be assigned the given BasicStroke value
86      * (assuming they do not match an earlier rule).
87      * @param p the rule Predicate
88      * @param stroke the BasicStroke
89      */

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

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

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

130     public void add(String JavaDoc expr, StrokeAction f) {
131         Predicate p = (Predicate)ExpressionParser.parse(expr);
132         super.add(p, f);
133     }
134     
135     // ------------------------------------------------------------------------
136

137     /**
138      * @see prefuse.action.ItemAction#process(prefuse.visual.VisualItem, double)
139      */

140     public void process(VisualItem item, double frac) {
141         item.setStroke(getStroke(item));
142     }
143     
144     /**
145      * Returns the stroke to use for a given VisualItem. Subclasses should
146      * override this method to perform customized Stroke assignment.
147      * @param item the VisualItem for which to get the Stroke
148      * @return the BasicStroke for the given item
149      */

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