| 1 27 28 package org.nightlabs.editor2d.editpolicy; 29 30 import java.awt.geom.AffineTransform ; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 import org.apache.log4j.Logger; 35 import org.eclipse.draw2d.IFigure; 36 import org.eclipse.draw2d.Label; 37 import org.eclipse.draw2d.Polyline; 38 import org.eclipse.draw2d.geometry.Dimension; 39 import org.eclipse.draw2d.geometry.Point; 40 import org.eclipse.draw2d.geometry.PrecisionRectangle; 41 import org.eclipse.draw2d.geometry.Rectangle; 42 import org.eclipse.gef.GraphicalEditPart; 43 import org.eclipse.gef.LayerConstants; 44 import org.eclipse.gef.Request; 45 import org.eclipse.gef.editpolicies.ResizableEditPolicy; 46 import org.eclipse.gef.requests.ChangeBoundsRequest; 47 import org.nightlabs.editor2d.EditorStateManager; 48 import org.nightlabs.editor2d.edit.ShapeDrawComponentEditPart; 49 import org.nightlabs.editor2d.figures.ShapeFigure; 50 import org.nightlabs.editor2d.handle.RotateCenterHandle; 51 import org.nightlabs.editor2d.handle.RotateHandleKit; 52 import org.nightlabs.editor2d.handle.ShapeEditHandleKit; 53 import org.nightlabs.editor2d.j2d.GeneralShape; 54 import org.nightlabs.editor2d.request.EditorEditShapeRequest; 55 import org.nightlabs.editor2d.request.EditorRequestConstants; 56 import org.nightlabs.editor2d.request.EditorRotateCenterRequest; 57 import org.nightlabs.editor2d.request.EditorRotateRequest; 58 import org.nightlabs.editor2d.util.EditorUtil; 59 import org.nightlabs.editor2d.util.FeedbackUtil; 60 import org.nightlabs.editor2d.util.J2DUtil; 61 62 63 public class DrawComponentResizeEditPolicy 64 extends ResizableEditPolicy 65 implements EditorRequestConstants 66 { 67 public static final Logger LOGGER = Logger.getLogger(DrawComponentResizeEditPolicy.class); 68 69 73 protected IFigure createDragSourceFeedbackFigure() 74 { 75 IFigure figure = getCustomFeedbackFigure(getHost().getModel()); 76 figure.setBounds(getInitialFeedbackBounds()); 77 addFeedback(figure); 78 return figure; 79 } 80 81 protected Polyline createPolylineFigure(GraphicalEditPart part) 82 { 83 ShapeDrawComponentEditPart sdcEP = (ShapeDrawComponentEditPart) part; 84 Polyline polyline = J2DUtil.toPolyline(sdcEP.getGeneralShape()); 85 polyline.setLineStyle(2); 86 polyline.setXOR(true); 87 polyline.setFill(true); 88 polyline.setBackgroundColor(FeedbackUtil.getBackgroundColor()); 89 polyline.setForegroundColor(FeedbackUtil.getForegroundColor()); 90 91 for (int i=0; i<polyline.getPoints().size(); i++) { 93 Point p = polyline.getPoints().getPoint(i); 94 Point newPoint = getConstraintFor(p); 95 polyline.getPoints().setPoint(newPoint, i); 96 } 97 98 return polyline; 99 } 100 101 protected ShapeFigure getCustomFeedbackFigure(Object modelPart) 102 { 103 return FeedbackUtil.getCustomFeedbackFigure(modelPart); 104 } 105 106 109 protected Rectangle getInitialFeedbackBounds() 110 { 111 return getHostFigure().getBounds(); 113 } 114 115 118 public boolean understandsRequest(Request request) 119 { 120 if (REQ_EDIT_SHAPE.equals(request.getType())) 121 return true; 122 123 return super.understandsRequest(request); 124 } 125 126 protected List createSelectionHandles() 127 { 128 if (EditorStateManager.getCurrentState() == EditorStateManager.STATE_EDIT_SHAPE) 129 { 130 List list = new ArrayList (); 131 if (getHost() instanceof ShapeDrawComponentEditPart) { 132 ShapeDrawComponentEditPart sdcEditPart = (ShapeDrawComponentEditPart) getHost(); 133 ShapeEditHandleKit.addHandles(sdcEditPart, list); 134 return list; 135 } 136 } 137 else if (EditorStateManager.getCurrentState() == EditorStateManager.STATE_ROTATE) 138 { 139 clearHandleLayer(); 140 List list = new ArrayList (); 141 RotateHandleKit.addHandles(getHost().getViewer().getSelectedEditParts(), list); 142 return list; 143 } 144 145 return super.createSelectionHandles(); 146 } 147 148 protected void clearHandleLayer() 149 { 150 IFigure layer = getLayer(LayerConstants.HANDLE_LAYER); 151 for (int i = 0; i < layer.getChildren().size(); i++) { 152 IFigure figure = (IFigure) layer.getChildren().get(i); 153 layer.remove(figure); 154 } 155 } 156 157 protected void removeSelectionHandles() 158 { 159 if (handles == null) 160 return; 161 162 IFigure layer = getLayer(LayerConstants.HANDLE_LAYER); 163 if (layer.getChildren().isEmpty()) 164 return; 165 166 for (int i = 0; i < handles.size(); i++) { 167 if (layer.getChildren().contains(handles.get(i))) 168 layer.remove((IFigure)handles.get(i)); 169 } 170 171 handles = null; 172 } 173 174 public void eraseSourceFeedback(Request request) 175 { 176 if (request.getType().equals(REQ_EDIT_SHAPE)) 177 eraseEditShapeFeedback((EditorEditShapeRequest)request); 178 else if (request.getType().equals(REQ_ROTATE)) 179 eraseRotateFeedback((EditorRotateRequest)request); 180 else if (request.getType().equals(REQ_EDIT_ROTATE_CENTER)) 181 eraseEditRotateCenterFeedback((EditorRotateCenterRequest)request); 182 else 185 super.eraseSourceFeedback(request); 186 } 187 188 193 protected void eraseEditShapeFeedback(EditorEditShapeRequest request) 194 { 195 if (feedback != null) { 196 removeFeedback(feedback); 197 } 198 feedback = null; 199 } 200 201 protected void eraseEditRotateCenterFeedback(EditorRotateCenterRequest request) 202 { 203 if (feedback != null) { 204 removeFeedback(feedback); 205 } 206 feedback = null; 207 } 208 209 public void showSourceFeedback(Request request) 210 { 211 if (request.getType().equals(REQ_EDIT_SHAPE)) 212 showEditShapeFeedback((EditorEditShapeRequest)request); 213 else if (request.getType().equals(REQ_ROTATE)) 214 showRotateFeedback((EditorRotateRequest)request); 215 else if (request.getType().equals(REQ_EDIT_ROTATE_CENTER)) 216 showEditRotateCenterFeedback((EditorRotateCenterRequest)request); 217 else 220 super.showSourceFeedback(request); 221 } 222 223 233 protected Rectangle rotateCenterBounds; 234 protected void showEditRotateCenterFeedback(EditorRotateCenterRequest request) 235 { 236 IFigure feedback = getEditRotateCenterFeedback(request); 237 Point location = request.getRotationCenter(); 238 location.translate(getScrollOffset()); 239 Point feedbackLocation = location.getCopy(); 240 feedbackLocation.translate(-rotateCenterBounds.width/2, -rotateCenterBounds.height/2); 241 feedback.setLocation(feedbackLocation); 242 feedback.repaint(); 243 244 LOGGER.debug("feedBack.location = "+feedback.getBounds()); 245 } 246 247 protected IFigure createEditRotateCenterFeedback(EditorRotateCenterRequest request) 248 { 249 RotateCenterHandle figure = new RotateCenterHandle(request.getEditParts()); 250 request.setMultiple(figure.isMultiple()); 251 figure.setBackgroundColor(FeedbackUtil.getBackgroundColor()); 252 figure.setForegroundColor(FeedbackUtil.getForegroundColor()); 253 return figure; 254 } 255 256 protected IFigure getEditRotateCenterFeedback(EditorRotateCenterRequest request) 257 { 258 if (feedback == null) { 259 feedback = createEditRotateCenterFeedback(request); 260 rotateCenterBounds = feedback.getBounds(); 261 addFeedback(feedback); 262 } 263 return feedback; 264 } 265 266 protected Polyline getPolylineFeedback() 267 { 268 if (feedback == null) { 269 feedback = createPolylineFigure((GraphicalEditPart)getHost()); 270 addFeedback(feedback); 271 } 272 return (Polyline) feedback; 273 } 274 275 protected void eraseRotateFeedback(EditorRotateRequest request) 276 { 277 if (feedback != null) 278 removeFeedback(feedback); 279 280 feedback = null; 281 rotatedShape = null; 282 unrotatedShape = null; 283 rotationCenter = null; 284 rotationOffset = Double.MAX_VALUE; 285 } 286 287 protected final AffineTransform at = new AffineTransform (); 288 protected Point rotationCenter; 289 protected GeneralShape unrotatedShape; 290 protected GeneralShape rotatedShape; 291 protected double rotationOffset = Double.MAX_VALUE; 292 protected void showRotateFeedback(EditorRotateRequest request) 293 { 294 ShapeFigure rotationFeedback = getRotateFeedbackFigure(); 295 296 if (unrotatedShape == null) 297 unrotatedShape = (GeneralShape) rotationFeedback.getGeneralShape().clone(); 298 299 if (rotationCenter == null && request.getRotationCenter() != null) 300 rotationCenter = getConstraintFor(request.getRotationCenter()); 301 302 Point location = request.getLocation(); 303 location.translate(getScrollOffset()); 304 305 if (rotationOffset == Double.MAX_VALUE) 306 rotationOffset = EditorUtil.calcRotation(location, rotationCenter); 307 308 double rotationTmp = EditorUtil.calcRotation(location, rotationCenter); 309 double rotation = - (rotationTmp - rotationOffset); 310 request.setRotation(rotation); 311 double rotationInRadinans = Math.toRadians(rotation); 312 at.setToIdentity(); 313 at.rotate(rotationInRadinans, rotationCenter.x, rotationCenter.y); 314 rotatedShape = (GeneralShape) unrotatedShape.clone(); 316 rotatedShape.transform(at); 317 rotationFeedback.setGeneralShape(rotatedShape); 318 getFeedbackLayer().repaint(); 319 320 LOGGER.debug("rotation = "+rotation); 321 } 322 323 333 355 385 protected ShapeFigure getRotateFeedbackFigure() 386 { 387 if (feedback == null) { 388 feedback = createDragSourceFeedbackFigure(); 390 391 PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy()); 392 feedback.setBounds(getConstraintFor(rect)); 393 } 394 return (ShapeFigure) feedback; 395 } 396 397 protected void showEditShapeFeedback(EditorEditShapeRequest request) 398 { 399 Polyline polyline = getPolylineFeedback(); 400 Point newPoint = new Point(request.getLocation().x, request.getLocation().y); 401 newPoint.translate(getScrollOffset()); 402 polyline.setPoint(newPoint, request.getPathSegmentIndex()); 403 } 404 405 protected ShapeFigure getEditShapeFeedbackFigure() 406 { 407 if (feedback == null) 408 feedback = createEditShapeFeedbackFigure(); 409 return (ShapeFigure) feedback; 410 } 411 412 protected IFigure createEditShapeFeedbackFigure() 414 { 415 IFigure figure = getCustomFeedbackFigure(getHost().getModel()); 416 figure.setBounds(getInitialFeedbackBounds()); 417 addFeedback(figure); 418 return figure; 419 } 420 421 public Rectangle getConstraintFor(Rectangle rectangle) { 422 return EditorUtil.oldToAbsolute((GraphicalEditPart)getHost(), rectangle); 423 } 424 428 public Point getConstraintFor(Point point){ 429 return EditorUtil.toAbsolute((GraphicalEditPart)getHost(), point); 430 } 431 435 protected Point getScrollOffset() { 436 return EditorUtil.getScrollOffset(getHost()); 437 } 438 439 441 protected IFigure feedback; 442 443 447 protected IFigure getDragSourceFeedbackFigure() 448 { 449 if (feedback == null) 450 feedback = createDragSourceFeedbackFigure(); 451 return feedback; 452 } 453 454 457 public void deactivate() { 458 if (feedback != null) { 459 removeFeedback(feedback); 460 feedback = null; 461 } 462 hideFocus(); 463 super.deactivate(); 464 } 465 466 468 protected boolean showFeedBackText = true; 469 protected Label feedbackLabel; 470 471 protected void showChangeBoundsFeedback(ChangeBoundsRequest request) 472 { 473 IFigure feedback = getDragSourceFeedbackFigure(); 474 475 PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy()); 476 getHostFigure().translateToAbsolute(rect); 477 rect.translate(request.getMoveDelta()); 478 rect.resize(request.getSizeDelta()); 479 480 feedback.translateToRelative(rect); 481 feedback.setBounds(rect); 482 483 if (showFeedBackText) { 484 showFeedbackText(request); 485 } 486 487 getFeedbackLayer().repaint(); 488 } 489 490 495 protected void eraseChangeBoundsFeedback(ChangeBoundsRequest request) 496 { 497 if (feedback != null) { 498 removeFeedback(feedback); 499 if (showFeedBackText) { 500 eraseFeedbackText(); 501 } 502 } 503 feedback = null; 504 } 505 506 protected Label getFeedbackTextFigure() 507 { 508 if (feedbackLabel == null) 509 feedbackLabel = createFeedbackTextFigure(""); 510 return feedbackLabel; 511 } 512 513 protected Label createFeedbackTextFigure(String text) 514 { 515 Label l = new Label(text); 516 l.setBounds(getInitialFeedbackBounds()); 517 addFeedback(l); 518 return l; 519 } 520 521 protected void showFeedbackText(ChangeBoundsRequest request) 522 { 523 Label feedbackText = getFeedbackTextFigure(); 524 525 feedbackText.setText(getText(request)); 526 feedbackText.setLocation(getFeedbackTextLocation(request)); 527 feedbackText.setSize(100, 20); 528 529 getFeedbackLayer().repaint(); 530 } 531 532 protected static final Dimension EMPTY_DIMENSION = new Dimension(0,0); 533 protected static final Point EMPTY_POINT = new Point(0,0); 534 535 protected Point getFeedbackTextLocation(ChangeBoundsRequest request) 536 { 537 Point loc = request.getLocation(); 538 loc.translate(EditorUtil.getScrollOffset(getHost())); 539 return loc; 540 } 541 542 protected String getText(ChangeBoundsRequest request) 543 { 544 Dimension sizeDelta = request.getSizeDelta(); 545 StringBuffer sb = new StringBuffer (); 547 Rectangle feedbackBounds = getDragSourceFeedbackFigure().getBounds(); 548 549 if (sizeDelta.equals(EMPTY_DIMENSION)) { 550 Point absoluteLocation = EditorUtil.toAbsolute(getHost(), feedbackBounds.x, feedbackBounds.y); 551 552 String x = "X"; 553 String y = "Y"; 554 sb.append(x+" "); 555 sb.append(absoluteLocation.x); 556 sb.append(", "); 557 sb.append(y+" "); 558 sb.append(absoluteLocation.y); 559 } 560 else { 561 Point absoluteSize = EditorUtil.toAbsolute(getHost(), feedbackBounds.width, feedbackBounds.height); 562 563 String width = "W"; 564 String height = "H"; 565 sb.append(width+" "); 566 sb.append(absoluteSize.x); 567 sb.append(", "); 568 sb.append(height+" "); 569 sb.append(absoluteSize.y); 570 } 571 return sb.toString(); 572 } 573 574 protected void eraseFeedbackText() 575 { 576 if (feedbackLabel != null) 577 removeFeedback(feedbackLabel); 578 579 feedbackLabel = null; 580 } 581 } 582 | Popular Tags |