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

« back to all changes in this revision

Viewing changes to properties/src/org/netbeans/modules/properties/KeyNode.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
 
 
43
package org.netbeans.modules.properties;
 
44
 
 
45
 
 
46
import java.awt.Component;
 
47
import java.beans.PropertyChangeEvent;
 
48
import java.beans.PropertyChangeListener;
 
49
import java.io.IOException;
 
50
import java.lang.reflect.InvocationTargetException;
 
51
import javax.swing.Action;
 
52
 
 
53
import org.openide.actions.*;
 
54
import org.openide.cookies.EditCookie;
 
55
import org.openide.cookies.SaveCookie;
 
56
import org.openide.cookies.OpenCookie;
 
57
import org.openide.nodes.AbstractNode;
 
58
import org.openide.nodes.Children;
 
59
import org.openide.nodes.Node;
 
60
import org.openide.nodes.PropertySupport;
 
61
import org.openide.nodes.Sheet;
 
62
import org.openide.NotifyDescriptor;
 
63
import org.openide.DialogDisplayer;
 
64
import org.openide.util.actions.SystemAction;
 
65
import org.openide.util.HelpCtx;
 
66
import org.openide.util.NbBundle;
 
67
import org.openide.util.WeakListeners;
 
68
 
 
69
 
 
70
/** 
 
71
 * Node representing a key-value-comment item in one .properties file.
 
72
 *
 
73
 * @author Petr Jiricka
 
74
 */
 
75
public class KeyNode extends AbstractNode implements PropertyChangeListener {
 
76
 
 
77
    /** Structure on top of which this element lives. */
 
78
    private PropertiesStructure propStructure;
 
79
    
 
80
    /** nonescaped Key for the element. */
 
81
    private String itemKey;
 
82
    
 
83
    /** Generated Serialized Version UID. */
 
84
    static final long serialVersionUID = -7882925922830244768L;
 
85
 
 
86
 
 
87
    /** Constructor.
 
88
     * @param propStructure structure of .properties file to work with
 
89
     * @param itemKey key value of item in properties structure
 
90
     */
 
91
    public KeyNode (PropertiesStructure propStructure, String itemKey) {
 
92
        super(Children.LEAF);
 
93
        
 
94
        this.propStructure = propStructure;
 
95
        this.itemKey = itemKey;
 
96
        
 
97
        super.setName(itemKey);
 
98
        
 
99
        setActions(
 
100
            new SystemAction[] {
 
101
                SystemAction.get(EditAction.class),
 
102
                SystemAction.get(OpenAction.class),
 
103
                SystemAction.get(FileSystemAction.class),
 
104
                null,
 
105
                SystemAction.get(CutAction.class),
 
106
                SystemAction.get(CopyAction.class),
 
107
                null,
 
108
                SystemAction.get(DeleteAction.class),
 
109
                SystemAction.get(RenameAction.class),
 
110
                null,
 
111
                SystemAction.get(ToolsAction.class),
 
112
                SystemAction.get(PropertiesAction.class)
 
113
            }
 
114
        );
 
115
        
 
116
        setIconBaseWithExtension("org/netbeans/modules/properties/propertiesKey.gif"); // NOI18N
 
117
 
 
118
        // Sets short description.
 
119
        updateShortDescription();
 
120
 
 
121
        // Sets cookies (Open and Edit).
 
122
        PropertiesDataObject pdo = ((PropertiesDataObject)propStructure.getParent().getEntry().getDataObject());
 
123
 
 
124
        getCookieSet().add(pdo.getOpenSupport().new PropertiesOpenAt(propStructure.getParent().getEntry(), itemKey));
 
125
        getCookieSet().add(propStructure.getParent().getEntry().getPropertiesEditor().new PropertiesEditAt(itemKey));
 
126
 
 
127
        Element.ItemElem item = getItem();
 
128
        PropertyChangeListener pcl = WeakListeners.propertyChange(this, item);
 
129
        item.addPropertyChangeListener(pcl);
 
130
    }
 
131
    
 
132
    public Action getPreferredAction() {
 
133
        return getActions(false)[0];
 
134
    }
 
135
 
 
136
    /** Gets <code>Element.ItemElem</code> represented by this node.
 
137
     * @return item element
 
138
     */
 
139
    public Element.ItemElem getItem() {
 
140
        return propStructure.getItem(itemKey);
 
141
    }
 
142
    
 
143
    /** Indicates whether the node may be destroyed. Overrides superclass method.
 
144
     * @return true.
 
145
     */
 
146
    public boolean canDestroy () {
 
147
        return true;
 
148
    }
 
149
 
 
150
    /** Destroyes the node. Overrides superclass method. */
 
151
    public void destroy () throws IOException {
 
152
        propStructure.deleteItem(itemKey);
 
153
        super.destroy ();
 
154
    }
 
155
 
 
156
    /** Indicates if node allows copying. Overrides superclass method.
 
157
     * @return true.
 
158
     */
 
159
    public final boolean canCopy () {
 
160
        return true;
 
161
    }
 
162
 
 
163
    /** Indicates if node allows cutting. Overrides superclass method.
 
164
     * @return true.
 
165
     */
 
166
    public final boolean canCut () {
 
167
        return true;
 
168
    }
 
169
 
 
170
    /** Indicates if node can be renamed. Overrides superclass method.
 
171
     * @returns true.
 
172
     */
 
173
    public final boolean canRename () {
 
174
        return true;
 
175
    }
 
176
 
 
177
    /** Sets name of the node. Overrides superclass method.
 
178
     * @param name new name for the object
 
179
     */
 
180
    public void setName(final String name) {
 
181
        // The new name is same -> do nothing.
 
182
        if(name.equals(itemKey)) return;
 
183
        
 
184
        String oldKey = itemKey;
 
185
        itemKey = name;
 
186
        if (false == propStructure.renameItem(oldKey, name)) {
 
187
            itemKey = oldKey;
 
188
            NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
 
189
                NbBundle.getBundle(KeyNode.class).getString("MSG_CannotRenameKey"),
 
190
                NotifyDescriptor.ERROR_MESSAGE
 
191
            );
 
192
            DialogDisplayer.getDefault().notify(msg);
 
193
            return;
 
194
        }
 
195
        
 
196
        updateCookieNames();
 
197
    }
 
198
 
 
199
    /** Initializes sheet of properties. Overrides superclass method.
 
200
     * @return default sheet to use
 
201
     */
 
202
    protected Sheet createSheet () {
 
203
        Sheet sheet = Sheet.createDefault ();
 
204
        Sheet.Set sheetSet = sheet.get (Sheet.PROPERTIES);
 
205
 
 
206
        Node.Property property;
 
207
 
 
208
        // Key property.
 
209
        property = new PropertySupport.ReadWrite<String>(
 
210
                PROP_NAME,
 
211
                String.class,
 
212
                NbBundle.getBundle(KeyNode.class).getString("PROP_item_key"),
 
213
                NbBundle.getBundle(KeyNode.class).getString("HINT_item_key")
 
214
            ) {
 
215
                public String getValue() {
 
216
                    return itemKey;
 
217
                }
 
218
 
 
219
                public void setValue(String val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
 
220
                    KeyNode.this.setName(val);
 
221
                }
 
222
            };
 
223
        property.setName(Element.ItemElem.PROP_ITEM_KEY);
 
224
        sheetSet.put (property);
 
225
 
 
226
        // Value property
 
227
        property = new PropertySupport.ReadWrite<String>(
 
228
                Element.ItemElem.PROP_ITEM_VALUE,
 
229
                String.class,
 
230
                NbBundle.getBundle(KeyNode.class).getString("PROP_item_value"),
 
231
                NbBundle.getBundle(KeyNode.class).getString("HINT_item_value")
 
232
            ) {
 
233
                public String getValue() {
 
234
                    return getItem().getValue();
 
235
                }
 
236
 
 
237
                public void setValue(String val) throws IllegalAccessException,
 
238
                    IllegalArgumentException, InvocationTargetException {
 
239
                    getItem().setValue(val);
 
240
                }
 
241
            };
 
242
        property.setName(Element.ItemElem.PROP_ITEM_VALUE);
 
243
        sheetSet.put (property);
 
244
 
 
245
        // Comment property
 
246
        property = new PropertySupport.ReadWrite<String>(
 
247
                Element.ItemElem.PROP_ITEM_COMMENT,
 
248
                String.class,
 
249
                NbBundle.getBundle(KeyNode.class).getString("PROP_item_comment"),
 
250
                NbBundle.getBundle(KeyNode.class).getString("HINT_item_comment")
 
251
            ) {
 
252
                public String getValue() {
 
253
                    return getItem().getComment();
 
254
                }
 
255
 
 
256
                public void setValue(String val) throws IllegalAccessException,
 
257
                    IllegalArgumentException, InvocationTargetException {
 
258
                    getItem().setComment(val);
 
259
                }
 
260
            };
 
261
        property.setName(Element.ItemElem.PROP_ITEM_COMMENT);
 
262
        sheetSet.put (property);
 
263
 
 
264
        return sheet;
 
265
    }
 
266
 
 
267
    /** Returns item as cookie in addition to "normal" cookies. Overrides superclass method. */
 
268
    @SuppressWarnings("unchecked")
 
269
    public <T extends Node.Cookie> T getCookie(Class<T> clazz) {
 
270
        if (clazz.isInstance(getItem())) {
 
271
            return (T) getItem();
 
272
        }
 
273
        if (clazz.equals(SaveCookie.class)) {
 
274
            return propStructure.getParent().getEntry().getCookie(clazz);
 
275
        }
 
276
        return super.getCookie(clazz);
 
277
    }
 
278
 
 
279
    /** Sets short description. Helper method. Calls superclass <code>updateShortDescription(String)</code> method.
 
280
     * @see java.beans.FeatureDescriptor#setShortDecription(String) */
 
281
    private void updateShortDescription() {
 
282
        String description;
 
283
        
 
284
        Element.ItemElem item = getItem();
 
285
 
 
286
        if(item != null) {
 
287
            String comment = item.getComment();
 
288
            if (comment != null) {
 
289
                int displayLenght = Math.min(comment.length(),72);
 
290
                description = comment.substring(0, displayLenght);
 
291
                if (displayLenght < comment.length()) {
 
292
                    description += "...";           //NOI18N
 
293
                }
 
294
            } else {
 
295
                description = item.getKey() + "=" + item.getValue(); // NOI18N
 
296
            }
 
297
        } else {
 
298
            description = itemKey;
 
299
        }
 
300
        
 
301
        setShortDescription(description);
 
302
    }
 
303
 
 
304
    /** Indicates whether has customizer. Overrides superclass method. 
 
305
     * @return <code>true</code> */
 
306
    public boolean hasCustomizer() {
 
307
        return true;
 
308
    }
 
309
    
 
310
    /** Gets customizer. Overrides superclass method. 
 
311
     * @return customizer for this key node, <code>PropertyPanel</code> instance */
 
312
    public Component getCustomizer() {
 
313
        return new PropertyPanel(getItem());
 
314
    }
 
315
    
 
316
    /** Updates the cookies for editing/viewing at a given position (position of key element representing by this node). Helper method. */
 
317
    private void updateCookieNames() {
 
318
        // Open cookie.
 
319
        Node.Cookie opener = getCookie(OpenCookie.class);
 
320
        if(opener instanceof PropertiesOpen.PropertiesOpenAt) {
 
321
            ((PropertiesOpen.PropertiesOpenAt)opener).setKey(itemKey);
 
322
        }
 
323
 
 
324
        // Edit cookie.
 
325
        Node.Cookie editor = getCookie(EditCookie.class);
 
326
        if(editor instanceof PropertiesEditorSupport.PropertiesEditAt) {
 
327
            ((PropertiesEditorSupport.PropertiesEditAt)editor).setKey(itemKey);
 
328
        }
 
329
    }
 
330
    
 
331
    /** Sets all actions for this node. Helper method.
 
332
     * @param actions new list of actions
 
333
     */
 
334
    private void setActions(SystemAction[] actions) {
 
335
        systemActions = actions;
 
336
    }
 
337
 
 
338
    /**
 
339
     * This method gets called when a bound property is changed.
 
340
     * @param evt A PropertyChangeEvent object describing the event source
 
341
     *          and the property that has changed.
 
342
     */
 
343
 
 
344
    public void propertyChange(PropertyChangeEvent evt) {
 
345
        if (Element.ItemElem.PROP_ITEM_COMMENT.equals(evt.getPropertyName())) {
 
346
            updateShortDescription();
 
347
        }
 
348
        else if (Element.ItemElem.PROP_ITEM_VALUE.equals(evt.getPropertyName())) {
 
349
            updateShortDescription();
 
350
        }
 
351
    }
 
352
 
 
353
}