MouseDisplacementTracker.java

Go to the documentation of this file.
00001 /*
00002  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
00003  * Copyright (C) 2008 - INRIA - Jean-Baptiste Silvy
00004  * 
00005  * This file must be used under the terms of the CeCILL.
00006  * This source file is licensed as described in the file COPYING, which
00007  * you should have received as part of this distribution.  The terms
00008  * are also available at    
00009  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
00010  *
00011  */
00012 
00013 package org.scilab.modules.gui.events;
00014 
00015 import java.awt.event.MouseEvent;
00016 import java.awt.event.MouseMotionListener;
00017 
00018 import org.scilab.modules.gui.bridge.canvas.SwingScilabCanvas;
00019 
00024 public class MouseDisplacementTracker implements MouseMotionListener {
00025 
00027         private SwingScilabCanvas canvas;
00029         private Object lock;
00030         
00032         private boolean coordinatesUpdated;
00033         
00034         // coordinates of last call and the one fo current call
00035         private int previousX;
00036         private int previousY;
00037         private int currentX;
00038         private int currentY;
00039         
00040         
00045         public MouseDisplacementTracker(SwingScilabCanvas canvas) {
00046                 this.canvas = canvas;
00047                 this.lock = new Object();
00048                 reinit();
00049         }
00050         
00054         protected SwingScilabCanvas getTrackedCanvas() {
00055                 return canvas;
00056         }
00057         
00061         protected Object getLock() {
00062                 return lock;
00063         }
00064         
00068         protected void reinit() {
00069                 this.coordinatesUpdated = false;
00070                 this.previousX = -1;
00071                 this.previousY = -1;
00072                 this.currentX = -1;
00073                 this.currentY = -1;
00074         }
00075         
00081         protected void activateTracking(int initX, int initY) {
00082                 previousX = initX;
00083                 previousY = initY;
00084                 canvas.addMouseMotionListener(this);
00085         }
00086         
00090         protected void desactivateTracking() {
00091                 canvas.removeMouseMotionListener(this);
00092         }
00093         
00099         public void getMouseDisplacement(int[] displacement) {
00100                 
00101                 synchronized (lock) {
00102                         if (!coordinatesUpdated) {
00103                                 // no new displacement, wait for one
00104                                 try {
00105                                         lock.wait();
00106                                 } catch (InterruptedException e) {
00107                                         e.printStackTrace();
00108                                 }
00109                         }
00110                         
00111                         // get displacement
00112                         displacement[0] = currentX - previousX;
00113                         displacement[1] = currentY - previousY;
00114                         
00115                         // update previous coordinates
00116                         previousX = currentX;
00117                         previousY = currentY;
00118                         
00119                         coordinatesUpdated = false;
00120                 }
00121         }
00122         
00127         protected void getImmediateMouseDisplacement(int[] displacement) {
00128                 synchronized (lock) {
00129                         // get displacement
00130                         displacement[0] = currentX - previousX;
00131                         displacement[1] = currentY - previousY;
00132                 }
00133         }
00134         
00138         public void mouseDragged(MouseEvent event) {
00139                 // same as mouse moved
00140                 mouseMoved(event);
00141         }
00142         
00143 
00147         public void mouseMoved(MouseEvent event) {
00148                 updateDisplacement(event.getX(), event.getY());
00149         }
00150         
00156         protected void updateDisplacement(int xPos, int yPos) {
00157                 synchronized (lock) {
00158                         // record new position
00159                         currentX = xPos;
00160                         currentY = yPos;
00161                         
00162                         coordinatesUpdated = true;
00163                         
00164                         // wake threads waiting for a displacement
00165                         // if some
00166                         lock.notifyAll();
00167                 }
00168         }
00169 
00170 }

Generated on Tue Sep 9 17:48:25 2008 for Scilab [trunk] by  doxygen 1.5.5