KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > gvt > AbstractPanInteractor


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.swing.gvt;
19
20 import java.awt.Cursor JavaDoc;
21 import java.awt.event.MouseEvent JavaDoc;
22 import java.awt.geom.AffineTransform JavaDoc;
23
24 /**
25  * This class represents a pan interactor.
26  * To use it, just redefine the {@link
27  * InteractorAdapter#startInteraction(InputEvent)} method.
28  *
29  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
30  * @version $Id: AbstractPanInteractor.java,v 1.5 2004/08/18 07:15:32 vhardy Exp $
31  */

32 public abstract class AbstractPanInteractor extends InteractorAdapter {
33
34     /**
35      * The cursor for panning.
36      */

37     public final static Cursor JavaDoc PAN_CURSOR = new Cursor JavaDoc(Cursor.MOVE_CURSOR);
38
39     /**
40      * Whether the interactor has finished.
41      */

42     protected boolean finished = true;
43
44     /**
45      * The mouse x start position.
46      */

47     protected int xStart;
48
49     /**
50      * The mouse y start position.
51      */

52     protected int yStart;
53
54     /**
55      * The mouse x current position.
56      */

57     protected int xCurrent;
58
59     /**
60      * The mouse y current position.
61      */

62     protected int yCurrent;
63
64     /**
65      * To store the previous cursor.
66      */

67     protected Cursor JavaDoc previousCursor;
68
69     /**
70      * Tells whether the interactor has finished.
71      */

72     public boolean endInteraction() {
73         return finished;
74     }
75
76     // MouseListener ///////////////////////////////////////////////////////
77

78     /**
79      * Invoked when a mouse button has been pressed on a component.
80      */

81     public void mousePressed(MouseEvent JavaDoc e) {
82         if (!finished) {
83             mouseExited(e);
84             return;
85         }
86         
87         finished = false;
88
89         xStart = e.getX();
90         yStart = e.getY();
91
92         JGVTComponent c = (JGVTComponent)e.getSource();
93
94         previousCursor = c.getCursor();
95         c.setCursor(PAN_CURSOR);
96     }
97
98     /**
99      * Invoked when a mouse button has been released on a component.
100      */

101     public void mouseReleased(MouseEvent JavaDoc e) {
102         if (finished) {
103             return;
104         }
105         finished = true;
106
107         JGVTComponent c = (JGVTComponent)e.getSource();
108
109         xCurrent = e.getX();
110         yCurrent = e.getY();
111
112         AffineTransform JavaDoc at =
113             AffineTransform.getTranslateInstance(xCurrent - xStart,
114                                                  yCurrent - yStart);
115         AffineTransform JavaDoc rt =
116             (AffineTransform JavaDoc)c.getRenderingTransform().clone();
117         rt.preConcatenate(at);
118         c.setRenderingTransform(rt);
119
120         if (c.getCursor() == PAN_CURSOR) {
121             c.setCursor(previousCursor);
122         }
123     }
124
125     /**
126      * Invoked when the mouse exits a component.
127      */

128     public void mouseExited(MouseEvent JavaDoc e) {
129         finished = true;
130         
131         JGVTComponent c = (JGVTComponent)e.getSource();
132         c.setPaintingTransform(null);
133         if (c.getCursor() == PAN_CURSOR) {
134             c.setCursor(previousCursor);
135         }
136     }
137
138     // MouseMotionListener /////////////////////////////////////////////////
139

140     /**
141      * Invoked when a mouse button is pressed on a component and then
142      * dragged. Mouse drag events will continue to be delivered to
143      * the component where the first originated until the mouse button is
144      * released (regardless of whether the mouse position is within the
145      * bounds of the component).
146      */

147     public void mouseDragged(MouseEvent JavaDoc e) {
148         JGVTComponent c = (JGVTComponent)e.getSource();
149
150         xCurrent = e.getX();
151         yCurrent = e.getY();
152
153         AffineTransform JavaDoc at =
154             AffineTransform.getTranslateInstance(xCurrent - xStart,
155                                                  yCurrent - yStart);
156         c.setPaintingTransform(at);
157     }
158 }
159
Popular Tags