KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > swt > AbstractSWTAutoScrollSupport


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.swt;
28
29 import org.apache.log4j.Logger;
30 import org.eclipse.swt.events.ControlEvent;
31 import org.eclipse.swt.events.ControlListener;
32 import org.eclipse.swt.events.DisposeEvent;
33 import org.eclipse.swt.events.DisposeListener;
34 import org.eclipse.swt.events.MouseEvent;
35 import org.eclipse.swt.events.MouseMoveListener;
36 import org.eclipse.swt.events.MouseTrackAdapter;
37 import org.eclipse.swt.events.MouseTrackListener;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40
41 import org.nightlabs.base.util.GeomUtil;
42 import org.nightlabs.editor2d.viewer.AbstractAutoScrollSupport;
43
44 public abstract class AbstractSWTAutoScrollSupport
45 extends AbstractAutoScrollSupport
46 {
47     public static final Logger LOGGER = Logger.getLogger(AbstractSWTAutoScrollSupport.class.getName());
48     
49     protected Control control;
50     public Control getControl() {
51         return control;
52     }
53     
54     public AbstractSWTAutoScrollSupport(Control control)
55     {
56         super();
57         this.control = control;
58         init();
59     }
60     
61     protected void init()
62     {
63         control.addControlListener(controlSizeListener);
64         control.addMouseMoveListener(autoScrollListener);
65         control.addMouseTrackListener(mouseTrackListener);
66         control.addDisposeListener(disposeListener);
67         initAutoScroll(GeomUtil.toAWTRectangle(control.getBounds()));
68     }
69     
70     protected DisposeListener disposeListener = new DisposeListener()
71     {
72         public void widgetDisposed(DisposeEvent e)
73         {
74             control.removeControlListener(controlSizeListener);
75             control.removeMouseMoveListener(autoScrollListener);
76             control.removeMouseTrackListener(mouseTrackListener);
77             stopTimers();
78         }
79     };
80     
81     protected ControlListener controlSizeListener = new ControlListener()
82     {
83         public void controlResized(ControlEvent evt)
84         {
85             LOGGER.debug("Control Resized!");
86             Control c = (Control) evt.getSource();
87             initAutoScroll(GeomUtil.toAWTRectangle(c.getBounds()));
88         }
89         public void controlMoved(ControlEvent arg0) {
90             
91         }
92     };
93         
94     protected MouseMoveListener autoScrollListener = new MouseMoveListener()
95     {
96         public void mouseMove(MouseEvent e)
97         {
98             mouseMoved(e.x, e.y);
99         }
100     };
101         
102     protected MouseTrackListener mouseTrackListener = new MouseTrackAdapter()
103     {
104         public void mouseExit(MouseEvent e)
105         {
106             mouseExited();
107         }
108         
109         public void mouseEnter(MouseEvent e) {
110             super.mouseEnter(e);
111         }
112     };
113     
114     protected void doScrollDown(final int scrollStep)
115     {
116         Display.getDefault().asyncExec(new Runnable JavaDoc()
117         {
118             public void run() {
119                 scrollDown(scrollStep);
120             }
121         });
122     }
123     
124     protected void doScrollUp(final int scrollStep)
125     {
126         Display.getDefault().asyncExec(new Runnable JavaDoc()
127         {
128             public void run() {
129                 scrollUp(scrollStep);
130             }
131         });
132     }
133
134     protected void doScrollLeft(final int scrollStep)
135     {
136         Display.getDefault().asyncExec(new Runnable JavaDoc()
137         {
138             public void run() {
139                 scrollLeft(scrollStep);
140             }
141         });
142     }
143
144     protected void doScrollRight(final int scrollStep)
145     {
146         Display.getDefault().asyncExec(new Runnable JavaDoc()
147         {
148             public void run() {
149                 scrollRight(scrollStep);
150             }
151         });
152     }
153             
154     protected abstract void scrollDown(int scrollStep);
155     protected abstract void scrollUp(int scrollStep);
156     protected abstract void scrollLeft(int scrollStep);
157     protected abstract void scrollRight(int scrollStep);
158 }
159
Popular Tags