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: import org.netbeans.modules.bpel.design.geometry.FPoint;
027:
028: import org.netbeans.modules.bpel.design.model.DiagramModel;
029:
030: import org.netbeans.modules.bpel.model.api.BpelEntity;
031: import org.netbeans.modules.bpel.design.model.connections.Connection;
032: import org.netbeans.modules.bpel.design.model.elements.BorderElement;
033:
034: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
035:
036: /**
037: *
038: * @author Alexey Yarmolenko
039: */
040: public abstract class CompositePattern extends Pattern {
041:
042: private BorderElement border;
043: private List<Pattern> patterns = new ArrayList<Pattern>();
044:
045: public CompositePattern(DiagramModel model) {
046: super (model);
047: ;
048: }
049:
050: public void setBorder(BorderElement newBorder) {
051: BorderElement oldBorder = this .border;
052: if (oldBorder != null) {
053: oldBorder.setPattern(null);
054: }
055: if (newBorder != null) {
056: newBorder.setPattern(this );
057: }
058: this .border = newBorder;
059: }
060:
061: public BorderElement getBorder() {
062: return border;
063: }
064:
065: public List<Pattern> getNestedPatterns() {
066: return patterns;
067: }
068:
069: public Pattern getNestedPattern() {
070: if (patterns.size() == 0) {
071: return null;
072: }
073: return patterns.get(0);
074: }
075:
076: protected abstract void onAppendPattern(Pattern nestedPattern);
077:
078: protected abstract void onRemovePattern(Pattern nestedPattern);
079:
080: protected void appendPattern(Pattern p) {
081: assert p != null : "Trying to add null pattern to " + this ;
082: int index = getChildIndex(p);
083: int maxIndex = patterns.size();
084: index = index < 0 || index > maxIndex ? patterns.size() : index;
085: patterns.add(index, p);
086: onAppendPattern(p);
087: }
088:
089: /**
090: * TODO m
091: */
092: private int getChildIndex(Pattern p) {
093: int prevSiblIndex = -1;
094: BpelEntity curEntity = p.getOMReference();
095: if (curEntity == null) {
096: return prevSiblIndex;
097: }
098:
099: BpelEntity parentEntity = getOMReference();
100:
101: List<BpelEntity> children = null;
102: if (parentEntity != null) {
103: children = parentEntity.getChildren();
104: }
105:
106: if (children != null) {
107: int patternIndexes = -1;
108: for (BpelEntity elem : children) {
109: for (Pattern patternElem : patterns) {
110: if (elem != null
111: && elem
112: .equals(patternElem
113: .getOMReference())) {
114: patternIndexes++;
115: break;
116: }
117: }
118:
119: if (curEntity.equals(elem)) {
120: patternIndexes++;
121: break;
122: }
123: }
124: prevSiblIndex = patternIndexes;
125: }
126:
127: return prevSiblIndex;
128: }
129:
130: protected void removePattern(Pattern p) {
131: patterns.remove(p);
132: onRemovePattern(p);
133: }
134:
135: public VisualElement getNamedElement() {
136: return getBorder();
137: }
138:
139: public void ensureConnectionsCount(List<Connection> connections,
140: int count) {
141: count = Math.max(count, 0);
142:
143: while (connections.size() > count) {
144: connections.remove(connections.size() - 1).remove();
145: }
146:
147: while (connections.size() < count) {
148: connections.add(new Connection(this ));
149: }
150: }
151:
152: public Pattern getNestedPattern(BpelEntity entity) {
153: Pattern nested = getModel().getPattern(entity);
154:
155: if (nested == null)
156: return null;
157:
158: if (nested.getParent() != this ) {
159: assert false;
160: return null;
161: }
162: return nested;
163: }
164:
165: public Area createSelection() {
166: return createOutline();
167: }
168:
169: public boolean isCollapsable() {
170: return true;
171: }
172:
173: public Area createOutline() {
174: Area result = new Area();
175:
176: BorderElement border = getBorder();
177: if (border != null) {
178: result.add(new Area(border.getShape()));
179: }
180:
181: for (VisualElement ve : getElements()) {
182: if (ve.getWidth() < 2 && ve.getHeight() < 2)
183: continue;
184: result.add(new Area(ve.getShape()));
185: }
186:
187: return result;
188: }
189:
190: }
|