/**
  * Builds the command which will execute the user-specified operations to create a new operand.
  *
  * @param nodeContainer the container in which to create the operand.
  * @param tool the tool to use to create the operand.
  * @param startingEndPredecessor the event end graphically preceding the starting position of the
  *     new Operand.
  * @param finishingEndPredecessor the event end graphically preceding the finishing position of
  *     the new Operand.
  * @return a command to create the execution.
  */
 public static org.eclipse.emf.common.command.Command buildCreateOperantCommandFromTool(
     DDiagramElementContainer nodeContainer,
     final OperandCreationTool tool,
     final EventEnd startingEndPredecessor,
     final EventEnd finishingEndPredecessor) {
   CommandBuilder builder =
       new OperandCreationCommandBuilder(
           tool, nodeContainer, startingEndPredecessor, finishingEndPredecessor);
   Session session = SessionManager.INSTANCE.getSession(nodeContainer.getTarget());
   return getCommand(builder, session);
 }
  private Command getLayoutConstraintsCommand(boolean collapsed, Command initialCommand) {
    if (getHost() instanceof IResizableCompartmentEditPart
        && getHost().getParent() instanceof AbstractDiagramElementContainerEditPart) {
      AbstractDiagramElementContainerEditPart regionPart =
          (AbstractDiagramElementContainerEditPart) getHost().getParent();
      View notationView = regionPart.getNotationView();
      if (notationView instanceof Node
          && ((Node) notationView).getLayoutConstraint() instanceof Size
          && notationView.getElement() instanceof DDiagramElementContainer) {
        TransactionalEditingDomain editingDomain = getEditingDomain();
        CompositeTransactionalCommand ctc =
            new CompositeTransactionalCommand(
                editingDomain,
                collapsed
                    ? Messages
                        .RegionCollapseAwarePropertyHandlerEditPolicy_collapseRegionCommandLabel
                    : Messages
                        .RegionCollapseAwarePropertyHandlerEditPolicy_expandRegionCommandLabel);
        Command result = new ICommandProxy(ctc);
        if (initialCommand != null) {
          ctc.add(new CommandProxy(initialCommand));
        }

        Size size = (Size) ((Node) notationView).getLayoutConstraint();
        DDiagramElementContainer ddec = (DDiagramElementContainer) notationView.getElement();
        Iterable<AbsoluteBoundsFilter> boundsFilters =
            Iterables.filter(ddec.getGraphicalFilters(), AbsoluteBoundsFilter.class);
        AbsoluteBoundsFilter expandedBoundsMarker =
            Iterables.isEmpty(boundsFilters) ? null : boundsFilters.iterator().next();

        // Update GMF size
        Dimension newGmfSize = new Dimension(size.getWidth(), size.getHeight());
        int parentStackDirection = regionPart.getParentStackDirection();
        if (parentStackDirection == PositionConstants.NORTH_SOUTH) {
          if (!collapsed) {
            newGmfSize.setHeight(
                expandedBoundsMarker == null ? -1 : expandedBoundsMarker.getHeight());
          } else if (size.getHeight() != -1) {
            newGmfSize.setHeight(LayoutUtils.COLLAPSED_VERTICAL_REGION_HEIGHT);
          }
        } else if (parentStackDirection == PositionConstants.EAST_WEST) {
          if (!collapsed) {
            newGmfSize.setWidth(
                expandedBoundsMarker == null ? -1 : expandedBoundsMarker.getWidth());
          } else if (!isTruncatedLabel(regionPart)) {
            // Change the GMF width only when label is not truncated
            // to avoid to have a collapsed Region bigger than the
            // expanded size (resized by the user).
            // Do not specify a collapsed width as it will depend on
            // the label size.
            newGmfSize.setWidth(-1);
          }
        }

        SetBoundsCommand setBoundsCommand =
            new SetBoundsCommand(
                editingDomain,
                Messages.RegionCollapseAwarePropertyHandlerEditPolicy_gmfSizeUpdateCommandLabel,
                new EObjectAdapter(notationView),
                newGmfSize);
        ctc.add(setBoundsCommand);

        // Remember expanded size: create/update/remove an
        // AbsoluteBoundsFilter marker on the DDiagramElement.
        if (collapsed) {
          if (expandedBoundsMarker != null) {
            ctc.add(
                new GMFCommandWrapper(
                    editingDomain,
                    new SetCommand(
                        editingDomain,
                        expandedBoundsMarker,
                        DiagramPackage.eINSTANCE.getAbsoluteBoundsFilter_Height(),
                        size.getHeight())));
            ctc.add(
                new GMFCommandWrapper(
                    editingDomain,
                    new SetCommand(
                        editingDomain,
                        expandedBoundsMarker,
                        DiagramPackage.eINSTANCE.getAbsoluteBoundsFilter_Width(),
                        size.getWidth())));
          } else {
            expandedBoundsMarker = DiagramFactory.eINSTANCE.createAbsoluteBoundsFilter();
            expandedBoundsMarker.setHeight(size.getHeight());
            expandedBoundsMarker.setWidth(size.getWidth());
            ctc.add(
                new GMFCommandWrapper(
                    editingDomain,
                    new AddCommand(
                        editingDomain, ddec.getGraphicalFilters(), expandedBoundsMarker)));
          }
        } else if (expandedBoundsMarker != null) {
          ctc.add(
              new GMFCommandWrapper(
                  editingDomain,
                  new RemoveCommand(
                      editingDomain, ddec.getGraphicalFilters(), expandedBoundsMarker)));
        }

        return result;
      }
    }
    return initialCommand;
  }