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.ArrayList;
024: import java.util.List;
025: import org.netbeans.modules.bpel.design.geometry.FBounds;
026:
027: import org.netbeans.modules.bpel.model.api.OnAlarmPick;
028: import org.netbeans.modules.bpel.model.api.OnMessage;
029: import org.netbeans.modules.bpel.model.api.Pick;
030: import org.netbeans.modules.bpel.design.geometry.FDimension;
031: import org.netbeans.modules.bpel.design.geometry.FRectangle;
032: import org.netbeans.modules.bpel.design.layout.LayoutManager;
033: import org.netbeans.modules.bpel.design.model.DiagramModel;
034: import org.netbeans.modules.bpel.design.model.connections.Connection;
035: import org.netbeans.modules.bpel.design.model.connections.ConnectionManager;
036: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
037: import org.netbeans.modules.bpel.design.model.elements.GroupBorder;
038: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
039: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
040:
041: public class PickPattern extends CompositePattern {
042:
043: private VisualElement forkGateway;
044: private VisualElement mergeGateway;
045: private List<Connection> onAlarmConnectionsStart = new ArrayList<Connection>();
046: private List<Connection> onAlarmConnectionsEnd = new ArrayList<Connection>();
047:
048: private List<Connection> onMessageConnectionsStart = new ArrayList<Connection>();
049: private List<Connection> onMessageConnectionsEnd = new ArrayList<Connection>();
050:
051: public PickPattern(DiagramModel model) {
052: super (model);
053: }
054:
055: public VisualElement getFirstElement() {
056: return forkGateway;
057: }
058:
059: public VisualElement getLastElement() {
060: return mergeGateway;
061: }
062:
063: protected void createElementsImpl() {
064: setBorder(new GroupBorder());
065: registerTextElement(getBorder());
066:
067: forkGateway = ContentElement.createPickGateway();
068: forkGateway.setLabelText(""); // NOI18N
069: mergeGateway = ContentElement.createPickGateway();
070: mergeGateway.setLabelText(""); // NOI18N
071:
072: appendElement(forkGateway);
073: appendElement(mergeGateway);
074:
075: OnMessage[] onMessages = ((Pick) getOMReference())
076: .getOnMessages();
077: for (OnMessage onMessage : onMessages) {
078: Pattern p = getModel().createPattern(onMessage);
079: p.setParent(this );
080: }
081:
082: OnAlarmPick[] onAlarms = ((Pick) getOMReference())
083: .getOnAlarms();
084: for (OnAlarmPick onAlarm : onAlarms) {
085: Pattern p = getModel().createPattern(onAlarm);
086: p.setParent(this );
087: }
088: }
089:
090: public FBounds layoutPattern(LayoutManager manager) {
091:
092: List<Pattern> patterns = new ArrayList<Pattern>();
093: OnMessage[] onMessages = ((Pick) getOMReference())
094: .getOnMessages();
095: for (OnMessage onMessage : onMessages) {
096: patterns.add(getNestedPattern(onMessage));
097: }
098:
099: OnAlarmPick[] onAlarms = ((Pick) getOMReference())
100: .getOnAlarms();
101: for (OnAlarmPick onAlarm : onAlarms) {
102: patterns.add(getNestedPattern(onAlarm));
103: }
104:
105: float width = 0;
106: float height = 24f;
107:
108: for (Pattern p : patterns) {
109: FDimension pSize = p.getBounds().getSize();
110: height = Math.max(height, pSize.height);
111: width += pSize.width;
112: }
113:
114: width += LayoutManager.VSPACING * (patterns.size() - 1);
115: height += LayoutManager.HSPACING * 2 + forkGateway.getHeight()
116: + mergeGateway.getWidth();
117:
118: width = Math.max(width, 40f);
119:
120: forkGateway.setCenter(0, -height / 2 + forkGateway.getHeight()
121: / 2);
122:
123: mergeGateway.setCenter(0, height / 2 - mergeGateway.getHeight()
124: / 2);
125:
126: float x = -width / 2;
127:
128: for (Pattern p : patterns) {
129: FDimension pSize = p.getBounds().getSize();
130: manager.setPatternPosition(p, x, -pSize.height / 2);
131: x += pSize.width + LayoutManager.VSPACING;
132: }
133:
134: getBorder().setClientRectangle(-width / 2, -height / 2, width,
135: height);
136: return getBorder().getBounds();
137:
138: }
139:
140: public String getDefaultName() {
141: return "Pick";
142: }
143:
144: public NodeType getNodeType() {
145: return NodeType.PICK;
146: }
147:
148: protected void onAppendPattern(Pattern nestedPattern) {
149: }
150:
151: protected void onRemovePattern(Pattern p) {
152:
153: }
154:
155: public void reconnectElements() {
156: Pick pick = (Pick) getOMReference();
157:
158: OnAlarmPick[] onAlarms = pick.getOnAlarms();
159: OnMessage[] onMessages = pick.getOnMessages();
160:
161: ensureConnectionsCount(onAlarmConnectionsStart, onAlarms.length);
162: ensureConnectionsCount(onAlarmConnectionsEnd, onAlarms.length);
163:
164: ensureConnectionsCount(onMessageConnectionsStart,
165: onMessages.length);
166: ensureConnectionsCount(onMessageConnectionsEnd,
167: onMessages.length);
168:
169: double x11 = forkGateway.getX();
170: double x12 = x11 + forkGateway.getWidth();
171:
172: double x21 = mergeGateway.getX();
173: double x22 = x21 + mergeGateway.getY();
174:
175: for (int i = 0; i < onAlarms.length; i++) {
176: Pattern p = getNestedPattern(onAlarms[i]);
177:
178: Connection cs = onAlarmConnectionsStart.get(i);
179: Connection ce = onAlarmConnectionsEnd.get(i);
180:
181: ConnectionManager.connectVerticaly(forkGateway, cs, p, ce,
182: mergeGateway);
183: }
184:
185: for (int i = 0; i < onMessages.length; i++) {
186: Pattern p = getNestedPattern(onMessages[i]);
187:
188: Connection cs = onMessageConnectionsStart.get(i);
189: Connection ce = onMessageConnectionsEnd.get(i);
190:
191: ConnectionManager.connectVerticaly(forkGateway, cs, p, ce,
192: mergeGateway);
193: }
194: }
195:
196: public Area createSelection() {
197: Area res = new Area(getBorder().getShape());
198: res.subtract(new Area(forkGateway.getShape()));
199: res.subtract(new Area(mergeGateway.getShape()));
200: return res;
201: }
202:
203: }
|