KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Created on May 26, 2005
22  *
23  * To change the template for this generated file go to
24  * Window - Preferences - Java - Code Generation - Code and Comments
25  */

26 package org.netbeans.modules.xml.wsdl.ui.view.treeeditor;
27
28 import java.awt.Image JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.swing.Action JavaDoc;
34 import javax.xml.namespace.QName JavaDoc;
35 import org.netbeans.modules.refactoring.api.ui.RefactoringActionsFactory;
36
37 //import org.netbeans.modules.xml.refactoring.actions.FindUsagesAction;
38
//import org.netbeans.modules.xml.refactoring.actions.RefactorAction;
39
import org.netbeans.modules.xml.schema.model.Attribute;
40 import org.netbeans.modules.xml.schema.model.Element;
41 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
42 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
43 import org.netbeans.modules.xml.wsdl.ui.actions.RemoveAttributesAction;
44 import org.netbeans.modules.xml.wsdl.ui.actions.extensibility.AddAttributeAction;
45 import org.netbeans.modules.xml.wsdl.ui.commands.ConstraintNamedPropertyAdapter;
46 import org.netbeans.modules.xml.wsdl.ui.commands.NamedPropertyAdapter;
47 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElement;
48 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementInfo;
49 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
50 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementsFactory;
51 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.impl.ExtensibilityUtils;
52 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
53 import org.netbeans.modules.xml.wsdl.ui.property.view.PropertyViewFactory;
54 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.SchemaDocumentationFinderVisitor;
55 import org.netbeans.modules.xml.wsdl.ui.spi.ExtensibilityElementConfigurator;
56 import org.netbeans.modules.xml.wsdl.ui.spi.ExtensibilityElementConfiguratorFactory;
57 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.ExtensibilityElementChildNewTypesFactory;
58 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.NewTypesFactory;
59 import org.netbeans.modules.xml.xam.NamedReferenceable;
60 import org.netbeans.modules.xml.xam.ui.XAMUtils;
61 import org.netbeans.modules.xml.xam.ui.actions.GoToAction;
62 import org.openide.ErrorManager;
63 import org.openide.actions.CopyAction;
64 import org.openide.actions.CutAction;
65 import org.openide.actions.DeleteAction;
66 import org.openide.actions.NewAction;
67 import org.openide.actions.PasteAction;
68 import org.openide.actions.PropertiesAction;
69 import org.openide.nodes.Node;
70 import org.openide.nodes.Sheet;
71 import org.openide.util.Utilities;
72 import org.openide.util.actions.SystemAction;
73
74
75
76 /**
77  * @author radval
78  *
79  * To change the template for this generated type comment go to
80  * Window - Preferences - Java - Code Generation - Code and Comments
81  */

82 public class ExtensibilityElementNode extends WSDLNamedElementNode{
83
84     
85     private static final Image JavaDoc ICON = Utilities.loadImage
86         ("org/netbeans/modules/xml/wsdl/ui/view/resources/generic.png");
87
88     
89     private ExtensibilityElement mWSDLConstruct;
90    
91     private Node mLayerDelegateNode;
92     
93     private WSDLExtensibilityElement mExtensibilityElement;
94     
95     private Element mSchemaElement;
96     
97     private boolean canRename = false;
98     
99     private QName JavaDoc mQName = null;
100     
101     private ExtensibilityElementConfigurator mConfigurator;
102     
103     public ExtensibilityElementNode(ExtensibilityElement wsdlConstruct) {
104         super(new GenericWSDLComponentChildren(wsdlConstruct), wsdlConstruct);
105         mWSDLConstruct = wsdlConstruct;
106         QName JavaDoc qName = mWSDLConstruct.getQName();
107         //Fix qname, sometimes there is no namespace associated with it.
108
String JavaDoc namespace = null;
109         if (qName.getPrefix() != null) {
110             namespace = Utility.getNamespaceURI(qName.getPrefix(), mWSDLConstruct);
111             mQName = new QName JavaDoc(namespace, qName.getLocalPart(), qName.getPrefix());
112         } else {
113             mQName = qName;
114         }
115         
116         String JavaDoc displayName = mQName != null ? Utility.fromQNameToString(mQName) : "Missing Name";
117         mConfigurator = new ExtensibilityElementConfiguratorFactory().getExtensibilityElementConfigurator(mQName);
118         boolean isNameSet = false;
119         if (isNamedReferenceable()) {
120             setNamedPropertyAdapter(new ExtensibilityElementConstrainedNamedPropertyAdapter());
121             canRename = true;
122         } else {
123             if (mConfigurator != null) {
124                 String JavaDoc attributeName = mConfigurator.getDisplayAttributeName(wsdlConstruct, mQName);
125                 
126                 if (attributeName != null) {
127                     setNamedPropertyAdapter(attributeName, new ExtensibilityElementNamedPropertyAdapter(attributeName));
128                     String JavaDoc value = wsdlConstruct.getAttribute(attributeName);
129                     if (value != null && value.trim().length() > 0) {
130                         isNameSet = true;
131                         setDisplayName(value);
132                     }
133                     canRename = true;
134                 }
135             }
136             
137             if (!isNameSet) {
138                 this.setDisplayName(displayName);
139                 setShortDescription(mQName != null ? mQName.toString() : "Missing Name");
140             }
141         }
142         try {
143             WSDLComponent parentComponent = mWSDLConstruct.getParent();
144             String JavaDoc extensibilityElementType = ExtensibilityUtils.getExtensibilityElementType(parentComponent);
145             if(extensibilityElementType != null) {
146                 WSDLExtensibilityElements elements = WSDLExtensibilityElementsFactory.getInstance().getWSDLExtensibilityElements();
147                 mExtensibilityElement = elements.getWSDLExtensibilityElement(extensibilityElementType);
148                 if(mExtensibilityElement != null) {
149                     WSDLExtensibilityElementInfo info = mExtensibilityElement.getWSDLExtensibilityElementInfos(mQName);
150                     if(info != null && info.getElement() != null) {
151                         this.mLayerDelegateNode = getLayerDelegateNode(info);
152                         
153                         this.mSchemaElement = info.getElement();
154                         /*SchemaElementCookie sCookie = new SchemaElementCookie(mElement);
155                         getLookupContents().add(sCookie);*/

156                     }
157                 }
158             } else {
159                 mSchemaElement = ExtensibilityUtils.getElement(mWSDLConstruct);
160                 
161 /* SchemaElementCookie sCookie = (SchemaElementCookie) parent.getCookie(SchemaElementCookie.class);
162                 if(sCookie != null && sCookie.getElement() != null) {
163                     Element parentElement = sCookie.getElement();
164                     if(parentElement != null) {
165                         SchemaElementFinderVisitor seFinder = new SchemaElementFinderVisitor(qName.getLocalPart());
166                         parentElement.accept(seFinder);
167                         this.mElement = seFinder.getSuccessorElement();
168                         if(this.mElement != null) {
169                             sCookie = new SchemaElementCookie(mElement);
170                             getLookupContents().add(sCookie);
171                         }
172                     }
173                     XMLType type = parentElement.getType();
174                      if(type instanceof ComplexType) {
175                      ComplexType cType = (ComplexType) type;
176                      this.mElement = cType.getElementDecl(this.mWSDLConstruct.getLocalName());
177                      if(this.mElement != null) {
178                      sCookie = new SchemaElementCookie(mElement);
179                      getLookupContents().add(sCookie);
180                      }
181                      }
182                 } else {
183                     mLogger.warning("Failed to find SchemaElementCookie in parent " + wsdlConstruct.toString() + " or SchemaElementCookie has null schema element");
184                 }*/

185             }
186         } catch(Exception JavaDoc ex) {
187             ErrorManager.getDefault().notify(ex);
188         }
189         
190         if(this.mSchemaElement != null) {
191             SchemaDocumentationFinderVisitor sdFinder = new SchemaDocumentationFinderVisitor();
192             this.mSchemaElement.accept(sdFinder);
193             String JavaDoc docStr = sdFinder.getDocumentation();
194             if(docStr != null && !docStr.trim().equals("")) {
195                 this.setShortDescription(docStr);
196                 
197             }
198         }
199         
200         
201         this.mWSDLConstruct.getModel().addComponentListener(this);
202         
203     }
204     
205     private Node getLayerDelegateNode(WSDLExtensibilityElementInfo elementInfo) {
206         Node delegateNode = elementInfo.getDataObject().getNodeDelegate();
207         return delegateNode;
208     }
209     
210     private boolean isNamedReferenceable() {
211         if (mWSDLConstruct instanceof NamedReferenceable) {
212             return true;
213         }
214         return false;
215     }
216     
217     @Override JavaDoc
218     public Image JavaDoc getIcon(int type) {
219         if(mLayerDelegateNode != null) {
220             return mLayerDelegateNode.getIcon(type);
221         }
222         return ICON;
223     }
224
225     @Override JavaDoc
226     public Image JavaDoc getOpenedIcon(int type) {
227         if(mLayerDelegateNode != null) {
228             return mLayerDelegateNode.getOpenedIcon(type);
229         }
230         return ICON;
231     }
232     
233     @Override JavaDoc
234     public Action JavaDoc[] getActions(boolean context) {
235         return createDynamicActions();
236     }
237     
238     
239     
240     @Override JavaDoc
241     public NewTypesFactory getNewTypesFactory() {
242         return new ExtensibilityElementChildNewTypesFactory(mSchemaElement);
243     }
244
245     @Override JavaDoc
246     public boolean canRename() {
247         return canRename;
248     }
249
250     private Action JavaDoc[] createDynamicActions() {
251         List JavaDoc<Action JavaDoc> actions = new ArrayList JavaDoc<Action JavaDoc>();
252         
253         //add these always
254
actions.add(SystemAction.get(CutAction.class));
255         actions.add(SystemAction.get(CopyAction.class));
256         actions.add(SystemAction.get(PasteAction.class));
257         actions.add(null);
258         actions.add(SystemAction.get(NewAction.class));
259         actions.add(SystemAction.get(DeleteAction.class));
260         actions.add(null);
261         //if optional attributes missing then add this action
262
if (mSchemaElement != null) {
263             if (Utils.isExtensionAttributesAllowed(mSchemaElement)) {
264                 actions.add(SystemAction.get(AddAttributeAction.class));
265                 actions.add(SystemAction.get(RemoveAttributesAction.class));
266                 actions.add(null);
267             }
268             actions.add(SystemAction.get(GoToAction.class));
269             if (isNamedReferenceable()) {
270                // actions.add(SystemAction.get(FindUsagesAction.class));
271
actions.add(RefactoringActionsFactory.whereUsedAction());
272                 actions.add(null);
273                 actions.add(RefactoringActionsFactory.editorSubmenuAction());
274             }
275             actions.add(null);
276             actions.add(SystemAction.get(PropertiesAction.class));
277         } else {
278             actions.add(SystemAction.get(GoToAction.class));
279             actions.add(null);
280             actions.add(SystemAction.get(PropertiesAction.class));
281         }
282         
283         return actions.toArray(new Action JavaDoc[actions.size()]);
284     }
285     
286     @Override JavaDoc
287     protected void refreshAttributesSheetSet() {
288         Sheet.Set defaultSet = null;
289         if (mSchemaElement != null) {
290             try {
291                 Sheet.Set[] sets = PropertyViewFactory.getInstance().getPropertySets(mWSDLConstruct, mQName, mSchemaElement);
292                 if (sets != null) {
293                     for (Sheet.Set set : sets) {
294                         mSheet.put(set);
295                     }
296                     if (sets.length > 0)
297                         defaultSet = sets[sets.length - 1];
298                 }
299                 if (defaultSet != null) {
300                     List JavaDoc<Node.Property> properties = createAlwaysPresentAttributeProperty();
301                     if(properties != null) {
302                         Iterator JavaDoc<Node.Property> itP = properties.iterator();
303                         while(itP.hasNext()) {
304                             Node.Property property = itP.next();
305                             //if property is not present then add it
306
if(defaultSet.get(property.getName()) == null) {
307                                 defaultSet.put(property);
308                             }
309                         }
310                     }
311                 }
312             } catch (Exception JavaDoc e) {
313                 ErrorManager.getDefault().notify(e);
314             }
315         } else {
316             super.refreshAttributesSheetSet();
317         }
318     }
319     
320     
321     public class ExtensibilityElementNamedPropertyAdapter implements NamedPropertyAdapter {
322         String JavaDoc attributeName;
323         
324         
325         public ExtensibilityElementNamedPropertyAdapter(String JavaDoc attributeName) {
326             this.attributeName = attributeName;
327         }
328
329         public void setName(String JavaDoc name) {
330             mWSDLConstruct.getModel().startTransaction();
331             mWSDLConstruct.setAttribute(attributeName, name);
332                 mWSDLConstruct.getModel().endTransaction();
333         }
334
335         public String JavaDoc getName() {
336             return mWSDLConstruct.getAttribute(attributeName);
337         }
338         
339         public boolean isWritable() {
340             return XAMUtils.isWritable(mWSDLConstruct.getModel());
341         }
342         
343     }
344     public class ExtensibilityElementConstrainedNamedPropertyAdapter extends ConstraintNamedPropertyAdapter {
345         public ExtensibilityElementConstrainedNamedPropertyAdapter() {
346             super(getWSDLComponent());
347         }
348
349
350         @Override JavaDoc
351         public boolean isNameExists(String JavaDoc name) {
352             return false;
353         }
354         
355     }
356
357
358     @Override JavaDoc
359     public String JavaDoc getTypeDisplayName() {
360         if (mConfigurator == null) return null;
361         
362         return mConfigurator.getTypeDisplayName(mWSDLConstruct, mQName);
363     }
364     
365     @Override JavaDoc
366     public String JavaDoc getHtmlDisplayName() {
367         String JavaDoc htmlDisplayName = super.getHtmlDisplayName();
368         
369         if (mConfigurator == null)
370             return htmlDisplayName;
371         
372         String JavaDoc decoration = mConfigurator.getHtmlDisplayNameDecoration(mWSDLConstruct, mQName);
373         
374         if (decoration == null)
375             return htmlDisplayName;
376         
377         return htmlDisplayName + " <font color='#999999'>"+decoration+"</font>";
378         
379     }
380 }
381
382
383
384
Popular Tags