/******************************************************************************
 * Copyright (c) 2006, Intalio Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intalio Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.stp.bpmn.diagram.edit.parts;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter;
import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateUnspecifiedTypeConnectionRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.MetamodelType;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.stp.bpmn.BpmnPackage;
import org.eclipse.stp.bpmn.SubProcess;
import org.eclipse.stp.bpmn.diagram.edit.policies.SubProcessCanonicalEditPolicy;
import org.eclipse.stp.bpmn.diagram.edit.policies.SubProcessGraphicalNodeEditPolicy;
import org.eclipse.stp.bpmn.diagram.edit.policies.SubProcessItemSemanticEditPolicy;
import org.eclipse.stp.bpmn.diagram.part.BpmnVisualIDRegistry;
import org.eclipse.stp.bpmn.diagram.providers.BpmnElementTypes;
import org.eclipse.stp.bpmn.figures.SubProcessBorderFigure;
import org.eclipse.stp.bpmn.figures.activities.ActivityPainter;
import org.eclipse.stp.bpmn.figures.connectionanchors.DefaultSizeNodeFigureEx;
import org.eclipse.stp.bpmn.figures.connectionanchors.impl.ConnectionAnchorFactory;
import org.eclipse.stp.bpmn.policies.ConnectionHandleEditPolicyEx;
import org.eclipse.stp.bpmn.policies.DelegateToCompartmentEditPolicy;
import org.eclipse.stp.bpmn.policies.GenericEAnnotationDropPolicy;
import org.eclipse.stp.bpmn.policies.ResizableSubProcessEditPolicy;
import org.eclipse.stp.bpmn.tools.AddChildLayoutListener;
import org.eclipse.stp.bpmn.tools.EdgeConnectionValidator;
import org.eclipse.stp.bpmn.tools.TaskDragEditPartsTrackerEx;

/**
 * @generated
 */
public class SubProcessEditPart extends ShapeNodeEditPart {
    /**
     * @notgenerated
     */
    public static final Dimension COLLAPSED_SIZE = new Dimension(111, 81);

    /**
     * @notgenerated
     */
    public static final Dimension EXPANDED_SIZE = new Dimension(131, 124);

    /**
     * @generated
     */
    public static final int VISUAL_ID = 2002;

    /**
     * @notgenerated
     */
    protected boolean needToUpdateContainer;

    /**
     * @generated
     */
    protected IFigure contentPane;

    /**
     * @generated
     */
    protected IFigure primaryShape;

    /**
     * @generated
     */
    public SubProcessEditPart(View view) {
        super(view);
    }

    /**
     * @generated
     */
    protected void createDefaultEditPoliciesGen() {
        installEditPolicy(EditPolicyRoles.CREATION_ROLE,
                new CreationEditPolicy() {

                    public Command getCommand(Request request) {
                        if (understandsRequest(request)) {
                            if (request instanceof CreateViewAndElementRequest) {
                                CreateElementRequestAdapter adapter = ((CreateViewAndElementRequest) request)
                                        .getViewAndElementDescriptor()
                                        .getCreateElementRequestAdapter();
                                IElementType type = (IElementType) adapter
                                        .getAdapter(IElementType.class);
                                if (type == BpmnElementTypes.Activity_2003) {
                                    EditPart compartmentEditPart = getChildBySemanticHint(BpmnVisualIDRegistry
                                            .getType(SubProcessSubProcessBorderCompartmentEditPart.VISUAL_ID));
                                    return compartmentEditPart == null ? null
                                            : compartmentEditPart
                                                    .getCommand(request);
                                }
                            }
                            return super.getCommand(request);
                        }
                        return null;
                    }
                });
        super.createDefaultEditPolicies();
        installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
                new SubProcessItemSemanticEditPolicy());
        installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
                new SubProcessGraphicalNodeEditPolicy());
        installEditPolicy(EditPolicyRoles.CANONICAL_ROLE,
                new SubProcessCanonicalEditPolicy());
        installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
    }

    /**
     * @notgenerated
     */
    protected void createDefaultEditPolicies() {
        createDefaultEditPoliciesGen();
        // no we don't want to generate an activity for a given subprocess
        // we want it to be in either the body or the border
        removeEditPolicy(EditPolicyRoles.CREATION_ROLE);

        // replace ConnectionHandleEditPolicy with our own
        installEditPolicy(EditPolicyRoles.CONNECTION_HANDLES_ROLE,
                new ConnectionHandleEditPolicyEx());

        // adding default drag and drop edit policy
        installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE,
                new GenericEAnnotationDropPolicy(this));
        // the compartment is not selectable anymore.
        // it looks like none of the containereidtpolicy gets called
        // because of that.
        // we use a mousedelegating edit policy to still call the proper
        // edit policy on the compartment.
        installEditPolicy(
                EditPolicy.CONTAINER_ROLE,
                new DelegateToCompartmentEditPolicy(
                        BpmnVisualIDRegistry
                                .getType(SubProcessSubProcessBodyCompartmentEditPart.VISUAL_ID),
                        EditPolicy.CONTAINER_ROLE));
    }

    /**
     * @generated
     */
    protected LayoutEditPolicy createLayoutEditPolicy() {
        LayoutEditPolicy lep = new LayoutEditPolicy() {

            protected EditPolicy createChildEditPolicy(EditPart child) {
                EditPolicy result = child
                        .getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
                if (result == null) {
                    result = new NonResizableEditPolicy();
                }
                return result;
            }

            protected Command getMoveChildrenCommand(Request request) {
                return null;
            }

            protected Command getCreateCommand(CreateRequest request) {
                return null;
            }
        };
        return lep;
    }

    /**
     * @generated
     */
    protected IFigure createNodeShape() {
        SubProcessFigure figure = new SubProcessFigure();
        return primaryShape = figure;
    }

    /**
     * @generated
     */
    public SubProcessFigure getPrimaryShape() {
        return (SubProcessFigure) primaryShape;
    }

    /**
     * @generated
     */
    protected boolean addFixedChild(EditPart childEditPart) {
        if (childEditPart instanceof SubProcessNameEditPart) {
            ((SubProcessNameEditPart) childEditPart).setLabel(getPrimaryShape()
                    .getFigureSubProcessNameFigure());
            return true;
        }
        if (childEditPart instanceof SubProcessSubProcessBodyCompartmentEditPart) {
            IFigure pane = getPrimaryShape().getFigureSubProcessBodyFigure();
            setupContentPane(pane); // FIXME each comparment should handle his
            // content pane in his own way
            pane
                    .add(((SubProcessSubProcessBodyCompartmentEditPart) childEditPart)
                            .getFigure());
            return true;
        }
        if (childEditPart instanceof SubProcessSubProcessBorderCompartmentEditPart) {
            IFigure pane = getPrimaryShape().getFigureSubProcessBorderFigure();
            setupContentPane(pane); // FIXME each comparment should handle his
            // content pane in his own way
            pane
                    .add(((SubProcessSubProcessBorderCompartmentEditPart) childEditPart)
                            .getFigure());
            return true;
        }
        return false;
    }

    /**
     * @generated
     */
    protected boolean removeFixedChild(EditPart childEditPart) {
        if (childEditPart instanceof SubProcessSubProcessBodyCompartmentEditPart) {
            IFigure pane = getPrimaryShape().getFigureSubProcessBodyFigure();
            pane
                    .remove(((SubProcessSubProcessBodyCompartmentEditPart) childEditPart)
                            .getFigure());
            return true;
        }
        if (childEditPart instanceof SubProcessSubProcessBorderCompartmentEditPart) {
            IFigure pane = getPrimaryShape().getFigureSubProcessBorderFigure();
            pane
                    .remove(((SubProcessSubProcessBorderCompartmentEditPart) childEditPart)
                            .getFigure());
            return true;
        }
        return false;
    }

    /**
     * @notgenerated
     */
    protected NodeFigure createNodePlate() {
        return new DefaultSizeNodeFigureEx(getMapMode().DPtoLP(
                EXPANDED_SIZE.width), getMapMode().DPtoLP(EXPANDED_SIZE.height),
                ConnectionAnchorFactory.INSTANCE) {

            @Override
            public Rectangle getHandleBounds() {
                SubProcessFigure figure = getPrimaryShape();
                return figure.getVisibleBounds();
            }
//
//            protected ConnectionAnchor createAnchor(PrecisionPoint p) {
//                if (p == null)
//                    // If the old terminal for the connection anchor cannot be
//                    // resolved (by SlidableAnchor) a null
//                    // PrecisionPoint will passed in - this is handled here
//                    return createDefaultAnchor();
//                return new SubProcessSlidableAnchor(this, p);
//            }
//
//            @Override
//            protected ConnectionAnchor createDefaultAnchor() {
//                return new SubProcessSlidableAnchor(this);
//            }

            @Override
            public void setBounds(Rectangle rect) {
                int x = bounds.x, y = bounds.y;

                boolean resize = (rect.width != bounds.width)
                        || (rect.height != bounds.height), translate = (rect.x != x)
                        || (rect.y != y);

                if ((resize || translate) && isVisible())
                    erase();
                if (translate) {
                    int dx = rect.x - x;
                    int dy = rect.y - y;
                    primTranslate(dx, dy);
                }

                bounds.width = rect.width;
                bounds.height = rect.height;

                if (translate || resize) {
                    if (resize || needToUpdateContainer && translate)
                        invalidate();
                    fireFigureMoved();
                    repaint();
                }
            }
        };
    }

    /**
     * Creates figure for this edit part.
     * 
     * Body of this method does not depend on settings in generation model so
     * you may safely remove <i>generated</i> tag and modify it.
     * 
     * @notgenerated
     */
    protected NodeFigure createNodeFigure() {
        NodeFigure figure = createNodePlate();
        figure.setLayoutManager(new StackLayout());
        IFigure shape = createNodeShape();
        figure.add(shape);
        contentPane = setupContentPane(shape);

        figure.addLayoutListener(new AddChildLayoutListener(this));

        return figure;
    }

    /**
     * Default implementation treats passed figure as content pane. Respects
     * layout one may have set for generated figure.
     * 
     * @param nodeShape
     *            instance of generated figure class
     * @generated
     */
    protected IFigure setupContentPaneGen(IFigure nodeShape) {
        if (nodeShape.getLayoutManager() == null) {
            ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
            layout.setSpacing(getMapMode().DPtoLP(5));
            nodeShape.setLayoutManager(layout);
        }
        return nodeShape; // use nodeShape itself as contentPane
    }

    /**
     * Set the border to null
     * 
     * @param nodeShape
     *            instance of generated figure class
     * @notgenerated
     */
    protected IFigure setupContentPane(IFigure nodeShape) {
        if (nodeShape.getLayoutManager() == null) {
            ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
            layout.setSpacing(0);
            nodeShape.setLayoutManager(layout);
            nodeShape.setBorder(null);
        }
        return nodeShape; // use nodeShape itself as contentPane
    }

    /**
     * @generated
     */
    public IFigure getContentPane() {
        if (contentPane != null) {
            return contentPane;
        }
        return super.getContentPane();
    }

    /**
     * @generated
     */
    public EditPart getPrimaryChildEditPart() {
        return getChildBySemanticHint(BpmnVisualIDRegistry
                .getType(SubProcessNameEditPart.VISUAL_ID));
    }

    /**
     * @generated
     */
    protected void addChildVisual(EditPart childEditPart, int index) {
        if (addFixedChild(childEditPart)) {
            return;
        }
        super.addChildVisual(childEditPart, -1);
    }

    /**
     * @generated
     */
    protected void removeChildVisual(EditPart childEditPart) {
        if (removeFixedChild(childEditPart)) {
            return;
        }
        super.removeChildVisual(childEditPart);
    }

    /**
     * @generated
     */
    public class SubProcessFigure extends
            org.eclipse.stp.bpmn.figures.SubProcessFigure {

        @Override
        public Dimension getMinimumSize(int wHint, int hHint) {
            return new Dimension(100, 100);
        }

        /**
         * @generated
         */
        public SubProcessFigure() {

            this.setForegroundColor(org.eclipse.draw2d.ColorConstants.black

            );

            createContents();
        }

        /**
         * @notgenerated
         */
        private void createContents() {
            org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel fig_0 = new org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel();
            fig_0.setText("");

            setFigureSubProcessNameFigure(fig_0);

            Object layData0 = null;

            this.add(fig_0, layData0);
            org.eclipse.stp.bpmn.figures.SubProcessBodyFigure fig_1 = new org.eclipse.stp.bpmn.figures.SubProcessBodyFigure() {
            };

            setFigureSubProcessBodyFigure(fig_1);

            Object layData1 = null;

            this.add(fig_1, layData1);
            org.eclipse.stp.bpmn.figures.SubProcessBorderFigure fig_2 = new org.eclipse.stp.bpmn.figures.SubProcessBorderFigure(
                    SubProcessEditPart.this.getMapMode());

            setFigureSubProcessBorderFigure(fig_2);

            Object layData2 = null;

            this.add(fig_2, layData2);
        }

        /**
         * @generated
         */
        private org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel fSubProcessNameFigure;

        /**
         * @generated
         */
        public org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel getFigureSubProcessNameFigure() {
            return fSubProcessNameFigure;
        }

        /**
         * @generated
         */
        private void setFigureSubProcessNameFigure(
                org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel fig) {
            fSubProcessNameFigure = fig;
        }

        /**
         * @generated
         */
        private org.eclipse.stp.bpmn.figures.SubProcessBodyFigure fSubProcessBodyFigure;

        /**
         * @generated
         */
        public org.eclipse.stp.bpmn.figures.SubProcessBodyFigure getFigureSubProcessBodyFigure() {
            return fSubProcessBodyFigure;
        }

        /**
         * @generated
         */
        private void setFigureSubProcessBodyFigure(
                org.eclipse.stp.bpmn.figures.SubProcessBodyFigure fig) {
            fSubProcessBodyFigure = fig;
        }

        /**
         * @generated
         */
        private org.eclipse.stp.bpmn.figures.SubProcessBorderFigure fSubProcessBorderFigure;

        /**
         * @generated
         */
        public org.eclipse.stp.bpmn.figures.SubProcessBorderFigure getFigureSubProcessBorderFigure() {
            return fSubProcessBorderFigure;
        }

        /**
         * @generated
         */
        private void setFigureSubProcessBorderFigure(
                org.eclipse.stp.bpmn.figures.SubProcessBorderFigure fig) {
            fSubProcessBorderFigure = fig;
        }

        /**
         * @generated
         */
        private boolean myUseLocalCoordinates = false;

        /**
         * @generated
         */
        protected boolean useLocalCoordinates() {
            return myUseLocalCoordinates;
        }

        /**
         * @generated
         */
        protected void setUseLocalCoordinates(boolean useLocalCoordinates) {
            myUseLocalCoordinates = useLocalCoordinates;
        }

        /**
         * @notgenerated Draws loop marker and collapse handle.
         */
        public void paint(Graphics graphics) {
            super.paint(graphics);
            SubProcess model = (SubProcess) ((View) getModel()).getElement();

            if (model.isLooping()) {
                Rectangle bounds = getAbsBoundsWithoutBorder();
                translateToRelative(bounds);
                PrecisionRectangle loopRect = ActivityPainter
                        .getLoopMarkerBounds(bounds);
                loopRect
                        .setX(loopRect.preciseX - loopRect.preciseWidth / 2 - 1);
                ActivityPainter.paintLoop(graphics, loopRect, this);
            }
            paintCollapseHandle(graphics);
        }

        /**
         * Draws &quot;+&quot; for collapsed and &quot;-&quot; for expanded
         * subprocess.
         * 
         * @notgenerated
         */
        private void paintCollapseHandle(Graphics graphics) {
            Rectangle bounds = getAbsCollapseHandleBounds();
            translateToRelative(bounds);
            int lineWidth = 1;
            bounds.shrink(lineWidth, lineWidth);
            graphics.drawRectangle(bounds);
            lineWidth = lineWidth * 2;
            graphics.setLineWidth(lineWidth);
            double delta = lineWidth;
            double y = bounds.y + bounds.height / 2.0;
            PrecisionPoint p1 = new PrecisionPoint(bounds.x + delta, y);
            PrecisionPoint p2 = new PrecisionPoint(bounds.x + bounds.width
                    - delta, y);
            graphics.drawLine(p1, p2);
            GraphicalEditPart bodyEditPart = null;
            for (Object object : children) {
                if (object instanceof SubProcessSubProcessBodyCompartmentEditPart) {
                    bodyEditPart = (GraphicalEditPart) object;
                    break;
                }
            }
            assert bodyEditPart != null;
            boolean isCollapsed = ((Boolean) bodyEditPart
                    .getStructuralFeatureValue(NotationPackage.eINSTANCE
                            .getDrawerStyle_Collapsed())).booleanValue();

            if (isCollapsed) {
                double x = bounds.x + bounds.width / 2.0;
                p1 = new PrecisionPoint(x, bounds.y + delta);
                p2 = new PrecisionPoint(x, bounds.y + bounds.height - delta);
                graphics.drawLine(p1, p2);
            }
        }
    }

    /**
     * @notgenerated
     */
    private static final int MIN_BODY_WIDTH = 50;

    /**
     * @notgenerated
     */
    private static final int MIN_BODY_HEIGHT = 50;

    /**
     * @notgenerated
     */
    private static final Insets INSETS = new Insets(5, 5, 5, 5);

    /**
     * @notgenerated
     */
    public Dimension calcMinSize() {
        List children = getChildren();
        GraphicalEditPart bodyEditPart = null;
        GraphicalEditPart borderEditPart = null;
        GraphicalEditPart nameEditPart = null;
        for (Object object : children) {
            GraphicalEditPart editPart = (GraphicalEditPart) object;
            if (editPart instanceof SubProcessSubProcessBodyCompartmentEditPart) {
                bodyEditPart = editPart;
            } else if (editPart instanceof SubProcessSubProcessBorderCompartmentEditPart) {
                borderEditPart = editPart;
            } else if (editPart instanceof SubProcessNameEditPart) {
                nameEditPart = editPart;
            }
        }
        Dimension labelSize = nameEditPart.getFigure().getSize();

        Rectangle r1;
        boolean isCollapsed = ((Boolean) bodyEditPart
                .getStructuralFeatureValue(NotationPackage.eINSTANCE
                        .getDrawerStyle_Collapsed())).booleanValue();
        if (!isCollapsed) {
            r1 = getChildrenBounds(bodyEditPart);
            if (r1.height == 0) {
                r1.width = MIN_BODY_WIDTH;
                r1.height = MIN_BODY_HEIGHT;
            }
        } else {
            r1 = new Rectangle(0, 0, MIN_BODY_WIDTH, MIN_BODY_HEIGHT);
        }

        Rectangle r2 = getChildrenBounds(borderEditPart);
        if (r2.height == 0) {
            r2.height = borderEditPart.getFigure().getBounds().height;
        }

        return new Dimension(Math.max(r1.width, r2.width) + INSETS.getWidth(),
                labelSize.height + r1.height + INSETS.getHeight() + r2.height);
    }

    /**
     * Claculates rectangle that contains all children of the specified edit
     * part
     * 
     * @notgenerated
     * @param editPart
     * @return
     */
    private static Rectangle getChildrenBounds(EditPart editPart) {
        Rectangle r = new Rectangle();

        List children = editPart.getChildren();
        boolean isFirst = true;
        for (Object object : children) {
            GraphicalEditPart child = (GraphicalEditPart) object;
            if (!isFirst) {
                r.union(child.getFigure().getBounds());
            } else {
                r = child.getFigure().getBounds().getCopy();
                isFirst = false;
            }
        }

        return r;
    }

    @Override
    /**
     * @notgenerated
     */
    public EditPolicy getPrimaryDragEditPolicy() {
        return new ResizableSubProcessEditPolicy();
    }

    @Override
    /**
     * @notgenerated
     */
    protected void refreshBounds() {
        super.refreshBounds();
        refreshChildrenLocation();
    }

    /**
     * Moves body comparment's children in case they exceed the bounds of the
     * compartment
     * 
     * @notgenerated
     */
    private void refreshChildrenLocation() {

        List children = getChildren();
        GraphicalEditPart bodyEditPart = null;
        GraphicalEditPart borderEditPart = null;
        GraphicalEditPart nameEditPart = null;
        for (Object object : children) {
            GraphicalEditPart editPart = (GraphicalEditPart) object;
            if (editPart instanceof SubProcessSubProcessBodyCompartmentEditPart) {
                bodyEditPart = editPart;
            } else if (editPart instanceof SubProcessSubProcessBorderCompartmentEditPart) {
                borderEditPart = editPart;
            } else if (editPart instanceof SubProcessNameEditPart) {
                nameEditPart = editPart;
            }
        }

        if (bodyEditPart != null) {
            boolean isCollapsed = ((Boolean) bodyEditPart
                    .getStructuralFeatureValue(NotationPackage.eINSTANCE
                            .getDrawerStyle_Collapsed())).booleanValue();
            if (!isCollapsed) {
                // move children only in expanded state
                int width = ((Integer) getStructuralFeatureValue(NotationPackage.eINSTANCE
                        .getSize_Width())).intValue();
                int totalHeight = ((Integer) getStructuralFeatureValue(NotationPackage.eINSTANCE
                        .getSize_Height())).intValue();

                int borderCompartmentHeight = borderEditPart.getFigure()
                        .getSize().height;
                int labelHeight = nameEditPart.getFigure().getSize().height;
                int height = totalHeight - borderCompartmentHeight
                        - labelHeight;

                Point moveDelta = new Point(0, 0);

                bodyEditPart
                        .getStructuralFeatureValue(NotationPackage.eINSTANCE
                                .getSize_Width());
                Rectangle rect = getChildrenBounds(bodyEditPart);

                if (rect.x + rect.width > width - INSETS.getWidth()) {
                    moveDelta.x = rect.x + rect.width
                            - (width - INSETS.getWidth());
                }
                if (rect.y + rect.height > height - INSETS.getHeight()) {
                    moveDelta.y = rect.y + rect.height
                            - (height - INSETS.getHeight());
                }

                children = bodyEditPart.getChildren();
                if ((moveDelta.x + moveDelta.y) > 0 && children.size() > 0) {
                    CompositeCommand command = new CompositeCommand(
                            "Move subprocess' children");
                    for (int i = 0; i < children.size(); i++) {
                        GraphicalEditPart part = (GraphicalEditPart) children
                                .get(i);
                        Rectangle bound = part.getFigure().getBounds();
                        Point loc = new Point(bound.x - moveDelta.x, bound.y
                                - moveDelta.y);
                        Rectangle newBounds = new Rectangle(loc, bound
                                .getSize());
                        SetBoundsCommand cmd = new SetBoundsCommand(
                                getEditingDomain(), "Set bounds", part,
                                newBounds);
                        command.add(cmd);
                    }

                    try {
                        command.execute(new NullProgressMonitor(), null);
                    } catch (ExecutionException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * Returns bounds of figure without border figure included in absolute
     * coordinates .
     * 
     * @notgenerated
     * @return
     */
    public Rectangle getAbsBoundsWithoutBorder() {
        boolean hasChildren = false;
        for (Object child : children) {
            if (child instanceof SubProcessSubProcessBorderCompartmentEditPart) {
                hasChildren = ((EditPart) child).getChildren().size() > 0;
                break;
            }
        }
        SubProcessFigure figure = getPrimaryShape();
        Rectangle theBounds = figure.getVisibleBounds();
        if (hasChildren) {
            int delta = SubProcessBorderFigure.getFixedHeightDP(figure) / 2;
            theBounds.height = theBounds.height - delta;
        }
        figure.translateToAbsolute(theBounds);
        return theBounds;
    }

    /**
     * Calculates bounds of collapse handle in absolute coordinates.
     * 
     * @notgenerated
     * @return
     */
    public Rectangle getAbsCollapseHandleBounds() {
        double size = 20.0;
        RootEditPart rootEditPart = getRoot();
        if (rootEditPart instanceof ScalableFreeformRootEditPart) {
            double zoom = ((ScalableFreeformRootEditPart) rootEditPart)
                    .getZoomManager().getZoom();
            size = size * zoom;
        }

        Rectangle theBounds = getAbsBoundsWithoutBorder();
        PrecisionRectangle handleBounds = new PrecisionRectangle(theBounds);

        handleBounds.setX(handleBounds.x + (handleBounds.width - size) / 2);
        handleBounds.setY(handleBounds.y + handleBounds.height - size);
        handleBounds.setWidth(size);
        handleBounds.setHeight(size);

        SubProcess model = (SubProcess) ((View) getModel()).getElement();
        if (model.isLooping()) {
            handleBounds.setX(handleBounds.x + size / 2 + 1);
        }
        return handleBounds;
    }

    /**
     * @notgenerated
     */
    @Override
    protected void handleNotificationEvent(Notification notification) {
        if (notification.getEventType() == Notification.SET) {
            if (BpmnPackage.eINSTANCE.getActivity_Looping().equals(
                    notification.getFeature())) {
                figure.repaint();
            }
        }
        super.handleNotificationEvent(notification);
    }

    /**
     * @notgenerated
     */
    public boolean isNeedToUpdateContainer() {
        return needToUpdateContainer;
    }

    /**
     * @notgenerated
     */
    public void setNeedToUpdateContainer(boolean isChildAdded) {
        this.needToUpdateContainer = isChildAdded;
    }

    /**
     * @notgenerated
     */
    @Override
    public DragTracker getDragTracker(Request request) {
        return new TaskDragEditPartsTrackerEx(this);
    }

    /**
     * @notgenerated
     */
    @Override
    public Command getCommand(Request _request) {
        if (_request instanceof CreateUnspecifiedTypeConnectionRequest) {
            Object model = ((CreateUnspecifiedTypeConnectionRequest) _request)
                    .getTargetEditPart().getModel();

            if ((model instanceof Node)) {
                List elTypes = ((CreateUnspecifiedTypeConnectionRequest) _request)
                        .getElementTypes();
                MetamodelType connType = (MetamodelType) BpmnElementTypes.MessagingEdge_3002;

                for (int i = 0; i < elTypes.size(); i++) {
                    if (elTypes.get(i) instanceof MetamodelType) {
                        connType = (MetamodelType) elTypes.get(i);
                        break;
                    }
                }

                if (connType == BpmnElementTypes.MessagingEdge_3002) {
                    // message connections for subprocess are not allowed.
                    return null;
                } else if (connType == BpmnElementTypes.SequenceEdge_3001) {
                    // validate edge connection.
                    EdgeConnectionValidator edgeValidator = new EdgeConnectionValidator();

                    if (_request instanceof CreateUnspecifiedTypeConnectionRequest) {
                        CreateUnspecifiedTypeConnectionRequest request = (CreateUnspecifiedTypeConnectionRequest) _request;

                        if (request.isDirectionReversed()) {
                            // do nothing
                        } else {
                            if ((request.getSourceEditPart() != null)
                                    && (request.getTargetEditPart() != null)) {
                                for (Iterator iter = request.getAllRequests()
                                        .iterator(); iter.hasNext();) {
                                    CreateConnectionRequest connectionRequest = (CreateConnectionRequest) iter
                                            .next();
                                    if (connectionRequest.getSourceEditPart() != null
                                            && connectionRequest
                                                    .getTargetEditPart() != null
                                            && connectionRequest
                                                    .getSourceEditPart() == request
                                                    .getSourceEditPart()
                                            && connectionRequest
                                                    .getTargetEditPart() == request
                                                    .getTargetEditPart()) {
                                        if (!edgeValidator.canConnect(request
                                                .getSourceEditPart(), request
                                                .getTargetEditPart())) {
                                            return null;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return super.getCommand(_request);
    }
}
