KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class FontAction extends EncoderAction {
33
34     protected Font JavaDoc defaultFont = FontLib.getFont("SansSerif",Font.PLAIN,10);
35     
36     /**
37      * Create a new FontAction that processes all data groups.
38      */

39     public FontAction() {
40         super();
41     }
42     
43     /**
44      * Create a new FontAction that processes the specified group.
45      * @param group the data group to process
46      */

47     public FontAction(String JavaDoc group) {
48         super(group);
49     }
50     
51     /**
52      * Create a new FontAction that processes the specified group.
53      * @param group the data group to process
54      * @param defaultFont the default Font to assign
55      */

56     public FontAction(String JavaDoc group, Font JavaDoc defaultFont) {
57         super(group);
58         this.defaultFont = defaultFont;
59     }
60     
61     // ------------------------------------------------------------------------
62

63     /**
64      * Set the default font to be assigned to items. Items will be assigned
65      * the default font if they do not match any registered rules.
66      * @param f the default font to use
67      */

68     public void setDefaultFont(Font JavaDoc f) {
69         defaultFont = f;
70     }
71     
72     /**
73      * Get the default font assigned to items.
74      * @return the default font
75      */

76     public Font JavaDoc getDefaultFont() {
77         return defaultFont;
78     }
79     
80     /**
81      * Add a font mapping rule to this FontAction. VisualItems that match
82      * the provided predicate will be assigned the given font value (assuming
83      * they do not match an earlier rule).
84      * @param p the rule Predicate
85      * @param font the font
86      */

87     public void add(Predicate p, Font JavaDoc font) {
88         super.add(p, font);
89     }
90
91     /**
92      * Add a font mapping rule to this FontAction. VisualItems that match
93      * the provided expression will be assigned the given font 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 font the font
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, Font JavaDoc font) {
102         Predicate p = (Predicate)ExpressionParser.parse(expr);
103         super.add(p, font);
104     }
105     
106     /**
107      * Add a font mapping rule to this FontAction. VisualItems that match
108      * the provided predicate will be assigned the font value returned by
109      * the given FontAction's getFont() method.
110      * @param p the rule Predicate
111      * @param f the delegate FontAction to use
112      */

113     public void add(Predicate p, FontAction f) {
114         super.add(p, f);
115     }
116
117     /**
118      * Add a font mapping rule to this FontAction. VisualItems that match
119      * the provided expression will be assigned the given font 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 FontAction 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, FontAction 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         Font JavaDoc f = getFont(item);
139         Font JavaDoc o = item.getFont();
140         item.setStartFont(o);
141         item.setEndFont(f);
142         item.setFont(f);
143     }
144     
145     /**
146      * Returns the Font to use for a given VisualItem. Subclasses should
147      * override this method to perform customized font assignment.
148      * @param item the VisualItem for which to get the Font
149      * @return the Font for the given item
150      */

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