1 5 package prefuse.action.assignment; 6 7 import java.util.logging.Logger ; 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 47 public class ColorAction extends EncoderAction { 48 49 protected String m_colorField; 50 protected String m_startField; 51 protected String m_endField; 52 53 protected int m_cidx, m_sidx, m_eidx; 54 55 protected int m_defaultColor = ColorLib.gray(0); 57 63 public ColorAction(String group, String field) { 64 super(group); 65 setField(field); 66 } 67 68 75 public ColorAction(String group, String field, int color) { 76 this(group, field); 77 m_defaultColor = color; 78 } 79 80 89 public ColorAction(String group, Predicate filter, String field) { 90 super(group, filter); 91 setField(field); 92 } 93 94 103 public ColorAction(String group, Predicate filter, String field, int color) 104 { 105 this(group, filter, field); 106 setDefaultColor(color); 107 } 108 109 115 public void setField(String field) { 116 m_colorField = field; 117 m_startField = PrefuseLib.getStartField(field); 118 m_endField = PrefuseLib.getEndField(field); 119 } 120 121 125 public int getDefaultColor() { 126 return m_defaultColor; 127 } 128 129 134 public void setDefaultColor(int color) { 135 m_defaultColor = color; 136 } 137 138 145 public void add(Predicate p, int color) { 146 super.add(p, new Integer (color)); 147 } 148 149 159 public void add(String expr, int color) { 160 Predicate p = (Predicate)ExpressionParser.parse(expr); 161 add(p, color); 162 } 163 164 171 public void add(Predicate p, ColorAction f) { 172 super.add(p, f); 173 } 174 175 185 public void add(String expr, ColorAction f) { 186 Predicate p = (Predicate)ExpressionParser.parse(expr); 187 super.add(p, f); 188 } 189 190 192 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 211 public int getColor(VisualItem item) { 212 Object o = lookup(item); 213 if ( o != null ) { 214 if ( o instanceof ColorAction ) { 215 return ((ColorAction)o).getColor(item); 216 } else if ( o instanceof Integer ) { 217 return ((Integer )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 } | Popular Tags |