KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > customizer > ExternalReferenceDataNode


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.xam.ui.customizer;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.modules.xml.xam.Model;
24 import org.openide.loaders.DataObject;
25 import org.openide.nodes.FilterNode;
26 import org.openide.nodes.Node;
27 import org.netbeans.modules.xml.xam.ui.ModelCookie;
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileObject;
30 import org.openide.nodes.Node.Property;
31 import org.openide.nodes.PropertySupport.Reflection;
32 import org.openide.nodes.Sheet;
33 import org.openide.nodes.Sheet.Set;
34 import org.openide.util.NbBundle;
35
36 /**
37  * Represents a collection of external references, or a single file.
38  *
39  * @author Ajit Bhate
40  * @author Nathan Fiedler
41  */

42 public class ExternalReferenceDataNode extends FilterNode
43         implements ExternalReferenceNode {
44     /** Name of the 'selected' property. */
45     public static final String JavaDoc PROP_SELECTED = "selected";
46     /** Name of the 'prefix' property. */
47     public static final String JavaDoc PROP_PREFIX = "prefix";
48     /** Controls the appearance of this node. */
49     private ExternalReferenceDecorator decorator;
50     /** Set of PropertySets. */
51     private Sheet sheet;
52     /** True if selected, false otherwise. */
53     private boolean selected;
54     /** The namespace prefix, if specified. */
55     private String JavaDoc prefix;
56
57     /**
58      * Creates a new instance of ExternalReferenceDataNode.
59      *
60      * @param original the delegate Node.
61      * @param decorator the external reference decorator.
62      */

63     public ExternalReferenceDataNode(Node original,
64             ExternalReferenceDecorator decorator) {
65         super(original, new Children(original, decorator));
66         this.decorator = decorator;
67     }
68
69     public boolean canRename() {
70         // Disable rename as it serves no purpose here and makes the
71
// single-click-select-toggle difficult to use.
72
return false;
73     }
74
75     /**
76      * Indicates if this node allows setting it selected.
77      *
78      * @return true if this node can be selected, false otherwise.
79      */

80     public boolean canSelect() {
81         DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
82         return dobj != null && !dobj.getPrimaryFile().isFolder() &&
83                 decorator.validate(this) == null;
84     }
85
86     /**
87      * Creates a node property of the given key (same as the column keys)
88      * and specific getter/setter methods on the given object.
89      *
90      * @param key property name (same as matching column).
91      * @param type Class of the property (e.g. String.class).
92      * @param inst object on which to reflect.
93      * @param getter name of getter method for property value.
94      * @param setter name of setter method for property value (may be null).
95      * @return new property.
96      */

97     private Node.Property createProperty(String JavaDoc key, Class JavaDoc type, Object JavaDoc inst,
98             String JavaDoc getter, String JavaDoc setter) {
99         Property prop = null;
100         try {
101             prop = new Reflection(inst, type, getter, setter);
102             prop.setName(key);
103             prop.setDisplayName(NbBundle.getMessage(
104                     ExternalReferenceDataNode.class,
105                     "CTL_ExternalReferenceCreator_Column_Name_" + key));
106             prop.setShortDescription(NbBundle.getMessage(
107                     ExternalReferenceDataNode.class,
108                     "CTL_ExternalReferenceCreator_Column_Desc_" + key));
109         } catch (NoSuchMethodException JavaDoc nsme) {
110             ErrorManager.getDefault().notify(nsme);
111         }
112         return prop;
113     }
114
115     protected Sheet createSheet() {
116         Sheet sheet = Sheet.createDefault();
117         Set set = sheet.get(Sheet.PROPERTIES);
118         set.put(createProperty(PROP_NAME, String JavaDoc.class, this,
119                 "getHtmlDisplayName", null));
120         if (canSelect()) {
121             set.put(createProperty(PROP_SELECTED, Boolean.TYPE, this,
122                     "isSelected", "setSelected"));
123             Node.Property prop = createProperty(PROP_PREFIX, String JavaDoc.class,
124                     this, "getPrefix", "setPrefix");
125             // Suppress the [...] button because it is not needed.
126
prop.setValue("suppressCustomEditor", Boolean.TRUE);
127             set.put(prop);
128         } else {
129             // Do not include this property so the checkbox is not shown.
130
//set.put(createProperty(PROP_SELECTED, Boolean.TYPE, this,
131
// "isSelected", null));
132
Node.Property prop = createProperty(PROP_PREFIX, String JavaDoc.class,
133                     this, "getPrefix", null);
134             // Suppress the [...] button because it is not needed.
135
prop.setValue("suppressCustomEditor", Boolean.TRUE);
136             set.put(prop);
137         }
138         return sheet;
139     }
140
141     protected final synchronized Sheet getSheet() {
142         if (sheet != null) {
143             return sheet;
144         }
145         sheet = createSheet();
146         firePropertySetsChange(null, null);
147         return sheet;
148     }
149
150     public PropertySet[] getPropertySets() {
151         Sheet s = getSheet();
152         return s.toArray();
153     }
154
155     public String JavaDoc getHtmlDisplayName() {
156         String JavaDoc name = getOriginal().getHtmlDisplayName();
157         if (decorator != null) {
158             if (name == null) {
159                 name = getDisplayName();
160             }
161             name = decorator.getHtmlDisplayName(name, this);
162         }
163         return name;
164     }
165
166     public String JavaDoc getNamespace() {
167         DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
168         if (dobj != null) {
169             ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
170             if (cookie != null) {
171                 try {
172                     Model model = cookie.getModel();
173                     return decorator.getNamespace(model);
174                 } catch (IOException JavaDoc ioe) {
175                     return null;
176                 }
177             }
178         }
179         return null;
180     }
181
182     public Model getModel() {
183         DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
184         if (dobj != null) {
185             ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
186             if (cookie != null) {
187                 try {
188                     return cookie.getModel();
189                 } catch (IOException JavaDoc ioe) {
190                     return null;
191                 }
192             }
193         }
194         return null;
195     }
196
197     public String JavaDoc getPrefix() {
198         if (prefix == null) {
199             prefix = decorator.generatePrefix(this);
200         }
201         return prefix;
202     }
203
204     public boolean isSelected() {
205         return selected;
206     }
207
208     public boolean hasModel() {
209         DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
210         if (dobj != null) {
211             ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
212             // Don't check for a model, as it may not be well-formed, and
213
// this method is not checking for that, just that we should
214
// have a model in the normal case.
215
return cookie != null;
216         }
217         return false;
218     }
219
220     public void setDisplayName(String JavaDoc s) {
221         super.disableDelegation(DELEGATE_GET_DISPLAY_NAME|DELEGATE_SET_DISPLAY_NAME);
222         super.setDisplayName(s);
223     }
224
225     /**
226      * Set the namespace prefix for this node.
227      *
228      * @param prefix new namespace prefix.
229      */

230     public void setPrefix(String JavaDoc prefix) {
231         String JavaDoc old = this.prefix;
232         this.prefix = prefix;
233         firePropertyChange(PROP_PREFIX, old, prefix);
234     }
235
236     /**
237      * Mark this node as selected.
238      *
239      * @param selected true to select, false to unselect.
240      */

241     public void setSelected(boolean selected) {
242         if (!canSelect()) {
243             throw new IllegalStateException JavaDoc("node cannot be selected");
244         }
245         boolean old = this.selected;
246         this.selected = selected;
247         firePropertyChange(PROP_SELECTED, old, selected);
248     }
249
250     private static class Children extends FilterNode.Children {
251         /** Controls the appearance of child nodes. */
252         private ExternalReferenceDecorator decorator;
253
254         public Children(Node original, ExternalReferenceDecorator decorator) {
255             super(original);
256             this.decorator = decorator;
257         }
258
259         protected Node[] createNodes(Node n) {
260             DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class);
261             if (dobj != null) {
262                 FileObject fobj = dobj.getPrimaryFile();
263                 if (fobj.isFolder() && fobj.getNameExt().equals("nbproject") &&
264                         fobj.getFileObject("project.xml") != null) {
265                     // It is the NetBeans project folder, ignore it.
266
return new Node[0];
267                 }
268                 ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
269                 String JavaDoc fname = fobj.getNameExt();
270                 String JavaDoc ext = decorator.getDocumentType().toString();
271                 if (fobj.isFolder() || cookie != null && fname.endsWith(ext)) {
272                     return super.createNodes(n);
273                 }
274             }
275             return new Node[0];
276         }
277
278         protected Node copyNode(Node node) {
279             return decorator.createExternalReferenceNode(node);
280         }
281     }
282 }
283
Popular Tags