1 8 package com.nightlabs.editor2d.editpolicy; 9 10 import java.util.List ; 11 12 import org.apache.log4j.Logger; 13 import org.eclipse.draw2d.IFigure; 14 import org.eclipse.draw2d.PositionConstants; 15 import org.eclipse.draw2d.Shape; 16 import org.eclipse.draw2d.XYLayout; 17 import org.eclipse.draw2d.geometry.Dimension; 18 import org.eclipse.draw2d.geometry.Point; 19 import org.eclipse.draw2d.geometry.Rectangle; 20 import org.eclipse.gef.EditPart; 21 import org.eclipse.gef.EditPolicy; 22 import org.eclipse.gef.GraphicalEditPart; 23 import org.eclipse.gef.Request; 24 import org.eclipse.gef.SnapToGuides; 25 import org.eclipse.gef.commands.Command; 26 import org.eclipse.gef.commands.CompoundCommand; 27 import org.eclipse.gef.editpolicies.LayoutEditPolicy; 28 import org.eclipse.gef.editpolicies.XYLayoutEditPolicy; 29 import org.eclipse.gef.requests.ChangeBoundsRequest; 30 import org.eclipse.gef.requests.CreateRequest; 31 import org.eclipse.gef.rulers.RulerProvider; 32 33 import com.nightlabs.editor2d.DrawComponent; 34 import com.nightlabs.editor2d.DrawComponentContainer; 35 import com.nightlabs.editor2d.EditorGuide; 36 import com.nightlabs.editor2d.EditorPlugin; 37 import com.nightlabs.editor2d.ShapeDrawComponent; 38 import com.nightlabs.editor2d.command.ChangeGuideCommand; 39 import com.nightlabs.editor2d.command.CreateDrawComponentCommand; 40 import com.nightlabs.editor2d.command.CreateImageCommand; 41 import com.nightlabs.editor2d.command.CreateShapeCommand; 42 import com.nightlabs.editor2d.command.CreateTextCommand; 43 import com.nightlabs.editor2d.command.EditShapeCommand; 44 import com.nightlabs.editor2d.command.RotateCenterCommand; 45 import com.nightlabs.editor2d.command.RotateCommand; 46 import com.nightlabs.editor2d.command.SetConstraintCommand; 47 import com.nightlabs.editor2d.command.ShearCommand; 48 import com.nightlabs.editor2d.edit.ShapeDrawComponentEditPart; 49 import com.nightlabs.editor2d.figures.AbstractShapeFigure; 50 import com.nightlabs.editor2d.figures.FeedbackShapeFigure; 51 import com.nightlabs.editor2d.figures.ShapeFigure; 52 import com.nightlabs.editor2d.j2d.GeneralShape; 53 import com.nightlabs.editor2d.request.EditorBoundsRequest; 54 import com.nightlabs.editor2d.request.EditorCreateRequest; 55 import com.nightlabs.editor2d.request.EditorEditShapeRequest; 56 import com.nightlabs.editor2d.request.EditorLocationRequest; 57 import com.nightlabs.editor2d.request.EditorRequestConstants; 58 import com.nightlabs.editor2d.request.EditorRotateCenterRequest; 59 import com.nightlabs.editor2d.request.EditorRotateRequest; 60 import com.nightlabs.editor2d.request.EditorShearRequest; 61 import com.nightlabs.editor2d.request.ImageCreateRequest; 62 import com.nightlabs.editor2d.request.LineCreateRequest; 63 import com.nightlabs.editor2d.request.TextCreateRequest; 64 import com.nightlabs.editor2d.util.EditorUtil; 65 import com.nightlabs.editor2d.util.J2DUtil; 66 67 public class DrawComponentContainerXYLayoutPolicy 68 extends XYLayoutEditPolicy 69 implements EditorRequestConstants 70 { 71 public static final Logger LOGGER = Logger.getLogger(DrawComponentContainerXYLayoutPolicy.class); 72 73 public DrawComponentContainerXYLayoutPolicy(XYLayout layout) { 74 super(); 75 setXyLayout(layout); 76 } 77 78 81 protected Command createAddCommand(EditPart child, Object constraint) 82 { 83 return null; 84 } 85 86 protected Command createChangeConstraintCommand(ChangeBoundsRequest request, 87 EditPart child, Object constraint) 88 { 89 SetConstraintCommand cmd = new SetConstraintCommand(); 90 DrawComponent part = (DrawComponent)child.getModel(); 91 cmd.setPart(part); 92 cmd.setBounds(J2DUtil.toAWTRectangle((Rectangle)constraint)); 93 Command result = cmd; 94 95 if ((request.getResizeDirection() & PositionConstants.NORTH_SOUTH) != 0) 96 { 97 Integer guidePos = (Integer )request.getExtendedData().get(SnapToGuides.KEY_HORIZONTAL_GUIDE); 98 if (guidePos != null) { 99 result = chainGuideAttachmentCommand(request, part, result, true); 100 } 101 else if (part.getHorizontalGuide() != null) 102 { 103 int alignment = part.getHorizontalGuide().getAlignment(part); 108 int edgeBeingResized = 0; 109 if ((request.getResizeDirection() & PositionConstants.NORTH) != 0) 110 edgeBeingResized = -1; 111 else 112 edgeBeingResized = 1; 113 if (alignment == edgeBeingResized) 114 result = result.chain(new ChangeGuideCommand(part, true)); 115 } 116 } 117 118 if ((request.getResizeDirection() & PositionConstants.EAST_WEST) != 0) 119 { 120 Integer guidePos = (Integer )request.getExtendedData().get(SnapToGuides.KEY_VERTICAL_GUIDE); 121 if (guidePos != null) { 122 result = chainGuideAttachmentCommand(request, part, result, false); 123 } 124 else if (part.getVerticalGuide() != null) 125 { 126 int alignment = part.getVerticalGuide().getAlignment(part); 127 int edgeBeingResized = 0; 128 if ((request.getResizeDirection() & PositionConstants.WEST) != 0) 129 edgeBeingResized = -1; 130 else 131 edgeBeingResized = 1; 132 if (alignment == edgeBeingResized) 133 result = result.chain(new ChangeGuideCommand(part, false)); 134 } 135 } 136 137 if (request.getType().equals(REQ_MOVE_CHILDREN) 138 || request.getType().equals(REQ_ALIGN_CHILDREN)) 139 { 140 result = chainGuideAttachmentCommand(request, part, result, true); 141 result = chainGuideAttachmentCommand(request, part, result, false); 142 result = chainGuideDetachmentCommand(request, part, result, true); 143 result = chainGuideDetachmentCommand(request, part, result, false); 144 } 145 146 return result; 147 } 148 149 152 protected Command createChangeConstraintCommand(EditPart child, Object constraint) { 153 return null; 154 } 155 156 protected EditPolicy createChildEditPolicy(EditPart child) { 157 return new DrawComponentResizeEditPolicy(); 158 } 159 160 163 protected Command getDeleteDependantCommand(Request request) { 164 return null; 165 } 166 167 protected Command getOrphanChildrenCommand(Request request) { 168 return null; 169 } 170 171 protected Command getAddCommand(Request generic) 172 { 173 175 ChangeBoundsRequest request = (ChangeBoundsRequest)generic; 176 List editParts = request.getEditParts(); 177 CompoundCommand command = new CompoundCommand(); 178 command.setDebugLabel("Add in ConstrainedLayoutEditPolicy"); GraphicalEditPart childPart; 180 Rectangle r; 181 Object constraint; 182 183 for (int i = 0; i < editParts.size(); i++) 184 { 185 childPart = (GraphicalEditPart)editParts.get(i); 186 r = childPart.getFigure().getBounds().getCopy(); 187 int oldWidth = r.width; 188 int oldHeight = r.height; 189 childPart.getFigure().translateToAbsolute(r); 191 r = request.getTransformedRectangle(r); 192 getLayoutContainer().translateToRelative(r); 194 195 if ((r.width != oldWidth || r.height != oldHeight) && 198 request.getSizeDelta().equals(0,0)) 199 r.setSize(oldWidth, oldHeight); 200 201 getLayoutContainer().translateFromParent(r); 202 r.translate(getLayoutOrigin().getNegated()); 203 constraint = getConstraintFor(r); 204 command.add(createAddCommand(generic, childPart, 205 translateToModelConstraint(constraint))); 206 } 207 return command.unwrap(); 208 } 209 210 protected Command chainGuideAttachmentCommand(Request request, DrawComponent part, Command cmd, boolean horizontal) 211 { 212 Command result = cmd; 213 214 Integer guidePos = (Integer )request.getExtendedData() 216 .get(horizontal ? SnapToGuides.KEY_HORIZONTAL_GUIDE 217 : SnapToGuides.KEY_VERTICAL_GUIDE); 218 if (guidePos != null) { 219 int alignment = ((Integer )request.getExtendedData() 220 .get(horizontal ? SnapToGuides.KEY_HORIZONTAL_ANCHOR 221 : SnapToGuides.KEY_VERTICAL_ANCHOR)).intValue(); 222 ChangeGuideCommand cgm = new ChangeGuideCommand(part, horizontal); 223 cgm.setNewGuide(findGuideAt(guidePos.intValue(), horizontal), alignment); 224 result = result.chain(cgm); 225 } 226 227 return result; 228 } 229 230 protected Command chainGuideDetachmentCommand(Request request, DrawComponent part, 231 Command cmd, boolean horizontal) { 232 Command result = cmd; 233 234 Integer guidePos = (Integer )request.getExtendedData() 236 .get(horizontal ? SnapToGuides.KEY_HORIZONTAL_GUIDE 237 : SnapToGuides.KEY_VERTICAL_GUIDE); 238 if (guidePos == null) 239 result = result.chain(new ChangeGuideCommand(part, horizontal)); 240 241 return result; 242 } 243 244 protected EditorGuide findGuideAt(int pos, boolean horizontal) 245 { 246 RulerProvider provider = ((RulerProvider)getHost().getViewer().getProperty( 247 horizontal ? RulerProvider.PROPERTY_VERTICAL_RULER 248 : RulerProvider.PROPERTY_HORIZONTAL_RULER)); 249 return (EditorGuide)provider.getGuideAt(pos); 250 } 251 252 263 protected Object getConstraintFor(CreateRequest request) 264 { 265 IFigure figure = getLayoutContainer(); 266 Point where = request.getLocation().getCopy(); 267 Dimension size = request.getSize(); 268 269 figure.translateToRelative(where); 270 figure.translateFromParent(where); 271 where.translate(getLayoutOrigin().getNegated()); 272 273 if (size == null || size.isEmpty()) 274 return getConstraintFor(where); 275 else 276 { 277 size = size.getCopy(); 280 figure.translateToRelative(size); 281 figure.translateFromParent(size); 282 283 return getConstraintFor(new Rectangle(where, size)); 284 } 285 } 286 287 private static final Dimension DEFAULT_SIZE = new Dimension(-1, -1); 288 289 public Rectangle getConstraintRectangleFor(Point point) 290 { 291 Point p = point.getCopy(); 292 IFigure figure = getLayoutContainer(); 293 figure.translateToRelative(p); 294 figure.translateFromParent(p); 295 p.translate(getLayoutOrigin().getNegated()); 296 return new Rectangle(p, DEFAULT_SIZE); 297 } 298 299 public Point getConstraintPointFor(Point point) 300 { 301 Point p = point.getCopy(); 302 IFigure figure = getLayoutContainer(); 303 figure.translateToRelative(p); 304 figure.translateFromParent(p); 305 p.translate(getLayoutOrigin().getNegated()); 306 return p; 307 } 308 309 public Rectangle getConstraintRectangleFor(Rectangle rectangle) 310 { 311 Rectangle r = rectangle.getCopy(); 312 IFigure figure = getLayoutContainer(); 313 figure.translateToRelative(r); 314 figure.translateFromParent(r); 315 r.translate(getLayoutOrigin().getNegated()); 316 return r; 317 } 318 319 public Command getCommand(Request request) 320 { 321 if (REQ_EDIT_SHAPE.equals(request.getType())) 322 return getEditShapeCommand((EditorEditShapeRequest)request); 323 324 if (request instanceof TextCreateRequest) 325 return getCreateTextCommand((TextCreateRequest)request); 326 327 if (request instanceof EditorRotateRequest) 328 return getRotateCommand((EditorRotateRequest)request); 329 330 if (request instanceof EditorRotateCenterRequest) 331 return getRotateCenterCommand((EditorRotateCenterRequest)request); 332 333 if (request instanceof ImageCreateRequest) 334 return getCreateImageCommand((ImageCreateRequest)request); 335 336 if (request instanceof EditorShearRequest) 337 return getShearCommand((EditorShearRequest) request); 338 339 return super.getCommand(request); 340 } 341 342 protected Command getShearCommand(EditorShearRequest request) 343 { 344 ShearCommand cmd = new ShearCommand(); 345 cmd.setEditParts(request.getEditParts()); 346 cmd.setAffineTransform(request.getAffineTransform()); 347 return cmd; 348 } 349 350 protected Command getRotateCenterCommand(EditorRotateCenterRequest request) 351 { 352 RotateCenterCommand cmd = new RotateCenterCommand(request); 353 Point rotationCenter = request.getRotationCenter().getCopy(); 354 rotationCenter = EditorUtil.toAbsolute(getHost(), rotationCenter.x, rotationCenter.y); 355 cmd.setRotationCenter(rotationCenter); 356 LOGGER.debug("cmd.rotationCenter = "+rotationCenter); 357 return cmd; 358 } 359 360 protected Command getRotateCommand(EditorRotateRequest request) 361 { 362 RotateCommand cmd = new RotateCommand(request); 363 double rotation = request.getRotation(); 364 cmd.setRotation(rotation); 365 LOGGER.debug("getRotateCommand().rotation = "+rotation); 366 return cmd; 367 } 368 369 public Command getCreateTextCommand(TextCreateRequest request) 370 { 371 CreateTextCommand create = new CreateTextCommand(request); 373 create.setParent(getDrawComponentContainer()); 374 375 Rectangle constraint = new Rectangle(); 376 constraint = (Rectangle)getConstraintFor((EditorBoundsRequest)request); 377 create.setLocation(constraint); 378 379 return create; 380 } 381 382 public Command getCreateImageCommand(ImageCreateRequest request) 383 { 384 CreateImageCommand create = new CreateImageCommand(); 385 create.setFileName(request.getFileName()); 386 DrawComponent newPart = (DrawComponent)request.getNewObject(); 387 create.setChild(newPart); 388 create.setParent(getDrawComponentContainer()); 389 Rectangle constraint = (Rectangle)getConstraintFor(request); 390 create.setLocation(constraint); 391 return create; 392 } 393 394 402 protected Command getEditShapeCommand(EditorEditShapeRequest request) 403 { 404 EditShapeCommand editShapeCommand = null; 405 if (editShapeCommand == null) 406 { 407 editShapeCommand = new EditShapeCommand(); 408 ShapeDrawComponentEditPart sdcEP = (ShapeDrawComponentEditPart) request.getTargetEditPart(); 409 ShapeDrawComponent sdc = sdcEP.getShapeDrawComponent(); 410 editShapeCommand.setShapeDrawComponent(sdc); 411 editShapeCommand.setPathSegmentIndex(request.getPathSegmentIndex()); 412 editShapeCommand.setLabel(EditorPlugin.getResourceString("command_edit_shape")); 413 } 414 Point modelPoint = getConstraintPointFor(request.getLocation()); 415 editShapeCommand.setLocation(modelPoint); 416 return editShapeCommand; 417 } 418 419 424 protected IFigure createSizeOnDropFeedback(CreateRequest createRequest) 425 { 426 if (createRequest instanceof EditorCreateRequest) 427 { 428 EditorCreateRequest editorRequest = (EditorCreateRequest) createRequest; 429 return createSizeOnDropFeedback(editorRequest); 430 } 431 return super.createSizeOnDropFeedback(createRequest); 432 } 433 434 protected IFigure createSizeOnDropFeedback(EditorCreateRequest editorRequest) 435 { 436 Rectangle constrainedBounds = (Rectangle)getConstraintFor((EditorBoundsRequest)editorRequest); 437 438 if (editorRequest.isUseShape()) 439 { 440 Shape shape = editorRequest.getShape(); 441 if (shape != null) 442 { 443 shape.setBounds(constrainedBounds); 444 addFeedback(shape); 445 return shape; 446 } 447 } 448 else 449 { 450 GeneralShape gp = editorRequest.getGeneralShape(); 451 if (gp != null) 452 { 453 if (editorRequest.getMode() == EditorCreateRequest.BOUNDS_FIX_MODE) 454 { 455 ShapeFigure shapeFigure = new AbstractShapeFigure(); 456 shapeFigure.setGeneralShape(gp); 457 shapeFigure.setFill(false); 458 addFeedback(shapeFigure); 459 return shapeFigure; 460 } 461 else 462 { 463 FeedbackShapeFigure shapeFigure = new FeedbackShapeFigure(); 464 shapeFigure.setGeneralShape(gp); 465 shapeFigure.setFill(false); 466 addFeedback(shapeFigure); 467 return shapeFigure; 468 } 469 } 470 } 471 return null; 472 } 473 474 485 protected Object getConstraintFor(EditorBoundsRequest request) 486 { 487 IFigure figure = getLayoutContainer(); 488 Point where = request.getLocation().getCopy(); 489 Dimension size = request.getSize(); 490 491 figure.translateToRelative(where); 492 figure.translateFromParent(where); 493 where.translate(getLayoutOrigin().getNegated()); 494 495 if (size == null || size.isEmpty()) 496 return getConstraintFor(where); 497 else { 498 size = size.getCopy(); 501 figure.translateToRelative(size); 502 figure.translateFromParent(size); 503 return getConstraintFor(new Rectangle(where, size)); 504 } 505 } 506 507 517 protected Point getConstraintFor(EditorLocationRequest request) 518 { 519 return getConstraintPointFor(request.getLocation()); 520 } 521 522 public DrawComponentContainer getDrawComponentContainer() 523 { 524 DrawComponentContainer container = (DrawComponentContainer)getHost().getModel(); 525 return container.getRoot().getCurrentLayer(); 526 } 527 528 protected Command createAddCommand(Request request, EditPart childEditPart, 529 Object constraint) 530 { 531 DrawComponent part = (DrawComponent)childEditPart.getModel(); 532 Rectangle rect = (Rectangle)constraint; 533 534 542 SetConstraintCommand setConstraint = new SetConstraintCommand(); 543 setConstraint.setBounds(J2DUtil.toAWTRectangle(rect)); 544 setConstraint.setPart(part); 545 setConstraint.setLabel(EditorPlugin.getResourceString("command_add_command")); 546 setConstraint.setDebugLabel("MLDC_XYEP setConstraint"); 548 Command cmd = setConstraint; 550 cmd = chainGuideAttachmentCommand(request, part, cmd, true); 551 cmd = chainGuideAttachmentCommand(request, part, cmd, false); 552 cmd = chainGuideDetachmentCommand(request, part, cmd, true); 553 return chainGuideDetachmentCommand(request, part, cmd, false); 554 } 555 556 protected Command getCreateCommand(CreateRequest request) 557 { 558 if (request instanceof EditorCreateRequest) 559 { 560 EditorCreateRequest req = (EditorCreateRequest) request; 561 return getEditorCreateCommand(req); 562 } 563 else { 564 CreateDrawComponentCommand create = new CreateDrawComponentCommand(); 565 DrawComponent newPart = (DrawComponent)request.getNewObject(); 566 create.setChild(newPart); 567 create.setParent(getDrawComponentContainer()); 568 569 Rectangle constraint = (Rectangle)getConstraintFor(request); 570 create.setLocation(constraint); 571 572 Command cmd = chainGuideAttachmentCommand(request, newPart, create, true); 573 return chainGuideAttachmentCommand(request, newPart, cmd, false); 574 } 575 } 576 577 protected Command getEditorCreateCommand(EditorCreateRequest request) 578 { 579 CreateShapeCommand create = new CreateShapeCommand(); 581 create.setGeneralShape(request.getGeneralShape()); 582 ShapeDrawComponent newPart = (ShapeDrawComponent)request.getNewObject(); 583 create.setChild(newPart); 584 create.setParent(getDrawComponentContainer()); 585 586 Rectangle constraint = new Rectangle(); 587 if (request instanceof LineCreateRequest) { 588 LineCreateRequest lineRequest = (LineCreateRequest) request; 589 if (lineRequest.getCreationBounds() != null) 590 constraint = getConstraintRectangleFor(lineRequest.getCreationBounds()); 591 } 592 else 593 constraint = (Rectangle)getConstraintFor((EditorBoundsRequest)request); 594 595 create.setLocation(constraint); 596 597 Command cmd = chainGuideAttachmentCommand(request, newPart, create, true); 598 return chainGuideAttachmentCommand(request, newPart, cmd, false); 599 } 600 601 605 protected void showSizeOnDropFeedback(EditorCreateRequest request) 606 { 607 Point p = request.getLocation().getCopy(); 608 Dimension size = request.getSize().getCopy(); 609 IFigure feedback = getSizeOnDropFeedback(request); 610 p.translate(getScrollOffset()); 611 612 Rectangle newBounds = new Rectangle(p, size).expand(getCreationFeedbackOffset(request)); 613 feedback.setBounds(newBounds); 614 } 615 616 protected void showSizeOnDropFeedback(CreateRequest request) 617 { 618 if (request instanceof EditorCreateRequest) 619 showSizeOnDropFeedback((EditorCreateRequest)request); 620 else 621 super.showSizeOnDropFeedback(request); 622 } 623 624 protected Point getScrollOffset() { 625 return EditorUtil.getScrollOffset(getHost()); 626 } 627 } 628 | Popular Tags |