KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > AbstractAutoScrollSupport


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer;
28
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32
33 import javax.swing.Timer JavaDoc;
34
35 import org.nightlabs.editor2d.viewer.preferences.Preferences;
36
37 /**
38  * This is an abstract implementation of a generic AutoScrollSupport
39  *
40  * @author Daniel.Mazurek [dot] NightLabs [dot] de
41  *
42  */

43 public abstract class AbstractAutoScrollSupport
44 implements IAutoScrollSupport
45 {
46 // public static final int DEFAULT_SCROLL_STEP = 1;
47
// public static final int DEFAULT_SCROLL_TOLERANCE = 30;
48
// public static final int DEFAULT_TIMER_DELAY = 10;
49

50     public static final int DEFAULT_SCROLL_STEP =
51         Preferences.getPreferenceStore().getDefaultInt(Preferences.PREFERENCE_SCROLL_STEP);
52     public static final int DEFAULT_SCROLL_TOLERANCE =
53         Preferences.getPreferenceStore().getDefaultInt(Preferences.PREFERENCE_SCROLL_TOLERANCE);
54     public static final int DEFAULT_TIMER_DELAY =
55         Preferences.getPreferenceStore().getDefaultInt(Preferences.PREFERENCE_TIMER_DELAY);
56
57 // public static int scrollStep = DEFAULT_SCROLL_STEP;
58
protected int scrollStep = DEFAULT_SCROLL_STEP;
59         
60     /**
61      *
62      * @param step the value which will be scrolled by each timerDelay
63      */

64     public void setScrollStep(int step) {
65         scrollStep = step;
66     }
67     
68     /**
69      *
70      * @return the value which will be scrolled by each timerDelay
71      */

72     public int getScrollStep() {
73         return scrollStep;
74     }
75     
76 // public static int scrollTolerance = DEFAULT_SCROLL_TOLERANCE;
77
protected int scrollTolerance = DEFAULT_SCROLL_TOLERANCE;
78     
79     /**
80      *
81      * @param scrollTolerance the value which determines size
82      * of the area in which autoScrolling begins
83      */

84     public void setScrollTolerance(int scrollTolerance) {
85         this.scrollTolerance = scrollTolerance;
86     }
87     
88     /**
89      *
90      * @return the value which determines size
91      * of the area in which autoScrolling begins
92      */

93     public int getScrollTolerance() {
94         return scrollTolerance;
95     }
96     
97 // public static int timerDelay = DEFAULT_TIMER_DELAY;
98
protected int timerDelay = DEFAULT_TIMER_DELAY;
99     
100     /**
101      *
102      * @param timerDelay determines the amount of time (milliseconds) which passes each time
103      * the AutoScrollTimer for the repsective direction is triggered
104      */

105     public void setTimerDelay(int timerDelay) {
106         this.timerDelay = timerDelay;
107         timerDown.setDelay(timerDelay);
108         timerUp.setDelay(timerDelay);
109         timerLeft.setDelay(timerDelay);
110         timerRight.setDelay(timerDelay);
111     }
112
113     /**
114      *
115      * @return determines the amount of time (milliseconds) which passes each time
116      * the AutoScrollTimer for the repsective direction is triggered
117      */

118     public int getTimerDelay() {
119         return timerDelay;
120     }
121         
122     protected ActionListener JavaDoc scrollRight = new ActionListener JavaDoc()
123     {
124         public void actionPerformed(ActionEvent JavaDoc e) {
125             doScrollRight(scrollStep);
126         }
127     };
128         
129     protected ActionListener JavaDoc scrollLeft = new ActionListener JavaDoc()
130     {
131         public void actionPerformed(ActionEvent JavaDoc e) {
132             doScrollLeft(scrollStep);
133         }
134     };
135     
136     protected ActionListener JavaDoc scrollUp = new ActionListener JavaDoc()
137     {
138         public void actionPerformed(ActionEvent JavaDoc e) {
139             doScrollUp(scrollStep);
140         }
141     };
142     
143     protected ActionListener JavaDoc scrollDown = new ActionListener JavaDoc()
144     {
145         public void actionPerformed(ActionEvent JavaDoc e) {
146             doScrollDown(scrollStep);
147         }
148     };
149     
150     private Timer JavaDoc timerLeft = new Timer JavaDoc(timerDelay, scrollLeft);
151     private Timer JavaDoc timerRight = new Timer JavaDoc(timerDelay, scrollRight);
152     private Timer JavaDoc timerUp = new Timer JavaDoc(timerDelay, scrollUp);
153     private Timer JavaDoc timerDown = new Timer JavaDoc(timerDelay, scrollDown);
154     
155     private Rectangle JavaDoc upperScrollArea = null;
156     private Rectangle JavaDoc bottomScrollArea = null;
157     private Rectangle JavaDoc leftScrollArea = null;
158     private Rectangle JavaDoc rightScrollArea = null;
159     
160     private boolean isInTop = false;
161     private boolean isInBottom = false;
162     private boolean isInLeft = false;
163     private boolean isInRight = false;
164     
165     protected void initAutoScroll(Rectangle JavaDoc bounds)
166     {
167         int width = bounds.width;
168         int height = bounds.height;
169         upperScrollArea = new Rectangle JavaDoc(0, 0, width, scrollTolerance);
170         bottomScrollArea = new Rectangle JavaDoc(0, height-scrollTolerance, width, scrollTolerance);
171         leftScrollArea = new Rectangle JavaDoc(0, 0, scrollTolerance, height);
172         rightScrollArea = new Rectangle JavaDoc(width - scrollTolerance, 0, scrollTolerance, height);
173     }
174     
175     protected abstract void scrollDown(int scrollStep);
176     protected abstract void scrollUp(int scrollStep);
177     protected abstract void scrollLeft(int scrollStep);
178     protected abstract void scrollRight(int scrollStep);
179         
180     protected void mouseMoved(int x, int y)
181     {
182         if (!upperScrollArea.contains(x,y)) {
183             if (isInTop == true) {
184                 isInTop = false;
185                 timerUp.stop();
186             }
187         }
188         else {
189             if (isInTop == false) {
190                 isInTop = true;
191                 timerUp.restart();
192             }
193         }
194         
195         if (!bottomScrollArea.contains(x, y)) {
196             if (isInBottom == true) {
197                 isInBottom = false;
198                 timerDown.stop();
199             }
200         }
201         else {
202             if (isInBottom == false) {
203                 isInBottom = true;
204                 timerDown.restart();
205             }
206         }
207         
208         if (!leftScrollArea.contains(x, y)) {
209             if (isInLeft == true) {
210                 isInLeft = false;
211                 timerLeft.stop();
212             }
213         }
214         else {
215             if (isInLeft == false) {
216                 isInLeft = true;
217                 timerLeft.restart();
218             }
219         }
220         
221         if (!rightScrollArea.contains(x, y)) {
222             if (isInRight == true) {
223                 isInRight = false;
224                 timerRight.stop();
225             }
226         }
227         else {
228             if (isInRight == false) {
229                 isInRight = true;
230                 timerRight.restart();
231             }
232         }
233     }
234     
235     protected void mouseExited()
236     {
237         if (isInTop) {
238             isInTop = false;
239             timerUp.stop();
240         }
241         if (isInBottom) {
242             isInBottom = false;
243             timerDown.stop();
244         }
245         if (isInRight) {
246             isInRight = false;
247             timerRight.stop();
248         }
249         if (isInLeft) {
250             isInLeft = false;
251             timerLeft.stop();
252         }
253     }
254     
255     protected void stopTimers()
256     {
257         timerLeft.stop();
258         timerRight.stop();
259         timerUp.stop();
260         timerDown.stop();
261     }
262     
263     protected void doScrollDown(final int scrollStep) {
264         scrollDown(scrollStep);
265     }
266     
267     protected void doScrollUp(final int scrollStep) {
268         scrollUp(scrollStep);
269     }
270     
271     protected void doScrollLeft(final int scrollStep) {
272         scrollLeft(scrollStep);
273     }
274     
275     protected void doScrollRight(final int scrollStep){
276         scrollRight(scrollStep);
277     }
278             
279 }
280
Popular Tags