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.model.patterns;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.List;
024: import org.netbeans.modules.bpel.design.DiagramView;
025: import org.netbeans.modules.bpel.design.geometry.FBounds;
026: import org.netbeans.modules.bpel.design.model.elements.InvokeOperationElement;
027: import org.netbeans.modules.bpel.design.model.elements.ReceiveOperationElement;
028:
029: import org.netbeans.modules.bpel.model.api.PartnerLink;
030: import org.netbeans.modules.bpel.model.api.references.WSDLReference;
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.PartnerRole;
034: import org.netbeans.modules.bpel.design.model.PartnerLinkHelper;
035: import org.netbeans.modules.bpel.design.model.connections.Connection;
036: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
037: import org.netbeans.modules.bpel.design.model.elements.ProcessBorder;
038: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
039: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
040: import org.netbeans.modules.bpel.nodes.synchronizer.ModelSynchronizer;
041: import org.netbeans.modules.bpel.nodes.synchronizer.SynchronisationListener;
042: import org.netbeans.modules.xml.wsdl.model.Operation;
043: import org.netbeans.modules.xml.wsdl.model.PortType;
044: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
045: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
046: import org.netbeans.modules.xml.xam.Component;
047: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
048: import org.openide.ErrorManager;
049:
050: /**
051: *
052: * @author Alexey Yarmolenko
053: */
054: public class PartnerlinkPattern extends CompositePattern implements
055: SynchronisationListener {
056:
057: // private HashMap<VisualElement, PortType> porttypes =
058: // new HashMap<VisualElement, PortType>();
059: //
060: private PartnerRole type;
061: private ModelSynchronizer synchronizer;
062: private boolean isCollapsed = false;
063:
064: public PartnerlinkPattern(DiagramModel model) {
065: this (model, false);
066:
067: }
068:
069: public PartnerlinkPattern(DiagramModel model, boolean isCollapsed) {
070: super (model);
071: this .isCollapsed = isCollapsed;
072: synchronizer = new ModelSynchronizer(this );
073:
074: }
075:
076: public PartnerRole getType() {
077: if (type == null) {
078: type = PartnerLinkHelper
079: .getPartnerlinkRole((PartnerLink) getOMReference());
080: }
081: return type;
082: }
083:
084: public VisualElement getFirstElement() {
085: return null;
086: }
087:
088: public VisualElement getLastElement() {
089: return null;
090: }
091:
092: public boolean isDraggable() {
093: return true;
094: }
095:
096: protected void onAppendPattern(Pattern p) {
097: }
098:
099: protected void onRemovePattern(Pattern p) {
100: }
101:
102: @Override
103: public DiagramView getView() {
104: return (getType() == PartnerRole.CONSUMER) ? getModel()
105: .getView().getConsumersView() : getModel().getView()
106: .getProvidersView();
107: }
108:
109: public Operation getOperation(VisualElement e) {
110:
111: String name = e.getText();
112: PartnerLink pl = (PartnerLink) getOMReference();
113:
114: if (pl.getPartnerLinkType() == null) {
115: return null;
116: }
117:
118: PartnerLinkType plt = pl.getPartnerLinkType().get();
119:
120: if (plt == null) {
121: return null;
122: }
123:
124: Operation result = findOperationInRole(plt.getRole1(), name);
125:
126: return (result != null) ? result : findOperationInRole(plt
127: .getRole2(), name);
128: }
129:
130: public VisualElement getElement(WSDLReference<Operation> op_ref) {
131: if (isCollapsed) {
132: return getBorder();
133: }
134: String name = op_ref.getRefString();
135: for (VisualElement e : getElements()) {
136: if (name.equals(e.getText())) {
137: return e;
138: }
139: }
140: return null;
141:
142: }
143:
144: public void setParent(CompositePattern newParent) {
145: super .setParent(newParent);
146:
147: if (newParent == null) {
148: synchronizer.unsubscribe();
149: }
150: }
151:
152: public FBounds layoutPattern(LayoutManager manager) {
153:
154: double ypos = 0;
155:
156: double x, y = 0, width = 0, height;
157:
158: for (VisualElement e : getElements()) {
159: e.setLocation(-e.getWidth() / 2, ypos);
160: ypos += e.getHeight() + LayoutManager.VSPACING;
161: width = Math.max(width, e.getWidth());
162: }
163:
164: height = ypos - LayoutManager.VSPACING;
165:
166: width = Math.max(width, 72);
167: height = Math.max(height, 24);
168:
169: x = -width / 2;
170: getBorder().setClientRectangle(x, y, width, height);
171:
172: return getBorder().getBounds();
173:
174: }
175:
176: public void reloadOperations() {
177:
178: PartnerLink pl = (PartnerLink) getOMReference();
179:
180: if (pl == null) {
181: //this may happen if orphaned pattern still listens to WSDL model changes
182: return;
183: }
184:
185: getBorder().setLabelText(pl.getName());
186:
187: removeAllElements();
188:
189: if (pl.getPartnerLinkType() == null) {
190: return;
191: }
192:
193: if (pl.isInDocumentModel()) {
194: PartnerLinkType plt = pl.getPartnerLinkType().get();
195:
196: if (plt != null) {
197: synchronizer.subscribe(plt.getModel());
198: processPortType(plt.getRole1(), pl.getMyRole(), pl
199: .getPartnerRole());
200: processPortType(plt.getRole2(), pl.getMyRole(), pl
201: .getPartnerRole());
202: }
203: }
204:
205: }
206:
207: protected void createElementsImpl() {
208: PartnerLink pl = (PartnerLink) getOMReference();
209: setBorder(new ProcessBorder());
210:
211: if (!isCollapsed) {
212: reloadOperations();
213: }
214: registerTextElement(getBorder());
215:
216: }
217:
218: private void processPortType(Role role, WSDLReference<Role> myRole,
219: WSDLReference<Role> partnerRole) {
220: if (role == null) {
221: return;
222: }
223:
224: String roleName = role.getName();
225: if (roleName == null) {
226: return;
227: }
228: //
229:
230: boolean isMyRole = false;
231:
232: if (myRole != null) {
233: isMyRole = myRole.references(role);
234: } else if (partnerRole != null) {
235: isMyRole = !partnerRole.references(role);
236: } else {
237: //ignore PLs which have no roles settings;
238: return;
239: }
240:
241: NamedComponentReference<PortType> ptReference = role
242: .getPortType();
243: if (ptReference == null) {
244: return;
245: }
246: //
247: PortType ptype = ptReference.get();
248: if (ptype == null) {
249: return;
250: }
251:
252: //
253: for (Operation op : ptype.getOperations()) {
254: VisualElement e = (isMyRole) ? new InvokeOperationElement()
255: : new ReceiveOperationElement();
256:
257: e.setLabelText(op.getName());
258: appendElement(e);
259:
260: }
261: }
262:
263: public NodeType getNodeType() {
264: return NodeType.PARTNER_LINK;
265:
266: }
267:
268: private Operation findOperationInRole(Role role, String name) {
269: if (role == null) {
270: return null;
271: }
272:
273: if (role.getPortType() == null) {
274: return null;
275: }
276:
277: PortType pt = role.getPortType().get();
278: Collection<Operation> ops = pt.getOperations();
279: for (Operation op : ops) {
280: if (name.equals(op.getName())) {
281: return op;
282: }
283: }
284: return null;
285: }
286:
287: private void updatePartnerLinkType() {
288: //fix for bug xxx: check if this pattern belongs to dead view
289: if (getModel().getView() == null || getParent() == null) {
290: return;
291: }
292: try {
293: reloadOperations();
294: getModel().getView().diagramChanged();
295: } catch (Exception ex) {
296: ErrorManager.getDefault()
297: .notify(ErrorManager.EXCEPTION, ex);
298: }
299: }
300:
301: public void componentUpdated(Component component) {
302: updatePartnerLinkType();
303: }
304:
305: public void childrenUpdated(Component component) {
306: updatePartnerLinkType();
307: }
308: }
|