KickJava   Java API By Example, From Geeks To Geeks.

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


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  * WidgetFactory.java
21  *
22  * Created on August 15, 2006, 11:27 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import org.netbeans.api.visual.model.ObjectScene;
33 import org.netbeans.api.visual.widget.Scene;
34 import org.netbeans.api.visual.widget.Widget;
35 import org.netbeans.modules.xml.wsdl.model.Fault;
36 import org.netbeans.modules.xml.wsdl.model.Input;
37 import org.netbeans.modules.xml.wsdl.model.Message;
38 import org.netbeans.modules.xml.wsdl.model.NotificationOperation;
39 import org.netbeans.modules.xml.wsdl.model.OneWayOperation;
40 import org.netbeans.modules.xml.wsdl.model.OperationParameter;
41 import org.netbeans.modules.xml.wsdl.model.Output;
42 import org.netbeans.modules.xml.wsdl.model.Part;
43 import org.netbeans.modules.xml.wsdl.model.PortType;
44 import org.netbeans.modules.xml.wsdl.model.RequestResponseOperation;
45 import org.netbeans.modules.xml.wsdl.model.SolicitResponseOperation;
46 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
47 import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
48 import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
49 import org.openide.util.Lookup;
50
51 /**
52  * Factory for creating Widget instances to represent the various WSDL
53  * model components (e.g. Operation, Role, PartnerLinkType).
54  *
55  * @author radval
56  * @author Nathan Fiedler
57  */

58 public class WidgetFactory {
59     /** The single instance of this class. */
60     private static WidgetFactory mFactory;
61     
62     /** Creates a new instance of WidgetFactory */
63     private WidgetFactory() {
64     }
65     
66     /**
67      * Get the singleton instance of this factory.
68      *
69      * @return WSDL widget factory.
70      */

71     public static WidgetFactory getInstance() {
72         if (mFactory == null) {
73             mFactory = new WidgetFactory();
74         }
75         return mFactory;
76     }
77     
78     /**
79      * Creates a Widget to represent the given WSDL model component.
80      *
81      * @param scene the widgets will be created in this Scene.
82      * @param component the WSDL component to represent.
83      * @return the new widget.
84      */

85     public Widget createWidget(Scene scene, WSDLComponent component) {
86         return createWidget(scene, component, false);
87     }
88     
89     /**
90      * Creates a Widget to represent the given WSDL model component.
91      *
92      * @param scene the widgets will be created in this Scene.
93      * @param component the WSDL component to represent.
94      * @param reuse if true, find and re-use an existing widget.
95      * @return the new widget.
96      */

97     public Widget createWidget(Scene scene, WSDLComponent component,
98             boolean reuse) {
99         // Default to using an empty Lookup, if none is given.
100
return createWidget(scene, component, Lookup.EMPTY, reuse);
101     }
102     
103     /**
104      * Creates a Widget to represent the given WSDL model component.
105      *
106      * @param scene the widgets will be created in this Scene.
107      * @param component the WSDL component to represent.
108      * @param lookup the Lookup for the widget.
109      * @return the new widget.
110      */

111     public Widget createWidget(Scene scene, WSDLComponent component,
112             Lookup lookup) {
113         return createWidget(scene, component, lookup, false);
114     }
115     
116     /**
117      * Creates a Widget to represent the given WSDL model component.
118      *
119      * @param scene the widgets will be created in this Scene.
120      * @param component the WSDL component to represent.
121      * @param lookup the Lookup for the widget.
122      * @param reuse if true, find and re-use an existing widget.
123      * @return the new widget.
124      */

125     public Widget createWidget(Scene scene, WSDLComponent component,
126             Lookup lookup, boolean reuse) {
127         Widget widget = null;
128         
129         if (reuse) {
130             widget = ((PartnerScene) scene).findWidget(component);
131             if (widget != null) {
132                 return widget;
133             }
134         }
135         
136         if (component instanceof Fault) {
137             Fault fault = (Fault)component;
138             widget = new FaultWidget(scene, fault, lookup,
139                     fault.getParent() instanceof SolicitResponseOperation );
140         } else if (component instanceof Input) {
141             widget = new InputWidget(scene, (Input) component, lookup);
142         } else if (component instanceof Message) {
143             widget = new MessageWidget(scene, (Message) component, lookup);
144         } else if (component instanceof Part) {
145             widget = new PartWidget(scene, (Part) component, lookup);
146         } else if (component instanceof NotificationOperation) {
147             widget = new NotificationOperationWidget(
148                     scene, (NotificationOperation) component, lookup);
149         } else if (component instanceof OneWayOperation) {
150             widget = new OneWayOperationWidget(
151                     scene, (OneWayOperation) component, lookup);
152         } else if (component instanceof Output) {
153             widget = new OutputWidget(scene, (Output) component, lookup);
154         } else if (component instanceof PartnerLinkType) {
155             widget = new PartnerLinkTypeWidget(
156                     scene, (PartnerLinkType) component, lookup);
157         } else if (component instanceof PortType) {
158             widget = new PortTypeWidget(scene, (PortType) component, lookup);
159         } else if (component instanceof RequestResponseOperation) {
160             widget = new RequestReplyOperationWidget(
161                     scene, (RequestResponseOperation) component, lookup);
162         } else if (component instanceof Role) {
163             widget = new RoleWidget(scene, (Role) component, lookup);
164         } else if (component instanceof SolicitResponseOperation) {
165             widget = new SolicitResponseOperationWidget(
166                     scene, (SolicitResponseOperation) component, lookup);
167         }
168         if (widget != null) {
169             prepareWidget(scene, widget, component);
170         }
171         return widget;
172     }
173     
174     /**
175      * Creates a Widget to represent a WSDL component of the given type.
176      * The widget will not have an assigned component, which means it
177      * can not be selected, and essentially does not exist. It is only
178      * useful as a placeholder in the diagram.
179      *
180      * @param scene the widgets will be created in this Scene.
181      * @param type the WSDL component type to be represented.
182      * @param lookup the Lookup for the widget.
183      * @return the new widget.
184      */

185     public Widget createWidget(Scene scene, Class JavaDoc<? extends WSDLComponent> type,
186             Lookup lookup) {
187         Widget widget = null;
188         
189         if (Fault.class.isAssignableFrom(type)) {
190             widget = new FaultWidget(scene, null, lookup, false);
191         } else if (Input.class.isAssignableFrom(type)) {
192             widget = new InputWidget(scene, null, lookup);
193         } else if (Message.class.isAssignableFrom(type)) {
194             widget = new MessageWidget(scene, null, lookup);
195         } else if (NotificationOperation.class.isAssignableFrom(type)) {
196             widget = new NotificationOperationWidget(scene, null, lookup);
197         } else if (OneWayOperation.class.isAssignableFrom(type)) {
198             widget = new OneWayOperationWidget(scene, null, lookup);
199         } else if (Output.class.isAssignableFrom(type)) {
200             widget = new OutputWidget(scene, null, lookup);
201         } else if (PartnerLinkType.class.isAssignableFrom(type)) {
202             widget = new PartnerLinkTypeWidget(scene, null, lookup);
203         } else if (PortType.class.isAssignableFrom(type)) {
204             
205             widget = new PortTypeWidget(scene, null, lookup);
206         } else if (RequestResponseOperation.class.isAssignableFrom(type)) {
207             widget = new RequestReplyOperationWidget(scene, null, lookup);
208         } else if (Role.class.isAssignableFrom(type)) {
209             widget = new RoleWidget(scene, null, lookup);
210         } else if (SolicitResponseOperation.class.isAssignableFrom(type)) {
211             widget = new SolicitResponseOperationWidget(scene, null, lookup);
212         }
213         if (widget != null) {
214             prepareWidget(scene, widget, null);
215         }
216         return widget;
217     }
218     
219     /**
220      * Creates a Widget to represent an OperationParameter, which could
221      * be either an input or an output. This method exists because it is
222      * not possible to create both Input/Output and OperationParameter
223      * widgets using the instanceof keyword.
224      *
225      * @param scene the widgets will be created in this Scene.
226      * @param component the operation parameter to be represented.
227      * @param lookup the Lookup for the widget.
228      * @return the new widget.
229      */

230     public Widget createOperationParameterWidget(Scene scene,
231             OperationParameter component, Lookup lookup) {
232         Widget widget = new OperationParameterWidget(scene, component, lookup);
233         prepareWidget(scene, widget, component);
234         return widget;
235     }
236     
237     /**
238      * Perform additional preparation on the widget now that it has been
239      * created, including adding actions and mapping it in the scene.
240      *
241      * @param scene the Scene for the widget.
242      * @param widget the widget to prepare.
243      * @param component the WSDL component for the widget; may be null.
244      */

245     private void prepareWidget(Scene scene, Widget widget,
246             WSDLComponent component) {
247         if (scene instanceof PartnerScene) {
248             // Add the object selection action to the widget.
249
widget.getActions().addAction(((PartnerScene) scene).getSelectAction());
250         }
251         if (scene instanceof ObjectScene) {
252             ObjectScene os = (ObjectScene) scene;
253             if (component != null) {
254                 // Add the object-widget mapping in the scene.
255
List JavaDoc<Widget> widgets = os.findWidgets(component);
256                 if (widgets == null) {
257                     widgets = new ArrayList JavaDoc<Widget>();
258                 } else {
259                     // Remove the original mapping.
260
os.removeObject(component);
261                     // The List that comes back is immutable...
262
widgets = new ArrayList JavaDoc<Widget>(widgets);
263                 }
264                 widgets.add(widget);
265                 os.addObject(component, widgets.toArray(
266                         new Widget[widgets.size()]));
267             }
268         }
269     }
270 }
271
Popular Tags