KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import javax.swing.Action JavaDoc;
29
30 import org.openide.loaders.DataObject;
31 import org.openide.nodes.*;
32 import org.openide.util.actions.SystemAction;
33 import org.openide.util.NbBundle;
34 import org.openide.util.WeakListeners;
35
36
37 /**
38  * Standard node representing a <code>PresentableFileEntry</code>.
39  *
40  * @see PresentableFileEntry
41  * @author Petr Jiricka
42  */

43 public class FileEntryNode extends AbstractNode {
44
45     /** generated Serialized Version UID */
46     static final long serialVersionUID = -7882925922830244768L;
47
48     /** FileEntry of this node. */
49     private PresentableFileEntry entry;
50
51
52     /**
53      * Creates a data node for a given file entry.
54      * The provided children object will be used to hold all child nodes.
55      *
56      * @param entry entry to work with
57      * @param ch children container for the node
58      */

59     public FileEntryNode (PresentableFileEntry entry, Children ch) {
60         super (ch);
61         this.entry = entry;
62         
63         PropL propListener = new PropL ();
64         entry.addPropertyChangeListener(
65                 WeakListeners.propertyChange(propListener, entry));
66         entry.getDataObject().addPropertyChangeListener (propListener);
67         
68         super.setName (entry.getName ());
69     }
70     
71     private String JavaDoc getBundleString(String JavaDoc s){
72         return NbBundle.getMessage(FileEntryNode.class, s);
73     }
74
75
76     /** Gets the represented entry.
77      * @return the entry
78      */

79     public PresentableFileEntry getFileEntry() {
80         return entry;
81     }
82
83     /** Indicate whether the node may be destroyed.
84      * @return tests {@link DataObject#isDeleteAllowed}
85      */

86     public boolean canDestroy () {
87         return entry.isDeleteAllowed ();
88     }
89
90     /** Destroyes the node. */
91     public void destroy () throws IOException JavaDoc {
92         entry.delete ();
93         super.destroy ();
94     }
95
96     /**
97      * @returns true if this node allows copying.
98      */

99     public final boolean canCopy () {
100         return entry.isCopyAllowed ();
101     }
102
103     /**
104      * @returns true if this node allows cutting.
105      */

106     public final boolean canCut () {
107         return entry.isMoveAllowed ();
108     }
109
110     /** Rename the data object.
111      * @param name new name for the object
112      * @exception IllegalArgumentException if the rename failed
113      */

114     public void setName (String JavaDoc name) {
115         try {
116             entry.renameEntry (name);
117             super.setName (name);
118         } catch (IOException JavaDoc ex) {
119             throw new IllegalArgumentException JavaDoc (ex.getMessage ());
120         }
121     }
122
123     /** Gets default action.
124      * @deprecated
125      * @return no action if the underlying entry is a template. Otherwise the abstract node's default action is returned, possibly <code>null</code>.
126      */

127     public SystemAction getDefaultAction () {
128         if (entry.isTemplate ()) {
129             return null;
130         } else {
131             Action JavaDoc a = getPreferredAction();
132             if(a instanceof SystemAction){
133                 return (SystemAction) a;
134             } else {
135                 return null;
136             }
137         }
138     }
139  
140     /** Gets default action.
141      * @return no action if the underlying entry is a template. Otherwise the abstract node's default action is returned, possibly <code>null</code>.
142      */

143     public Action JavaDoc getPreferredAction() {
144         if (entry.isTemplate ()) {
145             return null;
146         } else {
147             return super.getPreferredAction();
148         }
149     }
150     
151     /** Get a cookie.
152      * First of all {@link PresentableFileEntry#getCookie} is
153      * called. If it produces non-<code>null</code> result, that is returned.
154      * Otherwise the superclass is tried.
155      * @return the cookie or <code>null</code>
156      */

157     public <T extends Node.Cookie> T getCookie(Class JavaDoc<T> cl) {
158         T c = entry.getCookie(cl);
159         if (c != null) {
160             return c;
161         } else {
162             return super.getCookie (cl);
163         }
164     }
165
166     /** Initializes sheet of properties. Allows subclasses to
167      * overwrite it.
168      * @return the default sheet to use
169      */

170     protected Sheet createSheet () {
171         Sheet s = Sheet.createDefault ();
172         Sheet.Set ss = s.get (Sheet.PROPERTIES);
173
174         Node.Property p;
175
176         p = new PropertySupport.ReadWrite<String JavaDoc>(
177                 PROP_NAME,
178                 String JavaDoc.class,
179                 getBundleString("PROP_name"),
180                 getBundleString("HINT_name")
181             ) {
182                 public String JavaDoc getValue() {
183                     return entry.getName();
184                 }
185
186                 public void setValue(String JavaDoc val) throws IllegalAccessException JavaDoc,
187                     IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
188                     if (!canWrite()) {
189                         throw new IllegalAccessException JavaDoc();
190                     }
191                     FileEntryNode.this.setName(val);
192                 }
193
194                 public boolean canWrite () {
195                     return entry.isRenameAllowed();
196                 }
197             };
198         p.setName (DataObject.PROP_NAME);
199         ss.put (p);
200
201         try {
202             p = new PropertySupport.Reflection<Boolean JavaDoc>(
203                     entry, Boolean.TYPE, "isTemplate", "setTemplate" // NOI18N
204
);
205             p.setName (DataObject.PROP_TEMPLATE);
206             p.setDisplayName (getBundleString("PROP_template"));
207             p.setShortDescription (getBundleString("HINT_template"));
208             ss.put (p);
209         } catch(Exception JavaDoc ex) {
210             throw new IllegalStateException JavaDoc();
211         }
212         return s;
213     }
214
215
216     /**
217      * Support for firing property change.
218      *
219      * @param ev event describing the change
220      */

221     void fireChange (PropertyChangeEvent JavaDoc ev) {
222         String JavaDoc propertyName = ev.getPropertyName();
223         if (propertyName.equals(Node.PROP_COOKIE)) {
224             fireCookieChange();
225             return;
226         }
227         firePropertyChange(propertyName, ev.getOldValue(), ev.getNewValue());
228         if (propertyName.equals(DataObject.PROP_NAME)) {
229             super.setName (entry.getName ());
230         }
231     }
232
233     /** Property listener on data object that delegates all changes of
234      * properties to this node.
235      */

236     private class PropL extends Object JavaDoc implements PropertyChangeListener JavaDoc {
237         public void propertyChange (PropertyChangeEvent JavaDoc ev) {
238             fireChange (ev);
239         }
240     }
241     
242 }
243
Popular Tags