1 8 package com.nightlabs.editor2d.editpolicy; 9 10 import java.awt.geom.AffineTransform ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import org.apache.log4j.Logger; 16 import org.eclipse.draw2d.ColorConstants; 17 import org.eclipse.draw2d.IFigure; 18 import org.eclipse.draw2d.Polyline; 19 import org.eclipse.draw2d.PositionConstants; 20 import org.eclipse.draw2d.RectangleFigure; 21 import org.eclipse.draw2d.geometry.Point; 22 import org.eclipse.draw2d.geometry.PrecisionRectangle; 23 import org.eclipse.draw2d.geometry.Rectangle; 24 import org.eclipse.gef.GraphicalEditPart; 25 import org.eclipse.gef.LayerConstants; 26 import org.eclipse.gef.Request; 27 import org.eclipse.gef.editpolicies.ResizableEditPolicy; 28 import org.eclipse.gef.requests.ChangeBoundsRequest; 29 30 import com.nightlabs.editor2d.DrawComponent; 31 import com.nightlabs.editor2d.DrawComponentContainer; 32 import com.nightlabs.editor2d.EditorStateManager; 33 import com.nightlabs.editor2d.ImageDrawComponent; 34 import com.nightlabs.editor2d.ShapeDrawComponent; 35 import com.nightlabs.editor2d.edit.ShapeDrawComponentEditPart; 36 import com.nightlabs.editor2d.figures.FeedbackShapeFigure; 37 import com.nightlabs.editor2d.figures.ShapeFigure; 38 import com.nightlabs.editor2d.handle.RotateCenterHandle; 39 import com.nightlabs.editor2d.handle.RotateHandleKit; 40 import com.nightlabs.editor2d.handle.ShapeEditHandleKit; 41 import com.nightlabs.editor2d.j2d.GeneralShape; 42 import com.nightlabs.editor2d.j2d.GeneralShapeFactory; 43 import com.nightlabs.editor2d.request.EditorEditShapeRequest; 44 import com.nightlabs.editor2d.request.EditorRequestConstants; 45 import com.nightlabs.editor2d.request.EditorRotateCenterRequest; 46 import com.nightlabs.editor2d.request.EditorRotateRequest; 47 import com.nightlabs.editor2d.request.EditorShearRequest; 48 import com.nightlabs.editor2d.util.EditorUtil; 49 import com.nightlabs.editor2d.util.J2DUtil; 50 51 52 public class DrawComponentResizeEditPolicy 53 extends ResizableEditPolicy 54 implements EditorRequestConstants 55 { 56 public static final Logger LOGGER = Logger.getLogger(DrawComponentResizeEditPolicy.class); 57 58 62 protected IFigure createDragSourceFeedbackFigure() 63 { 64 IFigure figure = getCustomFeedbackFigure(getHost().getModel()); 66 figure.setBounds(getInitialFeedbackBounds()); 67 addFeedback(figure); 68 return figure; 69 } 70 71 protected Polyline createPolylineFigure(GraphicalEditPart part) 72 { 73 ShapeDrawComponentEditPart sdcEP = (ShapeDrawComponentEditPart) part; 74 ShapeDrawComponent sdc = sdcEP.getShapeDrawComponent(); 75 Polyline polyline = J2DUtil.toPolyline(sdcEP.getGeneralShape()); 76 polyline.setLineStyle(2); 77 polyline.setXOR(true); 78 polyline.setFill(true); 79 polyline.setBackgroundColor(ColorConstants.darkGray); 80 polyline.setForegroundColor(ColorConstants.white); 81 82 for (int i=0; i<polyline.getPoints().size(); i++) { 84 Point p = polyline.getPoints().getPoint(i); 85 Point newPoint = getConstraintFor(p); 86 polyline.getPoints().setPoint(newPoint, i); 87 } 88 89 return polyline; 90 } 91 92 protected IFigure getFeedbackFigure(Object modelPart) 93 { 94 RectangleFigure figure = new RectangleFigure(); 95 figure.setXOR(true); 96 figure.setFill(true); 97 figure.setBackgroundColor(ColorConstants.darkGray); 98 figure.setForegroundColor(ColorConstants.white); 99 return figure; 100 } 101 102 protected ShapeFigure getCustomFeedbackFigure(Object modelPart) 103 { 105 GeneralShape gs = null; 106 if (modelPart instanceof ShapeDrawComponent) { 107 ShapeDrawComponent sdc = (ShapeDrawComponent) modelPart; 108 gs = (GeneralShape)sdc.getGeneralShape().clone(); 109 } 110 else if (modelPart instanceof ImageDrawComponent) { 111 ImageDrawComponent idc = (ImageDrawComponent) modelPart; 112 gs = (GeneralShape) idc.getImageShape().clone(); 113 } 114 else if (modelPart instanceof DrawComponentContainer) 115 { 116 DrawComponentContainer container = (DrawComponentContainer) modelPart; 117 ShapeFigure containerFigure = new FeedbackShapeFigure(); 118 GeneralShape containerShape = GeneralShapeFactory.createRectangle(1,1,1,1); 119 containerFigure.setGeneralShape(containerShape); 120 containerFigure.setXOR(true); 121 containerFigure.setFill(true); 122 containerFigure.setBackgroundColor(ColorConstants.darkGray); 123 containerFigure.setForegroundColor(ColorConstants.white); 124 for (Iterator it = container.getDrawComponents().iterator(); it.hasNext(); ) { 125 DrawComponent dc = (DrawComponent) it.next(); 126 ShapeFigure figure = getCustomFeedbackFigure(dc); 127 containerFigure.add(figure); 128 } 129 return containerFigure; 130 } 131 else { 132 gs = GeneralShapeFactory.createRectangle(0, 0, 10, 10); 133 } 134 ShapeFigure shapeFigure = new FeedbackShapeFigure(); 135 shapeFigure.setGeneralShape(gs); 136 shapeFigure.setXOR(true); 137 shapeFigure.setFill(true); 138 shapeFigure.setBackgroundColor(ColorConstants.darkGray); 139 shapeFigure.setForegroundColor(ColorConstants.white); 140 return shapeFigure; 141 } 142 143 146 protected Rectangle getInitialFeedbackBounds() 147 { 148 return getHostFigure().getBounds(); 150 } 151 152 155 public boolean understandsRequest(Request request) 156 { 157 if (REQ_EDIT_SHAPE.equals(request.getType())) 158 return true; 159 160 return super.understandsRequest(request); 161 } 162 163 protected List createSelectionHandles() 164 { 165 if (EditorStateManager.getCurrentState() == EditorStateManager.STATE_EDIT_SHAPE) 166 { 167 List list = new ArrayList (); 168 if (getHost() instanceof ShapeDrawComponentEditPart) { 169 ShapeDrawComponentEditPart sdcEditPart = (ShapeDrawComponentEditPart) getHost(); 170 ShapeEditHandleKit.addHandles(sdcEditPart, list); 171 return list; 172 } 173 } 174 else if (EditorStateManager.getCurrentState() == EditorStateManager.STATE_ROTATE) 175 { 176 clearHandleLayer(); 177 List list = new ArrayList (); 178 RotateHandleKit.addHandles(getHost().getViewer().getSelectedEditParts(), list); 179 return list; 180 } 181 182 return super.createSelectionHandles(); 183 } 184 185 protected void clearHandleLayer() 186 { 187 IFigure layer = getLayer(LayerConstants.HANDLE_LAYER); 188 for (int i = 0; i < layer.getChildren().size(); i++) { 189 IFigure figure = (IFigure) layer.getChildren().get(i); 190 layer.remove(figure); 191 } 192 } 193 194 protected void removeSelectionHandles() 195 { 196 if (handles == null) 197 return; 198 199 IFigure layer = getLayer(LayerConstants.HANDLE_LAYER); 200 if (layer.getChildren().isEmpty()) 201 return; 202 203 for (int i = 0; i < handles.size(); i++) { 204 if (layer.getChildren().contains(handles.get(i))) 205 layer.remove((IFigure)handles.get(i)); 206 } 207 208 handles = null; 209 } 210 211 public void eraseSourceFeedback(Request request) 212 { 213 if (request.getType().equals(REQ_EDIT_SHAPE)) 214 eraseEditShapeFeedback((EditorEditShapeRequest)request); 215 else if (request.getType().equals(REQ_ROTATE)) 216 eraseRotateFeedback((EditorRotateRequest)request); 217 else if (request.getType().equals(REQ_EDIT_ROTATE_CENTER)) 218 eraseEditRotateCenterFeedback((EditorRotateCenterRequest)request); 219 else if (request.getType().equals(REQ_SHEAR)) 220 eraseShearFeedback(); 221 else 222 super.eraseSourceFeedback(request); 223 } 224 225 230 protected void eraseEditShapeFeedback(EditorEditShapeRequest request) 231 { 232 if (feedback != null) { 233 removeFeedback(feedback); 234 } 235 feedback = null; 236 } 237 238 protected void eraseEditRotateCenterFeedback(EditorRotateCenterRequest request) 239 { 240 if (feedback != null) { 241 removeFeedback(feedback); 242 } 243 feedback = null; 244 } 245 246 public void showSourceFeedback(Request request) 247 { 248 if (request.getType().equals(REQ_EDIT_SHAPE)) 249 showEditShapeFeedback((EditorEditShapeRequest)request); 250 else if (request.getType().equals(REQ_ROTATE)) 251 showRotateFeedback((EditorRotateRequest)request); 252 else if (request.getType().equals(REQ_EDIT_ROTATE_CENTER)) 253 showEditRotateCenterFeedback((EditorRotateCenterRequest)request); 254 else if (request.getType().equals(REQ_SHEAR)) 255 showShearFeedback((EditorShearRequest)request); 256 else 257 super.showSourceFeedback(request); 258 } 259 260 261 protected ShapeFigure getShearFeedbackFigure() 262 { 263 if (feedback == null) { 264 feedback = createDragSourceFeedbackFigure(); 265 PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy()); 266 feedback.setBounds(getConstraintFor(rect)); 267 } 268 return (ShapeFigure) feedback; 269 } 270 271 protected Rectangle rotateCenterBounds; 272 protected void showEditRotateCenterFeedback(EditorRotateCenterRequest request) 273 { 274 IFigure feedback = getEditRotateCenterFeedback(request); 275 Point location = request.getRotationCenter(); 276 location.translate(getScrollOffset()); 277 Point feedbackLocation = location.getCopy(); 278 feedbackLocation.translate(-rotateCenterBounds.width/2, -rotateCenterBounds.height/2); 279 feedback.setLocation(feedbackLocation); 280 feedback.repaint(); 281 282 LOGGER.debug("feedBack.location = "+feedback.getBounds()); 283 } 284 285 protected IFigure createEditRotateCenterFeedback(EditorRotateCenterRequest request) 286 { 287 RotateCenterHandle figure = new RotateCenterHandle(request.getEditParts()); 288 request.setMultiple(figure.isMultiple()); 289 figure.setBackgroundColor(ColorConstants.darkGray); 290 figure.setForegroundColor(ColorConstants.white); 291 return figure; 292 } 293 294 protected IFigure getEditRotateCenterFeedback(EditorRotateCenterRequest request) 295 { 296 if (feedback == null) { 297 feedback = createEditRotateCenterFeedback(request); 298 rotateCenterBounds = feedback.getBounds(); 299 addFeedback(feedback); 300 } 301 return feedback; 302 } 303 304 protected Polyline getPolylineFeedback() 305 { 306 if (feedback == null) { 307 feedback = createPolylineFigure((GraphicalEditPart)getHost()); 308 addFeedback(feedback); 309 } 310 return (Polyline) feedback; 311 } 312 313 protected void eraseRotateFeedback(EditorRotateRequest request) 314 { 315 if (feedback != null) 316 removeFeedback(feedback); 317 318 feedback = null; 319 rotatedShape = null; 320 unrotatedShape = null; 321 rotationCenter = null; 322 rotationOffset = Double.MAX_VALUE; 323 } 324 325 protected final AffineTransform at = new AffineTransform (); 326 protected Point rotationCenter; 327 protected GeneralShape unrotatedShape; 328 protected GeneralShape rotatedShape; 329 protected double rotationOffset = Double.MAX_VALUE; 330 protected void showRotateFeedback(EditorRotateRequest request) 331 { 332 ShapeFigure rotationFeedback = getRotateFeedbackFigure(); 333 334 if (unrotatedShape == null) 335 unrotatedShape = (GeneralShape) rotationFeedback.getGeneralShape().clone(); 336 337 if (rotationCenter == null && request.getRotationCenter() != null) 338 rotationCenter = getConstraintFor(request.getRotationCenter()); 339 340 Point location = request.getLocation(); 341 location.translate(getScrollOffset()); 342 343 if (rotationOffset == Double.MAX_VALUE) 344 rotationOffset = EditorUtil.calcRotation(location, rotationCenter); 345 346 double rotationTmp = EditorUtil.calcRotation(location, rotationCenter); 347 double rotation = - (rotationTmp - rotationOffset); 348 request.setRotation(rotation); 349 double rotationInRadinans = Math.toRadians(rotation); 350 at.setToIdentity(); 351 at.rotate(rotationInRadinans, rotationCenter.x, rotationCenter.y); 352 rotatedShape = (GeneralShape) unrotatedShape.clone(); 354 rotatedShape.transform(at); 355 rotationFeedback.setGeneralShape(rotatedShape); 356 getFeedbackLayer().repaint(); 357 358 LOGGER.debug("rotation = "+rotation); 359 } 360 361 protected void eraseShearFeedback() 362 { 363 if (feedback != null) 364 removeFeedback(feedback); 365 366 feedback = null; 367 rotatedShape = null; 368 unrotatedShape = null; 369 } 370 371 protected void showShearFeedback(EditorShearRequest request) 372 { 373 ShapeFigure shearFeedback = getShearFeedbackFigure(); 374 if (unrotatedShape == null) 375 unrotatedShape = (GeneralShape) shearFeedback.getGeneralShape().clone(); 376 377 Point location = request.getLocation(); 378 location.translate(getScrollOffset()); 379 LOGGER.debug("location = "+location); 380 381 Rectangle bounds = request.getShearBounds(); 382 LOGGER.debug("shearBounds = "+bounds); 383 384 AffineTransform at = getShearTransform(location, bounds, request.getDirection()); 385 rotatedShape = (GeneralShape) unrotatedShape.clone(); 386 rotatedShape.transform(at); 387 shearFeedback.setGeneralShape(rotatedShape); 388 getFeedbackLayer().repaint(); 389 390 request.setAffineTransform(at); 391 } 392 393 protected AffineTransform getShearTransform(Point location, Rectangle bounds, int direction) 394 { 395 double shear = 0.0d; 396 double idleShear = 0.0d; 397 at.setToIdentity(); 398 switch(direction) 399 { 400 case(PositionConstants.WEST): 401 case(PositionConstants.EAST): 402 double heightMiddle = bounds.y + bounds.height/2; 403 double y = location.y; 404 double distanceY = heightMiddle - y; 405 double height = bounds.height; 406 shear = distanceY / height; 407 at.shear(idleShear, shear); 408 break; 409 case(PositionConstants.NORTH): 410 case(PositionConstants.SOUTH): 411 double widthMiddle = bounds.x + bounds.width/2; 412 double x = location.x; 413 double distanceX = widthMiddle - x; 414 double width = bounds.width; 415 shear = distanceX / width; 416 at.shear(shear, idleShear); 417 break; 418 } 419 LOGGER.debug("shear = "+shear); 420 return at; 421 } 422 423 protected ShapeFigure getRotateFeedbackFigure() 424 { 425 if (feedback == null) { 426 feedback = createDragSourceFeedbackFigure(); 428 429 PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy()); 430 feedback.setBounds(getConstraintFor(rect)); 431 } 432 return (ShapeFigure) feedback; 433 } 434 435 protected void showEditShapeFeedback(EditorEditShapeRequest request) 436 { 437 Polyline polyline = getPolylineFeedback(); 438 Point newPoint = new Point(request.getLocation().x, request.getLocation().y); 439 newPoint.translate(getScrollOffset()); 440 polyline.setPoint(newPoint, request.getPathSegmentIndex()); 441 } 442 443 protected ShapeFigure getEditShapeFeedbackFigure() 444 { 445 if (feedback == null) 446 feedback = createEditShapeFeedbackFigure(); 447 return (ShapeFigure) feedback; 448 } 449 450 protected IFigure createEditShapeFeedbackFigure() 452 { 453 IFigure figure = getCustomFeedbackFigure(getHost().getModel()); 454 figure.setBounds(getInitialFeedbackBounds()); 455 addFeedback(figure); 456 return figure; 457 } 458 459 public Rectangle getConstraintFor(Rectangle rectangle) { 460 return EditorUtil.oldToAbsolute((GraphicalEditPart)getHost(), rectangle); 461 } 462 463 public Point getConstraintFor(Point point){ 464 return EditorUtil.toAbsolute((GraphicalEditPart)getHost(), point); 465 } 466 467 protected Point getScrollOffset() { 468 return EditorUtil.getScrollOffset(getHost()); 469 } 470 471 473 private IFigure feedback; 474 475 479 protected IFigure getDragSourceFeedbackFigure() 480 { 481 if (feedback == null) 482 feedback = createDragSourceFeedbackFigure(); 483 return feedback; 484 } 485 486 491 protected void eraseChangeBoundsFeedback(ChangeBoundsRequest request) 492 { 493 if (feedback != null) { 494 removeFeedback(feedback); 495 } 496 feedback = null; 497 } 498 499 502 public void deactivate() { 503 if (feedback != null) { 504 removeFeedback(feedback); 505 feedback = null; 506 } 507 hideFocus(); 508 super.deactivate(); 509 } 510 511 protected void showChangeBoundsFeedback(ChangeBoundsRequest request) 512 { 513 IFigure feedback = getDragSourceFeedbackFigure(); 514 515 PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy()); 516 getHostFigure().translateToAbsolute(rect); 517 rect.translate(request.getMoveDelta()); 518 rect.resize(request.getSizeDelta()); 519 520 feedback.translateToRelative(rect); 521 feedback.setBounds(rect); 522 523 getFeedbackLayer().repaint(); 524 } 525 } 527 | Popular Tags |