KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.gui </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 14.03.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.util;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Font JavaDoc;
12 import java.awt.GraphicsEnvironment JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.draw2d.FigureCanvas;
19 import org.eclipse.draw2d.Viewport;
20 import org.eclipse.draw2d.geometry.Point;
21 import org.eclipse.draw2d.geometry.Rectangle;
22 import org.eclipse.gef.EditPart;
23 import org.eclipse.gef.EditPartViewer;
24 import org.eclipse.gef.GraphicalEditPart;
25 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
26 import org.eclipse.gef.editparts.ZoomManager;
27 import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.swt.graphics.GC;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.widgets.Display;
32
33 import com.nightlabs.math.MathUtil;
34
35 public class EditorUtil
36 {
37   public static final Logger LOGGER = Logger.getLogger(EditorUtil.class);
38   
39   public EditorUtil() {
40     super();
41   }
42   
43   public static double calcRotation(Point mousePoint, Point centerPoint) {
44     return MathUtil.calcRotation(mousePoint.x, mousePoint.y, centerPoint.x, centerPoint.y);
45   }
46   
47   public static Point toAbsolute(GraphicalEditPart part, Point point)
48   {
49     Point p = point.getCopy();
50     part.getFigure().translateToAbsolute(p);
51         p.translate(getScrollOffset(part));
52         return p;
53   }
54
55   public static Rectangle oldToAbsolute(GraphicalEditPart part, Rectangle rect)
56   {
57     Rectangle r = rect.getCopy();
58     part.getFigure().translateToAbsolute(r);
59         r.translate(getScrollOffset(part));
60         return r;
61   }
62    
63   public static Point toRelative(GraphicalEditPart part, Point point)
64   {
65     Point p = point.getCopy();
66     part.getFigure().translateToRelative(p);
67     p.translate(getScrollOffset(part));
68     return p;
69   }
70
71   public static Rectangle toRelative(GraphicalEditPart part, Rectangle rect)
72   {
73     Rectangle r = rect.getCopy();
74     part.getFigure().translateToRelative(r);
75     r.translate(getScrollOffset(part));
76     return r;
77   }
78   
79   public static Point getScrollOffset(EditPart part)
80   {
81     EditPartViewer viewer = part.getRoot().getViewer();
82     if (viewer instanceof ScrollingGraphicalViewer) {
83       ScrollingGraphicalViewer scrollViewer = (ScrollingGraphicalViewer) viewer;
84       FigureCanvas canvas = (FigureCanvas) viewer.getControl();
85       Viewport viewport = canvas.getViewport();
86       Point viewLocation = viewport.getViewLocation();
87 // LOGGER.debug("scrollOffset = "+viewLocation);
88
return viewLocation;
89     }
90     return new Point();
91   }
92   
93   public static Rectangle toAbsolute(EditPart part, Rectangle rect)
94   {
95     Point xy = EditorUtil.toAbsolute(part, rect.x, rect.y);
96     Point wh = EditorUtil.toAbsolute(part, rect.width, rect.height);
97     Rectangle absoluteRect = new Rectangle(xy.x, xy.y, wh.x, wh.y);
98     Point scrollOffset = getScrollOffset(part);
99     Point absoluteScrollOffset = toAbsolute(part, scrollOffset.x, scrollOffset.y);
100     absoluteRect.translate(absoluteScrollOffset);
101     return absoluteRect;
102   }
103   
104   public static double getZoom(EditPart part)
105   {
106     if (part.getRoot() instanceof ScalableFreeformRootEditPart) {
107       ScalableFreeformRootEditPart root = (ScalableFreeformRootEditPart) part.getRoot();
108       return root.getZoomManager().getZoom();
109     }
110     return 1.0;
111   }
112   
113   public static ZoomManager getZoomManager(EditPart part)
114   {
115     if (part.getRoot() instanceof ScalableFreeformRootEditPart) {
116       return ((ScalableFreeformRootEditPart) part.getRoot()).getZoomManager();
117     }
118     return null;
119   }
120   
121   // TODO: calcAbsolute ScrollOffset and add it
122
public static Point toAbsolute(EditPart part, double x, double y)
123   {
124     double zoom = getZoom(part);
125     double newX = x / zoom;
126     double newY = y / zoom;
127     Point p = new Point(newX, newY);
128 // p.translate(getScrollOffset(part));
129
return p;
130   }
131   
132   // TODO: calcRelative ScrollOffset and add it
133
public static Point toRelative(EditPart part, double x, double y)
134   {
135     double zoom = getZoom(part);
136     double newX = x * zoom;
137     double newY = y * zoom;
138     Point p = new Point(newX, newY);
139 // p.translate(getScrollOffset(part));
140
return p;
141   }
142   
143   public static int toRelative(EditPart part, double dimension) {
144     return (int)(dimension * getZoom(part));
145   }
146     
147   public static Point getCenter(List JavaDoc editParts)
148   {
149     Rectangle totalBounds = new Rectangle();
150     int counter = 0;
151     for (Iterator JavaDoc it = editParts.iterator(); it.hasNext(); ) {
152       GraphicalEditPart editPart = (GraphicalEditPart) it.next();
153       Rectangle figureBounds = editPart.getFigure().getBounds();
154       if (counter == 0)
155         totalBounds = figureBounds.getCopy();
156       totalBounds = totalBounds.getUnion(figureBounds);
157       counter++;
158     }
159     return totalBounds.getCenter();
160   }
161   
162   public static void selectEditParts(List JavaDoc editParts)
163   {
164     if (editParts == null)
165       throw new IllegalArgumentException JavaDoc("Param editParts must not be null!");
166     
167     EditPartViewer viewer= null;
168     if (!editParts.isEmpty()) {
169       Object JavaDoc o = editParts.get(0);
170       if (o instanceof EditPart) {
171         EditPart editPart = (EditPart) o;
172         viewer = editPart.getViewer();
173       }
174     }
175     if (viewer != null) {
176       StructuredSelection selection = new StructuredSelection(editParts);
177       viewer.deselectAll();
178       viewer.setSelection(selection);
179     }
180   }
181   
182   public static void selectEditPart(EditPart editPart)
183   {
184     if (editPart == null)
185       throw new IllegalArgumentException JavaDoc("Param editPart must not be null!");
186     
187     EditPartViewer viewer = editPart.getViewer();
188     if (viewer != null) {
189       StructuredSelection selection = new StructuredSelection(editPart);
190       viewer.deselectAll();
191       viewer.setSelection(selection);
192     }
193   }
194     
195   public static final Font JavaDoc DEFAULT_FONT = new Font JavaDoc("Arial", Font.PLAIN, 24);
196     
197   public static void zoomToRelativeRect(Rectangle rect, ZoomManager zoomManager)
198   {
199     if (rect == null)
200             throw new IllegalArgumentException JavaDoc("Param rect must not be null!");
201     
202     if (zoomManager == null)
203             throw new IllegalArgumentException JavaDoc("Param zoomManager must not be null!");
204     
205     Rectangle relativeZoomRectangle = rect.getCopy();
206     Rectangle clientArea = zoomManager.getViewport().getClientArea();
207     double zoom = zoomManager.getZoom();
208     
209     double absoluteWidth = (double) relativeZoomRectangle.width;
210     double absoluteHeight = (double) relativeZoomRectangle.height;
211     double absoluteX = (double) relativeZoomRectangle.x;
212     double absoluteY = (double) relativeZoomRectangle.y;
213         
214     double absoluteClientWidth = (double) clientArea.width / zoom;
215     double absoluteClientHeight = (double) clientArea.height / zoom;
216     double absoluteClientX = (double) clientArea.x / zoom;
217     double absoluteClientY = (double) clientArea.y / zoom;
218         
219       double zoomX = absoluteClientWidth / absoluteWidth;
220       double zoomY = absoluteClientHeight / absoluteHeight;
221             
222     double newZoom = Math.min(zoomX, zoomY);
223     double realZoom = (double) newZoom * zoom;
224     double newX = (double) (absoluteX + absoluteClientX) * realZoom;
225     double newY = (double) (absoluteY + absoluteClientY) * realZoom;
226     
227     zoomManager.setZoom(realZoom);
228     // check if the zoom is beyond the max or min zoom
229
double z = zoomManager.getZoom();
230     if (z != realZoom) {
231         newX = (double) (absoluteX + absoluteClientX) * z;
232         newY = (double) (absoluteY + absoluteClientY) * z;
233     }
234     zoomManager.getViewport().setViewLocation((int)newX, (int)newY);
235     zoomManager.getViewport().getUpdateManager().performUpdate();
236   }
237   
238   public static void zoomToAbsoluteRect(Rectangle rect, ZoomManager zoomManager)
239   {
240     Rectangle relativeZoomRectangle = rect.getCopy();
241     Rectangle clientArea = zoomManager.getViewport().getClientArea();
242     
243     double zoom = zoomManager.getZoom();
244     double absoluteWidth = (double) relativeZoomRectangle.width / zoom;
245     double absoluteHeight = (double) relativeZoomRectangle.height / zoom;
246     double absoluteX = (double) relativeZoomRectangle.x / zoom;
247     double absoluteY = (double) relativeZoomRectangle.y / zoom;
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 }
274
Popular Tags