1 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 22 public abstract class EncoderAction extends ItemAction { 23 24 private PredicateChain m_chain = null; 25 26 29 public EncoderAction() { 30 super(); 31 } 32 33 38 public EncoderAction(Visualization vis) { 39 super(vis); 40 } 41 42 46 public EncoderAction(String group) { 47 super(group); 48 } 49 50 55 public EncoderAction(String group, Predicate filter) { 56 super(group, filter); 57 } 58 59 64 public EncoderAction(Visualization vis, String group) { 65 super(vis, group); 66 } 67 68 74 public EncoderAction(Visualization vis, String group, Predicate filter) { 75 super(vis, group, filter); 76 } 77 78 80 87 protected void add(Predicate p, Object 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 99 protected Object lookup(VisualItem item) { 100 return (m_chain == null ? null : m_chain.get(item)); 101 } 102 103 106 public void clear() { 107 if ( m_chain != null ) { 108 m_chain.clear(); 109 } 110 } 111 112 120 public boolean remove(Predicate p) { 121 return ( m_chain != null ? m_chain.remove(p) : false ); 122 } 123 124 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 135 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 155 protected void setup() { 156 } 158 159 164 protected void finish() { 165 } 167 168 170 173 private static abstract class ActionVisitor implements ExpressionVisitor { 174 public void visitExpression(Expression expr) { 175 if ( expr instanceof ObjectLiteral ) { 176 Object 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() { } 183 public void up() { } 184 } 185 186 190 private class SetVisualizationVisitor extends ActionVisitor { 191 public void visitAction(Action a) { 192 a.setVisualization(m_vis); 193 } 194 } 195 196 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 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 } | Popular Tags |