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