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.util.List;
023: import java.awt.geom.Area;
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import org.netbeans.modules.bpel.design.DiagramView;
027: import org.netbeans.modules.bpel.design.geometry.FBounds;
028: import org.netbeans.modules.bpel.model.api.BpelEntity;
029: import org.netbeans.modules.bpel.model.api.PartnerLink;
030: import org.netbeans.modules.bpel.model.api.PartnerLinkContainer;
031: import org.netbeans.modules.bpel.model.api.PartnerLinkReference;
032: import org.netbeans.modules.bpel.model.api.references.BpelReference;
033: import org.netbeans.modules.bpel.design.layout.LayoutManager;
034: import org.netbeans.modules.bpel.design.model.DiagramModel;
035: import org.netbeans.modules.bpel.design.model.PartnerRole;
036: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
037: import org.netbeans.modules.bpel.model.api.BpelEntity;
038: import org.netbeans.modules.bpel.model.api.references.BpelReference;
039:
040: /**
041: *
042: * @author Alexey Yarmolenko
043: */
044: public class PartnerLinksPattern extends CompositePattern {
045:
046: private List<Pattern> providers = new ArrayList<Pattern>();
047: private List<Pattern> consumers = new ArrayList<Pattern>();
048:
049: public PartnerLinksPattern(DiagramModel model) {
050: super (model);
051: }
052:
053: public List<Pattern> getConsumers() {
054: return consumers;
055: }
056:
057: public List<Pattern> getProviders() {
058: return providers;
059: }
060:
061: public boolean isSelectable() {
062: return false;
063: }
064:
065: public VisualElement getFirstElement() {
066: return null;
067: }
068:
069: public VisualElement getLastElement() {
070: return null;
071: }
072:
073: public DiagramView getView() {
074: return null;
075: }
076:
077: protected void onAppendPattern(Pattern p) {
078: if (((PartnerlinkPattern) p).getType() == PartnerRole.PROVIDER) {
079: providers.add(p);
080: } else {
081: consumers.add(p);
082: }
083: }
084:
085: protected void onRemovePattern(Pattern p) {
086:
087: consumers.remove(p);
088: providers.remove(p);
089: }
090:
091: protected void createElementsImpl() {
092:
093: // setBorder(new NullBorder());
094:
095: PartnerLinkContainer plc = (PartnerLinkContainer) getOMReference();
096: if (plc == null || plc.getPartnerLinks() == null) {
097: return;
098: }
099:
100: for (PartnerLink pl : plc.getPartnerLinks()) {
101:
102: Pattern p = getModel().createPattern(pl);
103:
104: p.setParent(this );
105: }
106:
107: }
108:
109: public FBounds layoutPattern(LayoutManager manager) {
110:
111: //just identify thhe maximum width
112: //PartnerLinks will be positioned, when all process activities are positioned
113: //see optimizePositions method
114:
115: double ypos = 0;
116:
117: for (Pattern p : consumers) {
118: FBounds bounds = p.getBounds();
119: manager.setPatternPosition(p, 0, ypos);
120: ypos += bounds.height + LayoutManager.HSPACING;
121: }
122:
123: ypos = 0;
124: for (Pattern p : providers) {
125: FBounds bounds = p.getBounds();
126: manager.setPatternPosition(p, 0, ypos);
127: ypos += bounds.height + LayoutManager.HSPACING;
128: }
129:
130: return null;
131: }
132:
133: /*
134: public void optimizePositions(LayoutManager manager) {
135: FBounds oldBounds = getBounds();
136: double ypos = oldBounds.y;
137: double width = 0;
138:
139:
140:
141: for (Position pos: new PositionCalculator().getResult()){
142: FBounds bounds = pos.pattern.getBounds();
143:
144: double y = Math.max(pos.position - bounds.height / 2, ypos);
145:
146: manager.setPatternPosition(pos.pattern, bounds.x, y);
147:
148: width = Math.max( width, bounds.width);
149:
150: ypos = y + bounds.height + LayoutManager.HSPACING;
151: // getBorder().setClientRectangle(0, 0, width, ypos);
152: }
153:
154:
155: setBounds(new FBounds(oldBounds.x, oldBounds.y,
156: oldBounds.width, ypos - oldBounds.y));
157: }
158: */
159: public String getDefaultName() {
160: return "Process";
161: }
162:
163: public Area createSelection() {
164: return new Area();
165:
166: }
167:
168: private class PositionCalculator {
169:
170: private ArrayList<Position> optimalPositions;
171:
172: public PositionCalculator() {
173:
174: optimalPositions = new ArrayList<Position>(
175: getNestedPatterns().size());
176:
177: //initialise the list of optimal y position per pattern
178: for (Pattern p : getNestedPatterns()) {
179: optimalPositions.add(new Position(p));
180: }
181:
182: //iterate over patterns tree and fill this list
183: calculatePositions(getModel().getRootPattern());
184:
185: //Complete calculations
186: for (Position p : optimalPositions) {
187: if (p.count > 0) {
188: p.position = p.position / p.count;
189: }
190: }
191:
192: //Sort elements by Y ccordinate
193: Collections.sort(optimalPositions);
194:
195: }
196:
197: public ArrayList<Position> getResult() {
198: return this .optimalPositions;
199: }
200:
201: private void calculatePositions(Pattern pattern) {
202: BpelEntity entity = pattern.getOMReference();
203: if (entity instanceof PartnerLinkReference) {
204: BpelReference<PartnerLink> pl_ref = ((PartnerLinkReference) entity)
205: .getPartnerLink();
206: if (pl_ref != null && !pl_ref.isBroken()) {
207: adjustPosition(getModel().getPattern(pl_ref.get()),
208: pattern.getBounds().getCenterY());
209:
210: }
211: }
212: if (pattern instanceof CompositePattern) {
213: for (Pattern p : ((CompositePattern) pattern)
214: .getNestedPatterns()) {
215: calculatePositions(p);
216: }
217: }
218: }
219:
220: private void adjustPosition(Pattern pattern, double new_value) {
221: Position pos = null;
222: for (Position p : optimalPositions) {
223: if (p.pattern == pattern) {
224: p.position += new_value;
225: p.count++;
226: }
227: }
228:
229: }
230: }
231:
232: class Position implements Comparable<Position> {
233: public double position = 0;
234: public double count = 0;
235: public Pattern pattern;
236:
237: public Position(Pattern pattern) {
238: this .pattern = pattern;
239:
240: }
241:
242: public int compareTo(Position p) {
243: return Double.compare(this.position, p.position);
244: }
245: }
246: }
|