1 27 28 package org.nightlabs.editor2d.util; 29 30 import java.awt.Font ; 31 import java.util.Iterator ; 32 import java.util.List ; 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 83 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(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 editParts) 183 { 184 Rectangle totalBounds = new Rectangle(); 185 int counter = 0; 186 for (Iterator 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 editParts) 198 { 199 if (editParts == null) 200 throw new IllegalArgumentException ("Param editParts must not be null!"); 201 202 EditPartViewer viewer= null; 203 if (!editParts.isEmpty()) { 204 Object 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 ("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 DEFAULT_FONT = new Font ("Arial", Font.PLAIN, 24); 231 232 public static void zoomToRelativeRect(Rectangle rect, ZoomManager zoomManager) 233 { 234 if (rect == null) 235 throw new IllegalArgumentException ("Param rect must not be null!"); 236 237 if (zoomManager == null) 238 throw new IllegalArgumentException ("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 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 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 325 } 326 | Popular Tags |