KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.swing.text.StyledDocument JavaDoc;
33
34 import org.netbeans.modules.xml.schema.model.Schema;
35 import org.netbeans.modules.xml.schema.model.SchemaComponent;
36 import org.netbeans.modules.xml.schema.ui.nodes.SchemaComponentNode;
37 import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategoryNode;
38 import org.netbeans.modules.xml.wsdl.model.Definitions;
39 import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
40 import org.netbeans.modules.xml.wsdl.model.Types;
41 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
42 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
43 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
44 import org.netbeans.modules.xml.wsdl.ui.validation.ValidationAnnotation;
45 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.FolderNode;
46 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.WSDLElementNode;
47 import org.netbeans.modules.xml.xam.ModelSource;
48 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
49 import org.netbeans.modules.xml.xam.ui.customizer.Customizer;
50 import org.openide.DialogDescriptor;
51 import org.openide.cookies.EditCookie;
52 import org.openide.cookies.LineCookie;
53 import org.openide.nodes.Node;
54 import org.openide.text.Line;
55 import org.openide.text.NbDocument;
56 import org.openide.util.Lookup;
57 import org.openide.util.NbBundle;
58
59 /**
60  *
61  * @author Ajit Bhate
62  */

63 public class UIUtilities {
64
65     /** Creates a new instance of UIUtilities */
66     private UIUtilities() {
67     }
68
69     /**
70      * finds path of given component from root node
71      */

72     public static List JavaDoc<Node> findPathFromRoot(Node root, WSDLComponent sc) {
73         if (sc.getModel() == null) {
74             return Collections.emptyList();
75         }
76         ArrayList JavaDoc<WSDLComponent> path = new ArrayList JavaDoc<WSDLComponent>();
77         WSDLComponent parent = sc;
78         while (parent != null) {
79             path.add(0,parent);
80             parent = parent.getParent();
81         }
82         if (path.get(0) != sc.getModel().getDefinitions()) {
83             return Collections.emptyList();
84         }
85         WSDLElementNode rootSCN = (WSDLElementNode) root.getCookie(
86                 WSDLElementNode.class);
87         if (rootSCN == null || !rootSCN.isSameAsMyWSDLElement(path.get(0))) {
88             return Collections.emptyList();
89         }
90         ArrayList JavaDoc<Node> selectionPath = new ArrayList JavaDoc<Node>();
91         selectionPath.add(root);
92         path.remove(0);
93         Node parentNode = root;
94         for (int ii = 0; ii < path.size(); ii++) {
95             WSDLComponent comp = path.get(ii);
96             List JavaDoc<Node> subPath = null;
97             Node[] children = parentNode.getChildren().getNodes();
98             if (children == null || children.length < 0) {
99                 return selectionPath;
100             }
101             for (Node n : children) {
102                 WSDLElementNode node = (WSDLElementNode) n.getCookie(
103                         WSDLElementNode.class);
104                 if (node != null) {
105                     WSDLComponent scomp = node.getWSDLComponent();
106                     int idx = path.indexOf(scomp);
107                     if (idx >= ii) {
108                         subPath = Collections.singletonList(n);
109                         parentNode = n;
110                         ii=idx;
111                         break;
112                     }
113                 }
114             }
115             if (subPath == null) {
116                 for (Node n : children) {
117                     FolderNode cNode = (FolderNode) n.getCookie(FolderNode.class);
118                     if (cNode != null && cNode.getChildType().isInstance(comp)) {
119                         Node[] nChildren = n.getChildren().getNodes();
120                         if (nChildren != null && nChildren.length > 0) {
121                             for (Node nChild : nChildren) {
122                                 WSDLElementNode node = (WSDLElementNode) nChild.
123                                         getCookie(WSDLElementNode.class);
124                                 if (node != null) {
125                                     WSDLComponent scomp = node.getWSDLComponent();
126                                     int idx = path.indexOf(scomp);
127                                     if (idx >= ii) {
128                                         subPath = new ArrayList JavaDoc<Node>();
129                                         subPath.add(n);
130                                         subPath.add(nChild);
131                                         parentNode = nChild;
132                                         ii = idx;
133                                         break;
134                                     }
135                                 }
136                             }
137                         }
138                         break;
139                     }
140                 }
141             }
142             if (subPath == null) {
143                 break;
144             }
145             selectionPath.addAll(subPath);
146         }
147         return selectionPath;
148     }
149
150     
151     /**
152      * finds path of given sceham component from root node representing its schema
153      * example
154      * 1. Schema->GlobalElement->ComplexType->Sequence
155      * results in SchemaNode->Elements Category Node->GlobalElement Node ->
156      * Complex Type Node -> Sequence Node
157      * 2. Schema->GlobalElement->ComplexType->Complex Content->Extension
158      * results in SchemaNode->Elements Category Node->GlobalElement Node ->
159      * Complex Type Node
160      * (as complex content and extension nodes are not shown)
161      */

162     public static List JavaDoc<Node> findPathFromRoot(Node root, SchemaComponent sc, WSDLModel model)
163     {
164         if (sc.getModel() == null) {
165             return Collections.emptyList();
166         }
167         
168         ArrayList JavaDoc<SchemaComponent> path = new ArrayList JavaDoc<SchemaComponent>();
169         SchemaComponent parent = sc;
170         while(parent!=null)
171         {
172             path.add(0, parent);
173             parent = parent.getParent();
174         }
175         
176         //Expecting schema as the root for this schemacomponent.
177
if (!(path.get(0) instanceof Schema)) return Collections.emptyList();
178         
179         Types types = model.getDefinitions().getTypes();
180         Collection JavaDoc<WSDLSchema> schemas = types.getExtensibilityElements(WSDLSchema.class);
181         
182         WSDLSchema wsdlSchema = null;
183         for (WSDLSchema schema : schemas) {
184             if (path.get(0) == schema.getSchemaModel().getSchema()) {
185                 wsdlSchema = schema;
186                 break;
187             }
188         }
189         
190         //Should be able to find a schema in the wsdl which contains this SchemaComponent.
191
if (wsdlSchema == null) return Collections.emptyList();
192         
193         //get the wsdlschema
194
List JavaDoc<Node> wsdlPathTillTypes = findPathFromRoot(root, types);
195         
196         if (wsdlPathTillTypes == null || wsdlPathTillTypes.isEmpty()) {
197             return Collections.emptyList();
198         }
199         
200         WSDLElementNode rootNode = (WSDLElementNode) root.getCookie(WSDLElementNode.class);
201         if (rootNode == null || rootNode != wsdlPathTillTypes.get(0)) {
202             return Collections.emptyList();
203         }
204         
205         
206         Node typesNode = wsdlPathTillTypes.get(wsdlPathTillTypes.size() - 1);
207         Node[] schemaNodes = typesNode.getChildren().getNodes();
208         Node schemaRootNode = null;
209         if (schemaNodes != null) {
210             for (Node schemaNode : schemaNodes) {
211                 SchemaComponentNode schemaCNode = (SchemaComponentNode) schemaNode.getCookie(SchemaComponentNode.class);
212                 if (schemaCNode.getReference().get().equals(wsdlSchema.getSchemaModel().getSchema())) {
213                     schemaRootNode = schemaNode;//pass the filter node, not the schemacomponentnode for the schema. fix for IZ 84466
214
break;
215                 }
216             }
217         }
218         
219         ArrayList JavaDoc<Node> selectionPath = new ArrayList JavaDoc<Node>();
220         selectionPath.add(schemaRootNode);
221         Class JavaDoc<? extends SchemaComponent> categoryType = null;
222         if(path.size() > 1) categoryType = path.get(1).getComponentType();
223         Node parentNode = schemaRootNode;
224         for(int i = 0; i < path.size(); i++)
225         {
226             SchemaComponent comp = path.get(i);
227             List JavaDoc<Node> subPath = null;
228             Node[] children = parentNode.getChildren().getNodes();
229             if (children == null || children.length < 0)
230             {
231                 return selectionPath;
232             }
233             if (subPath==null)
234             {
235                 for (Node n : children)
236                 {
237                     CategoryNode cNode = (CategoryNode) n.getCookie(CategoryNode.class);
238                     if (cNode != null && cNode.getChildType()==categoryType)
239                     {
240                         Node[] nChildren = n.getChildren().getNodes();
241                         if (nChildren != null && nChildren.length > 0)
242                         {
243                             for (Node nChild : nChildren)
244                             {
245                                 SchemaComponentNode node = (SchemaComponentNode) nChild.
246                                         getCookie(SchemaComponentNode.class);
247                                 if (node != null)
248                                 {
249                                     SchemaComponent scomp = node.getReference().get();
250                                     int idx = path.indexOf(scomp);
251                                     if (idx>=i)
252                                     {
253                                         subPath = new ArrayList JavaDoc<Node>();
254                                         subPath.add(n);
255                                         subPath.add(nChild);
256                                         parentNode = nChild;
257                                         i=idx;
258                                         if(i!=0&&i<path.size()) categoryType =
259                                                 path.get(i).getComponentType();
260                                         break;
261                                     }
262                                 }
263                             }
264                         }
265                         break;
266                     }
267                 }
268             }
269             if (subPath==null)
270             {
271                 for (Node n : children)
272                 {
273                     SchemaComponentNode node = (SchemaComponentNode) n.getCookie(
274                             SchemaComponentNode.class);
275                     if (node != null)
276                     {
277                         SchemaComponent scomp = node.getReference().get();
278                         int idx = path.indexOf(scomp);
279                         if (idx>=i)
280                         {
281                             subPath = Collections.singletonList(n);
282                             parentNode = n;
283                             i=idx;
284                             if(i!=0&&i<path.size()) categoryType =
285                                     path.get(i).getComponentType();
286                             break;
287                         }
288                     }
289                 }
290             }
291             if (subPath==null) break;
292             selectionPath.addAll(subPath);
293         }
294         wsdlPathTillTypes.addAll(selectionPath);
295         return wsdlPathTillTypes;
296     }
297     
298     /**
299      * Searches for node representing given component, under given node.
300      * Search is recusrive.
301      */

302     public static Node findNode(Node parent, ReferenceableWSDLComponent sc, WSDLModel currentModel) {
303         if (parent == null) return null;
304         Node[] children = parent.getChildren().getNodes();
305         if(children == null || children.length<0) return null;
306 // boolean primitive = sc.getModel()==
307
// WSDLModelFactory.getDefault().getPrimitiveTypesModel();
308
boolean externalRef = /*!primitive&&*/sc.getModel()!=currentModel;
309         for (Node n:children) {
310             WSDLElementNode node =(WSDLElementNode)n.
311                     getCookie(WSDLElementNode.class);
312             WSDLComponent scomp = node==null?null:node.getWSDLComponent();
313             if (scomp == sc) {
314                 return n;
315             }
316 // if(primitive)
317
// {
318
// if (n instanceof PrimitiveSimpleTypesNode)
319
// {
320
// return findNode(n,sc,currentModel);
321
// }
322
// }
323
if(externalRef) {
324                 if(scomp instanceof Definitions && scomp.getModel() == sc.getModel()) {
325                     return findNode(n,sc,scomp.getModel());
326                 }
327 // if(scomp instanceof WSDLModelReference)
328
// {
329
// Node result = findNode(n,sc,currentModel);
330
// if (result != null) return result;
331
// }
332
}
333 // if(scomp!=null) continue;
334
// CategoryNode cNode = (CategoryNode)n.getCookie(
335
// CategoryNode.class);
336
// if(cNode==null) continue;
337
// if(!externalRef && cNode.getChildType().
338
// isAssignableFrom(sc.getComponentType()))
339
// {
340
// return findNode(n,sc,currentModel);
341
// }
342
// else if(externalRef&& SchemaModelReference.class.
343
// isAssignableFrom(cNode.getChildType()))
344
// {
345
// return findNode(n,sc,currentModel);
346
// }
347
}
348         return null;
349     }
350
351     private static WSDLElementNode findWSDLComponentNode(Node node) {
352         WSDLElementNode scn =(WSDLElementNode)node
353                 .getCookie(WSDLElementNode.class);
354         if(scn!=null) return scn;
355         Node parent = node;
356         while((parent=parent.getParentNode())!=null) {
357             scn = (WSDLElementNode)parent
358                     .getCookie(WSDLElementNode.class);
359             if(scn!=null) return scn;
360         }
361         return null;
362     }
363
364     public static <T extends ReferenceableWSDLComponent> T findReferenceable(Node node,
365             Class JavaDoc<T> type) {
366         WSDLElementNode scn =findWSDLComponentNode(node);
367         if(scn==null) return null;
368         WSDLComponent component =
369                 scn.getWSDLComponent();
370         if(type.isInstance(component))
371             return type.cast(component);
372         while((component=component.getParent())!=null)
373             if(type.isInstance(component))
374                 return type.cast(component);
375         return null;
376     }
377
378     /**
379      * Create a dialog to contain a "creator" customizer (a customizer that
380      * is used to create something that does not yet exist), which generally
381      * means the title will be "Add Xyz".
382      *
383      * @return dialog description with appropriate title.
384      */

385     public static DialogDescriptor getCreatorDialog(
386             Customizer customizer, String JavaDoc title, boolean editable) {
387         title = NbBundle.getMessage(UIUtilities.class,
388                 "TITLE_WSDLElementNode_Creator", title);
389         return getCustomizerDialog0(customizer, title, editable);
390     }
391
392     /**
393      * Create a dialog to contain a customizer (a customizer that is used
394      * to edit something that already exists), which generally means the
395      * title will be "Xyz Customizer".
396      *
397      * @return dialog description with appropriate title.
398      */

399     public static DialogDescriptor getCustomizerDialog(
400             Customizer customizer, String JavaDoc title, boolean editable) {
401         title = NbBundle.getMessage(UIUtilities.class,
402                 "TITLE_WSDLElementNode_Customizer", title);
403         return getCustomizerDialog0(customizer, title, editable);
404     }
405
406     private static DialogDescriptor getCustomizerDialog0(
407             final Customizer customizer, final String JavaDoc title, final boolean editable) {
408         java.awt.Component JavaDoc component = customizer.getComponent();
409         final DialogDescriptor descriptor = new DialogDescriptor(component,
410                 title, true, null);
411         descriptor.setHelpCtx(customizer.getHelpCtx());
412         if(editable) {
413             // customizer's property change listener to enable/disable OK
414
final PropertyChangeListener JavaDoc pcl = new PropertyChangeListener JavaDoc() {
415                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
416                     if(evt.getSource()==customizer && evt.getPropertyName().
417                             equals(Customizer.PROP_ACTION_APPLY)) {
418                         descriptor.setValid(((Boolean JavaDoc) evt.getNewValue()).booleanValue());
419                     }
420                 }
421             };
422             customizer.addPropertyChangeListener(pcl);
423             // dialog's action listener
424
ActionListener JavaDoc al = new ActionListener JavaDoc() {
425                 public void actionPerformed(ActionEvent JavaDoc evt) {
426                     if (evt.getSource().equals(DialogDescriptor.OK_OPTION) ||
427                             evt.getSource().equals(DialogDescriptor.CANCEL_OPTION) ||
428                             evt.getSource().equals(DialogDescriptor.CLOSED_OPTION)) {
429                         customizer.removePropertyChangeListener(pcl);
430                     }
431                     if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) {
432                         try {
433                             customizer.apply();
434                         } catch (IOException JavaDoc ioe) {
435                         }
436                     }
437                 }
438             };
439             descriptor.setButtonListener(al);
440         }
441         return descriptor;
442     }
443     
444     /**
445      * annotates the source view and if shoudShowSource is true, then jumps to the line in the source editor
446      *
447      * @param dobj
448      * @param wsdlComp the component with error
449      * @param errorMessage the errormessage
450      * @param shouldShowSource whether should jump to source editor
451      */

452     public static void annotateSourceView(WSDLDataObject dobj, DocumentComponent wsdlComp, String JavaDoc errorMessage, boolean shouldShowSource) {
453         LineCookie lc = (LineCookie) dobj.getCookie(LineCookie.class);
454         EditCookie ec = (EditCookie) dobj.getCookie(EditCookie.class);
455         if (lc == null || ec == null) {
456             return;
457         }
458         ec.edit();
459         ValidationAnnotation.clearAll();
460         int lineNum = getLineNumber(wsdlComp);
461         if (lineNum < 1) {
462             return;
463         }
464         
465         Line l = lc.getLineSet().getCurrent(lineNum);
466         if (errorMessage != null) {
467             ValidationAnnotation annotation = ValidationAnnotation.getNewInstance();
468             annotation.setErrorMessage(errorMessage);
469             annotation.attach( l );
470             l.addPropertyChangeListener( annotation );
471         }
472         
473         if (shouldShowSource) {
474             l.show(Line.SHOW_GOTO);
475         }
476     }
477     
478     public static int getLineNumber(DocumentComponent comp) {
479         int position = comp.findPosition();
480         ModelSource modelSource = comp.getModel().getModelSource();
481         assert modelSource != null;
482         Lookup lookup = modelSource.getLookup();
483         
484         StyledDocument JavaDoc document = (StyledDocument JavaDoc)lookup.lookup(StyledDocument JavaDoc.class);
485         if (document == null) {
486             return -1;
487         }
488         return NbDocument.findLineNumber(document,position);
489     }
490 }
491
Popular Tags