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: package org.netbeans.modules.bpel.design.selection;
020:
021: import java.awt.Graphics2D;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import org.netbeans.modules.bpel.design.DesignView;
026: import org.netbeans.modules.bpel.design.DiagramView;
027: import org.netbeans.modules.bpel.design.geometry.FPoint;
028:
029: import org.netbeans.modules.bpel.model.api.Activity;
030: import org.netbeans.modules.bpel.model.api.BpelContainer;
031: import org.netbeans.modules.bpel.model.api.BpelEntity;
032: import org.netbeans.modules.bpel.model.api.CompensatableActivityHolder;
033: import org.netbeans.modules.bpel.design.model.patterns.CompensatePattern;
034: import org.netbeans.modules.bpel.design.model.patterns.CompositePattern;
035: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
036: import org.netbeans.modules.bpel.model.api.Else;
037: import org.openide.ErrorManager;
038:
039: public class PlaceHolderManager implements DnDTool {
040:
041: private List<PlaceHolder> placeHolders = new ArrayList<PlaceHolder>();
042: private PlaceHolder currentPlaceHolder;
043: private DiagramView diagramView;
044: private Pattern draggedPattern;
045: private PlaceHolder defaultPlaceholder;
046:
047: public PlaceHolderManager(DiagramView diagramView) {
048: this .diagramView = diagramView;
049: }
050:
051: public boolean isEmpty() {
052: return placeHolders.isEmpty();
053: }
054:
055: public void clear() {
056: if (!isEmpty()) {
057: placeHolders.clear();
058: currentPlaceHolder = null;
059: draggedPattern = null;
060: getDiagramView().repaint();
061: defaultPlaceholder = null;
062: }
063: //getDesignView().getButtonsManager().update();
064: }
065:
066: public DiagramView getDiagramView() {
067: return diagramView;
068: }
069:
070: public Pattern getDraggedPattern() {
071: return draggedPattern;
072: }
073:
074: private PlaceHolder findPlaceholder(float px, float py) {
075: for (int i = placeHolders.size() - 1; i >= 0; i--) {
076: PlaceHolder p = placeHolders.get(i);
077: if (p.contains(px, py)) {
078: return p;
079: }
080: }
081: return defaultPlaceholder;
082: }
083:
084: public void init(Pattern draggedPattern) {
085: this .draggedPattern = draggedPattern;
086: this .defaultPlaceholder = null;
087: createPlaceHolders();
088:
089: //FIXME getDesignView().getButtonsManager().clear();
090: }
091:
092: private boolean canContainCompensate(Pattern pattern) {
093: while (pattern != null) {
094: BpelEntity entity = pattern.getOMReference();
095: if (entity instanceof CompensatableActivityHolder) {
096: return true;
097: }
098:
099: pattern = pattern.getParent();
100: }
101: return false;
102: }
103:
104: private void createPlaceHolders() {
105:
106: getDiagramView().getPlaceholders(getDraggedPattern(),
107: placeHolders);
108:
109: Iterator<Pattern> patterns = getDiagramView().getPatterns();
110: while (patterns.hasNext()) {
111: Pattern pattern = patterns.next();
112: if (getDraggedPattern() == pattern) {
113: continue;
114: }
115: if (pattern.isNestedIn(getDraggedPattern())) {
116: continue;
117: }
118:
119: //special case for Compensate activity which breaks common rule. see bug 6364908
120: if (!(getDraggedPattern() instanceof CompensatePattern)
121: || canContainCompensate(pattern)) {
122: pattern.createPlaceholders(getDraggedPattern(),
123: placeHolders);
124:
125: //Try to create "implicit sequence" placeholders
126: if (getDraggedPattern().getOMReference() instanceof Activity) {
127: ImpliciteSequencePlaceHolder.create(
128: getDraggedPattern(), pattern, placeHolders);
129: }
130: }
131:
132: }
133: /*
134: * Find first placeholder marked as default
135: * This placheholder will recieve drop if element was dropped on whitespace
136: */
137:
138: for (PlaceHolder placeHolder : placeHolders) {
139: if (placeHolder instanceof DefaultPlaceholder) {
140: defaultPlaceholder = placeHolder;
141: break;
142: }
143: }
144: }
145:
146: public List<PlaceHolder> getPlaceHolders() {
147: return placeHolders;
148: }
149:
150: public void move(FPoint mp) {
151: if (isEmpty()) {
152: return;
153: }
154:
155: PlaceHolder newCurrentPlaceHolder = findPlaceholder(mp.x, mp.y);
156: PlaceHolder oldCurrentPlaceHolder = currentPlaceHolder;
157:
158: if (oldCurrentPlaceHolder != newCurrentPlaceHolder) {
159: if (oldCurrentPlaceHolder != null) {
160: oldCurrentPlaceHolder.dragExit();
161: }
162:
163: if (newCurrentPlaceHolder != null) {
164: newCurrentPlaceHolder.dragEnter();
165: }
166:
167: currentPlaceHolder = newCurrentPlaceHolder;
168: getDiagramView().repaint();
169: }
170: }
171:
172: public boolean isValidLocation() {
173: return (currentPlaceHolder != null);
174: }
175:
176: public void drop(FPoint mp) {
177: if (isEmpty()) {
178: return;
179: }
180: PlaceHolder targetPlaceholder = findPlaceholder(mp.x, mp.y);
181:
182: if (targetPlaceholder == null) {
183: targetPlaceholder = defaultPlaceholder;
184: }
185: if (targetPlaceholder != null) {
186: try {
187:
188: BpelEntity activity = (BpelEntity) getDraggedPattern()
189: .getOMReference();
190: BpelEntity activityParent = (activity != null) ? activity
191: .getParent()
192: : null;
193:
194: if (activityParent != null) {
195: activity.cut();
196:
197: if (activityParent instanceof Else) {
198: BpelContainer elseParent = activityParent
199: .getParent();
200: if (elseParent != null) {
201: elseParent.remove(activityParent);
202: }
203: }
204: }
205: targetPlaceholder.drop();
206:
207: } catch (Exception ex) {
208: ErrorManager.getDefault().notify(
209: ErrorManager.INFORMATIONAL, ex);
210: }
211: }
212:
213: clear();
214: }
215:
216: public void paint(Graphics2D g2) {
217: if (isEmpty()) {
218: return;
219: }
220: for (PlaceHolder p : placeHolders) {
221: p.paint(g2);
222: }
223: }
224: }
|