KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > netbeans > module > WSDLMultiViewSupport


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 package org.netbeans.modules.xml.wsdl.ui.netbeans.module;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import org.netbeans.core.api.multiview.MultiViewHandler;
27 import org.netbeans.core.api.multiview.MultiViewPerspective;
28 import org.netbeans.core.api.multiview.MultiViews;
29 import org.netbeans.modules.print.spi.PrintProvider;
30 import org.netbeans.modules.print.spi.PrintProviderCookie;
31 import org.netbeans.modules.xml.schema.model.SchemaComponent;
32 import org.netbeans.modules.xml.validation.ShowCookie;
33 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
34 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
35 import org.netbeans.modules.xml.xam.Component;
36 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
37 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
38 import org.netbeans.modules.xml.xam.ui.cookies.GetComponentCookie;
39 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
40 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie.View;
41 import org.openide.loaders.DataObject;
42 import org.openide.nodes.Node;
43 import org.openide.util.Lookup;
44 import org.openide.windows.TopComponent;
45 import org.openide.windows.WindowManager;
46
47 /**
48  * Implementation of various cookie interfaces, in particular
49  * ViewComponentCookie and ShowCookie.
50  * The instance of this class is in the WSDLDataObject cookie set.
51  *
52  * @author Ajit Bhate
53  * @author Nathan Fiedler
54  */

55 public class WSDLMultiViewSupport implements ViewComponentCookie, ShowCookie,
56         PrintProviderCookie {
57     /** The data object */
58     private WSDLDataObject dobj;
59     /** Set of components that are shown in the Partner (design) view. */
60     private static final Class JavaDoc[] DESIGNABLE_COMPONENTS = new Class JavaDoc[] {
61         org.netbeans.modules.xml.wsdl.model.Message.class,
62         // Includes: Notifcation, OneWay, RequestResponse, SolicitResponse
63
org.netbeans.modules.xml.wsdl.model.Operation.class,
64         // Includes: Fault, Input, Output
65
org.netbeans.modules.xml.wsdl.model.OperationParameter.class,
66         org.netbeans.modules.xml.wsdl.model.Part.class,
67         org.netbeans.modules.xml.wsdl.model.PortType.class,
68         org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType.class,
69         org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role.class,
70     };
71     
72     /**
73      * Creates a new instance of WSDLMultiViewSupport.
74      *
75      * @param dobj the data object.
76      */

77     public WSDLMultiViewSupport(WSDLDataObject dobj) {
78         this.dobj = dobj;
79     }
80     
81     public void view(final View view, final Component component,
82             final Object JavaDoc... parameters) {
83         if (!EventQueue.isDispatchThread()) {
84             EventQueue.invokeLater(new Runnable JavaDoc() {
85                 public void run() {
86                     viewInSwingThread(view, component, parameters);
87                 }
88             });
89         } else {
90             viewInSwingThread(view, component, parameters);
91         }
92     }
93
94     // see schemamultiviewsupport for implementation
95
private void viewInSwingThread(View view, Component component,
96             Object JavaDoc... parameters) {
97         if (canView(view,component)) {
98             WSDLEditorSupport editor = dobj.getWSDLEditorSupport();
99             editor.open();
100             if (view != null) {
101                 switch (view) {
102                     case SOURCE:
103                         WSDLMultiViewFactory.requestMultiviewActive(
104                                 WSDLSourceMultiviewDesc.PREFERRED_ID);
105                         break;
106                     case STRUCTURE:
107                         WSDLMultiViewFactory.requestMultiviewActive(
108                                 WSDLTreeViewMultiViewDesc.PREFERRED_ID);
109                         break;
110                     case DESIGN:
111                         WSDLMultiViewFactory.requestMultiviewActive(
112                                 WSDLDesignMultiViewDesc.PREFERRED_ID);
113                         break;
114                 }
115             }
116             TopComponent activeTC = TopComponent.getRegistry().getActivated();
117             ShowCookie showCookie = (ShowCookie) activeTC.getLookup().lookup(
118                     ShowCookie.class);
119             ResultItem resultItem = null;
120             if (parameters != null && parameters.length != 0) {
121                 for (Object JavaDoc o : parameters) {
122                     if (o instanceof ResultItem) {
123                         resultItem = (ResultItem) o;
124                         break;
125                     }
126                 }
127             }
128             if (showCookie != null) {
129                 if (resultItem == null) {
130                     resultItem = new ResultItem(null, null, component, null);
131                 }
132                 showCookie.show(resultItem);
133             }
134         }
135     }
136
137     // see schemamultiviewsupport for implementation
138
public boolean canView(ViewComponentCookie.View view, Component component) {
139         if (view != null && component != null) {
140             switch (view) {
141             case SOURCE:
142                 if (!WSDLSourceMultiviewDesc.PREFERRED_ID.equals(
143                         getMultiviewActive()) ||
144                         !getActiveComponents().contains(component)) {
145                     return true;
146                 }
147                 break;
148             case STRUCTURE:
149                 if (!(component instanceof SchemaComponent ||
150                         component instanceof WSDLComponent)) {
151                     return false;
152                 }
153                 if (WSDLTreeViewMultiViewDesc.PREFERRED_ID.equals(
154                         getMultiviewActive()) &&
155                         getActiveComponents().contains(component)) {
156                     TopComponent activeTC = TopComponent.getRegistry().getActivated();
157                     WSDLDataObject wdobj = (WSDLDataObject) activeTC.getLookup().
158                             lookup(WSDLDataObject.class);
159                     return wdobj != dobj;
160                 }
161                 return true;
162             case CURRENT:
163             case SUPER:
164                 return true;
165             case DESIGN:
166                 if (WSDLDesignMultiViewDesc.PREFERRED_ID.equals(
167                         getMultiviewActive())) {
168                     return false;
169                 }
170                 // Determine if this type of component is displayed
171
// in the partner view or not.
172
boolean okay = false;
173                 for (Class JavaDoc type : DESIGNABLE_COMPONENTS) {
174                     if (type.isInstance(component)) {
175                         return true;
176                     }
177                 }
178                 break;
179             }
180         }
181         return false;
182     }
183         
184     public void show(ResultItem resultItem) {
185         View view = View.STRUCTURE;
186         Component component = resultItem.getComponents();
187         if (component == null || component.getModel() == null ||
188                 component.getModel().getState() == WSDLModel.State.NOT_WELL_FORMED) {
189             view(View.SOURCE,component,resultItem);
190         } else {
191             if (component instanceof DocumentComponent) {
192                 UIUtilities.annotateSourceView(dobj, (DocumentComponent) component,
193                         resultItem.getDescription(), false);
194             }
195
196             TopComponent tc = WindowManager.getDefault().getRegistry().getActivated();
197             MultiViewHandler mvh = MultiViews.findMultiViewHandler(tc);
198
199             if (mvh == null) {
200                 return;
201             }
202
203             MultiViewPerspective mvp = mvh.getSelectedPerspective();
204             if (mvp.preferredID().equals(WSDLTreeViewMultiViewDesc.PREFERRED_ID)) {
205                 view(View.STRUCTURE, component, resultItem);
206             } else if (mvp.preferredID().equals(WSDLSourceMultiviewDesc.PREFERRED_ID)) {
207                 view(View.SOURCE, component, resultItem);
208             } else if (mvp.preferredID().equals(WSDLDesignMultiViewDesc.PREFERRED_ID)) {
209                 view(View.DESIGN, component, resultItem);
210             }
211         }
212     }
213
214     public PrintProvider getPrintProvider() {
215         TopComponent component = TopComponent.getRegistry().getActivated();
216         Lookup lookup = component.getLookup();
217         DataObject dobj = (DataObject) lookup.lookup(DataObject.class);
218         if (dobj == this.dobj) {
219             PrintProviderCookie cookie = (PrintProviderCookie) lookup.lookup(
220                     PrintProviderCookie.class);
221             // Avoid looping forever by ensuring we find a provider that
222
// is not ourselves.
223
if (cookie != null && cookie != this) {
224                 return cookie.getPrintProvider();
225             }
226         }
227         return null;
228     }
229
230     /**
231      * Finds the preferredID of active multiview element. If activated
232      * TopComponent is not MultiViewTopComponent, returns null.
233      *
234      * @return identifier of the active multiview element.
235      */

236     private static String JavaDoc getMultiviewActive() {
237         TopComponent activeTC = TopComponent.getRegistry().getActivated();
238         MultiViewHandler handler = MultiViews.findMultiViewHandler(activeTC);
239         if (handler != null) {
240             return handler.getSelectedPerspective().preferredID();
241         }
242         return null;
243     }
244
245     /**
246      * Finds the activated components of active TopComponent.
247      *
248      * @return collection of the active components.
249      */

250     private static Collection JavaDoc<Component> getActiveComponents() {
251         TopComponent activeTC = TopComponent.getRegistry().getActivated();
252         Collection JavaDoc<Component> activeComponents = Collections.emptySet();
253         for (Node node : activeTC.getActivatedNodes()) {
254             GetComponentCookie cake = (GetComponentCookie) node.
255                     getCookie(GetComponentCookie.class);
256             Component component = null;
257             if (cake != null) {
258                 component = cake.getComponent();
259             }
260             if (component == null) {
261                 component = (Component) node.getLookup().lookup(Component.class);
262             }
263             if (component != null) {
264                 if (activeComponents.isEmpty()) {
265                     activeComponents = new HashSet JavaDoc<Component>();
266                 }
267                 activeComponents.add(component);
268             }
269         }
270         return activeComponents;
271     }
272 }
273
Popular Tags