KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > gsf > GsfDataObject


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.gsf;
21
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectInput JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27 import org.netbeans.api.java.classpath.ClassPath;
28 import org.netbeans.api.gsf.CancellableTask;
29 import org.netbeans.api.retouche.source.ClasspathInfo;
30 import org.netbeans.api.retouche.source.Source;
31 import org.netbeans.api.retouche.source.WorkingCopy;
32 import org.netbeans.modules.gsf.Language;
33 //import org.netbeans.api.timers.TimesCollector;
34
import org.netbeans.spi.palette.PaletteController;
35 import org.openide.cookies.EditCookie;
36 import org.openide.cookies.EditorCookie;
37 import org.openide.cookies.OpenCookie;
38 import org.openide.cookies.PrintCookie;
39 import org.openide.cookies.SaveCookie;
40 import org.openide.filesystems.FileLock;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.loaders.DataFolder;
44 import org.openide.loaders.DataObject;
45 import org.openide.loaders.DataObjectExistsException;
46 import org.openide.loaders.MultiDataObject;
47 import org.openide.loaders.MultiFileLoader;
48 import org.openide.nodes.Node;
49 import org.openide.nodes.Node.Cookie;
50 import org.openide.text.CloneableEditor;
51 import org.openide.text.CloneableEditorSupport;
52 import org.openide.text.DataEditorSupport;
53 import org.openide.util.Lookup;
54 import org.openide.util.NbBundle;
55 import org.openide.util.Utilities;
56 import org.openide.util.lookup.AbstractLookup;
57 import org.openide.util.lookup.InstanceContent;
58 import org.openide.util.lookup.ProxyLookup;
59 import org.openide.windows.CloneableOpenSupport;
60
61 public class GsfDataObject extends MultiDataObject {
62     
63     private GenericEditorSupport jes;
64     private Language language;
65     
66     public GsfDataObject(FileObject pf, MultiFileLoader loader, Language language) throws DataObjectExistsException {
67         super(pf, loader);
68         this.language = language;
69     }
70     
71     public @Override JavaDoc Node createNodeDelegate() {
72         return new GsfDataNode(this, language);
73     }
74
75     public @Override JavaDoc Cookie getCookie(Class JavaDoc type) {
76         
77         if (type.isAssignableFrom(GenericEditorSupport.class)) {
78             return createEditorSupport ();
79         }
80         return super.getCookie(type);
81     }
82     
83     
84     protected @Override JavaDoc DataObject handleCreateFromTemplate(DataFolder df, String JavaDoc name) throws IOException JavaDoc {
85         if (name == null) {
86             // special case: name is null (unspecified or from one-parameter createFromTemplate)
87
name = FileUtil.findFreeFileName(df.getPrimaryFile(),
88                 getPrimaryFile().getName(), language.getExtensions()[0]);
89         }
90         else if (!Utilities.isJavaIdentifier(name)) {
91             throw new IOException JavaDoc (NbBundle.getMessage(GsfDataObject.class, "FMT_Not_Valid_FileName", name));
92         }
93         //IndentFileEntry entry = (IndentFileEntry)getPrimaryEntry();
94
//entry.initializeIndentEngine();
95
DataObject retValue = super.handleCreateFromTemplate(df, name);
96         FileObject fo = retValue.getPrimaryFile ();
97         assert fo != null;
98         ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE);
99         String JavaDoc pkgName;
100         if (cp != null) {
101             pkgName = cp.getResourceName(fo.getParent(),'.',false);
102         }
103         else {
104             pkgName = ""; //NOI18N
105
}
106 // renameJDO (retValue, pkgName, name, this.getPrimaryFile().getName());
107
return retValue;
108     }
109     
110     
111     private synchronized GenericEditorSupport createEditorSupport () {
112         if (jes == null) {
113             jes = new GenericEditorSupport(this, language);
114         }
115         return jes;
116     }
117     
118     public static class GenericEditorSupport extends DataEditorSupport implements OpenCookie, EditCookie, EditorCookie, PrintCookie, EditorCookie.Observable {
119         
120         private static class Environment extends DataEditorSupport.Env {
121             
122             private static final long serialVersionUID = -1;
123             
124             private transient SaveSupport saveCookie = null;
125             
126             private class SaveSupport implements SaveCookie {
127                 public void save() throws java.io.IOException JavaDoc {
128                     ((GenericEditorSupport)findCloneableOpenSupport()).saveDocument();
129                     getDataObject().setModified(false);
130                 }
131             }
132             
133             public Environment(GsfDataObject obj) {
134                 super(obj);
135             }
136             
137             protected FileObject getFile() {
138                 return this.getDataObject().getPrimaryFile();
139             }
140             
141             protected FileLock takeLock() throws java.io.IOException JavaDoc {
142                 return this.getFile().lock();
143             }
144             
145             public @Override JavaDoc CloneableOpenSupport findCloneableOpenSupport() {
146                 return (CloneableEditorSupport) ((GsfDataObject)this.getDataObject()).getCookie(EditorCookie.class);
147             }
148             
149             
150             public void addSaveCookie() {
151                 GsfDataObject javaData = (GsfDataObject) this.getDataObject();
152                 if (javaData.getCookie(SaveCookie.class) == null) {
153                     if (this.saveCookie == null)
154                         this.saveCookie = new SaveSupport();
155                     javaData.getCookieSet().add(this.saveCookie);
156                     javaData.setModified(true);
157                 }
158             }
159             
160             public void removeSaveCookie() {
161                 GsfDataObject javaData = (GsfDataObject) this.getDataObject();
162                 if (javaData.getCookie(SaveCookie.class) != null) {
163                     javaData.getCookieSet().remove(this.saveCookie);
164                     javaData.setModified(false);
165                 }
166             }
167         }
168         
169
170         public GenericEditorSupport(GsfDataObject dataObject, Language language) {
171             super(dataObject, new Environment(dataObject));
172             setMIMEType(language.getMimeType());
173         }
174         
175         
176         protected boolean notifyModified() {
177             if (!super.notifyModified())
178                 return false;
179             ((Environment)this.env).addSaveCookie();
180             return true;
181         }
182         
183         
184         protected @Override JavaDoc void notifyUnmodified() {
185             super.notifyUnmodified();
186             ((Environment)this.env).removeSaveCookie();
187         }
188
189         protected @Override JavaDoc CloneableEditor createCloneableEditor() {
190             return new GsfEditor(this);
191         }
192         
193         public @Override JavaDoc boolean close(boolean ask) {
194             return super.close(ask);
195         }
196     }
197     
198     private static final class GsfEditor extends CloneableEditor {
199         
200         private static final long serialVersionUID = -1;
201         
202         public GsfEditor() {
203         }
204         
205         public GsfEditor(GenericEditorSupport sup) {
206             super(sup);
207             initialize();
208         }
209         
210         void associatePalette(GenericEditorSupport s) {
211             DataObject dataObject = s.getDataObject();
212             if (!(dataObject instanceof GsfDataObject)) {
213                 return;
214             }
215
216             GsfDataObject gdo = (GsfDataObject)s.getDataObject();
217             PaletteController pc = gdo.language.getPalette();
218             if (pc == null) {
219                 return;
220             }
221
222             Node nodes[] = { gdo.getNodeDelegate() };
223             InstanceContent instanceContent = new InstanceContent();
224             associateLookup(new ProxyLookup(new Lookup[] { new AbstractLookup(instanceContent), nodes[0].getLookup()}));
225             instanceContent.add(getActionMap());
226
227             setActivatedNodes(nodes);
228
229             instanceContent.add(pc);
230         }
231         
232         private void initialize() {
233             associatePalette((GenericEditorSupport)cloneableEditorSupport());
234         }
235
236         @Override JavaDoc
237         public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
238             super.readExternal(in);
239             initialize();
240         }
241         
242         
243         
244         
245     }
246
247 // private static ClassPath getClassPath( Document doc, String type ) {
248
// DataObject dObj = (DataObject)doc.getProperty(doc.StreamDescriptionProperty );
249
// return ClassPath.getClassPath( dObj.getPrimaryFile(), type );
250
// }
251
}
252
Popular Tags