KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DefaultDataObject


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.openide.loaders;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import org.openide.DialogDisplayer;
29 import org.openide.NotifyDescriptor;
30 import org.openide.cookies.CloseCookie;
31 import org.openide.cookies.EditCookie;
32 import org.openide.cookies.EditorCookie;
33 import org.openide.cookies.OpenCookie;
34 import org.openide.cookies.PrintCookie;
35 import org.openide.filesystems.FileLock;
36 import org.openide.filesystems.FileObject;
37 import org.openide.util.HelpCtx;
38 import org.openide.util.NbBundle;
39 import org.openide.nodes.Node;
40
41 /** An implementation of a data object which consumes file objects not recognized by any other loaders.
42 */

43 final class DefaultDataObject extends MultiDataObject implements OpenCookie {
44     static final long serialVersionUID =-4936309935667095746L;
45     
46     /** generated Serialized Version UID */
47     // static final long serialVersionUID = 6305590675982925167L;
48

49     /** Constructs new data shadow for given primary file and referenced original.
50     * @param fo the primary file
51     * @param original original data object
52     */

53     DefaultDataObject (FileObject fo, MultiFileLoader loader) throws DataObjectExistsException {
54         super (fo, loader);
55     }
56  
57     /* Creates node delegate.
58     */

59     protected Node createNodeDelegate () {
60         DataNode dn = new DataNode (this, org.openide.nodes.Children.LEAF);
61         
62         // netbeans.core.nodes.description
63
dn.setShortDescription (NbBundle.getMessage (DefaultDataObject.class,
64                                 "HINT_DefaultDataObject")); // NOI18N
65
return dn;
66     }
67    
68     /** Get the name of the data object.
69     * <p>The implementation uses the name of the primary file and its exten.
70     * @return the name
71     */

72     
73     public String JavaDoc getName() {
74         return getPrimaryFile ().getNameExt ();
75     }
76
77     /* Help context for this object.
78     * @return help context
79     */

80     public HelpCtx getHelpCtx () {
81         return HelpCtx.DEFAULT_HELP;
82     }
83
84     
85     /* Handles renaming of the object.
86     * Must be overriden in children.
87     *
88     * @param name name to rename the object to
89     * @return new primary file of the object
90     * @exception IOException if an error occures
91     */

92     protected FileObject handleRename (String JavaDoc name) throws IOException JavaDoc {
93         FileLock lock = getPrimaryFile ().lock ();
94         int pos = name.lastIndexOf('.');
95         
96         try {
97             if (pos < 0){
98                 // file without separator
99
getPrimaryFile ().rename (lock, name, null);
100             } else if (pos == 0){
101                 getPrimaryFile ().rename (lock, name, getPrimaryFile ().getExt ());
102             } else {
103                 if (!name.equals(getPrimaryFile ().getNameExt())){
104                     getPrimaryFile ().rename (lock, name.substring(0, pos),
105                         name.substring(pos+1, name.length()));
106                     DataObjectPool.getPOOL().revalidate(
107                         new HashSet JavaDoc<FileObject> (java.util.Collections.singleton(getPrimaryFile ()))
108                     );
109                 }
110             }
111         } finally {
112             lock.releaseLock ();
113         }
114         return getPrimaryFile ();
115     }
116     
117     /* Creates new object from template.
118     * @exception IOException
119     */

120     protected DataObject handleCreateFromTemplate (
121         DataFolder df, String JavaDoc name
122     ) throws IOException JavaDoc {
123         // avoid doubling of extension
124
if (name != null && name.endsWith("." + getPrimaryFile ().getExt ())) // NOI18N
125
name = name.substring(0, name.lastIndexOf("." + getPrimaryFile ().getExt ())); // NOI18N
126

127         return super.handleCreateFromTemplate (df, name);
128     }
129     
130     /** Either opens the in text editor or asks user questions.
131      */

132     public void open() {
133         EditorCookie ic = getCookie(EditorCookie.class);
134         if (ic != null) {
135             ic.open();
136         } else {
137             // ask a query
138
List JavaDoc<Object JavaDoc> options = new ArrayList JavaDoc<Object JavaDoc>();
139             options.add (NotifyDescriptor.OK_OPTION);
140             options.add (NotifyDescriptor.CANCEL_OPTION);
141             NotifyDescriptor nd = new NotifyDescriptor (
142                 NbBundle.getMessage (DefaultDataObject.class, "MSG_BinaryFileQuestion"),
143                 NbBundle.getMessage (DefaultDataObject.class, "MSG_BinaryFileWarning"),
144                 NotifyDescriptor.DEFAULT_OPTION,
145                 NotifyDescriptor.QUESTION_MESSAGE,
146                 options.toArray(), null
147             );
148             Object JavaDoc ret = DialogDisplayer.getDefault().notify (nd);
149             if (ret != NotifyDescriptor.OK_OPTION) {
150                 return;
151             }
152             
153             EditorCookie c = getCookie(EditorCookie.class, true);
154             c.open ();
155         }
156     }
157
158     /** We implement OpenCookie and sometimes we also have cloneable
159      * editor cookie */

160     public <T extends Node.Cookie> T getCookie(Class JavaDoc<T> c) {
161         return getCookie (c, false);
162     }
163     
164     /** Getter for cookie.
165      * @param force if true, there are no checks for content of the file
166      */

167     final <T extends Node.Cookie> T getCookie(Class JavaDoc<T> c, boolean force) {
168         if (c == OpenCookie.class) {
169             return c.cast(this);
170         }
171
172         T cook = super.getCookie (c);
173         if (cook != null) {
174             return cook;
175         }
176             
177         if (
178             c.isAssignableFrom(EditCookie.class)
179             ||
180             c.isAssignableFrom(EditorCookie.Observable.class)
181             ||
182             c.isAssignableFrom(PrintCookie.class)
183             ||
184             c.isAssignableFrom(CloseCookie.class)
185             ||
186             c == DefaultES.class
187         ) {
188             try {
189                 if (!force) {
190                     // try to initialize the editor cookie set if the file
191
// seems editable
192
byte[] arr = new byte[2048];
193                     InputStream JavaDoc is = getPrimaryFile().getInputStream();
194                     try {
195                         int len = is.read (arr);
196                         for (int i = 0; i < len; i++) {
197                             if (arr[i] >= 0 && arr[i] <= 31 && arr[i] != '\n' && arr[i] != '\r' && arr[i] != '\t') {
198                                 return null;
199                             }
200                         }
201                     } finally {
202                         is.close ();
203                     }
204                 }
205                 DefaultES support = new DefaultES (
206                     this, getPrimaryEntry(), getCookieSet ()
207                 );
208                 getCookieSet().add(support);
209                 return getCookieSet ().getCookie(c);
210             } catch (IOException JavaDoc ex) {
211                 LOG.log(Level.INFO, "Cannot read " + getPrimaryEntry(), ex); // NOI18N
212
}
213         }
214         return null;
215     }
216 }
217
Popular Tags