예제 #1
0
  public static void updateDIShape(PictogramElement element) {

    PictogramLink link = element.getLink();
    if (link == null) {
      return;
    }

    BPMNShape bpmnShape = BusinessObjectUtil.getFirstElementOfType(element, BPMNShape.class);
    if (bpmnShape == null) {
      return;
    }

    ILocation loc = Graphiti.getLayoutService().getLocationRelativeToDiagram((Shape) element);
    Bounds bounds = bpmnShape.getBounds();

    bounds.setX(loc.getX());
    bounds.setY(loc.getY());

    GraphicsAlgorithm graphicsAlgorithm = element.getGraphicsAlgorithm();
    IDimension size = Graphiti.getGaService().calculateSize(graphicsAlgorithm);
    bounds.setHeight(size.getHeight());
    bounds.setWidth(size.getWidth());

    if (element instanceof ContainerShape) {
      EList<Shape> children = ((ContainerShape) element).getChildren();
      for (Shape shape : children) {
        if (shape instanceof ContainerShape) {
          updateDIShape(shape);
        }
      }
    }
  }
  @Override
  protected void preMoveShape(IMoveShapeContext context) {
    MoveShapeContext msc = (MoveShapeContext) context;
    ContainerShape oldContainer = context.getSourceContainer();
    ContainerShape newContainer = context.getTargetContainer();
    IPeLayoutService peLayoutService = Graphiti.getPeLayoutService();
    //		Shape shape = context.getShape();
    //		ILocation loc = peService.getLocationRelativeToDiagram(shape);
    ILocation oldLoc = peLayoutService.getLocationRelativeToDiagram(oldContainer);
    ILocation newLoc = peLayoutService.getLocationRelativeToDiagram(newContainer);
    //		System.out.println(
    //				(oldContainer==newContainer ? "inside:\n" : "outside:\n")+
    //				"oldContainer:\n" +
    //				"  x="+oldLoc.getX()+"\n"+
    //					"  y="+oldLoc.getY()+"\n"+
    //				"newContainer:\n" +
    //				"  x="+newLoc.getX()+"\n"+
    //					"  y="+newLoc.getY()+"\n"+
    //				"shape:\n" +
    //				"  rel x="+shape.getGraphicsAlgorithm().getX()+"\n"+
    //					"  rel y="+shape.getGraphicsAlgorithm().getY()+"\n"+
    //				"  abs x="+loc.getX()+"\n"+
    //					"  abs y="+loc.getY()+"\n"+
    //				"context:\n" +
    //				"  x="+msc.getX()+"\n"+
    //					"  y="+msc.getY()+"\n"+
    //				"  deltaX="+msc.getDeltaX()+"\n"+
    //					"  deltaY="+msc.getDeltaY()+"\n"+
    //				"\n"
    //		);

    if (oldContainer != newContainer) {
      int x = newLoc.getX() + msc.getX() - oldLoc.getX();
      int y = newLoc.getY() + msc.getY() - oldLoc.getY();
      int deltaX = newLoc.getX() + msc.getDeltaX() - oldLoc.getX();
      int deltaY = newLoc.getY() + msc.getDeltaY() - oldLoc.getY();

      //			System.out.println(
      //					"new context:\n"+
      //					"  x="+( newLoc.getX() + msc.getX() - oldLoc.getX() )+"\n"+
      //								"  y="+msc.getY()+"\n"+
      //					"  deltaX="+( newLoc.getX() + msc.getDeltaX() - oldLoc.getX() )+"\n"+
      //								"  deltaY="+msc.getDeltaY()+"\n"+
      //					"\n"
      //			);

      //			msc.setX(x);
      //			msc.setY(y);
      //			msc.setDeltaX(deltaX);
      //			msc.setDeltaY(deltaY);
      //			msc.setTargetContainer(oldContainer);
    }

    super.preMoveShape(context);
  }
  protected ContainerShape createNewShape(
      ModelHandler mh, ContainerShape oldShape, EClass newType) {
    ILayoutService layoutService = Graphiti.getLayoutService();
    boolean horz = Bpmn2Preferences.getInstance().isHorizontalDefault();

    ILocation loc = layoutService.getLocationRelativeToDiagram(oldShape);
    int x = loc.getX();
    int y = loc.getY();
    int xOffset = 0;
    int yOffset = 0;
    GraphicsAlgorithm ga = oldShape.getGraphicsAlgorithm();
    int width = ga.getWidth();
    int height = ga.getHeight();

    BPMN2FeatureProvider fp = (BPMN2FeatureProvider) getFeatureProvider();
    AbstractCreateFeature createFeature =
        (AbstractCreateFeature) fp.getCreateFeatureForBusinessObject(newType.getInstanceClass());

    CreateContext createContext = new CreateContext();
    createContext.putProperty(AbstractCreateFlowElementFeature.SKIP_ADD_GRAPHICS, true);
    createContext.setTargetContainer(oldShape.getContainer());

    FlowElement newObject = null;
    if (createFeature != null) {
      newObject = (FlowElement) createFeature.create(createContext)[0];
    } else {
      newObject = (FlowElement) mh.create(newType);
    }

    ContainerShape containerShape = oldShape.getContainer();
    if (containerShape != getDiagram()) {
      // we are adding a new shape to a control (e.g a SubProcess)
      // so we need to adjust the location to be relative to the
      // control instead of the diagram
      loc = layoutService.getLocationRelativeToDiagram(containerShape);
      xOffset = loc.getX();
      yOffset = loc.getY();
    }
    BaseElement oldObject = BusinessObjectUtil.getFirstElementOfType(oldShape, BaseElement.class);

    if (oldObject instanceof Lane) {
      ((Lane) oldObject).getFlowNodeRefs().add((FlowNode) newObject);
    }
    AddContext ac = new AddContext(new AreaContext(), newObject);
    AbstractAddBpmnShapeFeature af =
        (AbstractAddBpmnShapeFeature) getFeatureProvider().getAddFeature(ac);
    int w = af.getDefaultWidth();
    int h = af.getDefaultHeight();
    if (horz) {
      x += width + 50 + w / 2;
      y += height / 2;
      boolean done = false;
      while (!done) {
        done = true;
        List<Shape> shapes = getFlowElementChildren(containerShape);
        for (Shape s : shapes) {
          if (GraphicsUtil.intersects(s, x - w / 2, y - h / 2, w, h)) {
            y += 100;
            done = false;
            break;
          }
        }
      }
    } else {
      x += width / 2;
      y += height + 50 + h / 2;
      boolean done = false;
      while (!done) {
        done = true;
        List<Shape> shapes = getFlowElementChildren(containerShape);
        for (Shape s : shapes) {
          if (GraphicsUtil.intersects(s, x - w / 2, y - h / 2, w, h)) {
            x += 100;
            done = false;
            break;
          }
        }
      }
    }
    ac.setX(x - xOffset);
    ac.setY(y - yOffset);
    ac.setTargetContainer(oldShape.getContainer());

    return (ContainerShape) getFeatureProvider().addIfPossible(ac);
  }