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 org.netbeans.modules.bpel.design.geometry.FBounds;
024: import org.netbeans.modules.bpel.design.geometry.FDimension;
025: import org.netbeans.modules.bpel.design.geometry.FInsets;
026: import org.netbeans.modules.bpel.design.geometry.FRectangle;
027: import org.netbeans.modules.bpel.design.layout.LayoutManager;
028: import org.netbeans.modules.bpel.design.model.DiagramModel;
029: import org.netbeans.modules.bpel.design.model.PartnerLinkHelper;
030: import org.netbeans.modules.bpel.design.model.connections.Connection;
031: import org.netbeans.modules.bpel.design.model.connections.Direction;
032: import org.netbeans.modules.bpel.design.model.elements.BorderElement;
033: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
034: import org.netbeans.modules.bpel.design.model.elements.GroupBorder;
035: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
036: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
037: import org.netbeans.modules.bpel.model.api.BpelEntity;
038: import org.netbeans.modules.bpel.model.api.Catch;
039: import org.netbeans.modules.bpel.model.api.CatchAll;
040: import org.netbeans.modules.bpel.model.api.CompensationHandler;
041: import org.netbeans.modules.bpel.model.api.Invoke;
042:
043: /**
044: *
045: * @author Alexey Yarmolenko
046: */
047: public class InvokePattern extends CompositePattern {
048:
049: private VisualElement invokeElement;
050:
051: private VisualElement errorBadge;
052: private VisualElement compensationBadge;
053:
054: private BorderElement borderElement;
055:
056: public InvokePattern(DiagramModel model) {
057: super (model);
058: }
059:
060: protected void onAppendPattern(Pattern p) {
061: BpelEntity entity = p.getOMReference();
062:
063: if ((entity instanceof Catch) || (entity instanceof CatchAll)) {
064: if (!errorBadge.hasPattern()) {
065: appendElement(borderElement);
066: appendElement(errorBadge);
067: }
068: } else if (entity instanceof CompensationHandler) {
069: appendElement(compensationBadge);
070: }
071: }
072:
073: protected void onRemovePattern(Pattern p) {
074: Invoke invoke = (Invoke) getOMReference();
075:
076: if (errorBadge.hasPattern() && (invoke.sizeOfCathes() == 0)
077: && (invoke.getCatchAll() == null)) {
078: removeElement(borderElement);
079: removeElement(errorBadge);
080: } else if (compensationBadge.hasPattern()
081: && (invoke.getCompensationHandler() == null)) {
082: removeElement(compensationBadge);
083: }
084: }
085:
086: protected void createElementsImpl() {
087: invokeElement = ContentElement.createInvoke();
088: appendElement(invokeElement);
089: registerTextElement(invokeElement);
090:
091: errorBadge = ContentElement.createFaultBadge();
092: compensationBadge = ContentElement.createCompensateBadge();
093: borderElement = new GroupBorder();
094:
095: Invoke invoke = (Invoke) getOMReference();
096:
097: int catchCount = invoke.sizeOfCathes();
098: for (int i = 0; i < catchCount; i++) {
099: Catch c = invoke.getCatch(i);
100: Pattern p = getModel().createPattern(c);
101: p.setParent(this );
102: }
103:
104: CatchAll catchAll = invoke.getCatchAll();
105: if (catchAll != null) {
106: Pattern p = getModel().createPattern(catchAll);
107: p.setParent(this );
108: }
109:
110: CompensationHandler compensation = invoke
111: .getCompensationHandler();
112: if (compensation != null) {
113: Pattern p = getModel().createPattern(compensation);
114: p.setParent(this );
115: }
116: }
117:
118: public String getDefaultName() {
119: return "Invoke"; // NOI18N
120: }
121:
122: public NodeType getNodeType() {
123: return NodeType.INVOKE;
124: }
125:
126: public void reconnectElements() {
127: clearConnections();
128: new PartnerLinkHelper(getModel()).updateMessageFlowLinks(this );
129:
130: if (errorBadge.getPattern() != null) {
131: new Connection(this ).connect(errorBadge, Direction.RIGHT,
132: borderElement, Direction.TOP);
133: }
134:
135: if (compensationBadge.getPattern() != null) {
136: Invoke invoke = (Invoke) getOMReference();
137: CompensationHandler handler = invoke
138: .getCompensationHandler();
139: new Connection(this ).connect(compensationBadge,
140: Direction.RIGHT, getNestedPattern(handler)
141: .getFirstElement(), Direction.TOP);
142: }
143: }
144:
145: public VisualElement getFirstElement() {
146: return invokeElement;
147: }
148:
149: public VisualElement getLastElement() {
150: return invokeElement;
151: }
152:
153: public FBounds layoutPattern(LayoutManager manager) {
154:
155: double xmin = -invokeElement.getWidth()
156: - LayoutManager.HSPACING;
157: double ymin = -invokeElement.getHeight();
158:
159: double xmax = xmin + invokeElement.getWidth();
160: double ymax = ymin + invokeElement.getHeight();
161:
162: double xcenter = (xmin + xmax) / 2;
163:
164: double rightSideX = xmax;
165: double rightSideY0 = ymin;
166: double rightSideY1 = ymax;
167:
168: invokeElement.setLocation(xmin, ymin);
169: setOrigin((xmin + xmax) / 2, (ymin + ymax) / 2);
170:
171: Invoke invoke = (Invoke) getOMReference();
172:
173: int catchCount = invoke.sizeOfCathes();
174: CatchAll catchAll = invoke.getCatchAll();
175: CompensationHandler compensation = invoke
176: .getCompensationHandler();
177:
178: double x = 0;
179: double y = 0;
180:
181: if ((catchCount > 0) || (catchAll != null)) {
182: FInsets borderInsets = borderElement.getInsets();
183:
184: double x1 = x + borderInsets.left;
185: double y1 = y + borderInsets.top;
186:
187: double x2 = x1;
188: double y2 = y1;
189:
190: for (int i = 0; i < catchCount; i++) {
191: Pattern p = getNestedPattern(invoke.getCatch(i));
192: FBounds size = p.getBounds();
193: manager.setPatternPosition(p, x2, y1);
194: y2 = Math.max(y2, y1 + size.height);
195: x2 += size.width + LayoutManager.HSPACING;
196: }
197:
198: if (catchAll != null) {
199: Pattern p = getNestedPattern(catchAll);
200: FBounds size = p.getBounds();
201: manager.setPatternPosition(p, x2, y1);
202: y2 = Math.max(y2, y1 + size.height);
203: x2 += size.width;
204: } else {
205: x2 -= LayoutManager.HSPACING;
206: }
207:
208: x1 -= borderInsets.left;
209: y1 -= borderInsets.top;
210:
211: x2 += borderInsets.right;
212: y2 += borderInsets.left;
213:
214: borderElement.setSize(x2 - x1, y2 - y1);
215: borderElement.setLocation(x1, y1);
216:
217: x = x2;
218:
219: xmax = Math.max(xmax, x2);
220: ymax = Math.max(ymax, y2);
221: }
222:
223: if (compensation != null) {
224: if (x > 0) {
225: x += LayoutManager.HSPACING;
226: }
227:
228: Pattern p = getNestedPattern(compensation);
229: FBounds size = p.getBounds();
230:
231: manager.setPatternPosition(p, x, y);
232:
233: xmax = Math.max(xmax, x + size.width);
234: ymax = Math.max(ymax, y + size.height);
235: }
236:
237: //layout badges
238: if ((catchCount > 0) || (catchAll != null)) {
239: if (compensation != null) {
240: double y1 = 0.75 * rightSideY0 + 0.25 * rightSideY1;
241: double y2 = 0.25 * rightSideY0 + 0.75 * rightSideY1;
242:
243: compensationBadge.setCenter(rightSideX, y1);
244: errorBadge.setCenter(rightSideX, y2);
245: } else {
246: double yPos = 0.5 * (rightSideY0 + rightSideY1);
247: double xPos = rightSideX;
248: errorBadge.setCenter(xPos, yPos);
249: }
250: } else if (compensation != null) {
251: double yPos = 0.5f * (rightSideY0 + rightSideY1);
252: double xPos = rightSideX;
253:
254: compensationBadge.setCenter(xPos, yPos);
255: }
256:
257: return new FBounds(xmin, ymin, xmax - xmin, ymax - ymin);
258: }
259:
260: public Area createOutline() {
261: Area res = invokeElement.getShape().createArea();
262:
263: if (compensationBadge.getPattern() != null) {
264: res.add(compensationBadge.getShape().createArea());
265: }
266:
267: if (errorBadge.getPattern() != null) {
268: res.add(errorBadge.getShape().createArea());
269: }
270:
271: return res;
272: }
273:
274: public boolean isCollapsable() {
275: return false;
276: }
277: }
|