AbstractTimeCanvas.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.traceviewer.painter;
00002 
00003 import org.eclipse.swt.SWT;
00004 import org.eclipse.swt.events.FocusAdapter;
00005 import org.eclipse.swt.events.FocusEvent;
00006 import org.eclipse.swt.events.MouseEvent;
00007 import org.eclipse.swt.events.PaintEvent;
00008 import org.eclipse.swt.events.PaintListener;
00009 import org.eclipse.swt.graphics.Point;
00010 import org.eclipse.swt.graphics.Rectangle;
00011 import org.eclipse.swt.widgets.Composite;
00012 
00013 import edu.rice.cs.hpc.data.util.OSValidator;
00014 import edu.rice.cs.hpc.traceviewer.data.util.Constants;
00015 
00016 
00017 
00018 /**********************************************************************************
00019  * 
00020  * abstract class for helper information (located on the bottom of the window)
00021  *
00022  **********************************************************************************/
00023 public abstract class AbstractTimeCanvas
00024 extends BufferedCanvas
00025 implements ITraceCanvas, PaintListener
00026 {
00028     private ITraceCanvas.MouseState mouseState;
00029     
00031     private Point mouseDown;
00032     
00034     private Rectangle selection;
00035     
00036     protected enum RegionType {Vertical, Rectangle};
00037     
00038     final private RegionType regionType;
00039     
00040     /****************
00041      * Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.
00042      *
00043      * @param composite
00044      * @param style
00045      ****************/
00046     public AbstractTimeCanvas(Composite composite, int style) {
00047         this(composite, style, RegionType.Vertical);
00048     }
00049     
00050     public AbstractTimeCanvas(Composite composite, int style, RegionType regionType) {
00051         super(composite);
00052         mouseState = ITraceCanvas.MouseState.ST_MOUSE_INIT;
00053         this.regionType = regionType;
00054     }
00055     
00056     public void init() 
00057     {
00058         if (mouseState == ITraceCanvas.MouseState.ST_MOUSE_INIT) 
00059         {
00060             addMouseListener(this);
00061             addMouseMoveListener(this);
00062             addPaintListener(this);
00063             
00064             // need to initialize mouse selection variables when we lost the focus
00065             //  otherwise, Eclipse will keep the variables and draw useless selected rectangles
00066             addFocusListener(new FocusAdapter() {
00067                 /*
00068                  * (non-Javadoc)
00069                  * @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent)
00070                  */
00071                 public void focusLost(FocusEvent e) {
00072                     AbstractTimeCanvas.this.initMouseSelection();
00073                 }
00074             });
00075         }
00076         initMouseSelection();
00077         super.initBuffer();
00078     }
00079     
00080     /*****
00081      * initialize variables 
00082      */
00083     private void initMouseSelection()
00084     {
00085         selection = new Rectangle(0, 0, 0, 0);
00086         mouseState = ITraceCanvas.MouseState.ST_MOUSE_NONE;
00087     }
00088     
00089     @Override
00090     public void mouseMove(MouseEvent e) 
00091     {
00092         if (mouseState == ITraceCanvas.MouseState.ST_MOUSE_DOWN)
00093         {
00094             Point pos = new Point(e.x, e.y);
00095             adjustPosition(mouseDown, pos);
00096             
00097             redraw();
00098         }
00099     }
00100 
00101     @Override
00102     public void mouseDoubleClick(MouseEvent e) {}
00103 
00104     @Override
00105     public void mouseDown(MouseEvent e) 
00106     {
00107         // take into account ONLY when the button-1 is clicked and it's never been clicked before
00108         // the click is not right click (or modifier click on Mac)
00109         if (e.button == 1 && mouseState == ITraceCanvas.MouseState.ST_MOUSE_NONE 
00110                 && (e.stateMask & SWT.MODIFIER_MASK)==0 )
00111         {
00112             mouseState = ITraceCanvas.MouseState.ST_MOUSE_DOWN;
00113             mouseDown = new Point(e.x,e.y);
00114         }
00115     }
00116 
00117     @Override
00118     public void mouseUp(MouseEvent e) 
00119     {
00120         if (mouseState == ITraceCanvas.MouseState.ST_MOUSE_DOWN)
00121         {
00122             Point mouseUp = new Point(e.x,e.y);
00123             mouseState = ITraceCanvas.MouseState.ST_MOUSE_NONE;
00124             
00125             //difference in mouse movement < 3 constitutes a "single click"
00126             if(Math.abs(mouseUp.x-mouseDown.x)<3 && Math.abs(mouseUp.y-mouseDown.y)<3)
00127             {
00128                 changePosition(mouseDown);
00129             }
00130             else
00131             {
00132                 adjustPosition(mouseDown, mouseUp);
00133                 changeRegion(selection);
00134             }
00135             redraw();
00136         }
00137     }
00138 
00139     @Override
00140     public void paintControl(PaintEvent event) 
00141     {
00142         super.paintControl(event);
00143         
00144         //paints the selection currently being made
00145         if (mouseState==ITraceCanvas.MouseState.ST_MOUSE_DOWN)
00146         {
00147             // some Unix machines have no advanced graphic function
00148             // to render the alpha transformation.
00149             if (!OSValidator.isUnix()) 
00150             {
00151                 event.gc.setBackground(Constants.COLOR_WHITE);
00152                 event.gc.setAlpha(100);
00153                 event.gc.fillRectangle( selection );
00154                 event.gc.setAlpha(240);
00155     
00156             }           
00157             event.gc.setLineWidth(2);
00158             event.gc.setForeground(Constants.COLOR_BLACK);
00159             event.gc.drawRectangle(selection);
00160         }
00161     }
00162     
00163     
00164     private void adjustPosition(Point p1, Point p2) 
00165     {
00166         selection.x = Math.min(p1.x, p2.x);
00167         selection.width = Math.max(p1.x, p2.x) - selection.x;
00168         final Rectangle area   = getClientArea();
00169 
00170         switch(regionType) {
00171         case Rectangle:
00172             selection.y = Math.min(p1.y, p2.y);
00173             selection.height = Math.max(p1.y, p2.y) - selection.y;
00174             break;
00175         case Vertical:
00176             default:
00177             selection.y = 0;
00178             selection.height = area.height;
00179             break;
00180         }
00181     }
00182     
00183     /*************************
00184      * function called when there's a change of mouse click position
00185      * 
00186      * @param point
00187      *************************/
00188     protected abstract void changePosition(Point point);
00189     
00190     
00191     /***************************
00192      * function called when there's a change of selected region
00193      * 
00194      * @param left
00195      * @param right
00196      ***************************/
00197     protected abstract void changeRegion(Rectangle region);
00198 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1