KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > tool > AbstractTool


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.tool;
28
29 import java.awt.Point JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.nightlabs.editor2d.viewer.IViewer;
33 import org.nightlabs.editor2d.viewer.event.MouseEvent;
34 import org.nightlabs.editor2d.viewer.event.MouseListener;
35 import org.nightlabs.editor2d.viewer.event.MouseMoveListener;
36
37 public abstract class AbstractTool
38 implements ITool, MouseListener, MouseMoveListener
39 {
40     public static final Logger LOGGER = Logger.getLogger(AbstractTool.class);
41     
42     protected int id;
43     public int getID() {
44         return id;
45     }
46     public void setID(int id) {
47         this.id = id;
48     }
49     
50     public void activate()
51     {
52         leftPressed = false;
53         leftReleased = false;
54         rightPressed = false;
55         rightReleased = false;
56         
57         startPoint = new Point JavaDoc();
58         currentPoint = new Point JavaDoc();
59         deltaPoint = new Point JavaDoc();
60                 
61         LOGGER.debug("AbstractTool: activate called!");
62     }
63
64     public void deactivate()
65     {
66         leftPressed = false;
67         leftReleased = false;
68         rightPressed = false;
69         rightReleased = false;
70
71         startPoint = null;
72         currentPoint = null;
73         deltaPoint = null;
74                 
75         LOGGER.debug("AbstractTool: deactivate called!");
76     }
77
78     protected IViewer viewer = null;
79     public void setViewer(IViewer viewer) {
80         this.viewer = viewer;
81     }
82
83     public IViewer getViewer() {
84         return viewer;
85     }
86     
87     protected void addToTempContent(Object JavaDoc o) {
88         getViewer().getBufferedCanvas().getTempContentManager().addToTempContent(o);
89     }
90
91     protected void removeTempContent(Object JavaDoc o) {
92         getViewer().getBufferedCanvas().getTempContentManager().removeFromTempContent(o);
93     }
94         
95     protected void repaint() {
96         getViewer().getBufferedCanvas().repaint();
97     }
98     
99     protected Point JavaDoc startPoint = null;
100     protected Point JavaDoc currentPoint = null;
101     protected Point JavaDoc deltaPoint = null;
102     
103 // protected Point relativeStartPoint = null;
104
// protected Point relativeCurrentPoint = null;
105
// protected Point relativeDeltaPoint = null;
106

107     protected boolean leftPressed = false;
108     protected boolean leftReleased = false;
109     protected boolean rightPressed = false;
110     protected boolean rightReleased = false;
111     
112     public void mouseMoved(MouseEvent me)
113     {
114         currentPoint.setLocation(me.getX(), me.getY());
115 // relativeCurrentPoint.setLocation(getRelativeX(me.getX()), getRelativeY(me.getY()));
116
if (leftPressed) {
117             deltaPoint.setLocation(me.getX() - startPoint.x, me.getY() - startPoint.y);
118 // relativeDeltaPoint.setLocation(getRelativeX(me.getX()) - relativeStartPoint.x,
119
// getRelativeY(me.getY() - relativeStartPoint.y));
120
}
121     }
122             
123     public void mousePressed(MouseEvent me)
124     {
125         if (me.getButton() == MouseEvent.BUTTON1 ){
126             leftPressed = true;
127             leftReleased = false;
128             LOGGER.debug("left mouse button pressed!");
129         }
130         if (me.getButton() == MouseEvent.BUTTON3) {
131             rightPressed = true;
132             rightReleased = false;
133             LOGGER.debug("right mouse button pressed!");
134         }
135         
136         startPoint.setLocation(me.getX(), me.getY());
137 // relativeStartPoint.setLocation(getRelativeX(me.getX()), getRelativeY(me.getY()));
138
}
139
140     public void mouseReleased(MouseEvent me)
141     {
142         if (me.getButton() == MouseEvent.BUTTON1) {
143             leftReleased = true;
144 // leftPressed = false;
145
LOGGER.debug("left mouse button released!");
146         }
147         if (me.getButton() == MouseEvent.BUTTON2) {
148             rightReleased = true;
149 // rightPressed = false;
150
LOGGER.debug("right mouse button released!");
151         }
152                 
153     }
154         
155     protected double getZoom() {
156         return getViewer().getZoom();
157     }
158     
159     protected int getAbsoluteScrollOffsetX() {
160         return getViewer().getViewport().getOffsetX();
161     }
162     
163     protected int getAbsoluteScrollOffsetY() {
164         return getViewer().getViewport().getOffsetY();
165     }
166     
167     protected int getRelativeScrollOffsetX() {
168         return (int) Math.rint(getAbsoluteScrollOffsetX() / getZoom());
169     }
170
171     protected int getRelativeScrollOffsetY() {
172         return (int) Math.rint(getAbsoluteScrollOffsetY() / getZoom());
173     }
174     
175     public int getRelativeX(int x) {
176         return ((int) Math.rint(x / getZoom())) + getRelativeScrollOffsetX();
177     }
178     
179     public int getRelativeY(int y) {
180         return ((int) Math.rint(y / getZoom())) + getRelativeScrollOffsetY();
181     }
182     
183 }
184
Popular Tags