EventBrowser.java

00001 package edu.stanford.hci.r3.events.replay;
00002 
00003 import java.io.File;
00004 
00005 import javax.swing.JFrame;
00006 
00007 import prefuse.Constants;
00008 import prefuse.Display;
00009 import prefuse.Visualization;
00010 import prefuse.action.ActionList;
00011 import prefuse.action.RepaintAction;
00012 import prefuse.action.filter.VisibilityFilter;
00013 import prefuse.action.layout.AxisLayout;
00014 import prefuse.data.Table;
00015 import prefuse.data.expression.AndPredicate;
00016 import prefuse.data.query.RangeQueryBinding;
00017 import prefuse.render.AbstractShapeRenderer;
00018 import prefuse.render.AxisRenderer;
00019 import prefuse.render.Renderer;
00020 import prefuse.render.RendererFactory;
00021 import prefuse.render.ShapeRenderer;
00022 import prefuse.visual.VisualItem;
00023 import prefuse.visual.VisualTable;
00024 import prefuse.visual.expression.VisiblePredicate;
00025 import edu.stanford.hci.r3.PaperToolkit;
00026 import edu.stanford.hci.r3.events.EventEngine;
00027 import edu.stanford.hci.r3.events.EventType;
00028 import edu.stanford.hci.r3.util.DebugUtils;
00029 
00043 public class EventBrowser {
00044 
00048         private static final String penEventsGroup = "PenEventsGroup";
00049 
00053         public static void main(String[] args) {
00054                 PaperToolkit.initializeLookAndFeel();
00055                 EventBrowser browser = new EventBrowser(new EventEngine().getEventReplayManager());
00056                 browser.setVisible(true);
00057         }
00058 
00062         private Table dataTable;
00063 
00067         private Display display;
00068 
00072         private EventReplayManager eventReplayManager;
00073 
00077         private EventBrowserView view;
00078 
00082         private Visualization vis;
00083 
00088         public EventBrowser(EventReplayManager replayMgr) {
00089                 eventReplayManager = replayMgr;
00090                 setupVisualization();
00091                 view = new EventBrowserView(this, display);
00092                 eventReplayManager.loadMostRecentEventData();
00093         }
00094 
00098         public void clearLoadedEvents() {
00099                 eventReplayManager.clearLoadedEvents();
00100         }
00101 
00105         private Table getPenEventsTable() {
00106                 if (dataTable == null) {
00107                         dataTable = new Table();
00108                         dataTable.addColumn("Timestamp", Long.class);
00109                         dataTable.addColumn("Type", EventType.class);
00110 
00111                         importData(dataTable);
00112 
00113                         final int numRows = dataTable.getRowCount();
00114                         final int numCols = dataTable.getColumnCount();
00115                         for (int r = 0; r < numRows; r++) {
00116                                 StringBuilder sb = new StringBuilder();
00117                                 for (int c = 0; c < numCols; c++) {
00118                                         sb.append(dataTable.get(r, c) + "\t");
00119                                 }
00120                                 DebugUtils.println(sb);
00121                         }
00122                 }
00123                 return dataTable;
00124         }
00125 
00129         private void importData(Table data) {
00130                 data.addRows(1);
00131                 data.set(0, 0, 123L);
00132                 data.set(0, 1, EventType.STREAMING);
00133         }
00134 
00138         public void loadEventData(File f) {
00139                 eventReplayManager.loadEventDataFrom(f);
00140         }
00141 
00145         public void replayLoadedEvents() {
00146                 eventReplayManager.replayLoadedEvents();
00147         }
00148 
00152         public void setDefaultCloseOperationToExit() {
00153                 view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00154         }
00155 
00159         private void setupVisualization() {
00160                 vis = new Visualization();
00161                 VisualTable vt = vis.addTable(penEventsGroup, getPenEventsTable());
00162                 vis.setRendererFactory(new RendererFactory() {
00163                         Renderer arX = new AxisRenderer(Constants.CENTER, Constants.FAR_BOTTOM);
00164 
00165                         Renderer arY = new AxisRenderer(Constants.RIGHT, Constants.TOP);
00166 
00167                         AbstractShapeRenderer sr = new ShapeRenderer();
00168 
00169                         public Renderer getRenderer(VisualItem item) {
00170                                 return item.isInGroup("ylab") ? arY : item.isInGroup("xlab") ? arX : sr;
00171                         }
00172                 });
00173 
00174                 // --------------------------------------------------------------------
00175                 // STEP 2: create actions to process the visual data
00176 
00177                 // set up dynamic queries, search set
00178                 RangeQueryBinding eventsQuery = new RangeQueryBinding(vt, "Timestamp");
00179 
00180                 AxisLayout yaxis = new AxisLayout("Events Group", "Timestamp", Constants.Y_AXIS,
00181                                 VisiblePredicate.TRUE);
00182 
00183                 AndPredicate filter = new AndPredicate(eventsQuery.getPredicate());
00184 
00185                 ActionList update = new ActionList();
00186                 update.add(new VisibilityFilter(penEventsGroup, filter));
00187                 update.add(yaxis);
00188                 update.add(new RepaintAction());
00189                 vis.putAction("update", update);
00190 
00191                 display = new Display(vis);
00192         }
00193 
00197         public void setVisible(boolean b) {
00198                 view.setVisible(true);
00199         }
00200 }

Generated on Sat Apr 14 18:21:34 2007 for R3 Paper Toolkit by  doxygen 1.4.7