KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.xml.wsdl.ui.view.treeeditor;
22
23 import java.awt.Image JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import javax.swing.Action JavaDoc;
29 import javax.xml.namespace.QName JavaDoc;
30 import org.netbeans.modules.refactoring.api.ui.RefactoringActionsFactory;
31
32 //import org.netbeans.modules.xml.refactoring.actions.FindUsagesAction;
33
//import org.netbeans.modules.xml.refactoring.actions.RefactorAction;
34
import org.netbeans.modules.xml.schema.model.GlobalElement;
35 import org.netbeans.modules.xml.schema.model.GlobalType;
36 import org.netbeans.modules.xml.wsdl.model.Message;
37 import org.netbeans.modules.xml.wsdl.model.Part;
38 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
39 import org.netbeans.modules.xml.wsdl.ui.actions.ActionHelper;
40 import org.netbeans.modules.xml.wsdl.ui.actions.CommonAddExtensibilityAttributeAction;
41 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
42 import org.netbeans.modules.xml.wsdl.ui.actions.RemoveAttributesAction;
43 import org.netbeans.modules.xml.wsdl.ui.api.property.ElementOrType;
44 import org.netbeans.modules.xml.wsdl.ui.api.property.ElementOrTypeAttributeProperty;
45 import org.netbeans.modules.xml.wsdl.ui.api.property.ElementOrTypeProvider;
46 import org.netbeans.modules.xml.wsdl.ui.commands.ConstraintNamedPropertyAdapter;
47 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
48 import org.netbeans.modules.xml.wsdl.ui.view.property.BaseAttributeProperty;
49 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
50 import org.netbeans.modules.xml.xam.ui.actions.GoToAction;
51 import org.openide.ErrorManager;
52 import org.openide.actions.CopyAction;
53 import org.openide.actions.CutAction;
54 import org.openide.actions.DeleteAction;
55 import org.openide.actions.NewAction;
56 import org.openide.actions.PasteAction;
57 import org.openide.actions.PropertiesAction;
58 import org.openide.nodes.Node;
59 import org.openide.util.NbBundle;
60 import org.openide.util.Utilities;
61 import org.openide.util.actions.SystemAction;
62
63
64
65
66 /**
67  *
68  * @author Ritesh Adval
69  *
70  *
71  */

72 public class PartNode extends WSDLNamedElementNode {
73     
74     private Image JavaDoc ICON = Utilities.loadImage
75             ("org/netbeans/modules/xml/wsdl/ui/view/resources/part.png");
76     
77     private Part mWSDLConstruct;
78     
79     private PartPropertyAdapter mPropertyAdapter;
80     
81     private ElementOrTypeAttributeProperty mElementOrTypeProperty;
82
83     private static final SystemAction[] ACTIONS = new SystemAction[]{
84         SystemAction.get(CutAction.class),
85         SystemAction.get(CopyAction.class),
86         SystemAction.get(PasteAction.class),
87         null,
88         SystemAction.get(NewAction.class),
89         SystemAction.get(DeleteAction.class),
90         null,
91         SystemAction.get(CommonAddExtensibilityAttributeAction.class),
92         SystemAction.get(RemoveAttributesAction.class),
93         null,
94         SystemAction.get(GoToAction.class),
95         //SystemAction.get(FindUsagesAction.class),
96
(SystemAction)RefactoringActionsFactory.whereUsedAction(),
97         null,
98         (SystemAction)RefactoringActionsFactory.editorSubmenuAction(),
99         null,
100         SystemAction.get(PropertiesAction.class)
101     };
102
103     public PartNode(Part wsdlConstruct) {
104         super( new GenericWSDLComponentChildren(wsdlConstruct), wsdlConstruct);
105         //super( Children.LEAF, wsdlConstruct, dataObject);
106
mWSDLConstruct = wsdlConstruct;
107         
108         this.mPropertyAdapter = new PartPropertyAdapter();
109         super.setNamedPropertyAdapter(this.mPropertyAdapter);
110         ElementOrTypeProvider provider = new PartElementOrTypeProvider(wsdlConstruct, mPropertyAdapter);
111         try {
112             mElementOrTypeProperty = new ElementOrTypeAttributeProperty(provider);
113             mElementOrTypeProperty.setName(NbBundle.getMessage(PartNode.class, "PART_ELEMENT_OR_TYPE"));
114             mElementOrTypeProperty.setShortDescription(NbBundle.getMessage(PartNode.class, "PART_ELEMENT_OR_TYPE_SD"));
115         } catch(Exception JavaDoc ex) {
116             mLogger.log(Level.SEVERE, "failed to create property sheet for "+ getWSDLComponent(), ex);
117             ErrorManager.getDefault().notify(ex);
118         }
119     }
120     
121     
122     @Override JavaDoc
123     public Image JavaDoc getIcon(int type) {
124         return ICON;
125     }
126     
127     @Override JavaDoc
128     public Image JavaDoc getOpenedIcon(int type) {
129         return ICON;
130     }
131     
132     @Override JavaDoc
133     public Action JavaDoc[] getActions(boolean context) {
134         return ACTIONS;
135     }
136     
137     public Object JavaDoc getWSDLConstruct() {
138         return mWSDLConstruct;
139     }
140     
141     @Override JavaDoc
142     protected Node.Property createAttributeProperty(QName JavaDoc attrQName) {
143         Node.Property attrValueProperty = null;
144         try {
145             String JavaDoc attrName = attrQName.getLocalPart();
146             //name
147
if(attrName.equals(NAME_PROP)) { //NOT I18N
148
//name
149
attrValueProperty = createNameProperty();
150             } else if(attrName.equals("element") || attrName.equals("type")) {
151                 attrValueProperty = createElementOrTypeProperty();
152             } else {
153                 attrValueProperty = super.createAttributeProperty(attrQName);
154             }
155             
156         } catch(Exception JavaDoc ex) {
157             mLogger.log(Level.SEVERE, "failed to create property sheet for "+ getWSDLComponent(), ex);
158             ErrorManager.getDefault().notify(ex);
159         }
160         return attrValueProperty;
161     }
162     
163     
164     @Override JavaDoc
165     protected List JavaDoc<Node.Property> createAlwaysPresentAttributeProperty() throws Exception JavaDoc {
166         ArrayList JavaDoc<Node.Property> alwaysPresentAttrProperties = new ArrayList JavaDoc<Node.Property>();
167         alwaysPresentAttrProperties.add(createNameProperty());
168         alwaysPresentAttrProperties.add(createElementOrTypeProperty());
169         return alwaysPresentAttrProperties;
170     }
171     
172     
173     private Node.Property createNameProperty() throws NoSuchMethodException JavaDoc {
174         Node.Property attrValueProperty;
175         attrValueProperty = new BaseAttributeProperty(mPropertyAdapter,
176                 String JavaDoc.class, NAME_PROP);
177         
178         
179         attrValueProperty.setName(NbBundle.getMessage(PartNode.class, "PROP_NAME_DISPLAYNAME"));
180         attrValueProperty.setShortDescription(NbBundle.getMessage(PartNode.class, "PART_NAME_DESC"));
181         
182         return attrValueProperty;
183     }
184     
185     private Node.Property createElementOrTypeProperty() {
186         return mElementOrTypeProperty;
187     }
188     
189     
190     
191     public class PartPropertyAdapter extends ConstraintNamedPropertyAdapter {
192         private Part mPart;
193         private ElementOrType mElementOrType;
194         
195         PartPropertyAdapter() {
196             super(mWSDLConstruct);
197             this.mPart = mWSDLConstruct;
198         }
199         
200         @Override JavaDoc
201         public boolean isNameExists(String JavaDoc name) {
202             return NameGenerator.getInstance().isMessagePartExists(name,
203                     (Message) mWSDLConstruct.getParent());
204         }
205         
206         public ElementOrType getElementOrType() {
207             //since we always create ElementOrType
208
//so ElementOrType overrides equal and hashcode
209
//so that two ElementOrType can be compared
210
if (mPart.isInDocumentModel()) {
211                 NamedComponentReference element = this.mPart.getElement();
212                 if(element == null) {
213                     NamedComponentReference partType = this.mPart.getType();
214                     if(partType != null) {
215                         if (partType.get() != null) {
216                             this.mElementOrType = new ElementOrType((GlobalType) partType.get(), mPart.getModel());
217                         } else {
218                             this.mElementOrType = new ElementOrType(partType.getQName(), mWSDLConstruct.getModel(), false);
219                         }
220                     } else {
221                         mElementOrType = new ElementOrType(new QName JavaDoc(""), mWSDLConstruct.getModel(), false);
222                     }
223                 } else {
224                     if (element.get() != null)
225                         this.mElementOrType = new ElementOrType((GlobalElement) element.get(), mPart.getModel());
226                     else
227                         mElementOrType = new ElementOrType(element.getQName(), mWSDLConstruct.getModel(), true);
228                 }
229                 
230             }
231             return mElementOrType;
232         }
233         
234         public void setElementOrType(ElementOrType elementOrType) {
235             if(elementOrType == null) {
236                 return;
237             }
238             this.mElementOrType = elementOrType;
239             WSDLModel model = mPart.getModel();
240             model.startTransaction();
241             if (elementOrType.isElement()) {
242                 GlobalElement element = mElementOrType.getElement();
243                 Utility.addSchemaImport(element, model);
244                 Utility.addNamespacePrefix(element, mPart.getModel().getDefinitions(), null);
245                 mPart.setElement(mPart.createSchemaReference(mElementOrType.getElement(), GlobalElement.class));
246                 mPart.setType(null);
247             } else {
248                 mPart.setElement(null);
249                 Utility.addSchemaImport(mElementOrType.getType(), model);
250                 Utility.addNamespacePrefix(mElementOrType.getType().getModel().getSchema(), mPart.getModel(), null);
251                 mPart.setType(mPart.createSchemaReference(mElementOrType.getType(), GlobalType.class));
252             }
253             model.endTransaction();
254             ActionHelper.selectNode(mPart);
255         }
256         
257         
258     }
259     
260     public class PartElementOrTypeProvider implements ElementOrTypeProvider {
261         private Part mPart;
262         private PartPropertyAdapter mPartPropertyAdapter;
263         
264         PartElementOrTypeProvider(Part part, PartPropertyAdapter adapter) {
265             mPart = part;
266             mPartPropertyAdapter = adapter;
267         }
268         
269         public WSDLModel getModel() {
270             return mPart.getModel();
271         }
272     
273         public ElementOrType getElementOrType() {
274             return mPartPropertyAdapter.getElementOrType();
275         }
276     
277         public void setElementOrType(ElementOrType o) {
278             mPartPropertyAdapter.setElementOrType(o);
279         }
280     
281     }
282
283     @Override JavaDoc
284     public String JavaDoc getTypeDisplayName() {
285         return NbBundle.getMessage(PartNode.class, "LBL_PartNode_TypeDisplayName");
286     }
287
288
289     @Override JavaDoc
290     public String JavaDoc getHtmlDisplayName() {
291         String JavaDoc htmlDisplayName = super.getHtmlDisplayName();
292         NamedComponentReference<GlobalType> type = mWSDLConstruct.getType();
293         NamedComponentReference<GlobalElement> element = mWSDLConstruct.getElement();
294         String JavaDoc decoration = null;
295         if (type != null && type.get() != null) {
296             String JavaDoc tns = type.get().getModel().getSchema().getTargetNamespace();
297 /* decoration = NbBundle.getMessage(PartNode.class, "LBL_Typeof",
298                     Utility.getNameAndDropPrefixIfInCurrentModel(tns, type.get().getName(), mWSDLConstruct.getModel()));*/

299             decoration = Utility.getNameAndDropPrefixIfInCurrentModel(tns, type.get().getName(), mWSDLConstruct.getModel());
300         } else if (element != null && element.get() != null) {
301             String JavaDoc tns = element.get().getModel().getSchema().getTargetNamespace();
302             /*decoration = NbBundle.getMessage(PartNode.class, "LBL_Typeof",
303                     Utility.getNameAndDropPrefixIfInCurrentModel(tns, element.get().getName(), mWSDLConstruct.getModel()));*/

304             decoration = Utility.getNameAndDropPrefixIfInCurrentModel(tns, element.get().getName(), mWSDLConstruct.getModel());
305         }
306         if (decoration == null) {
307             //decoration = NbBundle.getMessage(PartNode.class, "LBL_TypeOrElementNotSet");
308
return htmlDisplayName;
309         }
310         return htmlDisplayName + " <font color='#999999'>"+decoration+"</font>";
311     }
312     
313     
314     
315
316     
317     
318 }
319
Popular Tags