TimelineCanvas.java

00001 package edu.stanford.hci.r3.browse;
00002 
00003 import java.awt.BasicStroke;
00004 import java.awt.Color;
00005 import java.awt.Dimension;
00006 import java.awt.geom.Line2D;
00007 import java.util.ArrayList;
00008 import java.util.List;
00009 
00010 import edu.stanford.hci.r3.util.DebugUtils;
00011 import edu.umd.cs.piccolo.PCanvas;
00012 import edu.umd.cs.piccolo.PNode;
00013 import edu.umd.cs.piccolo.event.PInputEvent;
00014 import edu.umd.cs.piccolo.nodes.PPath;
00015 import edu.umd.cs.piccolo.util.PBounds;
00016 
00029 public class TimelineCanvas extends PCanvas {
00030 
00031         private Color axisColor;
00032         private Color tickColor;
00033         private PPath axisPath;
00034         private int canvasHeight = 100;
00035 
00036         
00037         private List<PPath> tickMarksInView = new ArrayList<PPath>();
00038         
00039         public TimelineCanvas() {
00040                 useDefaultTheme();
00041                 setPreferredSize(new Dimension(getPreferredSize().width, canvasHeight));
00042 
00043                 setupVisualComponents();
00044 
00045                 setPanEventHandler(null);
00046                 setZoomEventHandler(null);
00047 
00048                 // setAnimatingRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);
00049 
00050                 // only drag left and right
00051                 addInputEventListener(new HorizontalPanEventHandler() {
00052                         protected void drag(PInputEvent e) {
00053                                 super.drag(e);
00054                                 handleDrag();
00055                         }
00056 
00057                 });
00058         }
00059 
00060         private void handleDrag() {
00061                 PBounds viewBounds = getCamera().getViewBounds();
00062                 DebugUtils.println(viewBounds);
00063                 
00064                 // find all the marks that are within the view, or that have scrolled off camera...
00065                 
00066                 for (PPath tick : tickMarksInView) {
00067                         double tickX = tick.getX();
00068                         if (tickX < viewBounds.x) {
00069                                 DebugUtils.println(tickX + " LEFT");
00070                         } else if (tickX >= viewBounds.x + viewBounds.width) {
00071                                 DebugUtils.println(tickX + " RIGHT");
00072                         } else {
00073                                 DebugUtils.println(tickX + " IN");
00074                         }
00075                 }
00076         }
00077 
00078         private PNode getAxisPath() {
00079                 if (axisPath == null) {
00080                         // HACK: -5 to 3000 is larger than most monitors today... =)
00081                         // Really, this should be resized to the window at all times.
00082                         axisPath = new PPath(new Line2D.Double(-5, canvasHeight / 2, 3000, canvasHeight / 2));
00083                         axisPath.setStroke(new BasicStroke(1));
00084                         axisPath.setStrokePaint(axisColor);
00085                 }
00086                 return axisPath;
00087         }
00088 
00089         private void setupVisualComponents() {
00090                 getCamera().addChild(getAxisPath());
00091 
00092                 // set up the initial vertical tick marks...
00093                 // remove them when they go off camera... add them when they are about to come on camera...?
00094                 for (int i = 0; i < 10; i++) {
00095                         PPath vTickMark = getVerticalTickMarkAtPercentageLocation(i / 10.0 + .25);
00096                         tickMarksInView.add(vTickMark);
00097                         getLayer().addChild(vTickMark);
00098                 }
00099         }
00100 
00106         private PPath getVerticalTickMarkAtPercentageLocation(double xLocation) {
00107                 PPath tick = new PPath(new Line2D.Double(xLocation * 1000, canvasHeight/2 - 10, xLocation * 1000,
00108                                 canvasHeight/2 + 10));
00109                 tick.setStroke(new BasicStroke(2));
00110                 tick.setStrokePaint(tickColor);
00111                 return tick;
00112         }
00113 
00114         public void useDefaultTheme() {
00115                 setBackground(new Color(20, 20, 20));
00116                 axisColor = new Color(180, 180, 180);
00117                 tickColor = new Color(108, 187, 252);
00118         }
00119 
00120 }

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