KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.apache.log4j.Logger;
35
36 public class ZoomSupport
37 implements IZoomSupport
38 {
39     public static final Logger LOGGER = Logger.getLogger(ZoomSupport.class);
40     
41     public ZoomSupport(IViewport viewport) {
42         super();
43         this.viewport = viewport;
44     }
45
46     protected IViewport viewport = null;
47     public IViewport getViewport() {
48         return viewport;
49     }
50     public void setViewport(IViewport viewport) {
51         this.viewport = viewport;
52     }
53     
54     protected Collection JavaDoc zoomListener;
55     public Collection JavaDoc getZoomListener()
56     {
57         if (zoomListener == null)
58             zoomListener = new ArrayList JavaDoc();
59         
60         return zoomListener;
61     }
62
63     /**
64      *
65      * @see org.nightlabs.editor2d.viewer.IZoomSupport#addZoomListener(org.nightlabs.editor2d.viewer.IZoomListener)
66      */

67     public void addZoomListener(IZoomListener zoomListener) {
68         getZoomListener().add(zoomListener);
69     }
70
71     /**
72      *
73      * @see org.nightlabs.editor2d.viewer.IZoomSupport#removeZoomListener(org.nightlabs.editor2d.viewer.IZoomListener)
74      */

75     public void removeZoomListener(IZoomListener zoomListener) {
76         getZoomListener().remove(zoomListener);
77     }
78
79     protected void fireZoomChanged()
80     {
81         for (Iterator JavaDoc it = getZoomListener().iterator(); it.hasNext(); ) {
82             IZoomListener listener = (IZoomListener) it.next();
83             listener.zoomChanged(zoom);
84         }
85     }
86     
87     protected double zoom = 1.0;
88     
89     /**
90      *
91      * @see org.nightlabs.editor2d.viewer.IZoomSupport#getZoom()
92      */

93     public double getZoom() {
94         return zoom;
95     }
96
97     /**
98      *
99      * @see org.nightlabs.editor2d.viewer.IZoomSupport#setZoom(double)
100      */

101     public void setZoom(double zoomFactor)
102     {
103         double oldZoom = zoom;
104         zoom = zoomFactor;
105         
106         if (zoomFactor > getMaxZoom())
107             zoom = getMaxZoom();
108         
109         if (zoomFactor < getMinZoom())
110             zoom = getMinZoom();
111         
112         if (oldZoom != zoom)
113             fireZoomChanged();
114     }
115
116     protected double maxZoom = 10.0d;
117     
118     /**
119      *
120      * @see org.nightlabs.editor2d.viewer.IZoomSupport#getMaxZoom()
121      */

122     public double getMaxZoom() {
123         return maxZoom;
124     }
125
126     protected double minZoom = 0.01d;
127     
128     /**
129      *
130      * @see org.nightlabs.editor2d.viewer.IZoomSupport#getMinZoom()
131      */

132     public double getMinZoom() {
133         return 0;
134     }
135
136     protected double zoomStep = 0.25d;
137     
138     /**
139      *
140      * @see org.nightlabs.editor2d.viewer.IZoomSupport#getZoomStep()
141      */

142     public double getZoomStep() {
143         return zoomStep;
144     }
145
146     /**
147      *
148      * @see org.nightlabs.editor2d.viewer.IZoomSupport#setMaxZoom(double)
149      */

150     public void setMaxZoom(double maxZoom) {
151         this.maxZoom = maxZoom;
152     }
153
154     /**
155      *
156      * @see org.nightlabs.editor2d.viewer.IZoomSupport#setMinZoom(double)
157      */

158     public void setMinZoom(double minZoom) {
159         this.minZoom = minZoom;
160     }
161
162     /**
163      *
164      * @see org.nightlabs.editor2d.viewer.IZoomSupport#setZoomStep(double)
165      */

166     public void setZoomStep(double zoomStep) {
167         this.zoomStep = zoomStep;
168     }
169
170     /**
171      *
172      * @see org.nightlabs.editor2d.viewer.IZoomSupport#canZoomIn()
173      */

174     public boolean canZoomIn()
175     {
176         return (zoom + zoomStep < maxZoom) ? true : false;
177     }
178
179     /**
180      *
181      * @see org.nightlabs.editor2d.viewer.IZoomSupport#canZoomOut()
182      */

183     public boolean canZoomOut()
184     {
185         return (zoom - zoomStep > minZoom) ? true : false;
186     }
187
188     /**
189      *
190      * @see org.nightlabs.editor2d.viewer.IZoomSupport#zoomIn()
191      */

192     public void zoomIn()
193     {
194         if (canZoomIn()) {
195             setZoom(zoom + zoomStep);
196         }
197     }
198
199     /**
200      *
201      * @see org.nightlabs.editor2d.viewer.IZoomSupport#zoomOut()
202      */

203     public void zoomOut()
204     {
205         if (canZoomOut()) {
206             setZoom(zoom - zoomStep);
207         }
208     }
209
210     /**
211      *
212      * @see org.nightlabs.editor2d.viewer.IZoomSupport#getZoomAsString()
213      */

214     public String JavaDoc getZoomAsString()
215     {
216         double z = Math.rint(zoom * 100);
217         return ""+z+" %";
218     }
219                         
220     /**
221      * zooms the IViewport of the ZoomSupport to the given Rectangle,
222      * which must have absolute coordinates
223      * This means the scale is calculated and set and the ViewLocation of the
224      * IViewport is adjusted
225      *
226      * @param r the rectangle to zoomTo
227      */

228     public void zoomTo(Rectangle JavaDoc r)
229     {
230         Rectangle JavaDoc absoluteRect = new Rectangle JavaDoc(r);
231         Rectangle JavaDoc absoluteView = getViewport().getViewBounds();
232                 
233         double oldZoom = getZoom();
234         double zoomX = oldZoom;
235         double zoomY = oldZoom;
236
237         if (absoluteView.width != 0 && absoluteRect.width != 0)
238             zoomX = (double)absoluteView.width / (double)absoluteRect.width;
239         if (absoluteView.height != 0 && absoluteRect.height != 0)
240             zoomY = (double)absoluteView.height / (double)absoluteRect.height;
241                         
242       double newZoom = Math.min(zoomX, zoomY);
243       setZoom(newZoom);
244       // maybe realZoom is beyond minZoom or maxZoom
245
double zoom = getZoom();
246
247       double newX = (double) ((absoluteRect.x) * zoom);
248       double newY = (double) ((absoluteRect.y) * zoom);
249           
250 // // center zoomRectangle
251
// double aspectRatio = 1.333333333333333333333;
252
// if (absoluteRect.width > absoluteRect.height) {
253
// aspectRatio = (double)absoluteView.height / (double)absoluteView.width;
254
// double newViewHeight = (double)absoluteRect.width * aspectRatio;
255
// double diffHeight = (newViewHeight - absoluteRect.height) / 2;
256
// double absoluteDiffHeight = diffHeight * zoom;
257
// LOGGER.debug("oldNewY = "+newY);
258
// newY =- absoluteDiffHeight;
259
// LOGGER.debug("newNewY = "+newY);
260
// LOGGER.debug("aspectRatio = "+aspectRatio);
261
// LOGGER.debug("diffHeight = "+diffHeight);
262
// LOGGER.debug("absoluteDiffHeight = "+absoluteDiffHeight);
263
// }
264
// else {
265
// aspectRatio = (double)absoluteView.width / (double)absoluteView.height;
266
// double newViewWidth = (double)absoluteRect.width * aspectRatio;
267
// double diffWidth = (newViewWidth - absoluteRect.width) / 2;
268
// double absoluteDiffWidth = diffWidth * zoom;
269
// LOGGER.debug("oldNewX = "+newX);
270
// newX =- absoluteDiffWidth;
271
// LOGGER.debug("newNewX = "+newX);
272
// LOGGER.debug("aspectRatio = "+aspectRatio);
273
// LOGGER.debug("diffWidth = "+diffWidth);
274
// LOGGER.debug("absoluteDiffWidth = "+absoluteDiffWidth);
275
// }
276

277     getViewport().setViewLocation((int)newX, (int)newY);
278                     
279         LOGGER.debug("absoluteRect = "+absoluteRect);
280         LOGGER.debug("absoluteView = "+absoluteView);
281         LOGGER.debug("newZoom = "+newZoom);
282         LOGGER.debug("newX = "+newX);
283         LOGGER.debug("newY = "+newY);
284         LOGGER.debug("");
285     }
286 }
287
Popular Tags