1 package prefuse.demos; 2 3 import java.awt.Font ; 4 import java.awt.Shape ; 5 import java.awt.event.ComponentAdapter ; 6 import java.awt.event.ComponentEvent ; 7 8 import javax.swing.BorderFactory ; 9 import javax.swing.JFrame ; 10 11 import prefuse.Constants; 12 import prefuse.Display; 13 import prefuse.Visualization; 14 import prefuse.action.Action; 15 import prefuse.action.ActionList; 16 import prefuse.action.RepaintAction; 17 import prefuse.action.animate.ColorAnimator; 18 import prefuse.action.animate.LocationAnimator; 19 import prefuse.action.assignment.ColorAction; 20 import prefuse.action.layout.AxisLayout; 21 import prefuse.activity.Activity; 22 import prefuse.activity.ActivityAdapter; 23 import prefuse.activity.SlowInSlowOutPacer; 24 import prefuse.data.Schema; 25 import prefuse.data.Table; 26 import prefuse.data.Tuple; 27 import prefuse.data.event.TupleSetListener; 28 import prefuse.data.expression.FunctionExpression; 29 import prefuse.data.expression.FunctionTable; 30 import prefuse.data.expression.Predicate; 31 import prefuse.data.expression.parser.ExpressionParser; 32 import prefuse.data.io.DataIOException; 33 import prefuse.data.io.DelimitedTextTableReader; 34 import prefuse.data.query.SearchQueryBinding; 35 import prefuse.data.search.SearchTupleSet; 36 import prefuse.data.tuple.TupleSet; 37 import prefuse.render.DefaultRendererFactory; 38 import prefuse.render.ShapeRenderer; 39 import prefuse.render.LabelRenderer; 40 import prefuse.util.ColorLib; 41 import prefuse.util.FontLib; 42 import prefuse.util.PrefuseLib; 43 import prefuse.util.ui.JSearchPanel; 44 import prefuse.visual.VisualItem; 45 import prefuse.visual.VisualTable; 46 47 58 public class ZipDecode extends Display implements Constants { 59 60 public static final String ZIPCODES = "/zipcode.txt"; 61 public static final String STATES = "/state.txt"; 62 63 private static final String DATA = "data"; 65 private static final String LABELS = "labels"; 66 private static final String FOCUS = Visualization.FOCUS_ITEMS; 67 68 public static class StateLookupFunction extends FunctionExpression { 69 private static Table s_states; 70 static { 71 try { 72 s_states = new DelimitedTextTableReader().readTable(STATES); 73 } catch ( Exception e ) { e.printStackTrace(); } 74 } 75 76 public StateLookupFunction() { super(1); } 77 public String getName() { return "STATE"; } 78 public Class getType(Schema s) { return String .class; } 79 public Object get(Tuple t) { 80 int code = s_states.index("code").get(param(0).getInt(t)); 81 return s_states.getString(code, "alpha"); 82 } 83 } 84 static { FunctionTable.addFunction("STATE", StateLookupFunction.class); } 86 87 88 public ZipDecode(final Table t) { 89 super(new Visualization()); 90 91 Predicate filter = (Predicate)ExpressionParser.parse( 93 "state >= 1 && state <= 56 && state != 2 && state != 15"); 94 VisualTable vt = m_vis.addTable(DATA, t, filter, getDataSchema()); 95 vt.addColumn("zipstr", "LPAD(zip,5,'0')"); 98 vt.addColumn("label", "CONCAT(CAP(city),', ',STATE(state),' ',zipstr)"); 100 101 Predicate loneResult = (Predicate)ExpressionParser.parse( 103 "INGROUP('_search_') AND GROUPSIZE('_search_')=1 AND " + 104 "LENGTH(QUERY('_search_'))=5"); 105 106 m_vis.addDerivedTable(LABELS, DATA, loneResult, getLabelSchema()); 111 112 114 DefaultRendererFactory rf = new DefaultRendererFactory(); 115 rf.setDefaultRenderer(new ShapeRenderer(1)); rf.add("INGROUP('labels')", new LabelRenderer("label") { 117 public Shape getShape(VisualItem item) { 118 setHorizontalAlignment(item.getX()>getWidth()/2 ? RIGHT:LEFT); 120 return super.getShape(item); 122 } 123 }); 124 m_vis.setRendererFactory(rf); 125 126 128 ActionList layout = new ActionList(); 129 layout.add(new AxisLayout(DATA, "lat", Y_AXIS)); 130 layout.add(new AxisLayout(DATA, "lon", X_AXIS)); 131 m_vis.putAction("layout", layout); 132 133 final Action update = new ZipColorAction(FOCUS); 137 m_vis.putAction("update", update); 138 139 ActionList animate = new ActionList(200); 144 animate.add(new ColorAnimator(FOCUS, VisualItem.FILLCOLOR)); 145 animate.add(new ColorAnimator(LABELS, VisualItem.TEXTCOLOR)); 146 animate.add(new RepaintAction()); 147 animate.addActivityListener(new ActivityAdapter() { 148 public void activityCancelled(Activity a) { 149 update.run(1.0); 151 } 152 }); 153 m_vis.putAction("animate", animate); 154 155 ActionList resize = new ActionList(1500); 158 resize.setPacingFunction(new SlowInSlowOutPacer()); 159 resize.add(new LocationAnimator(DATA)); 160 resize.add(new LocationAnimator(LABELS)); 161 resize.add(new RepaintAction()); 162 m_vis.putAction("resize", resize); 163 164 166 setSize(720, 360); 167 setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 168 setBackground(ColorLib.getGrayscale(50)); 169 setFocusable(false); 170 171 173 180 final TupleSet focus = m_vis.getFocusGroup(FOCUS); 183 184 SearchQueryBinding searchQ = new SearchQueryBinding(vt, "zipstr"); 186 final SearchTupleSet search = searchQ.getSearchSet(); 187 188 search.addTupleSetListener(new TupleSetListener() { 190 public void tupleSetChanged(TupleSet t, Tuple[] add, Tuple[] rem) { 191 m_vis.cancel("animate"); 192 193 focus.clear(); 195 for ( int i=0; i<add.length; ++i ) { 196 ((VisualItem)add[i]).setValidated(false); 197 focus.addTuple(add[i]); 198 } 199 for ( int i=0; i<rem.length; ++i ) { 200 ((VisualItem)rem[i]).setValidated(false); 201 focus.addTuple(rem[i]); 202 } 203 204 m_vis.run("update"); 205 m_vis.run("animate"); 206 } 207 }); 208 m_vis.addFocusGroup(Visualization.SEARCH_ITEMS, search); 209 210 final JSearchPanel searcher = searchQ.createSearchPanel(); 212 searcher.setLabelText("zip>"); searcher.setShowCancel(false); searcher.setShowBorder(false); searcher.setFont(FontLib.getFont("Georgia", Font.PLAIN, 22)); 216 searcher.setBackground(ColorLib.getGrayscale(50)); 217 searcher.setForeground(ColorLib.getColor(100,100,75)); 218 add(searcher); searcher.setBounds(10, getHeight()-40, 120, 30); 220 221 addComponentListener(new ComponentAdapter () { 222 public void componentResized(ComponentEvent e) { 223 m_vis.run("layout"); 224 m_vis.run("update"); 225 m_vis.run("resize"); 226 searcher.setBounds(10, getHeight()-40, 120, 30); 227 invalidate(); 228 } 229 }); 230 231 233 m_vis.run("layout"); 234 m_vis.run("animate"); 235 } 236 237 private static Schema getDataSchema() { 238 Schema s = PrefuseLib.getVisualItemSchema(); 239 s.setDefault(VisualItem.INTERACTIVE, false); 240 s.setDefault(VisualItem.FILLCOLOR, ColorLib.rgb(100,100,75)); 241 return s; 242 } 243 244 private static Schema getLabelSchema() { 245 Schema s = PrefuseLib.getMinimalVisualSchema(); 246 s.setDefault(VisualItem.INTERACTIVE, false); 247 248 s.addInterpolatedColumn( 250 VisualItem.FONT, Font .class, FontLib.getFont("Georgia",16)); 251 252 s.addInterpolatedColumn(VisualItem.FILLCOLOR, int.class); 254 s.setInterpolatedDefault(VisualItem.FILLCOLOR, 0); 255 256 s.addInterpolatedColumn(VisualItem.TEXTCOLOR, int.class); 257 s.setInterpolatedDefault(VisualItem.TEXTCOLOR, ColorLib.gray(255)); 259 s.setDefault(VisualItem.STARTTEXTCOLOR, ColorLib.gray(255,0)); 261 return s; 262 } 263 264 266 public static void main(String [] args) { 267 String datafile = ZIPCODES; 268 if ( args.length > 0 ) 269 datafile = args[0]; 270 271 try { 272 JFrame frame = demo(datafile); 273 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 274 frame.setVisible(true); 275 } catch ( Exception e ) { 276 e.printStackTrace(); 277 System.exit(1); 278 } 279 } 280 281 public static JFrame demo() { 282 try { 283 return demo(ZIPCODES); 284 } catch ( Exception e ) { 285 return null; 286 } 287 } 288 289 public static JFrame demo(String table) throws DataIOException { 290 DelimitedTextTableReader tr = new DelimitedTextTableReader(); 291 Table t = tr.readTable(table); 292 ZipDecode zd = new ZipDecode(t); 293 294 JFrame frame = new JFrame ("p r e f u s e | z i p d e c o d e"); 295 frame.getContentPane().add(zd); 296 frame.pack(); 297 return frame; 298 } 299 300 public static class ZipColorAction extends ColorAction { 301 public ZipColorAction(String group) { 302 super(group, VisualItem.FILLCOLOR); 303 } 304 305 public int getColor(VisualItem item) { 306 if ( item.isInGroup(Visualization.SEARCH_ITEMS) ) { 307 return ColorLib.gray(255); 308 } else { 309 return ColorLib.rgb(100,100,75); 310 } 311 } 312 } 313 314 } | Popular Tags |