AxesRotationTracker.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.Cursor;
00016 import java.awt.Image;
00017 import java.awt.Point;
00018 import java.awt.Toolkit;
00019 import java.awt.event.FocusEvent;
00020 import java.awt.event.FocusListener;
00021 import java.awt.event.MouseEvent;
00022 import java.awt.event.MouseListener;
00023 
00024 import org.scilab.modules.gui.bridge.canvas.SwingScilabCanvas;
00025 
00030 public class AxesRotationTracker extends MouseDisplacementTracker implements MouseListener, FocusListener {
00031         
00032         private boolean recordStarted;
00033         private boolean recordEnded;
00034         private boolean isWaitingForClick;
00035         
00036         private int clickPosX;
00037         private int clickPosY;
00038         
00043         public AxesRotationTracker(SwingScilabCanvas canvas) {
00044                 super(canvas);
00045                 reinit();
00046         }
00047         
00051         protected void reinit() {
00052                 super.reinit();
00053                 this.recordStarted = false;
00054                 this.recordEnded = false;
00055                 this.isWaitingForClick = false;
00056                 clickPosX = -1;
00057                 clickPosY = -1;
00058                 // Back to default cursor
00059                 getTrackedCanvas().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
00060         }
00061         
00066         public void waitForClick(int[] clickPosition) {
00067                 getTrackedCanvas().addMouseListener(this);
00068                 getTrackedCanvas().addFocusListener(this);
00069                 synchronized (getLock()) {
00070                         isWaitingForClick = true;
00071                         // wait until the click occures
00072                         try {
00073                                 getLock().wait();
00074                         } catch (InterruptedException e) {
00075                                 e.printStackTrace();
00076                         }
00077                         clickPosition[0] = clickPosX;
00078                         clickPosition[1] = clickPosY;
00079                 }
00080                 getTrackedCanvas().removeMouseListener(this);
00081                 getTrackedCanvas().removeFocusListener(this);
00082         }
00083         
00090         public boolean getDisplacement(int[] displacement) {
00091                 // Change cursor
00092                 
00093                 if (!recordStarted) {
00094                         // first call
00095                         Image icon = Toolkit.getDefaultToolkit().getImage(System.getenv("SCI") + "/modules/gui/images/icons/rotate.png");
00096                         getTrackedCanvas().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(icon, new Point(0, 0), "rotate"));
00097                         waitForClick(displacement);
00098                         startRecording(clickPosX, clickPosY);
00099                         return true;
00100                 } else if (!recordEnded) {
00101                         // inside tracking loop
00102                         // get mouse displacement since last call
00103                         getMouseDisplacement(displacement);
00104                         return true;
00105                 } else {
00106                         // last call get current displacement and return
00107                         getImmediateMouseDisplacement(displacement);
00108                         
00109                         // reinit for a next call
00110                         reinit();
00111 
00112                         return false;
00113                 }
00114         }
00115         
00119         public void cancelRecording() {
00120                 endRecording();
00121                 reinit();
00122         }
00123         
00129         protected void startRecording(int initX, int initY) {
00130                 activateTracking(initX, initY);
00131                 // for final click
00132                 getTrackedCanvas().addMouseListener(this);
00133                 getTrackedCanvas().addFocusListener(this);
00134                 recordStarted = true;
00135         }
00136         
00140         protected void endRecording() {
00141                 if (recordStarted) {
00142                         desactivateTracking();
00143                 }
00144                 getTrackedCanvas().removeMouseListener(this);
00145                 getTrackedCanvas().removeFocusListener(this);
00146                 recordEnded = true;
00147         }
00148         
00152         public void mouseClicked(MouseEvent event) {
00153                 // everything si done in mouse pressed
00154         }
00155 
00159         public void mouseEntered(MouseEvent event) {
00160                 // not used
00161 
00162         }
00163 
00167         public void mouseExited(MouseEvent event) {
00168                 // not used
00169 
00170         }
00171 
00175         public void mousePressed(MouseEvent event) {
00176                 if (isWaitingForClick) {
00177                         clickPosX = event.getX();
00178                         clickPosY = event.getY();
00179                         isWaitingForClick = false;
00180                         
00181                         // wake the click waiter
00182                         synchronized (getLock()) {
00183                                 getLock().notifyAll();
00184                         }
00185                         
00186                         return;
00187                 } else if (recordStarted && !recordEnded) {
00188                         // tracking loop
00189                         
00190                         // update position
00191                         // so next call to getDisplacement will get it
00192                         updateDisplacement(event.getX(), event.getY());
00193                         
00194                         // we finish record loop
00195                         endRecording();
00196                 }
00197         }
00198 
00202         public void mouseReleased(MouseEvent event) {
00203                 // nothing to do
00204 
00205         }
00206         
00210         public void focusGained(FocusEvent event) {
00211                 // nothing to do here
00212                 // canvas must always have focus
00213         }
00214 
00220         public void focusLost(FocusEvent event) {
00221                 // focus lost so stop recording
00222                 endRecording();
00223                 synchronized (getLock()) {
00224                         getLock().notifyAll();
00225                 }
00226         }
00227 
00228 }

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