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.Collection;
024: import org.netbeans.modules.bpel.design.geometry.FBounds;
025: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
026: import org.netbeans.modules.bpel.design.selection.PlaceHolder;
027: import org.netbeans.modules.bpel.model.api.Activity;
028: import org.netbeans.modules.bpel.model.api.BpelEntity;
029: import org.netbeans.modules.bpel.model.api.ElseIf;
030: import org.netbeans.modules.bpel.design.geometry.FDimension;
031: import org.netbeans.modules.bpel.design.geometry.FPoint;
032: import org.netbeans.modules.bpel.design.layout.LayoutManager;
033: import org.netbeans.modules.bpel.design.model.DiagramModel;
034: import org.netbeans.modules.bpel.design.model.connections.Connection;
035: import org.netbeans.modules.bpel.design.model.connections.Direction;
036: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
037: import org.netbeans.modules.bpel.design.model.elements.GroupBorder;
038: import org.netbeans.modules.bpel.design.model.elements.PlaceHolderElement;
039: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
040:
041: public class ElseIfPattern extends CompositePattern {
042:
043: private VisualElement gateway;
044: private PlaceHolderElement placeHolder;
045: private Connection connection;
046:
047: public ElseIfPattern(DiagramModel model) {
048: super (model);
049: }
050:
051: protected void onAppendPattern(Pattern nestedPattern) {
052: removeElement(placeHolder);
053: }
054:
055: protected void onRemovePattern(Pattern nestedPattern) {
056: appendElement(placeHolder);
057: }
058:
059: public VisualElement getFirstElement() {
060: return gateway;
061: }
062:
063: public VisualElement getLastElement() {
064: Pattern p = getNestedPattern();
065: return (p != null) ? p.getLastElement() : placeHolder;
066: }
067:
068: public void reconnectElements() {
069: Pattern p = getNestedPattern();
070: VisualElement t = (p == null) ? placeHolder : p
071: .getFirstElement();
072: connection.connect(gateway, Direction.BOTTOM, t, Direction.TOP);
073: }
074:
075: public FBounds layoutPattern(LayoutManager manager) {
076: Pattern p = getNestedPattern();
077:
078: double x0 = -gateway.getWidth() / 2.0;
079: double y0 = -gateway.getHeight() / 2.0;
080:
081: gateway.setLocation(x0, y0);
082:
083: double y1 = x0 + gateway.getHeight() + LayoutManager.VSPACING; // + 8; //TODO: Replace with constant
084: double x1 = gateway.getWidth() / 2;
085:
086: if (p != null) {
087: FBounds bounds = p.getBounds();
088: FPoint offset = manager.getOriginOffset(p);
089:
090: manager.setPatternPosition(p, -offset.x, y1);
091:
092: y1 += bounds.height;
093: x0 = Math.min(x0, -offset.x);
094: x1 = Math.max(x1, bounds.width - offset.x);
095: } else {
096: double width = placeHolder.getWidth();
097: double height = placeHolder.getHeight();
098: placeHolder.setLocation(-width / 2, y1);
099: y1 += height;
100: x0 = Math.min(x0, -width / 2);
101: x1 = Math.max(x1, width / 2);
102: }
103: getBorder().setClientRectangle(x0, y0, x1 - x0, y1 - y0);
104:
105: return getBorder().getBounds();
106: }
107:
108: public Area createSelection() {
109: Area result = new Area(getBorder().getShape());
110: result.subtract(new Area(gateway.getShape()));
111: return result;
112: }
113:
114: protected void createElementsImpl() {
115: setBorder(new GroupBorder());
116: getBorder().setText(getDefaultName());
117:
118: gateway = ContentElement.createElseIfGateway();
119: placeHolder = new PlaceHolderElement();
120:
121: appendElement(gateway);
122: appendElement(placeHolder);
123:
124: connection = new Connection(this );
125:
126: BpelEntity activity = ((ElseIf) getOMReference()).getActivity();
127: if (activity != null) {
128: getModel().createPattern(activity).setParent(this );
129: }
130: }
131:
132: public String getDefaultName() {
133: return "ElseIf"; // NOI18N
134: }
135:
136: public NodeType getNodeType() {
137: return NodeType.ELSE_IF;
138: }
139:
140: public boolean isCollapsable() {
141: return true;
142: }
143:
144: public void createPlaceholders(Pattern draggedPattern,
145: Collection<PlaceHolder> placeHolders) {
146: if (!(draggedPattern.getOMReference() instanceof Activity))
147: return;
148: if (((ElseIf) getOMReference()).getActivity() != null)
149: return;
150:
151: placeHolders.add(new InnerPlaceHolder(draggedPattern));
152: }
153:
154: class InnerPlaceHolder extends PlaceHolder {
155: public InnerPlaceHolder(Pattern draggedPattern) {
156: super (ElseIfPattern.this , draggedPattern, placeHolder
157: .getCenterX(), placeHolder.getCenterY());
158: }
159:
160: public void drop() {
161: ((ElseIf) getOMReference())
162: .setActivity((Activity) getDraggedPattern()
163: .getOMReference());
164: }
165: }
166:
167: }
|