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