~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/xamui/src/org/netbeans/modules/xml/xam/ui/customizer/ExternalReferenceDataNode.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.xam.ui.customizer;
 
43
 
 
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;
 
57
 
 
58
/**
 
59
 * Represents a collection of external references, or a single file.
 
60
 *
 
61
 * @author Ajit Bhate
 
62
 * @author Nathan Fiedler
 
63
 */
 
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. */
 
73
    private Sheet sheet;
 
74
    /** True if selected, false otherwise. */
 
75
    private boolean selected;
 
76
    /** The namespace prefix, if specified. */
 
77
    private String prefix;
 
78
 
 
79
    /**
 
80
     * Creates a new instance of ExternalReferenceDataNode.
 
81
     *
 
82
     * @param  original   the delegate Node.
 
83
     * @param  decorator  the external reference decorator.
 
84
     */
 
85
    public ExternalReferenceDataNode(Node original,
 
86
            ExternalReferenceDecorator decorator) {
 
87
        super(original, new Children(original, decorator));
 
88
        this.decorator = decorator;
 
89
    }
 
90
 
 
91
    public boolean canRename() {
 
92
        // Disable rename as it serves no purpose here and makes the
 
93
        // single-click-select-toggle difficult to use.
 
94
        return false;
 
95
    }
 
96
 
 
97
    /**
 
98
     * Indicates if this node allows setting it selected.
 
99
     *
 
100
     * @return  true if this node can be selected, false otherwise.
 
101
     */
 
102
    public boolean canSelect() {
 
103
        DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
 
104
        return dobj != null && !dobj.getPrimaryFile().isFolder() &&
 
105
                decorator.validate(this) == null;
 
106
    }
 
107
 
 
108
    /**
 
109
     * Creates a node property of the given key (same as the column keys)
 
110
     * and specific getter/setter methods on the given object.
 
111
     *
 
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.
 
118
     */
 
119
    private Node.Property createProperty(String key, Class type, Object inst,
 
120
            String getter, String setter) {
 
121
        Property prop = null;
 
122
        try {
 
123
            prop = new Reflection(inst, type, getter, setter);
 
124
            prop.setName(key);
 
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);
 
133
        }
 
134
        return prop;
 
135
    }
 
136
 
 
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));
 
142
        if (canSelect()) {
 
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);
 
149
            set.put(prop);
 
150
        } else {
 
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);
 
158
            set.put(prop);
 
159
        }
 
160
        return sheet;
 
161
    }
 
162
 
 
163
    protected final synchronized Sheet getSheet() {
 
164
        if (sheet != null) {
 
165
            return sheet;
 
166
        }
 
167
        sheet = createSheet();
 
168
        firePropertySetsChange(null, null);
 
169
        return sheet;
 
170
    }
 
171
 
 
172
    public PropertySet[] getPropertySets() {
 
173
        Sheet s = getSheet();
 
174
        return s.toArray();
 
175
    }
 
176
 
 
177
    public String getHtmlDisplayName() {
 
178
        String name = getOriginal().getHtmlDisplayName();
 
179
        if (decorator != null) {
 
180
            if (name == null) {
 
181
                name = getDisplayName();
 
182
            }
 
183
            name = decorator.getHtmlDisplayName(name, this);
 
184
        }
 
185
        return name;
 
186
    }
 
187
 
 
188
    public String getNamespace() {
 
189
        DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
 
190
        if (dobj != null) {
 
191
            ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
 
192
            if (cookie != null) {
 
193
                try {
 
194
                    Model model = cookie.getModel();
 
195
                    return decorator.getNamespace(model);
 
196
                } catch (IOException ioe) {
 
197
                    return null;
 
198
                }
 
199
            }
 
200
        }
 
201
        return null;
 
202
    }
 
203
 
 
204
    public Model getModel() {
 
205
        DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
 
206
        if (dobj != null) {
 
207
            ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
 
208
            if (cookie != null) {
 
209
                try {
 
210
                    return cookie.getModel();
 
211
                } catch (IOException ioe) {
 
212
                    return null;
 
213
                }
 
214
            }
 
215
        }
 
216
        return null;
 
217
    }
 
218
 
 
219
    public String getPrefix() {
 
220
        if (prefix == null) {
 
221
            prefix = decorator.generatePrefix(this);
 
222
        }
 
223
        return prefix;
 
224
    }
 
225
 
 
226
    public boolean isSelected() {
 
227
        return selected;
 
228
    }
 
229
 
 
230
    public boolean hasModel() {
 
231
        DataObject dobj = (DataObject) getLookup().lookup(DataObject.class);
 
232
        if (dobj != null) {
 
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;
 
238
        }
 
239
        return false;
 
240
    }
 
241
 
 
242
    public void setDisplayName(String s) {
 
243
        super.disableDelegation(DELEGATE_GET_DISPLAY_NAME|DELEGATE_SET_DISPLAY_NAME);
 
244
        super.setDisplayName(s);
 
245
    }
 
246
 
 
247
    /**
 
248
     * Set the namespace prefix for this node.
 
249
     *
 
250
     * @param  prefix  new namespace prefix.
 
251
     */
 
252
    public void setPrefix(String prefix) {
 
253
        String old = this.prefix;
 
254
        this.prefix = prefix;
 
255
        firePropertyChange(PROP_PREFIX, old, prefix);
 
256
    }
 
257
 
 
258
    /**
 
259
     * Mark this node as selected.
 
260
     *
 
261
     * @param  selected  true to select, false to unselect.
 
262
     */
 
263
    public void setSelected(boolean selected) {
 
264
        if (!canSelect()) {
 
265
            throw new IllegalStateException("node cannot be selected");
 
266
        }
 
267
        boolean old = this.selected;
 
268
        this.selected = selected;
 
269
        firePropertyChange(PROP_SELECTED, old, selected);
 
270
    }
 
271
 
 
272
    private static class Children extends FilterNode.Children {
 
273
        /** Controls the appearance of child nodes. */
 
274
        private ExternalReferenceDecorator decorator;
 
275
 
 
276
        public Children(Node original, ExternalReferenceDecorator decorator) {
 
277
            super(original);
 
278
            this.decorator = decorator;
 
279
        }
 
280
 
 
281
        protected Node[] createNodes(Node n) {
 
282
            DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class);
 
283
            if (dobj != null) {
 
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.
 
288
                    return new Node[0];
 
289
                }
 
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);
 
295
                }
 
296
            }
 
297
            return new Node[0];
 
298
        }
 
299
 
 
300
        protected Node copyNode(Node node) {
 
301
            return decorator.createExternalReferenceNode(node);
 
302
        }
 
303
    }
 
304
}