001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.design.model.patterns;
021:
022: import java.awt.geom.Area;
023: import java.util.Collection;
024: import org.netbeans.modules.bpel.design.geometry.FBounds;
025: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
026:
027: import org.netbeans.modules.bpel.model.api.Activity;
028: import org.netbeans.modules.bpel.model.api.While;
029: import org.netbeans.modules.bpel.design.geometry.FDimension;
030: import org.netbeans.modules.bpel.design.geometry.FRectangle;
031: import org.netbeans.modules.bpel.design.layout.LayoutManager;
032: import org.netbeans.modules.bpel.design.model.DiagramModel;
033: import org.netbeans.modules.bpel.design.model.connections.Connection;
034: import org.netbeans.modules.bpel.design.model.connections.Direction;
035: import org.netbeans.modules.bpel.design.model.elements.PlaceHolderElement;
036: import org.netbeans.modules.bpel.design.model.elements.SubprocessBorder;
037: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
038: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
039: import org.netbeans.modules.bpel.design.selection.PlaceHolder;
040:
041: public class WhilePattern extends CompositePattern {
042:
043: private VisualElement gateway;
044: private PlaceHolderElement placeHolder;
045: private Connection connection1;
046: private Connection connection2;
047:
048: public WhilePattern(DiagramModel model) {
049: super (model);
050: }
051:
052: public VisualElement getFirstElement() {
053: return getBorder();
054: }
055:
056: public VisualElement getLastElement() {
057: return getBorder();
058: }
059:
060: protected void createElementsImpl() {
061: connection1 = new Connection(this );
062: connection2 = new Connection(this );
063:
064: setBorder(new SubprocessBorder());
065: registerTextElement(getBorder());
066:
067: gateway = ContentElement.createWhileGateway();
068: appendElement(gateway);
069:
070: placeHolder = new PlaceHolderElement();
071: appendElement(placeHolder);
072:
073: While whileOM = (While) getOMReference();
074:
075: Activity activity = (Activity) whileOM.getActivity();
076:
077: if (activity != null) {
078: Pattern p = getModel().createPattern(activity);
079: p.setParent(this );
080: }
081: }
082:
083: public void createPlaceholders(Pattern draggedPattern,
084: Collection<PlaceHolder> placeHolders) {
085: if (!(draggedPattern.getOMReference() instanceof Activity))
086: return;
087:
088: if (placeHolder.getPattern() != null) {
089: placeHolders.add(new InnerPlaceHolder(draggedPattern));
090: }
091: }
092:
093: public FBounds layoutPattern(LayoutManager manager) {
094: double gx = -gateway.getWidth() - LayoutManager.HSPACING;
095: double gy = -gateway.getHeight() - LayoutManager.VSPACING;
096: gateway.setLocation(gx, gy);
097:
098: double width = -gx;
099: double height = -gy;
100:
101: if (getNestedPatterns().isEmpty()) {
102: placeHolder.setLocation(0, 0);
103:
104: width += placeHolder.getWidth();
105: height += placeHolder.getHeight();
106:
107: } else {
108: Pattern p = getNestedPattern();
109: FDimension pSize = p.getBounds().getSize();
110: manager.setPatternPosition(p, 0, 0);
111:
112: width += pSize.width;
113: height += pSize.height;
114: }
115: setOrigin(gx + width / 2, gy + height / 2);
116:
117: getBorder().setClientRectangle(gx, gy, width, height);
118: return getBorder().getBounds();
119:
120: }
121:
122: public String getDefaultName() {
123: return "While"; // NOI18N
124: }
125:
126: protected void onAppendPattern(Pattern nestedPattern) {
127: removeElement(placeHolder);
128: }
129:
130: protected void onRemovePattern(Pattern nestedPattern) {
131: appendElement(placeHolder);
132: }
133:
134: public void reconnectElements() {
135: Pattern p = getNestedPattern();
136:
137: if (p != null) {
138: connection1.connect(gateway, Direction.RIGHT, p
139: .getFirstElement(), Direction.TOP);
140:
141: connection2.connect(p.getLastElement(), Direction.LEFT,
142: gateway, Direction.BOTTOM);
143: } else {
144: connection1.connect(gateway, Direction.RIGHT, placeHolder,
145: Direction.TOP);
146:
147: connection2.connect(placeHolder, Direction.LEFT, gateway,
148: Direction.BOTTOM);
149: }
150: }
151:
152: public NodeType getNodeType() {
153: return NodeType.WHILE;
154: }
155:
156: public Area createSelection() {
157: Area res = new Area(getBorder().getShape());
158: res.subtract(new Area(gateway.getShape()));
159: return res;
160: }
161:
162: class InnerPlaceHolder extends PlaceHolder {
163: public InnerPlaceHolder(Pattern draggedPattern) {
164: super (WhilePattern.this , draggedPattern, placeHolder
165: .getCenterX(), placeHolder.getCenterY());
166: }
167:
168: public void drop() {
169: Pattern p = getDraggedPattern();
170: ((While) getOMReference()).setActivity((Activity) p
171: .getOMReference());
172: }
173: }
174:
175: }
|