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.Collection;
025: import java.util.List;
026: import org.netbeans.modules.bpel.design.GUtils;
027: import org.netbeans.modules.bpel.design.geometry.FBounds;
028: import org.netbeans.modules.bpel.model.api.Activity;
029: import org.netbeans.modules.bpel.model.api.BpelEntity;
030: import org.netbeans.modules.bpel.model.api.CompensationHandler;
031: import org.netbeans.modules.bpel.model.api.CompensationHandlerHolder;
032: import org.netbeans.modules.bpel.model.api.EventHandlers;
033: import org.netbeans.modules.bpel.model.api.FaultHandlers;
034: import org.netbeans.modules.bpel.model.api.Scope;
035: import org.netbeans.modules.bpel.design.geometry.FDimension;
036: import org.netbeans.modules.bpel.design.geometry.FPoint;
037: import org.netbeans.modules.bpel.design.geometry.FRectangle;
038: import org.netbeans.modules.bpel.design.layout.LayoutManager;
039: import org.netbeans.modules.bpel.design.model.DiagramModel;
040: import org.netbeans.modules.bpel.design.model.connections.Connection;
041: import org.netbeans.modules.bpel.design.model.connections.Direction;
042: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
043: import org.netbeans.modules.bpel.design.model.elements.PlaceHolderElement;
044: import org.netbeans.modules.bpel.design.model.elements.SubprocessBorder;
045: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
046: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
047: import org.netbeans.modules.bpel.design.selection.PlaceHolder;
048: import org.netbeans.modules.bpel.model.api.TerminationHandler;
049:
050: public class ScopePattern extends CompositePattern {
051:
052: private PlaceHolderElement placeHolder;
053: private VisualElement startEvent;
054: private VisualElement endEvent;
055:
056: private VisualElement compensateBadge;
057: private VisualElement eventsBadge;
058: private VisualElement faultBadge;
059: private VisualElement terminationBadge;
060:
061: private Connection startConnection;
062: private Connection endConnection;
063:
064: private Connection eventsConnection;
065: private Connection faultConnection;
066: private Connection compConnection;
067: private Connection terminationConnection;
068:
069: public ScopePattern(DiagramModel model) {
070: super (model);
071: startConnection = new Connection(this );
072: endConnection = new Connection(this );
073: }
074:
075: public VisualElement getFirstElement() {
076: return getBorder();
077: }
078:
079: public VisualElement getLastElement() {
080: return getBorder();
081: }
082:
083: public Pattern getActivityPattern() {
084: BpelEntity activity = ((Scope) getOMReference()).getActivity();
085: if (activity == null) {
086: return null;
087: }
088: return getNestedPattern(activity);
089: }
090:
091: public Pattern getCompensationHandlerPattern() {
092: CompensationHandler compHandler = ((CompensationHandlerHolder) getOMReference())
093: .getCompensationHandler();
094:
095: return (compHandler != null) ? getNestedPattern(compHandler)
096: : null;
097: }
098:
099: public Pattern getEventHandlersPattern() {
100: EventHandlers eventHandlers = ((Scope) getOMReference())
101: .getEventHandlers();
102:
103: return (eventHandlers != null) ? getNestedPattern(eventHandlers)
104: : null;
105: }
106:
107: public Pattern getFaultHandlersPattern() {
108: FaultHandlers faultHandlers = ((Scope) getOMReference())
109: .getFaultHandlers();
110:
111: return (faultHandlers != null) ? getNestedPattern(faultHandlers)
112: : null;
113: }
114:
115: public Pattern getTerminationHandlerPattern() {
116: TerminationHandler terminationHandler = ((Scope) getOMReference())
117: .getTerminationHandler();
118:
119: return (terminationHandler != null) ? getNestedPattern(terminationHandler)
120: : null;
121: }
122:
123: protected void createElementsImpl() {
124: setBorder(new SubprocessBorder());
125: getBorder().setLabelText(getDefaultName());
126: registerTextElement(getBorder());
127:
128: startEvent = ContentElement.createStartEvent();
129: startEvent.setLabelText(""); // NOI18N
130:
131: endEvent = ContentElement.createEndEvent();
132: endEvent.setLabelText(""); // NOI18N
133:
134: placeHolder = new PlaceHolderElement();
135:
136: compensateBadge = ContentElement.createCompensateBadge();
137: eventsBadge = ContentElement.createEventBadge();
138: faultBadge = ContentElement.createFaultBadge();
139: terminationBadge = ContentElement.createTerminationBadge();
140:
141: appendElement(placeHolder);
142: appendElement(startEvent);
143: appendElement(endEvent);
144:
145: Scope scopeOM = (Scope) getOMReference();
146:
147: Activity activity = (Activity) scopeOM.getActivity();
148:
149: if (activity != null) {
150: Pattern p = getModel().createPattern(activity);
151: p.setParent(this );
152: }
153:
154: CompensationHandler compHandler = scopeOM
155: .getCompensationHandler();
156: if (compHandler != null) {
157: Pattern p = getModel().createPattern(compHandler);
158: p.setParent(this );
159: }
160:
161: EventHandlers eventHandlers = scopeOM.getEventHandlers();
162: if (eventHandlers != null) {
163: Pattern p = getModel().createPattern(eventHandlers);
164: p.setParent(this );
165: }
166:
167: FaultHandlers faultHandlers = scopeOM.getFaultHandlers();
168: if (faultHandlers != null) {
169: Pattern p = getModel().createPattern(faultHandlers);
170: p.setParent(this );
171: }
172:
173: TerminationHandler terminationHandler = scopeOM
174: .getTerminationHandler();
175: if (terminationHandler != null) {
176: Pattern p = getModel().createPattern(terminationHandler);
177: p.setParent(this );
178: }
179: }
180:
181: public void onAppendPattern(Pattern p) {
182: BpelEntity entity = p.getOMReference();
183:
184: if (entity instanceof CompensationHandler) {
185: appendElement(compensateBadge);
186: } else if (entity instanceof EventHandlers) {
187: appendElement(eventsBadge);
188: } else if (entity instanceof FaultHandlers) {
189: appendElement(faultBadge);
190: } else if (entity instanceof TerminationHandler) {
191: appendElement(terminationBadge);
192: } else {
193: removeElement(placeHolder);
194: }
195: }
196:
197: public void onRemovePattern(Pattern p) {
198: Scope scope = (Scope) getOMReference();
199:
200: if (compensateBadge.hasPattern()
201: && (scope.getCompensationHandler() == null)) {
202: removeElement(compensateBadge);
203: } else if (eventsBadge.hasPattern()
204: && (scope.getEventHandlers() == null)) {
205: removeElement(eventsBadge);
206: } else if (faultBadge.hasPattern()
207: && (scope.getFaultHandlers() == null)) {
208: removeElement(faultBadge);
209: } else if (terminationBadge.hasPattern()
210: && (scope.getTerminationHandler() == null)) {
211: removeElement(terminationBadge);
212: } else {
213: appendElement(placeHolder);
214: }
215: }
216:
217: public FBounds layoutPattern(LayoutManager manager) {
218: List<FBounds> bounds = new ArrayList<FBounds>();
219: Pattern activityPattern = getActivityPattern();
220:
221: if (activityPattern == null) {
222: placeHolder.setCenter(0, 0);
223: bounds.add(placeHolder.getBounds());
224: } else {
225: FPoint origin = manager.getOriginOffset(activityPattern);
226:
227: bounds.add(manager.setPatternPosition(activityPattern,
228: -origin.x, 0));
229: }
230:
231: double sHeight = startEvent.getHeight();
232: double eHeight = endEvent.getHeight();
233:
234: FBounds nested = new FBounds(bounds);
235: startEvent.setCenter(0, nested.y - LayoutManager.VSPACING
236: - sHeight / 2);
237: bounds.add(startEvent.getBounds());
238:
239: endEvent.setCenter(0, nested.height + LayoutManager.VSPACING
240: + eHeight / 2);
241: bounds.add(endEvent.getBounds());
242:
243: getBorder().setClientRectangle(new FBounds(bounds));
244:
245: FBounds borderRect = getBorder().getBounds();
246:
247: setOrigin(borderRect.getCenter().x, 0);
248:
249: Pattern eventHandlers = getEventHandlersPattern();
250: Pattern compHandler = getCompensationHandlerPattern();
251: Pattern terminationHandler = getTerminationHandlerPattern();
252: Pattern faultHandlers = getFaultHandlersPattern();
253:
254: int rightHandlerCount = 0;
255:
256: if (eventHandlers != null) {
257: rightHandlerCount++;
258: }
259: if (compHandler != null) {
260: rightHandlerCount++;
261: }
262: if (terminationHandler != null) {
263: rightHandlerCount++;
264: }
265: if (faultHandlers != null) {
266: rightHandlerCount++;
267: }
268:
269: double ry = borderRect.y + getBorder().getInsets().top + 8;
270: double rx = borderRect.x + borderRect.width
271: + LayoutManager.HSPACING;
272: double lx = borderRect.x - LayoutManager.HSPACING;
273:
274: double hy = ry + LayoutManager.VSPACING * rightHandlerCount;
275:
276: double hHeight = 0;
277:
278: int handlerNumber = 1;
279:
280: if (eventHandlers != null) {
281: eventsBadge.setCenter(borderRect.x + borderRect.width, ry
282: + (rightHandlerCount - handlerNumber)
283: * LayoutManager.VSPACING);
284:
285: manager.setPatternPosition(eventHandlers, rx, hy);
286:
287: FBounds hSize = eventHandlers.getBounds();
288:
289: rx += hSize.width + LayoutManager.HSPACING;
290: hHeight = Math.max(hHeight, hSize.height);
291:
292: handlerNumber++;
293: }
294:
295: if (compHandler != null) {
296: compensateBadge.setCenter(borderRect.x + borderRect.width,
297: ry + (rightHandlerCount - handlerNumber)
298: * LayoutManager.VSPACING);
299:
300: manager.setPatternPosition(compHandler, rx, hy);
301:
302: FBounds hSize = compHandler.getBounds();
303:
304: rx += hSize.width + LayoutManager.HSPACING;
305: hHeight = Math.max(hHeight, hSize.height);
306:
307: handlerNumber++;
308: }
309:
310: if (terminationHandler != null) {
311: terminationBadge.setCenter(borderRect.x + borderRect.width,
312: ry + (rightHandlerCount - handlerNumber)
313: * LayoutManager.VSPACING);
314:
315: manager.setPatternPosition(terminationHandler, rx, hy);
316:
317: FBounds hSize = terminationHandler.getBounds();
318:
319: rx += hSize.width + LayoutManager.HSPACING;
320: hHeight = Math.max(hHeight, hSize.height);
321:
322: handlerNumber++;
323: }
324:
325: if (faultHandlers != null) {
326: faultBadge.setCenter(borderRect.x + borderRect.width, ry
327: + (rightHandlerCount - handlerNumber)
328: * LayoutManager.VSPACING);
329:
330: manager.setPatternPosition(faultHandlers, rx, hy);
331:
332: FBounds hSize = faultHandlers.getBounds();
333:
334: rx += hSize.width + LayoutManager.HSPACING;
335: hHeight = Math.max(hHeight, hSize.height);
336: }
337:
338: // height = Math.max(height, hHeight + hy - y - getBorder().getInsets().top);
339: //
340: // manager.setBorderBounds(getBorder(), x, y, width, height);
341:
342: return null; //LM will caculate pattern bounds by itself.
343: }
344:
345: public void createPlaceholders(Pattern draggedPattern,
346: Collection<PlaceHolder> placeHolders) {
347: if (draggedPattern == this )
348: return;
349: if (isNestedIn(draggedPattern))
350: return;
351: if (!(draggedPattern.getOMReference() instanceof Activity))
352: return;
353:
354: if (placeHolder.getPattern() != null) {
355: placeHolders.add(new InnerPlaceHolder(draggedPattern));
356: }
357: }
358:
359: public String getDefaultName() {
360: return "Scope"; // NOI18N
361: }
362:
363: public void reconnectElements() {
364: Pattern p = getActivityPattern();
365: if (p != null) {
366: startConnection.connect(startEvent, Direction.BOTTOM, p
367: .getFirstElement(), Direction.TOP);
368:
369: endConnection.connect(p.getLastElement(), Direction.BOTTOM,
370: endEvent, Direction.TOP);
371: } else {
372: startConnection.connect(startEvent, Direction.BOTTOM,
373: placeHolder, Direction.TOP);
374: endConnection.connect(placeHolder, Direction.BOTTOM,
375: endEvent, Direction.TOP);
376: }
377:
378: Pattern compHandler = getCompensationHandlerPattern();
379:
380: if (compHandler != null) {
381: if (compConnection == null) {
382: compConnection = new Connection(this );
383: }
384: compConnection.connect(compensateBadge, Direction.RIGHT,
385: compHandler.getFirstElement(), Direction.TOP);
386: } else {
387: if (compConnection != null) {
388: compConnection.remove();
389: compConnection = null;
390: }
391: }
392:
393: Pattern terminationHandler = getTerminationHandlerPattern();
394:
395: if (terminationHandler != null) {
396: if (terminationConnection == null) {
397: terminationConnection = new Connection(this );
398: }
399: terminationConnection.connect(terminationBadge,
400: Direction.RIGHT, terminationHandler
401: .getFirstElement(), Direction.TOP);
402: } else {
403: if (terminationConnection != null) {
404: terminationConnection.remove();
405: terminationConnection = null;
406: }
407: }
408:
409: Pattern eventHandlres = getEventHandlersPattern();
410:
411: if (eventHandlres != null) {
412: if (eventsConnection == null) {
413: eventsConnection = new Connection(this );
414: }
415: eventsConnection.connect(eventsBadge, Direction.RIGHT,
416: eventHandlres.getFirstElement(), Direction.TOP);
417: } else {
418: if (eventsConnection != null) {
419: eventsConnection.remove();
420: eventsConnection = null;
421: }
422: }
423:
424: Pattern faultHandlers = getFaultHandlersPattern();
425:
426: if (faultHandlers != null) {
427: if (faultConnection == null) {
428: faultConnection = new Connection(this );
429: }
430: faultConnection.connect(faultBadge, Direction.RIGHT,
431: faultHandlers.getFirstElement(), Direction.TOP);
432: } else {
433: if (faultConnection != null) {
434: faultConnection.remove();
435: faultConnection = null;
436: }
437: }
438: }
439:
440: public NodeType getNodeType() {
441: return NodeType.SCOPE;
442: }
443:
444: public Area createSelection() {
445: Area a = new Area(getBorder().getShape());
446: if (compensateBadge.getPattern() != null) {
447: a.add(new Area(compensateBadge.getShape()));
448: }
449: if (eventsBadge.getPattern() != null) {
450: a.add(new Area(eventsBadge.getShape()));
451: }
452: if (faultBadge.getPattern() != null) {
453: a.add(new Area(faultBadge.getShape()));
454: }
455: if (terminationBadge.getPattern() != null) {
456: a.add(new Area(terminationBadge.getShape()));
457: }
458:
459: a.subtract(new Area(startEvent.getShape()));
460: a.subtract(new Area(endEvent.getShape()));
461:
462: return a;
463: }
464:
465: class InnerPlaceHolder extends PlaceHolder {
466: public InnerPlaceHolder(Pattern draggedPattern) {
467: super (ScopePattern.this , draggedPattern, placeHolder
468: .getCenterX(), placeHolder.getCenterY());
469: }
470:
471: public void drop() {
472: ((Scope) getOMReference())
473: .setActivity((Activity) getDraggedPattern()
474: .getOMReference());
475: }
476: }
477: }
|