KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > KeyNode


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
21 package org.netbeans.modules.properties;
22
23
24 import java.awt.Component JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import javax.swing.Action JavaDoc;
30
31 import org.openide.actions.*;
32 import org.openide.cookies.EditCookie;
33 import org.openide.cookies.SaveCookie;
34 import org.openide.cookies.OpenCookie;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.nodes.PropertySupport;
39 import org.openide.nodes.Sheet;
40 import org.openide.NotifyDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.util.actions.SystemAction;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.NbBundle;
45 import org.openide.util.WeakListeners;
46
47
48 /**
49  * Node representing a key-value-comment item in one .properties file.
50  *
51  * @author Petr Jiricka
52  */

53 public class KeyNode extends AbstractNode implements PropertyChangeListener JavaDoc {
54
55     /** Structure on top of which this element lives. */
56     private PropertiesStructure propStructure;
57     
58     /** nonescaped Key for the element. */
59     private String JavaDoc itemKey;
60     
61     /** Generated Serialized Version UID. */
62     static final long serialVersionUID = -7882925922830244768L;
63
64
65     /** Constructor.
66      * @param propStructure structure of .properties file to work with
67      * @param itemKey key value of item in properties structure
68      */

69     public KeyNode (PropertiesStructure propStructure, String JavaDoc itemKey) {
70         super(Children.LEAF);
71         
72         this.propStructure = propStructure;
73         this.itemKey = itemKey;
74         
75         super.setName(itemKey);
76         
77         setActions(
78             new SystemAction[] {
79                 SystemAction.get(EditAction.class),
80                 SystemAction.get(OpenAction.class),
81                 SystemAction.get(FileSystemAction.class),
82                 null,
83                 SystemAction.get(CutAction.class),
84                 SystemAction.get(CopyAction.class),
85                 null,
86                 SystemAction.get(DeleteAction.class),
87                 SystemAction.get(RenameAction.class),
88                 null,
89                 SystemAction.get(ToolsAction.class),
90                 SystemAction.get(PropertiesAction.class)
91             }
92         );
93         
94         setIconBaseWithExtension("org/netbeans/modules/properties/propertiesKey.gif"); // NOI18N
95

96         // Sets short description.
97
updateShortDescription();
98
99         // Sets cookies (Open and Edit).
100
PropertiesDataObject pdo = ((PropertiesDataObject)propStructure.getParent().getEntry().getDataObject());
101
102         getCookieSet().add(pdo.getOpenSupport().new PropertiesOpenAt(propStructure.getParent().getEntry(), itemKey));
103         getCookieSet().add(propStructure.getParent().getEntry().getPropertiesEditor().new PropertiesEditAt(itemKey));
104
105         Element.ItemElem item = getItem();
106         PropertyChangeListener JavaDoc pcl = WeakListeners.propertyChange(this, item);
107         item.addPropertyChangeListener(pcl);
108     }
109     
110     public Action JavaDoc getPreferredAction() {
111         return getActions(false)[0];
112     }
113
114     /** Gets <code>Element.ItemElem</code> represented by this node.
115      * @return item element
116      */

117     public Element.ItemElem getItem() {
118         return propStructure.getItem(itemKey);
119     }
120     
121     /** Indicates whether the node may be destroyed. Overrides superclass method.
122      * @return true.
123      */

124     public boolean canDestroy () {
125         return true;
126     }
127
128     /** Destroyes the node. Overrides superclass method. */
129     public void destroy () throws IOException JavaDoc {
130         propStructure.deleteItem(itemKey);
131         super.destroy ();
132     }
133
134     /** Indicates if node allows copying. Overrides superclass method.
135      * @return true.
136      */

137     public final boolean canCopy () {
138         return true;
139     }
140
141     /** Indicates if node allows cutting. Overrides superclass method.
142      * @return true.
143      */

144     public final boolean canCut () {
145         return true;
146     }
147
148     /** Indicates if node can be renamed. Overrides superclass method.
149      * @returns true.
150      */

151     public final boolean canRename () {
152         return true;
153     }
154
155     /** Sets name of the node. Overrides superclass method.
156      * @param name new name for the object
157      */

158     public void setName(final String JavaDoc name) {
159         // The new name is same -> do nothing.
160
if(name.equals(itemKey)) return;
161         
162         String JavaDoc oldKey = itemKey;
163         itemKey = name;
164         if (false == propStructure.renameItem(oldKey, name)) {
165             itemKey = oldKey;
166             NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
167                 NbBundle.getBundle(KeyNode.class).getString("MSG_CannotRenameKey"),
168                 NotifyDescriptor.ERROR_MESSAGE
169             );
170             DialogDisplayer.getDefault().notify(msg);
171             return;
172         }
173         
174         updateCookieNames();
175     }
176
177     /** Initializes sheet of properties. Overrides superclass method.
178      * @return default sheet to use
179      */

180     protected Sheet createSheet () {
181         Sheet sheet = Sheet.createDefault ();
182         Sheet.Set sheetSet = sheet.get (Sheet.PROPERTIES);
183
184         Node.Property property;
185
186         // Key property.
187
property = new PropertySupport.ReadWrite<String JavaDoc>(
188                 PROP_NAME,
189                 String JavaDoc.class,
190                 NbBundle.getBundle(KeyNode.class).getString("PROP_item_key"),
191                 NbBundle.getBundle(KeyNode.class).getString("HINT_item_key")
192             ) {
193                 public String JavaDoc getValue() {
194                     return itemKey;
195                 }
196
197                 public void setValue(String JavaDoc val) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
198                     KeyNode.this.setName(val);
199                 }
200             };
201         property.setName(Element.ItemElem.PROP_ITEM_KEY);
202         sheetSet.put (property);
203
204         // Value property
205
property = new PropertySupport.ReadWrite<String JavaDoc>(
206                 Element.ItemElem.PROP_ITEM_VALUE,
207                 String JavaDoc.class,
208                 NbBundle.getBundle(KeyNode.class).getString("PROP_item_value"),
209                 NbBundle.getBundle(KeyNode.class).getString("HINT_item_value")
210             ) {
211                 public String JavaDoc getValue() {
212                     return getItem().getValue();
213                 }
214
215                 public void setValue(String JavaDoc val) throws IllegalAccessException JavaDoc,
216                     IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
217                     getItem().setValue(val);
218                 }
219             };
220         property.setName(Element.ItemElem.PROP_ITEM_VALUE);
221         sheetSet.put (property);
222
223         // Comment property
224
property = new PropertySupport.ReadWrite<String JavaDoc>(
225                 Element.ItemElem.PROP_ITEM_COMMENT,
226                 String JavaDoc.class,
227                 NbBundle.getBundle(KeyNode.class).getString("PROP_item_comment"),
228                 NbBundle.getBundle(KeyNode.class).getString("HINT_item_comment")
229             ) {
230                 public String JavaDoc getValue() {
231                     return getItem().getComment();
232                 }
233
234                 public void setValue(String JavaDoc val) throws IllegalAccessException JavaDoc,
235                     IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
236                     getItem().setComment(val);
237                 }
238             };
239         property.setName(Element.ItemElem.PROP_ITEM_COMMENT);
240         sheetSet.put (property);
241
242         return sheet;
243     }
244
245     /** Returns item as cookie in addition to "normal" cookies. Overrides superclass method. */
246     @SuppressWarnings JavaDoc("unchecked")
247     public <T extends Node.Cookie> T getCookie(Class JavaDoc<T> clazz) {
248         if (clazz.isInstance(getItem())) {
249             return (T) getItem();
250         }
251         if (clazz.equals(SaveCookie.class)) {
252             return propStructure.getParent().getEntry().getCookie(clazz);
253         }
254         return super.getCookie(clazz);
255     }
256
257     /** Sets short description. Helper method. Calls superclass <code>updateShortDescription(String)</code> method.
258      * @see java.beans.FeatureDescriptor#setShortDecription(String) */

259     private void updateShortDescription() {
260         String JavaDoc description;
261         
262         Element.ItemElem item = getItem();
263
264         if(item != null) {
265             String JavaDoc comment = item.getComment();
266             if (comment != null) {
267                 int displayLenght = Math.min(comment.length(),72);
268                 description = comment.substring(0, displayLenght);
269                 if (displayLenght < comment.length()) {
270                     description += "..."; //NOI18N
271
}
272             } else {
273                 description = item.getKey() + "=" + item.getValue(); // NOI18N
274
}
275         } else {
276             description = itemKey;
277         }
278         
279         setShortDescription(description);
280     }
281
282     /** Indicates whether has customizer. Overrides superclass method.
283      * @return <code>true</code> */

284     public boolean hasCustomizer() {
285         return true;
286     }
287     
288     /** Gets customizer. Overrides superclass method.
289      * @return customizer for this key node, <code>PropertyPanel</code> instance */

290     public Component JavaDoc getCustomizer() {
291         return new PropertyPanel(getItem());
292     }
293     
294     /** Updates the cookies for editing/viewing at a given position (position of key element representing by this node). Helper method. */
295     private void updateCookieNames() {
296         // Open cookie.
297
Node.Cookie opener = getCookie(OpenCookie.class);
298         if(opener instanceof PropertiesOpen.PropertiesOpenAt) {
299             ((PropertiesOpen.PropertiesOpenAt)opener).setKey(itemKey);
300         }
301
302         // Edit cookie.
303
Node.Cookie editor = getCookie(EditCookie.class);
304         if(editor instanceof PropertiesEditorSupport.PropertiesEditAt) {
305             ((PropertiesEditorSupport.PropertiesEditAt)editor).setKey(itemKey);
306         }
307     }
308     
309     /** Sets all actions for this node. Helper method.
310      * @param actions new list of actions
311      */

312     private void setActions(SystemAction[] actions) {
313         systemActions = actions;
314     }
315
316     /**
317      * This method gets called when a bound property is changed.
318      * @param evt A PropertyChangeEvent object describing the event source
319      * and the property that has changed.
320      */

321
322     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
323         if (Element.ItemElem.PROP_ITEM_COMMENT.equals(evt.getPropertyName())) {
324             updateShortDescription();
325         }
326         else if (Element.ItemElem.PROP_ITEM_VALUE.equals(evt.getPropertyName())) {
327             updateShortDescription();
328         }
329     }
330
331 }
332
Popular Tags