2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.xam.ui.customizer;
44
import java.io.IOException;
45
import org.netbeans.modules.xml.xam.Model;
46
import org.openide.loaders.DataObject;
47
import org.openide.nodes.FilterNode;
48
import org.openide.nodes.Node;
49
import org.netbeans.modules.xml.xam.ui.ModelCookie;
50
import org.openide.ErrorManager;
51
import org.openide.filesystems.FileObject;
52
import org.openide.nodes.Node.Property;
53
import org.openide.nodes.PropertySupport.Reflection;
54
import org.openide.nodes.Sheet;
55
import org.openide.nodes.Sheet.Set;
56
import org.openide.util.NbBundle;
59
* Represents a collection of external references, or a single file.
62
* @author Nathan Fiedler
64
public class ExternalReferenceDataNode extends FilterNode
65
implements ExternalReferenceNode {
66
/** Name of the 'selected' property. */
67
public static final String PROP_SELECTED = "selected";
68
/** Name of the 'prefix' property. */
69
public static final String PROP_PREFIX = "prefix";
70
/** Controls the appearance of this node. */
71
private ExternalReferenceDecorator decorator;
72
/** Set of PropertySets. */
74
/** True if selected, false otherwise. */
75
private boolean selected;
76
/** The namespace prefix, if specified. */
77
private String prefix;
80
* Creates a new instance of ExternalReferenceDataNode.
82
* @param original the delegate Node.
83
* @param decorator the external reference decorator.
85
public ExternalReferenceDataNode(Node original,
86
ExternalReferenceDecorator decorator) {
87
super(original, new Children(original, decorator));
88
this.decorator = decorator;
91
public boolean canRename() {
92
// Disable rename as it serves no purpose here and makes the
93
// single-click-select-toggle difficult to use.
98
* Indicates if this node allows setting it selected.
100
* @return true if this node can be selected, false otherwise.
102
public boolean canSelect() {
103
DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
104
return dobj != null && !dobj.getPrimaryFile().isFolder() &&
105
decorator.validate(this) == null;
109
* Creates a node property of the given key (same as the column keys)
110
* and specific getter/setter methods on the given object.
112
* @param key property name (same as matching column).
113
* @param type Class of the property (e.g. String.class).
114
* @param inst object on which to reflect.
115
* @param getter name of getter method for property value.
116
* @param setter name of setter method for property value (may be null).
117
* @return new property.
119
private Node.Property createProperty(String key, Class type, Object inst,
120
String getter, String setter) {
121
Property prop = null;
123
prop = new Reflection(inst, type, getter, setter);
125
prop.setDisplayName(NbBundle.getMessage(
126
ExternalReferenceDataNode.class,
127
"CTL_ExternalReferenceCreator_Column_Name_" + key));
128
prop.setShortDescription(NbBundle.getMessage(
129
ExternalReferenceDataNode.class,
130
"CTL_ExternalReferenceCreator_Column_Desc_" + key));
131
} catch (NoSuchMethodException nsme) {
132
ErrorManager.getDefault().notify(nsme);
137
protected Sheet createSheet() {
138
Sheet sheet = Sheet.createDefault();
139
Set set = sheet.get(Sheet.PROPERTIES);
140
set.put(createProperty(PROP_NAME, String.class, this,
141
"getHtmlDisplayName", null));
143
set.put(createProperty(PROP_SELECTED, Boolean.TYPE, this,
144
"isSelected", "setSelected"));
145
Node.Property prop = createProperty(PROP_PREFIX, String.class,
146
this, "getPrefix", "setPrefix");
147
// Suppress the [...] button because it is not needed.
148
prop.setValue("suppressCustomEditor", Boolean.TRUE);
151
// Do not include this property so the checkbox is not shown.
152
//set.put(createProperty(PROP_SELECTED, Boolean.TYPE, this,
153
// "isSelected", null));
154
Node.Property prop = createProperty(PROP_PREFIX, String.class,
155
this, "getPrefix", null);
156
// Suppress the [...] button because it is not needed.
157
prop.setValue("suppressCustomEditor", Boolean.TRUE);
163
protected final synchronized Sheet getSheet() {
167
sheet = createSheet();
168
firePropertySetsChange(null, null);
172
public PropertySet[] getPropertySets() {
173
Sheet s = getSheet();
177
public String getHtmlDisplayName() {
178
String name = getOriginal().getHtmlDisplayName();
179
if (decorator != null) {
181
name = getDisplayName();
183
name = decorator.getHtmlDisplayName(name, this);
188
public String getNamespace() {
189
DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
191
ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
192
if (cookie != null) {
194
Model model = cookie.getModel();
195
return decorator.getNamespace(model);
196
} catch (IOException ioe) {
204
public Model getModel() {
205
DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
207
ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
208
if (cookie != null) {
210
return cookie.getModel();
211
} catch (IOException ioe) {
219
public String getPrefix() {
220
if (prefix == null) {
221
prefix = decorator.generatePrefix(this);
226
public boolean isSelected() {
230
public boolean hasModel() {
231
DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
233
ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
234
// Don't check for a model, as it may not be well-formed, and
235
// this method is not checking for that, just that we should
236
// have a model in the normal case.
237
return cookie != null;
242
public void setDisplayName(String s) {
243
super.disableDelegation(DELEGATE_GET_DISPLAY_NAME|DELEGATE_SET_DISPLAY_NAME);
244
super.setDisplayName(s);
248
* Set the namespace prefix for this node.
250
* @param prefix new namespace prefix.
252
public void setPrefix(String prefix) {
253
String old = this.prefix;
254
this.prefix = prefix;
255
firePropertyChange(PROP_PREFIX, old, prefix);
259
* Mark this node as selected.
261
* @param selected true to select, false to unselect.
263
public void setSelected(boolean selected) {
265
throw new IllegalStateException("node cannot be selected");
267
boolean old = this.selected;
268
this.selected = selected;
269
firePropertyChange(PROP_SELECTED, old, selected);
272
private static class Children extends FilterNode.Children {
273
/** Controls the appearance of child nodes. */
274
private ExternalReferenceDecorator decorator;
276
public Children(Node original, ExternalReferenceDecorator decorator) {
278
this.decorator = decorator;
281
protected Node[] createNodes(Node n) {
282
DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class);
284
FileObject fobj = dobj.getPrimaryFile();
285
if (fobj.isFolder() && fobj.getNameExt().equals("nbproject") &&
286
fobj.getFileObject("project.xml") != null) {
287
// It is the NetBeans project folder, ignore it.
290
ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
291
String fname = fobj.getNameExt();
292
String ext = decorator.getDocumentType().toString();
293
if (fobj.isFolder() || cookie != null && fname.endsWith(ext)) {
294
return super.createNodes(n);
300
protected Node copyNode(Node node) {
301
return decorator.createExternalReferenceNode(node);