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.
43
package org.netbeans.modules.properties;
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;
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;
71
* Node representing a key-value-comment item in one .properties file.
73
* @author Petr Jiricka
75
public class KeyNode extends AbstractNode implements PropertyChangeListener {
77
/** Structure on top of which this element lives. */
78
private PropertiesStructure propStructure;
80
/** nonescaped Key for the element. */
81
private String itemKey;
83
/** Generated Serialized Version UID. */
84
static final long serialVersionUID = -7882925922830244768L;
88
* @param propStructure structure of .properties file to work with
89
* @param itemKey key value of item in properties structure
91
public KeyNode (PropertiesStructure propStructure, String itemKey) {
94
this.propStructure = propStructure;
95
this.itemKey = itemKey;
97
super.setName(itemKey);
101
SystemAction.get(EditAction.class),
102
SystemAction.get(OpenAction.class),
103
SystemAction.get(FileSystemAction.class),
105
SystemAction.get(CutAction.class),
106
SystemAction.get(CopyAction.class),
108
SystemAction.get(DeleteAction.class),
109
SystemAction.get(RenameAction.class),
111
SystemAction.get(ToolsAction.class),
112
SystemAction.get(PropertiesAction.class)
116
setIconBaseWithExtension("org/netbeans/modules/properties/propertiesKey.gif"); // NOI18N
118
// Sets short description.
119
updateShortDescription();
121
// Sets cookies (Open and Edit).
122
PropertiesDataObject pdo = ((PropertiesDataObject)propStructure.getParent().getEntry().getDataObject());
124
getCookieSet().add(pdo.getOpenSupport().new PropertiesOpenAt(propStructure.getParent().getEntry(), itemKey));
125
getCookieSet().add(propStructure.getParent().getEntry().getPropertiesEditor().new PropertiesEditAt(itemKey));
127
Element.ItemElem item = getItem();
128
PropertyChangeListener pcl = WeakListeners.propertyChange(this, item);
129
item.addPropertyChangeListener(pcl);
132
public Action getPreferredAction() {
133
return getActions(false)[0];
136
/** Gets <code>Element.ItemElem</code> represented by this node.
137
* @return item element
139
public Element.ItemElem getItem() {
140
return propStructure.getItem(itemKey);
143
/** Indicates whether the node may be destroyed. Overrides superclass method.
146
public boolean canDestroy () {
150
/** Destroyes the node. Overrides superclass method. */
151
public void destroy () throws IOException {
152
propStructure.deleteItem(itemKey);
156
/** Indicates if node allows copying. Overrides superclass method.
159
public final boolean canCopy () {
163
/** Indicates if node allows cutting. Overrides superclass method.
166
public final boolean canCut () {
170
/** Indicates if node can be renamed. Overrides superclass method.
173
public final boolean canRename () {
177
/** Sets name of the node. Overrides superclass method.
178
* @param name new name for the object
180
public void setName(final String name) {
181
// The new name is same -> do nothing.
182
if(name.equals(itemKey)) return;
184
String oldKey = itemKey;
186
if (false == propStructure.renameItem(oldKey, name)) {
188
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
189
NbBundle.getBundle(KeyNode.class).getString("MSG_CannotRenameKey"),
190
NotifyDescriptor.ERROR_MESSAGE
192
DialogDisplayer.getDefault().notify(msg);
199
/** Initializes sheet of properties. Overrides superclass method.
200
* @return default sheet to use
202
protected Sheet createSheet () {
203
Sheet sheet = Sheet.createDefault ();
204
Sheet.Set sheetSet = sheet.get (Sheet.PROPERTIES);
206
Node.Property property;
209
property = new PropertySupport.ReadWrite<String>(
212
NbBundle.getBundle(KeyNode.class).getString("PROP_item_key"),
213
NbBundle.getBundle(KeyNode.class).getString("HINT_item_key")
215
public String getValue() {
219
public void setValue(String val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
220
KeyNode.this.setName(val);
223
property.setName(Element.ItemElem.PROP_ITEM_KEY);
224
sheetSet.put (property);
227
property = new PropertySupport.ReadWrite<String>(
228
Element.ItemElem.PROP_ITEM_VALUE,
230
NbBundle.getBundle(KeyNode.class).getString("PROP_item_value"),
231
NbBundle.getBundle(KeyNode.class).getString("HINT_item_value")
233
public String getValue() {
234
return getItem().getValue();
237
public void setValue(String val) throws IllegalAccessException,
238
IllegalArgumentException, InvocationTargetException {
239
getItem().setValue(val);
242
property.setName(Element.ItemElem.PROP_ITEM_VALUE);
243
sheetSet.put (property);
246
property = new PropertySupport.ReadWrite<String>(
247
Element.ItemElem.PROP_ITEM_COMMENT,
249
NbBundle.getBundle(KeyNode.class).getString("PROP_item_comment"),
250
NbBundle.getBundle(KeyNode.class).getString("HINT_item_comment")
252
public String getValue() {
253
return getItem().getComment();
256
public void setValue(String val) throws IllegalAccessException,
257
IllegalArgumentException, InvocationTargetException {
258
getItem().setComment(val);
261
property.setName(Element.ItemElem.PROP_ITEM_COMMENT);
262
sheetSet.put (property);
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();
273
if (clazz.equals(SaveCookie.class)) {
274
return propStructure.getParent().getEntry().getCookie(clazz);
276
return super.getCookie(clazz);
279
/** Sets short description. Helper method. Calls superclass <code>updateShortDescription(String)</code> method.
280
* @see java.beans.FeatureDescriptor#setShortDecription(String) */
281
private void updateShortDescription() {
284
Element.ItemElem item = getItem();
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
295
description = item.getKey() + "=" + item.getValue(); // NOI18N
298
description = itemKey;
301
setShortDescription(description);
304
/** Indicates whether has customizer. Overrides superclass method.
305
* @return <code>true</code> */
306
public boolean hasCustomizer() {
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());
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() {
319
Node.Cookie opener = getCookie(OpenCookie.class);
320
if(opener instanceof PropertiesOpen.PropertiesOpenAt) {
321
((PropertiesOpen.PropertiesOpenAt)opener).setKey(itemKey);
325
Node.Cookie editor = getCookie(EditCookie.class);
326
if(editor instanceof PropertiesEditorSupport.PropertiesEditAt) {
327
((PropertiesEditorSupport.PropertiesEditAt)editor).setKey(itemKey);
331
/** Sets all actions for this node. Helper method.
332
* @param actions new list of actions
334
private void setActions(SystemAction[] actions) {
335
systemActions = actions;
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.
344
public void propertyChange(PropertyChangeEvent evt) {
345
if (Element.ItemElem.PROP_ITEM_COMMENT.equals(evt.getPropertyName())) {
346
updateShortDescription();
348
else if (Element.ItemElem.PROP_ITEM_VALUE.equals(evt.getPropertyName())) {
349
updateShortDescription();