KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > grapheditor > OperationSceneLayer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * OperationSceneLayer.java
22  *
23  * Created on November 6, 2006, 6:08 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor;
30
31 import java.awt.Dimension JavaDoc;
32 import java.awt.Rectangle JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 import javax.swing.SwingUtilities JavaDoc;
36
37 import org.netbeans.api.visual.layout.LayoutFactory;
38 import org.netbeans.api.visual.layout.LayoutFactory.SerialAlignment;
39 import org.netbeans.api.visual.widget.Scene;
40 import org.netbeans.api.visual.widget.Widget;
41 import org.netbeans.modules.xml.wsdl.model.Operation;
42 import org.netbeans.modules.xml.wsdl.model.PortType;
43 import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
44 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget.OperationWidget;
45 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget.PartnerLinkTypeContentWidget;
46 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget.RoleWidget;
47 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget.WidgetFactory;
48 import org.netbeans.modules.xml.xam.ComponentEvent;
49 import org.netbeans.modules.xml.xam.ComponentListener;
50 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
51
52 /**
53  *
54  * @author radval
55  */

56 public class OperationSceneLayer extends Widget implements ComponentListener {
57     
58     private static final int OPERATION_GAP = 25;
59     private PartnerLinkTypeContentWidget mOuterWidget;
60     private Widget dummyOperationWidget;
61     private PortType leftPortType;
62     private PortType rightPortType;
63     private Widget dummyEndWidget;
64     
65     public OperationSceneLayer(Scene scene, PartnerLinkTypeContentWidget outerWidget) {
66         super(scene);
67         mOuterWidget = outerWidget;
68         mOuterWidget.getWSDLComponent().getModel().addComponentListener(this);
69         // Get the port types involved so we can detect when they are deleted.
70
rightPortType = getPortType(mOuterWidget.getRightRoleWidget());
71         leftPortType = getPortType(mOuterWidget.getLeftRoleWidget());
72         init();
73     }
74     
75     private void init() {
76 // setBorder(BorderFactory.createLineBorder(Color.CYAN));
77
setLayout(LayoutFactory.createVerticalLayout(SerialAlignment.JUSTIFY, OPERATION_GAP));
78         dummyOperationWidget = new Widget(getScene());
79         dummyOperationWidget.setPreferredBounds(new Rectangle JavaDoc(0, 67));//67 = height of a operation widget
80
dummyEndWidget = new Widget(getScene());
81         dummyEndWidget.setPreferredBounds(new Rectangle JavaDoc(0, 105));//67 = height of a operation widget
82
refreshOperations();
83         setMinimumSize(new Dimension JavaDoc(400, 0));
84     }
85
86     public void childrenAdded(ComponentEvent evt) {
87         if (evt.getSource() instanceof PortType) {
88             //Check whether the port type that changed was what we are interested in.
89
PortType pt = (PortType) evt.getSource();
90             if (pt == rightPortType || pt == leftPortType) {
91                 refreshOperations();
92             }
93         }
94     }
95
96     
97     public void childrenDeleted(ComponentEvent evt) {
98         if (evt.getSource() instanceof PortType) {
99             PortType pt = (PortType) evt.getSource();
100             if (pt == rightPortType || pt == leftPortType) {
101                 refreshOperations();
102             }
103         } else {
104             // Assume the source is the Definitions instance.
105
PortType rpt = getPortType(mOuterWidget.getRightRoleWidget());
106             PortType lpt = getPortType(mOuterWidget.getLeftRoleWidget());
107             if (lpt != leftPortType || rpt != rightPortType) {
108                 // Looks like one or more of our port types changed.
109
leftPortType = lpt;
110                 rightPortType = rpt;
111                 refreshOperations();
112             }
113         }
114     }
115     
116     
117     public void valueChanged(ComponentEvent evt) {
118         if (evt.getSource() instanceof Role) {
119             Role role = (Role) evt.getSource();
120             //Check whether the role that changed was what we are interested in.
121
if (role.equals(mOuterWidget.getRightRoleWidget().getWSDLComponent())) {
122                 rightPortType = getPortType(mOuterWidget.getRightRoleWidget());
123                 refreshOperations();
124             } else if (role.equals(mOuterWidget.getLeftRoleWidget().getWSDLComponent())) {
125                 leftPortType = getPortType(mOuterWidget.getLeftRoleWidget());
126                 refreshOperations();
127             }
128         }
129     }
130     
131     /**
132      * Remove all of the children and reconstruct based on the current
133      * state of the port types.
134      */

135     private void refreshOperations() {
136         SwingUtilities.invokeLater(new Runnable JavaDoc() {
137             public void run() {
138                 removeChildren();
139                 mOuterWidget.getRightRoleWidget().showHotSpot(false);
140                 renderOperations(true);
141                 mOuterWidget.getLeftRoleWidget().showHotSpot(false);
142                 renderOperations(false);
143                 addChild(dummyEndWidget);
144                 getScene().validate();
145             }
146         });
147
148     }
149
150     /**
151      * Renders the operations..
152      *
153      * @param right true if right-sided, false if left.
154      */

155     private void renderOperations(boolean right) {
156         PortType pt = right ? rightPortType : leftPortType;
157         if (pt != null) {
158             Collection JavaDoc<Operation> operations = pt.getOperations();
159             WidgetFactory factory = WidgetFactory.getInstance();
160             for (Operation operation : operations) {
161                 OperationWidget operationWidget =
162                     (OperationWidget) factory.createWidget(getScene(), operation);
163                 operationWidget.setRightSided(right);
164                 addChild(operationWidget);
165             }
166         }
167     }
168
169
170
171     /**
172      * Retrieve the PortType from the given RoleWidget.
173      *
174      * @param rw RoleWidget from which to get PortType.
175      * @return PortType, or null if not available.
176      */

177     private PortType getPortType(RoleWidget rw) {
178         try {
179             if (rw != null) {
180                 Role role = rw.getWSDLComponent();
181                 if (role != null) {
182                     NamedComponentReference<PortType> ptref = role.getPortType();
183                     if (ptref != null) {
184                         return ptref.get();
185                     }
186                 }
187             }
188         } catch (IllegalStateException JavaDoc ise) {
189             // Indicates the referencing component is no longer in the model.
190
// Fall through and return null.
191
}
192         return null;
193     }
194
195
196
197     public void showBlankWidget(int i) {
198         if (i == -1) return;
199         
200         removeBlankWidget();
201         addChild(i, dummyOperationWidget);
202     }
203
204     public void removeBlankWidget() {
205         if (getChildren().contains(dummyOperationWidget)) {
206             removeChild(dummyOperationWidget);
207         }
208     }
209 }
210
Popular Tags