KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2004-2006 Regents of the University of California.
3  * See "license-prefuse.txt" for licensing terms.
4  */

5 package prefuse.action.assignment;
6
7 import java.util.logging.Logger JavaDoc;
8
9 import prefuse.action.EncoderAction;
10 import prefuse.data.expression.Predicate;
11 import prefuse.data.expression.parser.ExpressionParser;
12 import prefuse.util.ColorLib;
13 import prefuse.util.PrefuseLib;
14 import prefuse.visual.VisualItem;
15
16
17 /**
18  * <p>Assignment Action that assigns color values to VisualItems for a
19  * given color field (e.g., the stroke, text, or fill color).</p>
20  *
21  * <p>By default, a ColorAction simply assigns a single default color value
22  * to all items (the initial default color is black). Clients can change this
23  * default value to achieve uniform color assignment, or can add any number
24  * of additional rules for color assignment. Rules are specified by a Predicate
25  * instance which, if returning true, will trigger that rule, causing either the
26  * provided color value or the result of a delegate ColorAction to be
27  * applied. Rules are evaluated in the order in which they are added to the
28  * ColorAction, so earlier rules will have precedence over rules added later.
29  * </p>
30  *
31  * <p>In addition, subclasses can simply override {@link #getColor(VisualItem)}
32  * to achieve custom color assignment. In some cases, this may be the simplest
33  * or most flexible approach.</p>
34  *
35  * <p>To automatically assign color values based on varying values of a
36  * particular data field, consider using the DataColorAction.</p>
37  *
38  * <p>Color values are represented using integers, into which 8-bit values for
39  * the red, green, blue, and alpha channels are stored. For more information
40  * and utilities for creating and manipulating color values, see the
41  * {@link prefuse.util.ColorLib} class.</p>
42  *
43  * @author <a HREF="http://jheer.org">jeffrey heer</a>
44  * @see prefuse.util.ColorLib
45  * @see DataColorAction
46  */

47 public class ColorAction extends EncoderAction {
48     
49     protected String JavaDoc m_colorField;
50     protected String JavaDoc m_startField;
51     protected String JavaDoc m_endField;
52     
53     protected int m_cidx, m_sidx, m_eidx;
54     
55     protected int m_defaultColor = ColorLib.gray(0); // initial default = black
56

57     /**
58      * Constructor, sets the data group and color field for color assignment.
59      * Uses an initial default color value of black [RGB value (0,0,0)].
60      * @param group the data group processed by this Action
61      * @param field the color field assigned by this Action
62      */

63     public ColorAction(String JavaDoc group, String JavaDoc field) {
64         super(group);
65         setField(field);
66     }
67
68     /**
69      * Constructor, sets the data group, color field, and default color value
70      * for color assignment.
71      * @param group the data group processed by this Action
72      * @param field the color field assigned by this Action
73      * @param color the default color value assigned by this ColorAction
74      */

75     public ColorAction(String JavaDoc group, String JavaDoc field, int color) {
76         this(group, field);
77         m_defaultColor = color;
78     }
79     
80     /**
81      * Constructor, sets the data group, filter predicate and color field
82      * for color assignment.
83      * Uses an initial default color value of black [RGB value (0,0,0)].
84      * @param group the data group processed by this Action
85      * @param filter the filter predicate
86      * {@link prefuse.data.expression.Predicate}
87      * @param field the color field assigned by this Action
88      */

89     public ColorAction(String JavaDoc group, Predicate filter, String JavaDoc field) {
90         super(group, filter);
91         setField(field);
92     }
93      
94     /**
95      * Constructor, sets the data group, filter predicate,
96      * color field, and default color value for color assignment.
97      * @param group the data group processed by this Action
98      * @param filter the filter predicate
99      * {@link prefuse.data.expression.Predicate}
100      * @param field the color field assigned by this Action
101      * @param color the default color value assigned by this ColorAction
102      */

103     public ColorAction(String JavaDoc group, Predicate filter, String JavaDoc field, int color)
104     {
105         this(group, filter, field);
106         setDefaultColor(color);
107     }
108     
109     /**
110      * Set the color field name that this ColorAction should set. The
111      * ColorAction will automatically try to update the start and end
112      * values for this field if it is an interpolated field.
113      * @param field
114      */

115     public void setField(String JavaDoc field) {
116         m_colorField = field;
117         m_startField = PrefuseLib.getStartField(field);
118         m_endField = PrefuseLib.getEndField(field);
119     }
120     
121     /**
122      * Returns the default color for this ColorAction
123      * @return the default color value
124      */

125     public int getDefaultColor() {
126         return m_defaultColor;
127     }
128     
129     /**
130      * Sets the default color for this ColorAction. Items will be assigned
131      * the default color if they do not match any registered rules.
132      * @param color the new default color
133      */

134     public void setDefaultColor(int color) {
135         m_defaultColor = color;
136     }
137     
138     /**
139      * Add a color mapping rule to this ColorAction. VisualItems that match
140      * the provided predicate will be assigned the given color value (assuming
141      * they do not match an earlier rule).
142      * @param p the rule Predicate
143      * @param color the color value
144      */

145     public void add(Predicate p, int color) {
146         super.add(p, new Integer JavaDoc(color));
147     }
148
149     /**
150      * Add a color mapping rule to this ColorAction. VisualItems that match
151      * the provided expression will be assigned the given color value (assuming
152      * they do not match an earlier rule). The provided expression String will
153      * be parsed to generate the needed rule Predicate.
154      * @param expr the expression String, should parse to a Predicate.
155      * @param color the color value
156      * @throws RuntimeException if the expression does not parse correctly or
157      * does not result in a Predicate instance.
158      */

159     public void add(String JavaDoc expr, int color) {
160         Predicate p = (Predicate)ExpressionParser.parse(expr);
161         add(p, color);
162     }
163     
164     /**
165      * Add a color mapping rule to this ColorAction. VisualItems that match
166      * the provided predicate will be assigned the color value returned by
167      * the given ColorAction's getColor() method.
168      * @param p the rule Predicate
169      * @param f the delegate ColorAction to use
170      */

171     public void add(Predicate p, ColorAction f) {
172         super.add(p, f);
173     }
174
175     /**
176      * Add a color mapping rule to this ColorAction. VisualItems that match
177      * the provided expression will be assigned the given color value (assuming
178      * they do not match an earlier rule). The provided expression String will
179      * be parsed to generate the needed rule Predicate.
180      * @param expr the expression String, should parse to a Predicate.
181      * @param f the delegate ColorAction to use
182      * @throws RuntimeException if the expression does not parse correctly or
183      * does not result in a Predicate instance.
184      */

185     public void add(String JavaDoc expr, ColorAction f) {
186         Predicate p = (Predicate)ExpressionParser.parse(expr);
187         super.add(p, f);
188     }
189     
190     // ------------------------------------------------------------------------
191

192     /**
193      * @see prefuse.action.ItemAction#process(prefuse.visual.VisualItem, double)
194      */

195     public void process(VisualItem item, double frac) {
196         int c = getColor(item);
197         int o = item.getInt(m_colorField);
198         item.setInt(m_startField, o);
199         item.setInt(m_endField, c);
200         item.setInt(m_colorField, c);
201     }
202
203     /**
204      * Returns a color value for the given item. Colors are represented as
205      * integers, interpreted as holding values for the red, green, blue, and
206      * alpha channels. This is the same color representation returned by
207      * the Color.getRGB() method.
208      * @param item the item for which to get the color value
209      * @return the color value for the item
210      */

211     public int getColor(VisualItem item) {
212         Object JavaDoc o = lookup(item);
213         if ( o != null ) {
214             if ( o instanceof ColorAction ) {
215                 return ((ColorAction)o).getColor(item);
216             } else if ( o instanceof Integer JavaDoc ) {
217                 return ((Integer JavaDoc)o).intValue();
218             } else {
219                 Logger.getLogger(this.getClass().getName())
220                     .warning("Unrecognized Object from predicate chain.");
221             }
222         }
223         return m_defaultColor;
224     }
225     
226 } // end of class ColorAction
227
Popular Tags