KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > treeeditor > DefinitionsNode


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.view.treeeditor;
21
22 import java.awt.Image JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.xml.XMLConstants JavaDoc;
31 import org.netbeans.modules.xml.wsdl.model.Definitions;
32 import org.netbeans.modules.xml.wsdl.model.Documentation;
33 import org.netbeans.modules.xml.wsdl.model.Types;
34 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
35 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
36 import org.netbeans.modules.xml.wsdl.ui.api.property.PropertyAdapter;
37 import org.netbeans.modules.xml.wsdl.ui.commands.NamedPropertyAdapter;
38 import org.netbeans.modules.xml.wsdl.ui.common.Constants;
39 import org.netbeans.modules.xml.wsdl.ui.cookies.WSDLDefinitionNodeCookie;
40 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
41 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
42 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.BindingNewType;
43 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.DocumentationNewType;
44 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.ExtensibilityElementNewTypesFactory;
45 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.MessageNewType;
46 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.NewTypesFactory;
47 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.PortTypeNewType;
48 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.ServiceNewType;
49 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.TypesNewType;
50 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
51 import org.openide.ErrorManager;
52 import org.openide.actions.NewAction;
53 import org.openide.actions.PropertiesAction;
54 import org.openide.explorer.ExplorerManager;
55 import org.openide.nodes.Node;
56 import org.openide.nodes.Sheet;
57 import org.openide.util.HelpCtx;
58 import org.openide.util.NbBundle;
59 import org.openide.util.Utilities;
60 import org.openide.util.actions.SystemAction;
61 import org.openide.util.datatransfer.NewType;
62 import org.openide.windows.TopComponent;
63 import com.sun.org.apache.xml.internal.utils.XMLChar;
64 import java.util.HashSet JavaDoc;
65 import java.util.Set JavaDoc;
66 import org.netbeans.modules.xml.wsdl.ui.view.property.BaseAttributeProperty;
67 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.ImportSchemaNewType;
68 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.ImportWSDLNewType;
69 import org.netbeans.modules.xml.xam.ui.actions.GoToAction;
70 import org.openide.actions.CopyAction;
71 import org.openide.actions.CutAction;
72 import org.openide.actions.PasteAction;
73
74 /**
75  *
76  * @author Ritesh Adval
77  *
78  *
79  */

80 public class DefinitionsNode extends WSDLExtensibilityElementNode {
81     
82     
83     private Image JavaDoc ICON = Utilities.loadImage
84     ("org/netbeans/modules/xml/wsdl/ui/netbeans/module/resources/wsdl16.png");
85     
86     private static final String JavaDoc TARGETNAMESPACE_PROP = "targetNamespace";//NOI18N
87

88     private DefinitionsPropertyAdapter mPropertyAdapter;
89     
90     private ExplorerManager mManager;
91     
92     private TopComponent mTopComponent;
93
94     private static final SystemAction[] ACTIONS = new SystemAction[]{
95         SystemAction.get(CutAction.class),
96         SystemAction.get(CopyAction.class),
97         SystemAction.get(PasteAction.class),
98         null,
99         SystemAction.get(NewAction.class),
100         null,
101 // Currently there are only, partnerlink type and property and property
102
// alias which is going to be put in main add.
103
// SystemAction.get(CommonAddExtensibiltyElementAction.class),
104
// null,
105
SystemAction.get(GoToAction.class),
106         null,
107         SystemAction.get(PropertiesAction.class)
108     };
109
110     public DefinitionsNode(Definitions mWSDLDef,
111             ExplorerManager manager,
112             TopComponent topComponent) {
113         this(mWSDLDef);
114         this.mManager = manager;
115         this.mTopComponent = topComponent;
116         
117     }
118     
119     public DefinitionsNode(Definitions mWSDLDef) {
120         super(new DefinitionsChildren(mWSDLDef), mWSDLDef, new DefinitionsNewTypesFactory());
121        
122         getLookupContents().add( new WSDLDefinitionNodeCookie(this));
123         
124         this.mPropertyAdapter = new DefinitionsPropertyAdapter();
125         super.setNamedPropertyAdapter(this.mPropertyAdapter);
126         
127     }
128     
129     public ExplorerManager getExplorerManager() {
130         return mManager;
131     }
132     
133     public TopComponent getTopComponent() {
134         return this.mTopComponent;
135     }
136     
137     @Override JavaDoc
138     public String JavaDoc getNameInLayer() {
139         return WSDLExtensibilityElements.ELEMENT_DEFINITIONS;
140     }
141     
142     @Override JavaDoc
143     public Image JavaDoc getIcon(int type) {
144         return ICON;
145     }
146     
147     @Override JavaDoc
148     public Image JavaDoc getOpenedIcon(int type) {
149         return ICON;
150     }
151     
152     @Override JavaDoc
153     public Action JavaDoc[] getActions(boolean context) {
154         return ACTIONS;
155     }
156
157     @Override JavaDoc
158     public boolean canCopy() {
159         return false;
160     }
161
162     @Override JavaDoc
163     public boolean canCut() {
164         return false;
165     }
166
167     protected void updateDisplayName() {
168         Definitions defs = (Definitions) getWSDLComponent();
169         String JavaDoc name = defs.getTargetNamespace();
170         if (name == null) {
171             name = NbBundle.getMessage(DefinitionsNode.class,
172                     "LBL_DefinitionsNode_NoTargetNamespace");
173         }
174         setDisplayName(name);
175     }
176
177     @Override JavaDoc
178     protected void refreshAttributesSheetSet() {
179         Sheet.Set ss = createPropertiesSheetSet();
180         
181         try {
182             //name
183
Node.Property nameProperty = new BaseAttributeProperty(mPropertyAdapter,
184                     String JavaDoc.class, NAME_PROP);
185             nameProperty.setName(NbBundle.getMessage(DefinitionsNode.class, "PROP_NAME_DISPLAYNAME"));
186             nameProperty.setShortDescription(NbBundle.getMessage(DefinitionsNode.class, "DEFINITIONSNODE_NAME_DESCRIPTION"));
187             ss.put(nameProperty);
188             
189             //targetNamespace
190
Node.Property targetNamespaceProperty = new BaseAttributeProperty(mPropertyAdapter,
191                     String JavaDoc.class, TARGETNAMESPACE_PROP);
192             targetNamespaceProperty.setName(NbBundle.getMessage(DefinitionsNode.class, "PROP_TARGET_NAMESPACE_DISPLAYNAME"));
193             targetNamespaceProperty.setShortDescription(NbBundle.getMessage(DefinitionsNode.class, "DEFINITIONSNODE_TARGETNAMESPACE_DESCRIPTION"));
194             ss.put(targetNamespaceProperty);
195             
196             
197             //default namespace
198
Node.Property defaultNamespaceProperty = new BaseAttributeProperty(mPropertyAdapter,
199                     String JavaDoc.class,"defaultNamespace"); //NOI18N
200
if(defaultNamespaceProperty.getValue() == null) {
201                 defaultNamespaceProperty.setValue(Constants.WSDL_DEFAUL_NAMESPACE);
202             }
203             
204             defaultNamespaceProperty.setName(NbBundle.getMessage(DefinitionsNode.class, "PROP_DEFAULT_NAMESPACE"));//NOI18N
205
defaultNamespaceProperty.setShortDescription(NbBundle.getMessage(DefinitionsNode.class, "DEFINITIONS_NODE_DEFAULTNAMESPACE_DESC"));
206             ss.put(defaultNamespaceProperty);
207             
208             //add prefixes sheet
209
refreshPrefixesSheetSet(Utility.getNamespaces((Definitions) getWSDLComponent()));
210             
211         } catch(Exception JavaDoc ex) {
212             mLogger.log(Level.SEVERE, "failed to create property sheet for "+ getWSDLComponent(), ex);
213         }
214     }
215     
216     private void refreshPrefixesSheetSet(Map JavaDoc prefixToNamespaceMap) throws Exception JavaDoc {
217         Sheet.Set prefixesSheetSet = createPrefixesSheetSet(prefixToNamespaceMap, (Definitions) getWSDLComponent());
218         //remove default namespace prefix since it is also added in properties
219
prefixesSheetSet.remove(NbBundle.getMessage(DefinitionsNode.class, "PROP_DEFAULT_NAMESPACE"));
220         
221         mSheet.remove(NbBundle.getMessage(DefinitionsNode.class, "PROP_SHEET_CATEGORY_PREFIXES"));
222         mSheet.put(prefixesSheetSet);
223     }
224     public static Sheet.Set createPrefixesSheetSet(final Map JavaDoc prefixToNamespaceMap, Definitions definitions) throws Exception JavaDoc {
225         
226         Sheet.Set prefixesSheetSet = new Sheet.Set();
227         String JavaDoc prefixesSheetName = NbBundle.getMessage(DefinitionsNode.class, "PROP_SHEET_CATEGORY_PREFIXES");
228         prefixesSheetSet.setName(prefixesSheetName);
229         prefixesSheetSet.setDisplayName(prefixesSheetName);
230         
231         Iterator JavaDoc it = prefixToNamespaceMap.keySet().iterator();
232         while(it.hasNext()) {
233             final String JavaDoc prefix = (String JavaDoc) it.next();
234             PropertyAdapter pn = null;
235             Node.Property prefixToNamespaceProperty = null;
236             
237             
238             pn = new PrefixToNamespace(prefix, definitions);
239             
240             
241             
242             prefixToNamespaceProperty = new BaseAttributeProperty(pn, String JavaDoc.class, "namespace"); //NOI18N
243
if(prefix.equals(XMLConstants.XMLNS_ATTRIBUTE) || prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
244                 //prefixToNamespaceProperty.setName(XMLConstants.XMLNS_ATTRIBUTE);
245
} else {
246                 prefixToNamespaceProperty.setName(prefix);
247                 prefixToNamespaceProperty.setShortDescription(NbBundle.getMessage(DefinitionsNode.class, "DEFINITIONSNODE_USER_DEF_PREFIX_DESC"));
248                 prefixesSheetSet.put(prefixToNamespaceProperty);
249             }
250
251         }
252         
253         return prefixesSheetSet;
254     }
255     
256     public static class PrefixToNamespace extends PropertyAdapter {
257         
258         private Definitions mWSDLConstruct;
259         
260         private String JavaDoc mPrefix;
261         
262         public PrefixToNamespace(String JavaDoc prefix, Definitions definitions) {
263             super(definitions);
264             this.mPrefix = prefix;
265             this.mWSDLConstruct = definitions;
266         }
267         
268         public String JavaDoc getPrefix() {
269             return this.mPrefix;
270         }
271         
272         public void setPrefix(String JavaDoc prefix) {
273             
274             String JavaDoc namespace = getNamespace();
275             mWSDLConstruct.getModel().startTransaction();
276             ((AbstractDocumentComponent) mWSDLConstruct).removePrefix(mPrefix);
277             ((AbstractDocumentComponent) mWSDLConstruct).addPrefix(prefix, namespace);
278                 mWSDLConstruct.getModel().endTransaction();
279             mPrefix = prefix;
280         }
281         
282         public void setNamespace(String JavaDoc namespace) {
283             mWSDLConstruct.getModel().startTransaction();
284             ((AbstractDocumentComponent) mWSDLConstruct).addPrefix(mPrefix, namespace);
285                 mWSDLConstruct.getModel().endTransaction();
286         }
287         
288         public String JavaDoc getNamespace() {
289             return Utility.getNamespaceURI(this.mPrefix, mWSDLConstruct);
290         }
291     }
292     public static final class DefinitionsChildren extends GenericWSDLComponentChildren {
293         
294         public DefinitionsChildren(Definitions definitions) {
295             super(definitions);
296             //specialTargetNamespacesSet.addAll(BPELExtensibilityElementsFolderNode.getSupportedExtensionNamespaceSet());
297
}
298         
299         @Override JavaDoc
300         protected Node[] createNodes(Object JavaDoc key) {
301             Node node = null;
302            
303             if (IMPORTS_FOLDER.equals(key)) {
304                 node = new ImportFolderNode((Definitions) getWSDLComponent());
305             }
306             if (MESSAGES_FOLDER.equals(key)) {
307                 node = new MessageFolderNode((Definitions) getWSDLComponent());
308             }
309             if (PORTTYPES_FOLDER.equals(key)) {
310                 node = new PortTypeFolderNode((Definitions) getWSDLComponent());
311             }
312             if (BINDING_FOLDER.equals(key)) {
313                 node = new BindingFolderNode((Definitions) getWSDLComponent());
314             }
315             if (SERVICES_FOLDER.equals(key)) {
316                 node = new ServiceFolderNode((Definitions) getWSDLComponent());
317             }
318             
319             if (EXTENSIBILITY_ELEMENTS_FOLDER.equals(key)) {
320                 node = new ExtensibilityElementsFolderNode((Definitions) getWSDLComponent());
321             }
322             
323             
324             if(node != null) {
325                 return new Node[] {node};
326             }
327             return super.createNodes(key);
328         }
329         
330         @Override JavaDoc
331         protected Collection JavaDoc getKeys() {
332             Collection JavaDoc<Object JavaDoc> keys = new ArrayList JavaDoc<Object JavaDoc>();
333             
334             Definitions def = ((Definitions) getWSDLComponent());
335             Documentation doc = def.getDocumentation();
336             if (doc != null) {
337                 keys.add(doc);
338             }
339             Types types = def.getTypes();
340             if (types != null) {
341                 keys.add(types);
342             }
343 /* Collection imports = def.getImports();
344             if (imports != null && imports.size() > 0) {*/

345                 keys.add(IMPORTS_FOLDER);
346 // }
347
keys.add(MESSAGES_FOLDER);
348             keys.add(PORTTYPES_FOLDER);
349             keys.add(BINDING_FOLDER);
350             keys.add(SERVICES_FOLDER);
351             
352             
353             keys.add(EXTENSIBILITY_ELEMENTS_FOLDER);
354
355             return keys;
356         }
357         
358         
359         
360     }
361     
362     public static final String JavaDoc IMPORTS_FOLDER = "IMPORTS_FOLDER"; //NOI18N
363
public static final String JavaDoc MESSAGES_FOLDER = "MESSAGES_FOLDER";//NOI18N
364
public static final String JavaDoc PORTTYPES_FOLDER = "PORTTYPES_FOLDER";//NOI18N
365
public static final String JavaDoc BINDING_FOLDER = "BINDING_FOLDER";//NOI18N
366
public static final String JavaDoc SERVICES_FOLDER = "SERVICES_FOLDER";//NOI18N
367
public static final String JavaDoc EXTENSIBILITY_ELEMENTS_FOLDER = "EXTENSIBILITY_ELEMENTS_FOLDER";//NOI18N
368
//public static final String PREFIXES_FOLDER = "PREFIXES_FOLDER";//NOI18N
369

370     
371     public class DefinitionsPropertyAdapter extends PropertyAdapter implements NamedPropertyAdapter {
372         
373         public DefinitionsPropertyAdapter() {
374             super(getWSDLComponent());
375         }
376         
377         public void setName(String JavaDoc name) {
378             if (name != null && name.trim().length() == 0) {
379                 name = null;
380             }
381             
382             if (name != null && !XMLChar.isValidNCName(name)) {
383                 ErrorManager.getDefault().notify(new Exception JavaDoc(NbBundle.getMessage(DefinitionsNode.class, "ERR_MSG_INVALID_NMTOKEN")));
384                 return;
385             }
386             getWSDLComponent().getModel().startTransaction();
387             ((Definitions) getWSDLComponent()).setName(name);
388             getWSDLComponent().getModel().endTransaction();
389         }
390         
391         public String JavaDoc getName() {
392             String JavaDoc name = ((Definitions)getWSDLComponent()).getName();
393             if(name == null) {
394                 return "";
395             }
396             
397             return name;
398         }
399         
400         public void setTargetNamespace(String JavaDoc targetNamespace) {
401             getWSDLComponent().getModel().startTransaction();
402             ((Definitions) getWSDLComponent()).setTargetNamespace(targetNamespace);
403                 getWSDLComponent().getModel().endTransaction();
404         }
405         
406         public String JavaDoc getTargetNamespace() {
407             String JavaDoc tns = ((Definitions)getWSDLComponent()).getTargetNamespace();
408             if(tns == null) {
409                 return "";
410             }
411             
412             return tns;
413         }
414         
415         public void setDefaultNamespace(String JavaDoc defaultNamespace) {
416             WSDLModel model = getWSDLComponent().getModel();
417             model.startTransaction();
418             ((AbstractDocumentComponent)getWSDLComponent()).addPrefix(XMLConstants.DEFAULT_NS_PREFIX, defaultNamespace);//getPrefixes().put("", defaultNamespace);
419
model.endTransaction();
420         }
421         
422         public String JavaDoc getDefaultNamespace() {
423             return (String JavaDoc) ((AbstractDocumentComponent) getWSDLComponent()).getPrefixes().get(XMLConstants.DEFAULT_NS_PREFIX);
424         }
425     }
426     
427     public static final class DefinitionsNewTypesFactory implements NewTypesFactory{
428         
429         public NewType[] getNewTypes(WSDLComponent def) {
430             
431             ArrayList JavaDoc<NewType> list = new ArrayList JavaDoc<NewType>();
432             if (def.getDocumentation() == null) {
433                 list.add(new DocumentationNewType(def));
434             }
435             if (((Definitions) def).getTypes() == null) {
436                 list.add(new TypesNewType(def));
437             }
438             list.add(new ImportSchemaNewType(def));
439             list.add(new ImportWSDLNewType(def));
440             list.add(new MessageNewType(def));
441             list.add(new PortTypeNewType(def));
442             list.add(new BindingNewType(def));
443             list.add(new ServiceNewType(def));
444             list.addAll(Arrays.asList(new ExtensibilityElementNewTypesFactory(WSDLExtensibilityElements.ELEMENT_DEFINITIONS).getNewTypes(def)));
445
446             return list.toArray(new NewType[list.size()]);
447         }
448     }
449
450     @Override JavaDoc
451     public HelpCtx getHelpCtx() {
452         return new HelpCtx(DefinitionsNode.class);
453     }
454
455     @Override JavaDoc
456     public String JavaDoc getTypeDisplayName() {
457         return NbBundle.getMessage(DefinitionsNode.class, "LBL_DefinitionsNode_TypeDisplayName");
458     }
459     
460     
461 }
462
Popular Tags