KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > EncoderAction


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;
6
7 import prefuse.Visualization;
8 import prefuse.data.expression.Expression;
9 import prefuse.data.expression.ExpressionVisitor;
10 import prefuse.data.expression.ObjectLiteral;
11 import prefuse.data.expression.Predicate;
12 import prefuse.util.PredicateChain;
13 import prefuse.visual.VisualItem;
14
15 /**
16  * ItemAction instance that can also maintain a collection of rule mappings
17  * that can be used by subclasses to create particular rule-mappings for
18  * encoding data values.
19  *
20  * @author <a HREF="http://jheer.org">jeffrey heer</a>
21  */

22 public abstract class EncoderAction extends ItemAction {
23
24     private PredicateChain m_chain = null;
25     
26     /**
27      * Create a new EncoderAction that processes all data groups.
28      */

29     public EncoderAction() {
30         super();
31     }
32     
33     /**
34      * Create a new EncoderAction that processes all groups.
35      * @param vis the {@link prefuse.Visualization} to process
36      * @see Visualization#ALL_ITEMS
37      */

38     public EncoderAction(Visualization vis) {
39         super(vis);
40     }
41     
42     /**
43      * Create a new EncoderAction that processes the specified group.
44      * @param group the name of the group to process
45      */

46     public EncoderAction(String JavaDoc group) {
47         super(group);
48     }
49     
50     /**
51      * Create a new EncoderAction that processes the specified group.
52      * @param group the name of the group to process
53      * @param filter the filtering {@link prefuse.data.expression.Predicate}
54      */

55     public EncoderAction(String JavaDoc group, Predicate filter) {
56         super(group, filter);
57     }
58     
59     /**
60      * Create a new EncoderAction that processes the specified group.
61      * @param vis the {@link prefuse.Visualization} to process
62      * @param group the data group to process
63      */

64     public EncoderAction(Visualization vis, String JavaDoc group) {
65         super(vis, group);
66     }
67     
68     /**
69      * Create a new EncoderAction that processes the specified group.
70      * @param vis the {@link prefuse.Visualization} to process
71      * @param group the name of the group to process
72      * @param filter the filtering {@link prefuse.data.expression.Predicate}
73      */

74     public EncoderAction(Visualization vis, String JavaDoc group, Predicate filter) {
75         super(vis, group, filter);
76     }
77     
78     // ------------------------------------------------------------------------
79

80     /**
81      * Add a mapping rule to this EncoderAction. This method is protected,
82      * subclasses should crate public add methods of their own to enforce
83      * their own type constraints.
84      * @param p the rule Predicate
85      * @param value the value to map to
86      */

87     protected void add(Predicate p, Object JavaDoc value) {
88         if ( m_chain == null ) m_chain = new PredicateChain();
89         if ( value instanceof Action )
90             ((Action)value).setVisualization(m_vis);
91         m_chain.add(p, value);
92     }
93     
94     /**
95      * Lookup the value mapped to by the given item.
96      * @param item the item to lookup
97      * @return the result of the rule lookup
98      */

99     protected Object JavaDoc lookup(VisualItem item) {
100         return (m_chain == null ? null : m_chain.get(item));
101     }
102     
103     /**
104      * Remove all rule mappings from this encoder.
105      */

106     public void clear() {
107         if ( m_chain != null ) {
108             m_chain.clear();
109         }
110     }
111     
112     /**
113      * Remove rules using the given predicate from this encoder.
114      * This method will not remove rules in which this predicate is used
115      * within a composite of clauses, such as an AND or OR. It only removes
116      * rules using this predicate as the top-level trigger.
117      * @param p the predicate to remove
118      * @return true if a rule was successfully removed, false otherwise
119      */

120     public boolean remove(Predicate p) {
121         return ( m_chain != null ? m_chain.remove(p) : false );
122     }
123     
124     /**
125      * @see prefuse.action.Action#setVisualization(prefuse.Visualization)
126      */

127     public void setVisualization(Visualization vis) {
128         super.setVisualization(vis);
129         if ( m_chain != null )
130             m_chain.getExpression().visit(new SetVisualizationVisitor());
131     }
132     
133     // ------------------------------------------------------------------------
134

135     /**
136      * @see prefuse.action.Action#run(double)
137      */

138     public void run(double frac) {
139         setup();
140         if ( m_chain != null )
141             m_chain.getExpression().visit(SetupVisitor.getInstance());
142         
143         super.run(frac);
144         
145         if ( m_chain != null )
146             m_chain.getExpression().visit(FinishVisitor.getInstance());
147         finish();
148     }
149     
150     /**
151      * Perform any necessary setup before this encoder can be used. By default
152      * does nothing. Subclasses can override this method to perform custom
153      * setup before the Action is used.
154      */

155     protected void setup() {
156         // do nothing be default
157
}
158     
159     /**
160      * Perform any necessary clean-up after this encoder can be used. By
161      * default does nothing. Subclasses can override this method to perform
162      * custom clean-up after the Action is used.
163      */

164     protected void finish() {
165         // do nothing be default
166
}
167     
168     // ------------------------------------------------------------------------
169

170     /**
171      * Abstract class for processing the Actions stored in the predicate chain.
172      */

173     private static abstract class ActionVisitor implements ExpressionVisitor {
174         public void visitExpression(Expression expr) {
175             if ( expr instanceof ObjectLiteral ) {
176                 Object JavaDoc val = expr.get(null);
177                 if ( val instanceof Action )
178                     visitAction(((Action)val));
179             }
180         }
181         public abstract void visitAction(Action a);
182         public void down() { /* do nothing */ }
183         public void up() { /* do nothing */ }
184     }
185     
186     /**
187      * Sets the visualization status for any Actions contained within the
188      * predicate chain.
189      */

190     private class SetVisualizationVisitor extends ActionVisitor {
191         public void visitAction(Action a) {
192             a.setVisualization(m_vis);
193         }
194     }
195     
196     /**
197      * Calls the "setup" method for any delegate actions contained within
198      * the rule-mappings for this encoder.
199      */

200     private static class SetupVisitor extends ActionVisitor {
201         private static SetupVisitor s_instance;
202         public static SetupVisitor getInstance() {
203             if ( s_instance == null )
204                 s_instance = new SetupVisitor();
205             return s_instance;
206         }
207         public void visitAction(Action a) {
208             if ( a instanceof EncoderAction )
209                 ((EncoderAction)a).setup();
210         }
211     }
212     
213     /**
214      * Calls the "setup" method for any delegate actions contained within
215      * the rule-mappings for this encoder.
216      */

217     private static class FinishVisitor extends ActionVisitor {
218         private static FinishVisitor s_instance;
219         public static FinishVisitor getInstance() {
220             if ( s_instance == null )
221                 s_instance = new FinishVisitor();
222             return s_instance;
223         }
224         public void visitAction(Action a) {
225             if ( a instanceof EncoderAction )
226                 ((EncoderAction)a).setup();
227         }
228     }
229     
230 } // end of class EncoderAction
Popular Tags