KickJava   Java API By Example, From Geeks To Geeks.

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


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 17, 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.Arrays JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.logging.Level JavaDoc;
34
35 import javax.xml.namespace.QName JavaDoc;
36
37 import org.netbeans.modules.xml.wsdl.model.Binding;
38 import org.netbeans.modules.xml.wsdl.model.PortType;
39 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
40 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
41 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
42 import org.netbeans.modules.xml.wsdl.ui.commands.ConstraintNamedPropertyAdapter;
43 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
44 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
45 import org.netbeans.modules.xml.wsdl.ui.view.property.BaseAttributeProperty;
46 import org.netbeans.modules.xml.wsdl.ui.view.property.BindingTypeAttributeProperty;
47 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.BindingOperationNewType;
48 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.DocumentationNewType;
49 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.ExtensibilityElementNewTypesFactory;
50 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.NewTypesFactory;
51 import org.netbeans.modules.xml.xam.ComponentEvent;
52 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
53 import org.openide.ErrorManager;
54 import org.openide.nodes.Node;
55 import org.openide.util.HelpCtx;
56 import org.openide.util.NbBundle;
57 import org.openide.util.Utilities;
58 import org.openide.util.datatransfer.NewType;
59
60
61 /**
62  * @author Ritesh Adval
63  *
64  */

65 public class BindingNode extends WSDLExtensibilityElementNode {
66     
67     
68     private Binding mWSDLConstruct;
69     
70     private BindingPropertyAdapter mPropertyAdapter = null;
71     
72     private static Image JavaDoc ICON = Utilities.loadImage
73             ("org/netbeans/modules/xml/wsdl/ui/view/resources/binding.png");
74
75     private static final String JavaDoc TYPE_PROP = "type";//NOI18N
76

77     public BindingNode(Binding wsdlConstruct) {
78         super(new GenericWSDLComponentChildren(wsdlConstruct), wsdlConstruct, new BindingNewTypesFactory());
79         mWSDLConstruct = wsdlConstruct;
80         
81         this.mPropertyAdapter = new BindingPropertyAdapter();
82         this.setNamedPropertyAdapter(this.mPropertyAdapter);
83     }
84     
85     @Override JavaDoc
86     public String JavaDoc getNameInLayer() {
87         return WSDLExtensibilityElements.ELEMENT_BINDING;
88     }
89     
90     @Override JavaDoc
91     public Image JavaDoc getIcon(int type) {
92         return ICON;
93     }
94     
95     @Override JavaDoc
96     public Image JavaDoc getOpenedIcon(int type) {
97         return ICON;
98     }
99     
100     @Override JavaDoc
101     public void childrenAdded(ComponentEvent evt) {
102 /* if (!isSameAsMyWSDLElement((WSDLComponent) evt.getSource())) {
103             return;
104         }
105  
106         Collection ee = mWSDLConstruct.getExtensibilityElements();
107         if (ee != null && ee.size() > 0) {
108             Cookie cookie = getCookie(ExtensibilityElementCookie.class);
109             if (cookie != null) {
110                 getLookupContents().remove(cookie);
111             }
112         }*/

113     }
114     
115     @Override JavaDoc
116     public void childrenDeleted(ComponentEvent evt) {
117 /* if (!isSameAsMyWSDLElement((WSDLComponent) evt.getSource())) {
118             return;
119         }
120  
121         Collection ee = mWSDLConstruct.getExtensibilityElements();
122         if (ee != null && ee.size() > 0) {
123             Cookie cookie = getCookie(ExtensibilityElementCookie.class);
124             if (cookie == null) {
125                 getLookupContents().add(cookie);
126             }
127         }*/

128     }
129     
130     @Override JavaDoc
131     protected Node.Property createAttributeProperty(QName JavaDoc attrQName) {
132         Node.Property attrValueProperty = null;
133         try {
134             String JavaDoc attrName = attrQName.getLocalPart();
135             //name
136
if(attrName.equals(NAME_PROP)) { //NOT I18N
137
attrValueProperty = createNameProperty();
138                 
139             } else if(attrName.equals(TYPE_PROP)) { //NOT I18N
140
//type
141
attrValueProperty = createTypeProperty();
142                 
143             } else {
144                 attrValueProperty = super.createAttributeProperty(attrQName);
145             }
146             
147         } catch(Exception JavaDoc ex) {
148             mLogger.log(Level.SEVERE, "failed to create property sheet for "+ getWSDLComponent(), ex);
149             ErrorManager.getDefault().notify(ex);
150         }
151         return attrValueProperty;
152     }
153     
154     @Override JavaDoc
155      protected List JavaDoc<Node.Property> createAlwaysPresentAttributeProperty() throws Exception JavaDoc {
156         ArrayList JavaDoc<Node.Property> alwaysPresentAttrProperties = new ArrayList JavaDoc<Node.Property>();
157         alwaysPresentAttrProperties.add(createNameProperty());
158         alwaysPresentAttrProperties.add(createTypeProperty());
159         return alwaysPresentAttrProperties;
160     }
161     
162     
163     private Node.Property createNameProperty() throws NoSuchMethodException JavaDoc {
164         Node.Property attrValueProperty;
165         attrValueProperty = new BaseAttributeProperty(mPropertyAdapter, String JavaDoc.class, NAME_PROP);
166         attrValueProperty.setName(NbBundle.getMessage(BindingNode.class, "PROP_NAME_DISPLAYNAME"));
167         attrValueProperty.setShortDescription(NbBundle.getMessage(BindingNode.class, "BINDINGNODE_NAME_DESCRIPTION"));
168         
169         return attrValueProperty;
170     }
171     
172     private Node.Property createTypeProperty() throws NoSuchMethodException JavaDoc {
173         Node.Property attrValueProperty;
174         attrValueProperty = new BindingTypeAttributeProperty(mPropertyAdapter,
175                 String JavaDoc.class, "getType", "setType");
176         
177         attrValueProperty.setName(NbBundle.getMessage(BindingNode.class, "PROP_TYPE_DISPLAYNAME"));
178         attrValueProperty.setShortDescription(NbBundle.getMessage(BindingNode.class, "BINDINGNODE_TYPE_DESCRIPTION"));
179         
180         return attrValueProperty;
181     }
182     
183     @Override JavaDoc
184     public HelpCtx getHelpCtx() {
185         return new HelpCtx(BindingNode.class);
186     }
187     
188     public class BindingPropertyAdapter extends ConstraintNamedPropertyAdapter {
189         public BindingPropertyAdapter() {
190             super(mWSDLConstruct);
191         }
192         
193         @Override JavaDoc
194         public boolean isNameExists(String JavaDoc name) {
195             WSDLModel model = mWSDLConstruct.getModel();
196             return NameGenerator.getInstance().isBindingExists(name, model);
197         }
198         
199         public void setType(String JavaDoc type) {
200             if(type != null) {
201                 try {
202                     Binding binding = (Binding) getWSDLComponent();
203                     org.netbeans.modules.xml.wsdl.ui.common.QName portTypeQName = org.netbeans.modules.xml.wsdl.ui.common.QName.getQNameFromString(type);
204                     if(portTypeQName == null) {
205                         binding.getModel().startTransaction();
206                         binding.setType(null);
207                         binding.getModel().endTransaction();
208                     } else {
209                         
210                         String JavaDoc ns = portTypeQName.getNamespaceURI();
211                         String JavaDoc prefix = portTypeQName.getPrefix();
212                         if(ns == null || ns.trim().equals("")) {
213                             ns = Utility.getNamespaceURI(prefix, binding.getModel());
214                         }
215                         
216                         QName JavaDoc qname = null;
217                         if (ns != null) {
218                             qname = new QName JavaDoc(ns, portTypeQName.getLocalName());
219                         }
220                         
221                         if(qname != null) {
222                             PortType pType = binding.getModel().findComponentByName(qname, PortType.class);
223                             if (pType == null) {
224                                 ErrorManager.getDefault().notify(ErrorManager.ERROR, new Exception JavaDoc("Not a valid type"));
225                             } else {
226                                 binding.getModel().startTransaction();
227                                 binding.setType(binding.createReferenceTo(pType, PortType.class));
228                                 
229                                 getWSDLComponent().getModel().endTransaction();
230                             }
231                         }
232                     }
233                 } catch (Exception JavaDoc e) {
234                     ErrorManager.getDefault().notify(e);
235                 }
236             }
237         }
238         
239         public String JavaDoc getType() {
240             NamedComponentReference pType = mWSDLConstruct.getType();
241             if (pType != null) {
242                 QName JavaDoc portTypeQName = pType.getQName();
243                 if(portTypeQName != null) {
244                     return Utility.fromQNameToString(portTypeQName);
245                 }
246             }
247             return "";
248         }
249         
250     }
251     
252     
253     public static final class BindingNewTypesFactory implements NewTypesFactory{
254         
255         public NewType[] getNewTypes(WSDLComponent def) {
256             Binding binding = (Binding) def;
257             ArrayList JavaDoc<NewType> list = new ArrayList JavaDoc<NewType>();
258             if (def.getDocumentation() == null) {
259                 list.add(new DocumentationNewType(def));
260             }
261             if (isBindingOperationActionEnabled(binding)) {
262                 list.add(new BindingOperationNewType(def));
263             }
264             
265             list.addAll(Arrays.asList(new ExtensibilityElementNewTypesFactory(WSDLExtensibilityElements.ELEMENT_BINDING).getNewTypes(def)));
266             return list.toArray(new NewType[]{});
267         }
268         
269         private boolean isBindingOperationActionEnabled(Binding binding) {
270             boolean enable = false;
271             PortType type = binding.getType() != null ? binding.getType().get() : null;
272             if (type == null) return true;
273             if (type != null) {
274                 Collection JavaDoc operations = type.getOperations();
275                 if (operations != null) {
276                     int boSize = binding.getBindingOperations() != null ? binding.getBindingOperations().size() : 0;
277                     if (type.getOperations().size() > boSize) {
278                         enable = true;
279                     }
280                 }
281             }
282             
283             return enable;
284         }
285         
286         
287         
288     }
289
290
291     @Override JavaDoc
292     public String JavaDoc getTypeDisplayName() {
293         return NbBundle.getMessage(BindingNode.class, "LBL_BindingNode_TypeDisplayName");
294     }
295     
296     @Override JavaDoc
297     public String JavaDoc getHtmlDisplayName() {
298         String JavaDoc htmlDisplayName = super.getHtmlDisplayName();
299         NamedComponentReference<PortType> type = mWSDLConstruct.getType();
300         
301         String JavaDoc decoration = null;
302         if (type != null && type.get() != null) {
303             String JavaDoc tns = type.get().getModel().getDefinitions().getTargetNamespace();
304             
305             decoration = NbBundle.getMessage(OperationParameterNode.class, "LBL_PortType",
306                     Utility.getNameAndDropPrefixIfInCurrentModel(tns, type.get().getName(), mWSDLConstruct.getModel()));
307         }
308         
309         if (decoration == null) {
310             //decoration = NbBundle.getMessage(OperationParameterNode.class, "LBL_PortTypeNotSet");
311
return htmlDisplayName;
312         }
313         return htmlDisplayName + " <font color='#999999'>"+decoration+"</font>";
314     }
315 }
316
317
318
Popular Tags