KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.properties;
21
22 import java.awt.datatransfer.*;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.*;
26
27 import javax.swing.*;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30
31 import org.openide.loaders.*;
32 import org.openide.*;
33 import org.openide.util.datatransfer.*;
34 import org.openide.filesystems.FileObject;
35 import org.openide.util.*;
36 import org.openide.nodes.*;
37
38 /**
39  * Object that represents one <code>FileEntry</code> and has support
40  * for presentation of this entry as a node. I.&thinsp;i. it contains methods
41  * required for a node, so that a node for the entry may just delegate to it.
42  *
43  * @author Jaroslav Tulach, Petr Jiricka
44  */

45 public abstract class PresentableFileEntry extends FileEntry
46                                            implements Node.Cookie {
47     
48     /** generated Serialized Version UID */
49     static final long serialVersionUID = 3328227388376142699L;
50     
51     /** The node delegate for this data object. */
52     private transient Node nodeDelegate;
53     
54     /** Modified flag */
55     private boolean modif = false;
56     
57     /** property change listener support */
58     private transient PropertyChangeSupport JavaDoc changeSupport;
59     
60     /** listener for changes in the cookie set */
61     private ChangeListener JavaDoc cookieL = new ChangeListener JavaDoc () {
62         public void stateChanged (ChangeEvent JavaDoc ev) {
63             firePropertyChange (Node.PROP_COOKIE, null, null);
64         }
65     };
66     
67     /** array of cookies for this entry */
68     private transient CookieSet cookieSet;
69
70     // guard used in getNodeDelegate
71
private transient Object JavaDoc nodeDelegateMutex = new Object JavaDoc();
72     
73     /**
74      * Creates a new presentable file entry initially attached
75      * to a given file object.
76      *
77      * @param obj the data object this entry belongs to
78      * @param fo the file object for the entry
79      */

80     public PresentableFileEntry(MultiDataObject obj, FileObject fo) {
81         super (obj, fo);
82     }
83     
84
85     /** Creates a node delegate for this entry. */
86     protected abstract Node createNodeDelegate();
87     
88     /**
89      * Gets a node delegate for this data object entry. Either
90      * {@linkplain #createNodeDelegate creates it} (if it does not exist yet)
91      * or returns a previously created instance of it.
92      *
93      * @return the node delegate (without parent) for this data object
94      */

95     public final Node getNodeDelegate () {
96         synchronized (nodeDelegateMutex) {
97             if (nodeDelegate == null) {
98                 nodeDelegate = createNodeDelegate();
99             }
100             return nodeDelegate;
101         }
102     }
103     
104     /**
105      * Sets value of attribute &quot;is template?&quot; for a given file.
106      * Used also from FileEntry.
107      *
108      * @param fo file to assign the attribute to
109      * @param newValue new value of the attribute
110      * @return <code>true</code> if the value was changed;
111      * <code>false</code> if the new value was the same
112      * as the old value
113      * @exception java.io.IOException if the operation failed
114      */

115     private static boolean setTemplate(FileObject fo,
116                                        boolean newValue) throws IOException {
117         Object JavaDoc old = fo.getAttribute(DataObject.PROP_TEMPLATE);
118         boolean oldValue = Boolean.TRUE.equals(old);
119         if (newValue == oldValue) {
120             return false;
121         }
122         fo.setAttribute(DataObject.PROP_TEMPLATE,
123                         newValue ? Boolean.TRUE : null);
124         return true;
125     }
126     
127     /**
128      * Sets value of attribute &quot;is template?&quot; for this entry's file.
129      *
130      * @param newValue new value of the attribute
131      * @return <code>true</code> if the value was changed;
132      * <code>false</code> if the new value was the same
133      * as the old value
134      * @exception java.io.IOException if setting the template state fails
135      */

136     public final void setTemplate(boolean newValue) throws IOException {
137         if (!setTemplate(getFile(), newValue)) {
138             // no change in state
139
return;
140         }
141         firePropertyChange(DataObject.PROP_TEMPLATE,
142                            Boolean.valueOf(!newValue),
143                            Boolean.valueOf(newValue));
144     }
145     
146     /**
147      * Get the template status of this data object entry.
148      *
149      * @return <code>true</code> if it is a template;
150      * <code>false</code> otherwise
151      */

152     public boolean isTemplate() {
153         Object JavaDoc o = getFile().getAttribute(DataObject.PROP_TEMPLATE);
154         return Boolean.TRUE.equals(o);
155     }
156     
157     /**
158      * Renames underlying fileobject. This implementation returns the
159      * same file. Fires property change. Called when the DO is renamed, not the entry
160      *
161      * @param name new name
162      * @return file object with renamed file
163      * @see #renameEntry
164      */

165     public FileObject rename (String JavaDoc name) throws IOException {
166         String JavaDoc oldName = getName();
167         FileObject fo = super.rename(name);
168         firePropertyChange(DataObject.PROP_NAME, oldName, name);
169         return fo;
170     }
171     
172     /** Renames underlying fileobject. This implementation return the
173      * same file. Fires property change. Called when the file entry is renamed, not the DO
174      *
175      * @param name new name
176      * @return file object with renamed file
177      * @see #rename
178      */

179     public FileObject renameEntry (String JavaDoc name) throws IOException {
180         return rename(name);
181     }
182     
183     /** Deletes file object and fires property change. */
184     public void delete () throws IOException {
185         super.delete();
186         
187         firePropertyChange(DataObject.PROP_VALID, Boolean.TRUE, Boolean.FALSE);
188     }
189     
190     
191     /** Test whether the object may be deleted.
192      * @return <code>true</code> if it may
193      */

194     public abstract boolean isDeleteAllowed ();
195     
196     /** Test whether the object may be copied.
197      * @return <code>true</code> if it may
198      */

199     public abstract boolean isCopyAllowed ();
200     
201     /** Test whether the object may be moved.
202      * @return <code>true</code> if it may
203      */

204     public abstract boolean isMoveAllowed ();
205     
206     /** Test whether the object may create shadows.
207      * <p>The default implementation returns <code>true</code>.
208      * @return <code>true</code> if it may
209      */

210     public boolean isShadowAllowed () {
211         return true;
212     }
213     
214     /** Test whether the object may be renamed.
215      * @return <code>true</code> if it may
216      */

217     public abstract boolean isRenameAllowed ();
218     
219     
220     /** Test whether the object is modified.
221      * @return <code>true</code> if it is modified
222      */

223     public boolean isModified() {
224         return modif;
225     }
226     
227     /** Set whether the object is considered modified.
228      * Also fires a change event.
229      * If the new value is <code>true</code>, the data object is added into
230      * a {@link #getRegistry registry} of opened data objects.
231      * If the new value is <code>false</code>,
232      * the data object is removed from the registry.
233      */

234     public void setModified(boolean modif) {
235         if (this.modif != modif) {
236             this.modif = modif;
237             firePropertyChange(DataObject.PROP_MODIFIED,
238                                Boolean.valueOf(!modif),
239                                Boolean.valueOf(modif));
240         }
241     }
242     
243     /**
244      * Get help context for this object.
245      *
246      * @return the help context
247      */

248     public abstract HelpCtx getHelpCtx ();
249     
250     /**
251      * Returns name of the data object.
252      * <p>
253      * The default implementation returns name of the primary file.
254      *
255      * @return the name of the data object
256      */

257     public String JavaDoc getName () {
258         return getFile ().getName ();
259     }
260     
261     /**
262      * Returns a folder this data object is stored in.
263      *
264      * @return folder this data object is stored in;
265      * or <code>null</code> if the data object's primary file
266      * is the {@linkplain FileObject#isRoot root} of its filesystem
267      */

268     public final DataFolder getFolder () {
269         FileObject fo = getFile ().getParent ();
270         // could throw IllegalArgumentException but only if fo is not folder
271
// => then there is a bug in filesystem implementation
272
return fo == null ? null : DataFolder.findFolder (fo);
273     }
274     
275     
276     //
277
// Property change support
278
//
279

280     /** @param l the listener
281      */

282     public synchronized void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
283         getChangeSupport ().addPropertyChangeListener (l);
284     }
285     
286     /** @param l the listener
287      */

288     public void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
289         getChangeSupport ().removePropertyChangeListener (l);
290     }
291     
292     /** Fires property change notification to all listeners registered via
293      * {@link #addPropertyChangeListener}.
294      *
295      * @param name of property
296      * @param oldValue old value
297      * @param newValue new value
298      */

299     protected final void firePropertyChange (String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
300         getChangeSupport ().firePropertyChange (name, oldValue, newValue);
301     }
302     
303     /** Getter for standard property change support. This is used in
304      * this class and by this method provided to subclasses.
305      *
306      * @return support
307      */

308     private synchronized final PropertyChangeSupport JavaDoc getChangeSupport () {
309         if (changeSupport == null) {
310             changeSupport = new PropertyChangeSupport JavaDoc (this);
311         }
312         return changeSupport;
313     }
314     
315     /** Set the set of cookies.
316      * To the provided cookie set a listener is attached,
317      * and any change to the set is propagated by
318      * firing a change on {@link #PROP_COOKIE}.
319      *
320      * @param s the cookie set to use
321      * @deprecated
322      */

323     protected final synchronized void setCookieSet (CookieSet s) {
324         if (cookieSet != null) {
325             cookieSet.removeChangeListener (cookieL);
326         }
327         
328         s.addChangeListener (cookieL);
329         cookieSet = s;
330         
331         firePropertyChange (Node.PROP_COOKIE, null, null);
332     }
333     
334     /** Get the set of cookies.
335      * If the set had been
336      * previously set by {@link #setCookieSet}, that set
337      * is returned. Otherwise an empty set is
338      * returned.
339      *
340      * @return the cookie set (never <code>null</code>)
341      */

342     protected final CookieSet getCookieSet () {
343         CookieSet s = cookieSet;
344         if (s != null) {
345             return s;
346         }
347         synchronized (this) {
348             if (cookieSet != null) {
349                 return cookieSet;
350             }
351             // sets an empty sheet and adds a listener to it
352
setCookieSet (new CookieSet ());
353             return cookieSet;
354         }
355     }
356     
357     /**
358      * Looks for a cookie in the current cookie set matching the requested class.
359      *
360      * @param type the class to look for
361      * @return an instance of that class, or <code>null</code> if this class of cookie
362      * is not supported
363      */

364     @SuppressWarnings JavaDoc("unchecked")
365     public <T extends Node.Cookie> T getCookie(Class JavaDoc<T> type) {
366         CookieSet c = cookieSet;
367         if (c != null) {
368             T cookie = c.getCookie(type);
369             if (cookie != null) {
370                 return cookie;
371             }
372         }
373         
374         if (type.isInstance (this)) {
375             return (T) this;
376         }
377         return null;
378     }
379 }
380
Popular Tags