KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > util > EditorUtil


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

27
28 package org.nightlabs.editor2d.util;
29
30 import java.awt.Font JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.eclipse.draw2d.FigureCanvas;
36 import org.eclipse.draw2d.Viewport;
37 import org.eclipse.draw2d.geometry.Point;
38 import org.eclipse.draw2d.geometry.Rectangle;
39 import org.eclipse.gef.EditPart;
40 import org.eclipse.gef.EditPartViewer;
41 import org.eclipse.gef.GraphicalEditPart;
42 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
43 import org.eclipse.gef.editparts.ZoomManager;
44 import org.eclipse.jface.viewers.StructuredSelection;
45 import org.nightlabs.math.MathUtil;
46
47 public class EditorUtil
48 {
49   public static final Logger LOGGER = Logger.getLogger(EditorUtil.class);
50   
51   public EditorUtil() {
52     super();
53   }
54   
55   public static double calcRotation(Point mousePoint, Point centerPoint) {
56     return MathUtil.calcRotation(mousePoint.x, mousePoint.y, centerPoint.x, centerPoint.y);
57   }
58   
59   public static Point toAbsolute(GraphicalEditPart part, Point point)
60   {
61     Point p = point.getCopy();
62     part.getFigure().translateToAbsolute(p);
63         p.translate(getScrollOffset(part));
64         return p;
65   }
66
67   public static Rectangle oldToAbsolute(GraphicalEditPart part, Rectangle rect)
68   {
69     Rectangle r = rect.getCopy();
70     part.getFigure().translateToAbsolute(r);
71         r.translate(getScrollOffset(part));
72         return r;
73   }
74    
75 // public static Point toRelative(GraphicalEditPart part, Point point)
76
// {
77
// Point p = point.getCopy();
78
// part.getFigure().translateToRelative(p);
79
// p.translate(getScrollOffset(part));
80
// return p;
81
// }
82

83 // public static Rectangle toRelative(GraphicalEditPart part, Rectangle rect)
84
// {
85
// Rectangle r = rect.getCopy();
86
// part.getFigure().translateToRelative(r);
87
// r.translate(getScrollOffset(part));
88
// return r;
89
// }
90

91   public static Point getScrollOffset(EditPart part)
92   {
93     EditPartViewer viewer = part.getRoot().getViewer();
94     FigureCanvas canvas = (FigureCanvas) viewer.getControl();
95     Viewport viewport = canvas.getViewport();
96     Point viewLocation = viewport.getViewLocation();
97     return viewLocation;
98   }
99         
100   public static Point getZoomedScrollOffset(EditPart part)
101   {
102     Point absoluteScrollOffset = getScrollOffset(part);
103     double zoom = getZoom(part);
104     double x = absoluteScrollOffset.x;
105     double y = absoluteScrollOffset.y;
106     if (absoluteScrollOffset.x != 0)
107         x = absoluteScrollOffset.x / zoom;
108     if (absoluteScrollOffset.y != 0)
109         y = absoluteScrollOffset.y / zoom;
110     return new Point(Math.rint(x), Math.rint(y));
111   }
112   
113   public static Rectangle toAbsolute(EditPart part, Rectangle rect)
114   {
115     Point xy = EditorUtil.toAbsolute(part, rect.x, rect.y);
116     Point wh = EditorUtil.toAbsolute(part, rect.width, rect.height);
117     Rectangle absoluteRect = new Rectangle(xy.x, xy.y, wh.x, wh.y);
118     return absoluteRect;
119   }
120     
121   public static Rectangle toAbsoluteWithScrollOffset(EditPart part, Rectangle rect)
122   {
123     Point xy = EditorUtil.toAbsolute(part, rect.x, rect.y);
124     Point wh = EditorUtil.toAbsolute(part, rect.width, rect.height);
125     Rectangle absoluteRect = new Rectangle(xy.x, xy.y, wh.x, wh.y);
126     absoluteRect.translate(getScrollOffset(part));
127     return absoluteRect;
128   }
129     
130   public static Point toAbsolute(EditPart part, double x, double y)
131   {
132     double zoom = getZoom(part);
133     double newX = x / zoom;
134     double newY = y / zoom;
135     Point p = new Point(newX, newY);
136     return p;
137   }
138        
139   public static Point toAbsoluteWithScrollOffset(EditPart part, double x, double y)
140   {
141     double zoom = getZoom(part);
142     double newX = x / zoom;
143     double newY = y / zoom;
144     Point p = new Point(newX, newY);
145 // p.translate(getScrollOffset(part));
146
p.translate(getZoomedScrollOffset(part));
147     return p;
148   }
149   
150   public static Point toRelative(EditPart part, double x, double y)
151   {
152     double zoom = getZoom(part);
153     double newX = x * zoom;
154     double newY = y * zoom;
155     Point p = new Point(newX, newY);
156     return p;
157   }
158     
159   public static int toRelative(EditPart part, double dimension) {
160     return (int)(dimension * getZoom(part));
161   }
162     
163   public static double getZoom(EditPart part)
164   {
165     if (part != null && part.getRoot() != null) {
166       if (part.getRoot() instanceof ScalableFreeformRootEditPart) {
167         ScalableFreeformRootEditPart root = (ScalableFreeformRootEditPart) part.getRoot();
168         return root.getZoomManager().getZoom();
169       }
170     }
171     return 1.0;
172   }
173   
174   public static ZoomManager getZoomManager(EditPart part)
175   {
176     if (part.getRoot() instanceof ScalableFreeformRootEditPart) {
177       return ((ScalableFreeformRootEditPart) part.getRoot()).getZoomManager();
178     }
179     return null;
180   }
181   
182   public static Point getCenter(List JavaDoc editParts)
183   {
184     Rectangle totalBounds = new Rectangle();
185     int counter = 0;
186     for (Iterator JavaDoc it = editParts.iterator(); it.hasNext(); ) {
187       GraphicalEditPart editPart = (GraphicalEditPart) it.next();
188       Rectangle figureBounds = editPart.getFigure().getBounds();
189       if (counter == 0)
190         totalBounds = figureBounds.getCopy();
191       totalBounds = totalBounds.getUnion(figureBounds);
192       counter++;
193     }
194     return totalBounds.getCenter();
195   }
196   
197   public static void selectEditParts(List JavaDoc editParts)
198   {
199     if (editParts == null)
200       throw new IllegalArgumentException JavaDoc("Param editParts must not be null!");
201     
202     EditPartViewer viewer= null;
203     if (!editParts.isEmpty()) {
204       Object JavaDoc o = editParts.get(0);
205       if (o instanceof EditPart) {
206         EditPart editPart = (EditPart) o;
207         viewer = editPart.getViewer();
208       }
209     }
210     if (viewer != null) {
211       StructuredSelection selection = new StructuredSelection(editParts);
212       viewer.deselectAll();
213       viewer.setSelection(selection);
214     }
215   }
216   
217   public static void selectEditPart(EditPart editPart)
218   {
219     if (editPart == null)
220       throw new IllegalArgumentException JavaDoc("Param editPart must not be null!");
221     
222     EditPartViewer viewer = editPart.getViewer();
223     if (viewer != null) {
224       StructuredSelection selection = new StructuredSelection(editPart);
225       viewer.deselectAll();
226       viewer.setSelection(selection);
227     }
228   }
229     
230   public static final Font JavaDoc DEFAULT_FONT = new Font JavaDoc("Arial", Font.PLAIN, 24);
231     
232   public static void zoomToRelativeRect(Rectangle rect, ZoomManager zoomManager)
233   {
234     if (rect == null)
235             throw new IllegalArgumentException JavaDoc("Param rect must not be null!");
236     
237     if (zoomManager == null)
238             throw new IllegalArgumentException JavaDoc("Param zoomManager must not be null!");
239     
240     Rectangle relativeZoomRectangle = rect.getCopy();
241     Rectangle clientArea = zoomManager.getViewport().getClientArea();
242     double zoom = zoomManager.getZoom();
243     
244     double absoluteWidth = (double) relativeZoomRectangle.width;
245     double absoluteHeight = (double) relativeZoomRectangle.height;
246     double absoluteX = (double) relativeZoomRectangle.x;
247     double absoluteY = (double) relativeZoomRectangle.y;
248         
249     double absoluteClientWidth = (double) clientArea.width / zoom;
250     double absoluteClientHeight = (double) clientArea.height / zoom;
251     double absoluteClientX = (double) clientArea.x / zoom;
252     double absoluteClientY = (double) clientArea.y / zoom;
253         
254       double zoomX = absoluteClientWidth / absoluteWidth;
255       double zoomY = absoluteClientHeight / absoluteHeight;
256             
257     double newZoom = Math.min(zoomX, zoomY);
258     double realZoom = (double) newZoom * zoom;
259     double newX = (double) (absoluteX + absoluteClientX) * realZoom;
260     double newY = (double) (absoluteY + absoluteClientY) * realZoom;
261     
262     zoomManager.setZoom(realZoom);
263     // check if the zoom is beyond the max or min zoom
264
double z = zoomManager.getZoom();
265     if (z != realZoom) {
266         newX = (double) (absoluteX + absoluteClientX) * z;
267         newY = (double) (absoluteY + absoluteClientY) * z;
268     }
269     zoomManager.getViewport().setViewLocation((int)newX, (int)newY);
270     zoomManager.getViewport().getUpdateManager().performUpdate();
271   }
272   
273   public static void zoomToAbsoluteRect(Rectangle rect, ZoomManager zoomManager)
274   {
275     Rectangle relativeZoomRectangle = rect.getCopy();
276     Rectangle clientArea = zoomManager.getViewport().getClientArea();
277     
278     double zoom = zoomManager.getZoom();
279     double absoluteWidth = (double) relativeZoomRectangle.width / zoom;
280     double absoluteHeight = (double) relativeZoomRectangle.height / zoom;
281     double absoluteX = (double) relativeZoomRectangle.x / zoom;
282     double absoluteY = (double) relativeZoomRectangle.y / zoom;
283         
284     double absoluteClientWidth = (double) clientArea.width / zoom;
285     double absoluteClientHeight = (double) clientArea.height / zoom;
286     double absoluteClientX = (double) clientArea.x / zoom;
287     double absoluteClientY = (double) clientArea.y / zoom;
288         
289       double zoomX = absoluteClientWidth / absoluteWidth;
290       double zoomY = absoluteClientHeight / absoluteHeight;
291             
292     double newZoom = Math.min(zoomX, zoomY);
293     double realZoom = (double) newZoom * zoom;
294     double newX = (double) (absoluteX + absoluteClientX) * realZoom;
295     double newY = (double) (absoluteY + absoluteClientY) * realZoom;
296     
297     zoomManager.setZoom(realZoom);
298     // check if the zoom is beyond the max or min zoom
299
double z = zoomManager.getZoom();
300     if (z != realZoom) {
301         newX = (double) (absoluteX + absoluteClientX) * z;
302         newY = (double) (absoluteY + absoluteClientY) * z;
303     }
304     zoomManager.getViewport().setViewLocation((int)newX, (int)newY);
305     zoomManager.getViewport().getUpdateManager().performUpdate();
306   }
307   
308 // public static Comparator idComparator = new Comparator()
309
// {
310
// public int compare(Object arg0, Object arg1)
311
// {
312
// if (arg0 instanceof DrawComponent && arg1 instanceof DrawComponent)
313
// {
314
// DrawComponent dc1 = (DrawComponent) arg0;
315
// DrawComponent dc2 = (DrawComponent) arg1;
316
// if (dc1.getId() < dc2.getId())
317
// return -1;
318
// if (dc1.getId() > dc2.getId())
319
// return 1;
320
// }
321
// return 0;
322
// }
323
// };
324

325 }
326
Popular Tags