1 27 28 package org.nightlabs.editor2d.tools; 29 30 import org.eclipse.draw2d.ColorConstants; 31 import org.eclipse.draw2d.Figure; 32 import org.eclipse.draw2d.Graphics; 33 import org.eclipse.draw2d.IFigure; 34 import org.eclipse.draw2d.geometry.Rectangle; 35 import org.eclipse.gef.EditPartViewer; 36 import org.eclipse.gef.GraphicalViewer; 37 import org.eclipse.gef.SharedCursors; 38 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart; 39 import org.eclipse.gef.editparts.ScalableRootEditPart; 40 import org.eclipse.gef.editparts.ZoomManager; 41 import org.eclipse.gef.tools.AbstractTool; 42 import org.eclipse.swt.widgets.Display; 43 44 import org.nightlabs.editor2d.request.EditorRequestConstants; 45 import org.nightlabs.editor2d.util.EditorUtil; 46 47 48 public class ZoomTool 49 extends AbstractTool 50 implements EditorRequestConstants 51 { 52 protected IFigure zoomRectangleFigure; 53 54 protected ZoomManager zoomManager; 55 56 public ZoomTool() 57 { 58 setDefaultCursor(SharedCursors.CROSS); 59 } 60 61 protected String getCommandName() 62 { 63 return REQ_ZOOM_RECT; 64 } 65 66 private void eraseZoomFeedback() 67 { 68 if (zoomRectangleFigure != null) { 69 removeFeedback(zoomRectangleFigure); 70 zoomRectangleFigure = null; 71 } 72 } 73 74 77 public void deactivate() 78 { 79 if (isInState(STATE_DRAG_IN_PROGRESS)) { 80 eraseZoomFeedback(); 81 } 82 super.deactivate(); 83 setState(STATE_TERMINAL); 84 } 85 86 89 protected String getDebugName() 90 { 91 return "Zoom Tool"; } 93 94 protected IFigure getZoomFeedbackFigure() 95 { 96 if (zoomRectangleFigure == null) { 97 zoomRectangleFigure = new ZoomRectangleFigure(); 98 addFeedback(zoomRectangleFigure); 99 } 100 return zoomRectangleFigure; 101 } 102 103 protected Rectangle getZoomSelectionRectangle() 104 { 105 return new Rectangle(getStartLocation(), getLocation()); 106 } 107 108 protected ZoomManager getZoomManager() 109 { 110 if (getCurrentViewer().getContents().getRoot() instanceof ScalableRootEditPart) 111 { 112 if (zoomManager == null) { 113 zoomManager = ((ScalableRootEditPart) getCurrentViewer().getContents().getRoot()).getZoomManager(); 114 } 115 return zoomManager; 116 } 117 else if (getCurrentViewer().getContents().getRoot() instanceof ScalableFreeformRootEditPart) 118 { 119 if (zoomManager == null) { 120 zoomManager = ((ScalableFreeformRootEditPart) getCurrentViewer().getContents().getRoot()).getZoomManager(); 121 } 122 return zoomManager; 123 } 124 return null; 125 } 126 127 protected boolean isGraphicalViewer() 128 { 129 return getCurrentViewer() instanceof GraphicalViewer; 130 } 131 132 135 protected boolean handleButtonDown(int button) 136 { 137 if (!isGraphicalViewer()) 138 return true; 139 if (button != 1) { 140 setState(STATE_INVALID); 141 handleInvalidInput(); 142 } 143 if (stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS)) 144 { 145 } 151 return true; 152 } 153 154 157 protected boolean handleButtonUp(int button) 158 { 159 if (stateTransition(STATE_DRAG_IN_PROGRESS, STATE_TERMINAL)) { 160 eraseZoomFeedback(); 161 performZoom(); 162 } 163 handleFinished(); 164 return true; 165 } 166 167 170 protected boolean handleDragInProgress() 171 { 172 if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) { 173 showZoomFeedback(); 174 } 175 return true; 176 } 177 178 181 protected boolean handleFocusLost() 182 { 183 if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) { 184 handleFinished(); 185 return true; 186 } 187 return false; 188 } 189 190 194 protected boolean handleInvalidInput() 195 { 196 eraseZoomFeedback(); 197 return true; 198 } 199 200 203 public void setViewer(EditPartViewer viewer) 204 { 205 if (viewer == getCurrentViewer()) 206 return; 207 super.setViewer(viewer); 208 if (viewer instanceof GraphicalViewer) 209 setDefaultCursor(SharedCursors.CROSS); 210 else 211 setDefaultCursor(SharedCursors.NO); 212 } 213 214 protected void performZoom() 215 { 216 EditorUtil.zoomToAbsoluteRect(getZoomSelectionRectangle().getCopy(), getZoomManager()); 218 } 219 220 protected void showZoomFeedback() 221 { 222 Rectangle rect = getZoomSelectionRectangle().getCopy(); 223 getZoomFeedbackFigure().translateToRelative(rect); 224 getZoomFeedbackFigure().setBounds(rect); 225 } 226 227 class ZoomRectangleFigure 228 extends Figure 229 { 230 private int offset = 0; 231 private boolean schedulePaint = true; 232 private static final int DELAY = 110; 234 237 protected void paintFigure(Graphics graphics) 238 { 239 Rectangle bounds = getBounds().getCopy(); 240 graphics.translate(getLocation()); 241 242 graphics.setXORMode(true); 243 graphics.setForegroundColor(ColorConstants.white); 244 graphics.setBackgroundColor(ColorConstants.black); 245 246 graphics.setLineStyle(Graphics.LINE_DOT); 247 248 int[] points = new int[6]; 249 250 points[0] = 0 + offset; 251 points[1] = 0; 252 points[2] = bounds.width - 1; 253 points[3] = 0; 254 points[4] = bounds.width - 1; 255 points[5] = bounds.height - 1; 256 257 graphics.drawPolyline(points); 258 259 points[0] = 0; 260 points[1] = 0 + offset; 261 points[2] = 0; 262 points[3] = bounds.height - 1; 263 points[4] = bounds.width - 1; 264 points[5] = bounds.height - 1; 265 266 graphics.drawPolyline(points); 267 268 graphics.translate(getLocation().getNegated()); 269 270 if (schedulePaint) { 271 Display.getCurrent().timerExec(DELAY, new Runnable () { 272 public void run() { 273 offset++; 274 if (offset > 5) 275 offset = 0; 276 277 schedulePaint = true; 278 repaint(); 279 } 280 }); 281 } 282 283 schedulePaint = false; 284 } 285 286 } 288 } 289 | Popular Tags |