public PictogramElement add(IAddContext context) {
    IAddConnectionContext addConContext = (IAddConnectionContext) context;
    EActionLink addedEReference = (EActionLink) context.getNewObject();
    IPeCreateService peCreateService = Graphiti.getPeCreateService();

    // CONNECTION WITH POLYLINE
    Connection connection = peCreateService.createFreeFormConnection(getDiagram());
    connection.setStart(addConContext.getSourceAnchor());
    connection.setEnd(addConContext.getTargetAnchor());

    IGaService gaService = Graphiti.getGaService();
    Polyline polyline = gaService.createPolyline(connection);
    polyline.setLineWidth(3);
    polyline.setForeground(manageColor(IColorConstant.LIGHT_ORANGE));

    // create link and wire it
    link(connection, addedEReference);

    // add dynamic text decorator for the association name
    ConnectionDecorator textDecorator =
        peCreateService.createConnectionDecorator(connection, true, 0.5, true);
    Text text = gaService.createText(textDecorator);
    // createDefaultText(textDecorator);
    text.setForeground(manageColor(IColorConstant.BLACK));
    gaService.setLocation(text, 10, 0);
    text.setValue("has the action");

    return connection;
  }
  public IReason updateNeeded(IUpdateContext context) {
    // retrieve name from pictogram model
    String pictogramName = null;
    PictogramElement pictogramElement = context.getPictogramElement();
    if (pictogramElement instanceof ContainerShape) {
      ContainerShape cs = (ContainerShape) pictogramElement;
      for (Shape shape : cs.getChildren()) {
        if (shape.getGraphicsAlgorithm() instanceof Text) {
          Text text = (Text) shape.getGraphicsAlgorithm();
          pictogramName = text.getValue();
        }
      }
    }

    // retrieve name from business model
    String businessName = null;
    Object bo = getBusinessObjectForPictogramElement(pictogramElement);
    if (bo instanceof EClass) {
      EClass eClass = (EClass) bo;
      businessName = eClass.getName();
    }

    // update needed, if names are different
    boolean updateNameNeeded =
        ((pictogramName == null && businessName != null)
            || (pictogramName != null && !pictogramName.equals(businessName)));
    if (updateNameNeeded) {
      return Reason.createTrueReason("Name is out of date");
    } else {
      return Reason.createFalseReason();
    }
  }
  @Override
  public PictogramElement add(IAddContext context) {
    IPeService peService = Graphiti.getPeService();
    IGaService gaService = Graphiti.getGaService();

    BaseElement element = (BaseElement) context.getNewObject();
    IAddConnectionContext addConContext = (IAddConnectionContext) context;

    Connection connection = peService.createFreeFormConnection(getDiagram());

    Object importProp = context.getProperty(DIImport.IMPORT_PROPERTY);
    if (importProp != null && (Boolean) importProp) {
      connection.setStart(addConContext.getSourceAnchor());
      connection.setEnd(addConContext.getTargetAnchor());
    } else {
      ContainerShape sourceContainer =
          (ContainerShape) addConContext.getSourceAnchor().eContainer();
      ContainerShape targetContainer =
          (ContainerShape) addConContext.getTargetAnchor().eContainer();
      Tuple<FixPointAnchor, FixPointAnchor> anchors =
          AnchorUtil.getSourceAndTargetBoundaryAnchors(
              sourceContainer, targetContainer, connection);

      connection.setStart(anchors.getFirst());
      connection.setEnd(anchors.getSecond());
    }

    if (ModelUtil.hasName(element)) {
      ConnectionDecorator labelDecorator =
          Graphiti.getPeService().createConnectionDecorator(connection, true, 0.5, true);
      Text text = gaService.createText(labelDecorator, ModelUtil.getName(element));
      peService.setPropertyValue(
          labelDecorator, UpdateBaseElementNameFeature.TEXT_ELEMENT, Boolean.toString(true));
      text.setStyle(StyleUtil.getStyleForText(getDiagram()));
    }

    Polyline connectionLine = gaService.createPolyline(connection);
    connectionLine.setForeground(manageColor(StyleUtil.CLASS_FOREGROUND));

    decorateConnectionLine(connectionLine);

    createDIEdge(connection, element);
    createConnectionDecorators(connection);
    hook(addConContext, connection, element);

    return connection;
  }
  @Override
  public IReason updateNeeded(IUpdateContext context) {
    if (!canUpdate(context)) {
      return Reason.createFalseReason();
    }

    ContainerShape cs = (ContainerShape) context.getPictogramElement();
    Reference reference = (Reference) getBusinessObjectForPictogramElement(cs);

    // make sure the component still exists in the model
    if (!GraphitiInternal.getEmfService().isObjectAlive(reference)) {
      return Reason.createTrueReason(
          String.format("Reference {0} has been removed.", reference.getName()));
    }

    // retrieve name from pictogram model
    String pictogramName = null;
    Text foundText = GraphitiUtil.findChildGA(cs.getGraphicsAlgorithm(), Text.class);
    if (foundText != null) {
      pictogramName = foundText.getValue();
    }

    // update needed, if names are different
    String businessName = reference.getName();
    boolean updateNameNeeded =
        ((pictogramName == null && businessName != null)
            || (pictogramName != null && !pictogramName.contentEquals(businessName)));
    if (updateNameNeeded) {
      return Reason.createTrueReason("Reference name is out of date");
    }

    // check the wiring
    final Set<Contract> existingConnections = getExistingConnections(cs);
    for (ComponentReference promotedReference : reference.getPromote()) {
      if (promotedReference != null && !existingConnections.remove(promotedReference)) {
        return Reason.createTrueReason("Update connections.");
      }
    }

    if (existingConnections.size() > 0) {
      return Reason.createTrueReason("Update connections.");
    }

    return Reason.createFalseReason();
  }
  @Override
  protected void hook(
      Activity activity, ContainerShape container, IAddContext context, int width, int height) {
    super.hook(activity, container, context, width, height);
    Graphiti.getPeService().setPropertyValue(container, TRIGGERED_BY_EVENT, "false");

    IPeService peService = Graphiti.getPeService();
    IGaService gaService = Graphiti.getGaService();

    Shape textShape = peService.createShape(container, false);
    Text text = gaService.createDefaultText(textShape, activity.getName());
    gaService.setLocationAndSize(text, 5, 5, width - 10, 15);
    text.setStyle(StyleUtil.getStyleForText(getDiagram()));
    text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
    text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
    text.getFont().setBold(true);
    link(textShape, activity);
  }
  @Override
  public PictogramElement add(IAddContext context) {
    final NamespaceDeclaration namespaceDeclaration = (NamespaceDeclaration) context.getNewObject();

    final Diagram targetDiagram = (Diagram) context.getTargetContainer();

    // CONTAINER SHAPE WITH ROUNDED RECTANGLE
    IPeCreateService peCreateService = Graphiti.getPeCreateService();
    ContainerShape containerShape = peCreateService.createContainerShape(targetDiagram, true);

    // define a default size for the shape
    int width = 100;
    int height = 50;
    IGaService gaService = Graphiti.getGaService();
    RoundedRectangle roundedRectangle; // need to access it later

    // create and set graphics algorithm
    roundedRectangle = gaService.createRoundedRectangle(containerShape, 5, 5);
    roundedRectangle.setForeground(manageColor(IColorConstant.BLACK));
    roundedRectangle.setBackground(manageColor(IColorConstant.WHITE));
    roundedRectangle.setLineWidth(2);
    gaService.setLocationAndSize(roundedRectangle, context.getX(), context.getY(), width, height);

    // create link and wire it
    link(containerShape, namespaceDeclaration);

    // create shape for text
    Shape shape = peCreateService.createShape(containerShape, false);

    // create and set text graphics algorithm
    Text text = gaService.createText(shape, namespaceDeclaration.getName());
    text.setForeground(manageColor(IColorConstant.BLACK));
    text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
    // vertical alignment has as default value "center"
    text.setFont(gaService.manageDefaultFont(getDiagram(), false, true));
    gaService.setLocationAndSize(text, 0, 0, width, 20);

    // create link and wire it
    link(shape, namespaceDeclaration);

    return containerShape;
  }
  public PictogramElement add(IAddContext context) {
    Object newObject = context.getNewObject();
    Shape shape = Graphiti.getPeCreateService().createShape(context.getTargetContainer(), true);

    IGaService gaService = Graphiti.getGaService();
    Rectangle r = gaService.createRectangle(shape);
    gaService.setLocationAndSize(r, context.getX(), context.getY(), 400, 100);
    r.setBackground(gaService.manageColor(getDiagram(), IColorConstant.WHITE));

    String text = newObject.getClass().getName() + " - " + newObject.toString();
    Text textGa = gaService.createDefaultText(getDiagram(), r, text);
    gaService.setLocationAndSize(textGa, 0, 0, 400, 100);
    textGa.setRotation(-1d);

    // Create an italic font to use it later in the rich tooltip
    gaService.manageFont(
        getDiagram(), IGaService.DEFAULT_FONT, IGaService.DEFAULT_FONT_SIZE, true, false);

    return shape;
  }
  @Override
  public PictogramElement createShape(
      Object businessObject,
      ContainerShape layoutParent,
      int width,
      int height,
      IAddContext context) {
    final IPeCreateService peCreateService = Graphiti.getPeCreateService();
    final ContainerShape containerShape = peCreateService.createContainerShape(layoutParent, true);
    final IGaService gaService = Graphiti.getGaService();

    Diagram diagram = getFeatureProvider().getDiagramTypeProvider().getDiagram();
    final Pool addedPool = (Pool) context.getNewObject();

    // check whether the context has a size (e.g. from a create feature)
    // otherwise define a default size for the shape
    width = width <= 0 ? 500 : width;
    height = height <= 0 ? 150 : height;

    // create invisible outer rectangle expanded by
    // the width needed for the anchor
    final Rectangle invisibleRectangle = gaService.createInvisibleRectangle(containerShape);
    gaService.setLocationAndSize(invisibleRectangle, context.getX(), context.getY(), width, height);

    // create and set visible rectangle inside invisible rectangle
    Rectangle rectangle = gaService.createRectangle(invisibleRectangle);
    rectangle.setParentGraphicsAlgorithm(invisibleRectangle);
    rectangle.setStyle(StyleUtil.getStyleForPool(diagram));
    gaService.setLocationAndSize(rectangle, 0, 0, width, height);

    // create shape for text
    final Shape shape = peCreateService.createShape(containerShape, false);

    // create and set text graphics algorithm
    String name = BpmnExtensionUtil.getPoolName(addedPool, ActivitiPlugin.getDefault());
    final Text text = gaService.createDefaultText(diagram, shape, name);
    text.setStyle(StyleUtil.getStyleForEvent(diagram));
    text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
    text.setVerticalAlignment(Orientation.ALIGNMENT_MIDDLE);
    gaService.setLocationAndSize(text, 0, 0, 20, height);
    text.setAngle(-90);
    Font font = null;
    if (OSUtil.getOperatingSystem() == OSEnum.Mac) {
      font = gaService.manageFont(diagram, text.getFont().getName(), 11, false, true);
    } else {
      font = gaService.manageDefaultFont(diagram, false, true);
    }
    text.setFont(font);

    // create link and wire it
    getFeatureProvider().link(shape, addedPool);

    return containerShape;
  }
    private static void updateSPPFigure(SPPRef spp, PictogramElement pe, Color dark, Color bright) {
      ContainerShape container = (ContainerShape) pe;

      // we clear the figure and rebuild it
      GraphicsAlgorithm invisibleRect = pe.getGraphicsAlgorithm();
      invisibleRect.getGraphicsAlgorithmChildren().clear();

      createSPPFigure(spp, false, container, invisibleRect, dark, bright);

      GraphicsAlgorithm ga = container.getChildren().get(0).getGraphicsAlgorithm();
      if (ga instanceof Text) {
        ((Text) ga).setValue(spp.getName());
      }
    }
  public boolean update(IUpdateContext context) {
    // retrieve name from business model
    String businessName = null;
    PictogramElement pictogramElement = context.getPictogramElement();
    Object bo = getBusinessObjectForPictogramElement(pictogramElement);
    if (bo instanceof EClass) {
      EClass eClass = (EClass) bo;
      businessName = eClass.getName();
    }

    // Set name in pictogram model
    if (pictogramElement instanceof ContainerShape) {
      ContainerShape cs = (ContainerShape) pictogramElement;
      for (Shape shape : cs.getChildren()) {
        if (shape.getGraphicsAlgorithm() instanceof Text) {
          Text text = (Text) shape.getGraphicsAlgorithm();
          text.setValue(businessName);
          return true;
        }
      }
    }

    return false;
  }
  @Override
  public PictogramElement add(IAddContext context) {
    // TODO Auto-generated method stub
    IAddConnectionContext addConContext = (IAddConnectionContext) context;
    AdviceEdge edge = (AdviceEdge) context.getNewObject();
    IPeCreateService peCreateService = Graphiti.getPeCreateService();

    Connection connection = peCreateService.createFreeFormConnection(getDiagram());
    connection.setStart(addConContext.getSourceAnchor());
    connection.setEnd(addConContext.getTargetAnchor());

    IGaService gaService = Graphiti.getGaService();
    Polyline polyline = gaService.createPlainPolyline(connection);
    polyline.setStyle(StyleUtil.getStyleForElement(getDiagram()));
    polyline.setLineStyle(LineStyle.DOT);

    link(connection, edge);

    // add dynamic text decorator for the reference name
    ConnectionDecorator textDecorator =
        peCreateService.createConnectionDecorator(connection, true, 0.5, true);
    Text text = gaService.createPlainText(textDecorator);
    text.setStyle(StyleUtil.getStyleForTextDecorator((getDiagram())));
    gaService.setLocation(text, 10, 0);

    text.setValue(edge.getAdvicetype().toString());

    ConnectionDecorator cd;
    cd = peCreateService.createConnectionDecorator(connection, false, 1.0, true);
    int[] xy = {getPointCutR() * 2, 0, 0, -getPointCutR() * 2, 0, 0, 0, getPointCutR() * 2};
    Polygon polygon = Graphiti.getGaCreateService().createPolygon(cd, xy);
    polygon.setStyle(StyleUtil.getStyleForElement(getDiagram()));
    polygon.setLineStyle(LineStyle.DOT);

    return connection;
  }
  // Adds a User Application figure to the target object
  @Override
  public PictogramElement add(final IAddContext context) {

    TDeploymentArtifact addedClass = (TDeploymentArtifact) context.getNewObject();

    ContainerShape targetDiagram = (ContainerShape) context.getTargetContainer();
    Object[] targetDiagrams = targetDiagram.getChildren().toArray();
    int ySD = 0;
    int interdist = 0;
    for (int i = 0; i < targetDiagrams.length; i++) {
      Object o = getBusinessObjectForPictogramElement((Shape) targetDiagrams[i]);
      if (o instanceof UserApplication) interdist = 3;
    }
    for (int i = 0; i < targetDiagrams.length; i++) {
      Object o = getBusinessObjectForPictogramElement((Shape) targetDiagrams[i]);
      if (((Shape) targetDiagrams[i]).getGraphicsAlgorithm() instanceof Rectangle
          || (o instanceof SoftwareDependency)) {
        if (((Shape) targetDiagrams[i]).getGraphicsAlgorithm() instanceof Rectangle) {
          ySD = ((Shape) targetDiagrams[i]).getGraphicsAlgorithm().getY();
        }
        ((Shape) targetDiagrams[i])
            .getGraphicsAlgorithm()
            .setY(((Shape) targetDiagrams[i]).getGraphicsAlgorithm().getY() + 20 + interdist);
      }
    }
    // CONTAINER SHAPE WITH ROUNDED RECTANGLE
    IPeCreateService peCreateService = Graphiti.getPeCreateService();
    ContainerShape containerShape = peCreateService.createContainerShape(targetDiagram, true);
    final int width = StyleUtil.SOFT_DEPENDENCY_WIDTH;
    final int height = 20;
    IGaService gaService = Graphiti.getGaService();
    RoundedRectangle roundedRectangle;
    {
      // create and set graphics algorithm
      int x = (StyleUtil.APP_COMPONENT_WIDTH - width) / 2;
      roundedRectangle = gaService.createRoundedRectangle(containerShape, 5, 5);
      roundedRectangle.setForeground(manageColor(E_CLASS_FOREGROUND));
      roundedRectangle.setBackground(manageColor(E_CLASS_BACKGROUND));
      roundedRectangle.setLineWidth(2);
      gaService.setLocationAndSize(roundedRectangle, x, ySD - 10 + interdist, width, height);
      if (addedClass.eResource() == null) {
        getDiagram().eResource().getContents().add(addedClass);
      }
      // create link and wire it
      link(containerShape, addedClass);
    }
    // SHAPE WITH LINE
    {
      // create shape for line
      Shape shape = peCreateService.createShape(containerShape, false);
      // create and set graphics algorithm
      Polyline polyline = gaService.createPolyline(shape, new int[] {0, 20, width, 20});
      polyline.setForeground(manageColor(E_CLASS_FOREGROUND));
      polyline.setLineWidth(2);
    }
    // SHAPE WITH TEXT
    {
      // create shape for text
      Shape shape = peCreateService.createShape(containerShape, false);
      // create and set text graphics algorithm
      Text text = gaService.createText(shape, addedClass.getName());
      text.setForeground(manageColor(E_CLASS_TEXT_FOREGROUND));
      text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
      // vertical alignment has as default value "center"
      text.setFont(gaService.manageDefaultFont(getDiagram(), false, true));
      gaService.setLocationAndSize(text, 0, 0, width, 20);
      // create link and wire it
      link(shape, addedClass);
    }
    return containerShape;
  }
  @Override
  public boolean update(IUpdateContext context) {
    _hasDoneChanges = false;

    // retrieve name from business model
    ContainerShape cs = (ContainerShape) context.getPictogramElement();
    Reference reference = (Reference) getBusinessObjectForPictogramElement(cs);

    // remove it if it's gone
    if (!GraphitiInternal.getEmfService().isObjectAlive(reference)) {
      IRemoveContext removeContext = new RemoveContext(context.getPictogramElement());
      final IRemoveFeature removeFeature = getFeatureProvider().getRemoveFeature(removeContext);
      if (removeFeature != null && removeFeature.canRemove(removeContext)) {
        removeFeature.remove(removeContext);
        _hasDoneChanges = removeFeature.hasDoneChanges();
        return true;
      }
    }

    // Set name in pictogram model
    String pictogramName = null;
    Text foundText = GraphitiUtil.findChildGA(cs.getGraphicsAlgorithm(), Text.class);
    if (foundText != null) {
      pictogramName = foundText.getValue();
    }
    String businessName = reference.getName();
    boolean updateNameNeeded =
        ((pictogramName == null && businessName != null)
            || (pictogramName != null && !pictogramName.contentEquals(businessName)));
    if (updateNameNeeded) {
      foundText.setValue(businessName);
      _hasDoneChanges = true;
    }

    // update the wires
    final Set<Contract> existingConnections = getExistingConnections(cs);
    final Anchor anchor = cs.getAnchors().get(0);
    for (ComponentReference promotedReference : reference.getPromote()) {
      if (promotedReference != null && !existingConnections.remove(promotedReference)) {
        for (PictogramElement pe :
            getFeatureProvider().getAllPictogramElementsForBusinessObject(promotedReference)) {
          if (pe instanceof Anchor) {
            AddConnectionContext addContext = new AddConnectionContext((Anchor) pe, anchor);
            addContext.setNewObject(reference);
            updatePictogramElement(getFeatureProvider().addIfPossible(addContext));
            _hasDoneChanges = true;
            break;
          }
        }
      }
    }

    for (Connection connection : new ArrayList<Connection>(anchor.getIncomingConnections())) {
      Object bo = getBusinessObjectForPictogramElement(connection.getStart());
      if (bo == null || existingConnections.remove(bo)) {
        RemoveContext removeContext = new RemoveContext(connection);
        IRemoveFeature removeFeature = getFeatureProvider().getRemoveFeature(removeContext);
        if (removeFeature.canExecute(removeContext)) {
          removeFeature.execute(removeContext);
          _hasDoneChanges = _hasDoneChanges || removeFeature.hasDoneChanges();
        }
      }
    }

    return _hasDoneChanges;
  }
Exemple #14
0
 /**
  * Returns the minimal height needed to display the value of given Text with its associated Font.
  *
  * @param text
  * @return
  */
 public static int getTextMinHeight(Text text) {
   final IUiLayoutService uiLayoutService = GraphitiUi.getUiLayoutService();
   final Font currentFont = Graphiti.getGaService().getFont(text, true);
   return uiLayoutService.calculateTextSize(text.getValue(), currentFont).getHeight();
 }