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.selection;
021:
022: import java.util.Collection;
023: import java.util.List;
024: import java.util.concurrent.Callable;
025: import org.netbeans.modules.bpel.design.geometry.FPath;
026: import org.netbeans.modules.bpel.design.geometry.FPoint;
027: import org.netbeans.modules.bpel.design.model.connections.Connection;
028: import org.netbeans.modules.bpel.design.model.connections.MessageConnection;
029: import org.netbeans.modules.bpel.design.model.elements.BorderElement;
030: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
031: import org.netbeans.modules.bpel.design.model.patterns.CollapsedPattern;
032: import org.netbeans.modules.bpel.design.model.patterns.CompositePattern;
033: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
034: import org.netbeans.modules.bpel.model.api.Activity;
035: import org.netbeans.modules.bpel.model.api.ActivityHolder;
036: import org.netbeans.modules.bpel.model.api.BpelEntity;
037: import org.netbeans.modules.bpel.model.api.Sequence;
038:
039: public class ImpliciteSequencePlaceHolder extends PlaceHolder {
040:
041: private boolean last;
042:
043: public ImpliciteSequencePlaceHolder(Pattern ownerPattern,
044: Pattern draggedPattern, float cx, float cy) {
045: this (ownerPattern, draggedPattern, cx, cy, false);
046: }
047:
048: public ImpliciteSequencePlaceHolder(Pattern ownerPattern,
049: Pattern draggedPattern, float cx, float cy, boolean last) {
050: super (ownerPattern, draggedPattern, cx, cy);
051: this .last = last;
052: }
053:
054: public ImpliciteSequencePlaceHolder(Pattern ownerPattern,
055: Pattern draggedPattern, FPath path) {
056: this (ownerPattern, draggedPattern, path, false);
057: }
058:
059: public ImpliciteSequencePlaceHolder(Pattern ownerPattern,
060: Pattern draggedPattern, FPath path, boolean last) {
061: super (ownerPattern, draggedPattern, path);
062: this .last = last;
063: }
064:
065: public void drop() {
066: Sequence s = getOwnerPattern().getBpelModel().getBuilder()
067: .createSequence();
068:
069: ActivityHolder holder = (ActivityHolder) getOwnerPattern()
070: .getOMReference();
071:
072: Activity oldActivity = (Activity) holder.getActivity().cut();
073: Activity newActivity = (Activity) getDraggedPattern()
074: .getOMReference();
075:
076: holder.setActivity(s);
077:
078: if (last) {
079: s.addActivity(oldActivity);
080: s.addActivity(newActivity);
081: } else {
082: s.addActivity(newActivity);
083: s.addActivity(oldActivity);
084: }
085: }
086:
087: public static void create(Pattern draggedPattern,
088: Pattern ownerPattern, Collection<PlaceHolder> placeHolders) {
089: if (!(ownerPattern.getOMReference() instanceof ActivityHolder)) {
090: return;
091: }
092:
093: if (draggedPattern.getParent() == ownerPattern) {
094: return;
095: }
096:
097: ActivityHolder holder = (ActivityHolder) ownerPattern
098: .getOMReference();
099:
100: BpelEntity child = holder.getActivity();
101:
102: if ((child == null) || (child instanceof Sequence)
103: || (ownerPattern instanceof CollapsedPattern)) {
104: return;
105: }
106:
107: Pattern p = ((CompositePattern) ownerPattern)
108: .getNestedPattern(child);
109:
110: Connection c1 = getInboundConnection(p);
111: Connection c2 = getOutboundConnection(p);
112:
113: FPoint center1 = null;
114: FPoint center2 = null;
115:
116: if (c1 != null) {
117: FPath path = c1.getPath();
118: if (p instanceof CompositePattern) {
119: CompositePattern composite = (CompositePattern) p;
120: BorderElement border = composite.getBorder();
121:
122: if (border != null) {
123: path = path.subtract(border.getShape());
124: }
125: }
126: double pathLength = path.length();
127: center1 = path
128: .point((pathLength >= 20) ? ((pathLength - 10) / pathLength)
129: : 0.5f);
130: } else {
131: VisualElement e = null;
132: if (p instanceof CompositePattern) {
133: CompositePattern composite = (CompositePattern) p;
134: e = composite.getBorder();
135: }
136:
137: if (e == null) {
138: e = p.getFirstElement();
139: }
140:
141: center1 = new FPoint(e.getCenterX(), e.getY() - 10);
142: }
143:
144: if (c2 != null) {
145: FPath path = c2.getPath();
146: if (p instanceof CompositePattern) {
147: CompositePattern composite = (CompositePattern) p;
148: BorderElement border = composite.getBorder();
149:
150: if (border != null) {
151: path = path.subtract(border.getShape());
152: }
153: }
154: double pathLength = path.length();
155: center2 = path.point((pathLength >= 20) ? (10 / pathLength)
156: : 0.5f);
157: } else {
158: if (p instanceof CompositePattern) {
159: CompositePattern composite = (CompositePattern) p;
160:
161: VisualElement e = composite.getBorder();
162:
163: if (e == null) {
164: e = p.getLastElement();
165: }
166:
167: center2 = new FPoint(e.getCenterX(), e.getY()
168: + e.getHeight() + 10);
169: } else {
170: VisualElement e = p.getLastElement();
171: center2 = new FPoint(e.getCenterX(), e.getY()
172: + e.getHeight() + 10);
173: }
174: }
175:
176: placeHolders.add(new ImpliciteSequencePlaceHolder(ownerPattern,
177: draggedPattern, center1.x, center1.y));
178: placeHolders.add(new ImpliciteSequencePlaceHolder(ownerPattern,
179: draggedPattern, center2.x, center2.y, true));
180: }
181:
182: private static Connection getInboundConnection(Pattern p) {
183: for (Connection c : p.getFirstElement()
184: .getIncomingConnections()) {
185: if (c instanceof MessageConnection)
186: continue;
187: return c;
188: }
189: return null;
190: }
191:
192: private static Connection getOutboundConnection(Pattern p) {
193: for (Connection c : p.getLastElement()
194: .getOutcomingConnections()) {
195: if (c instanceof MessageConnection)
196: continue;
197: return c;
198: }
199: return null;
200: }
201:
202: }
|