KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import org.netbeans.api.gsf.Completable;
27 import org.netbeans.api.gsf.BracketCompletion;
28 import org.netbeans.api.gsf.DeclarationFinder;
29 import org.netbeans.api.gsf.Formatter;
30 import org.netbeans.api.gsf.InstantRenamer;
31 import org.netbeans.api.gsf.Indexer;
32 import org.netbeans.modules.gsf.Language;
33 import org.netbeans.api.gsf.Parser;
34 import org.netbeans.api.gsf.GsfLanguage;
35 import org.netbeans.api.gsf.StructureScanner;
36 import org.netbeans.spi.palette.PaletteController;
37 import org.openide.ErrorManager;
38 import org.openide.cookies.InstanceCookie;
39 import org.openide.filesystems.FileObject;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42
43 /**
44  *
45  * @author Tor Norbye
46  */

47 public class DefaultLanguage implements Language {
48     private String JavaDoc displayName;
49     private String JavaDoc iconBase;
50     private String JavaDoc mime;
51     private List JavaDoc<String JavaDoc> extensions;
52     private List JavaDoc<Action JavaDoc> actions;
53     private GsfLanguage language;
54     private Parser parser;
55     private Completable completionProvider;
56     private InstantRenamer renamer;
57     private DeclarationFinder declarationFinder;
58     private Formatter formatter;
59     private BracketCompletion bracketCompletion;
60     private Indexer indexer;
61     private StructureScanner structure;
62     private PaletteController palette;
63     private FileObject parserFile;
64     private FileObject languageFile;
65     private FileObject navigationFilterFile;
66     private FileObject completionProviderFile;
67     private FileObject renamerFile;
68     private FileObject declarationFinderFile;
69     private FileObject formatterFile;
70     private FileObject bracketCompletionFile;
71     private FileObject indexerFile;
72     private FileObject structureFile;
73     private FileObject paletteFile;
74     
75     /** Creates a new instance of DefaultLanguage */
76     public DefaultLanguage(String JavaDoc mime) {
77         this.mime = mime;
78     }
79
80     public String JavaDoc getDisplayName() {
81         return displayName;
82     }
83
84     public void setDisplayName(String JavaDoc displayName) {
85         this.displayName = displayName;
86     }
87
88     public String JavaDoc getIconBase() {
89         return iconBase;
90     }
91
92     public void setIconBase(String JavaDoc iconBase) {
93         this.iconBase = iconBase;
94     }
95
96     public String JavaDoc getMimeType() {
97         return mime;
98     }
99
100     public void setMimeType(String JavaDoc mime) {
101         this.mime = mime;
102     }
103
104     public String JavaDoc[] getExtensions() {
105         if (extensions != null) {
106             return extensions.toArray(new String JavaDoc[extensions.size()]);
107         } else {
108             return new String JavaDoc[0];
109         }
110     }
111
112     public Action JavaDoc[] getEditorActions() {
113         if (actions != null) {
114             return actions.toArray(new Action JavaDoc[actions.size()]);
115         } else {
116             return new Action JavaDoc[0];
117         }
118     }
119
120     public GsfLanguage getGsfLanguage() {
121         if (language == null && languageFile != null) {
122             // Lazily construct Language
123
language = (GsfLanguage)createInstance(languageFile);
124             if (language == null) {
125                 // Don't keep trying
126
languageFile = null;
127             }
128         }
129         return language;
130     }
131
132     //public void setGsfLanguage(GsfLanguage scanner) {
133
// this.language = language;
134
//}
135

136     public void setGsfLanguageFile(FileObject languageFile) {
137         this.languageFile = languageFile;
138     }
139     
140     public Parser getParser() {
141         if (parser == null && parserFile != null) {
142             // Lazily construct Parser
143
parser = (Parser)createInstance(parserFile);
144             if (parser == null) {
145                 // Don't keep trying
146
parserFile = null;
147             }
148         }
149         return parser;
150     }
151
152     public void setParser(Parser parser) {
153         this.parser = parser;
154     }
155     
156     public void setParserFile(FileObject parserFile) {
157         this.parserFile = parserFile;
158     }
159     
160     public void addAction(Action JavaDoc action) {
161         if (actions == null) {
162             actions = new ArrayList JavaDoc<Action JavaDoc>();
163         }
164         actions.add(action);
165     }
166     
167     public void addExtension(String JavaDoc extension) {
168         if (extension == null || extension.length() == 0 || extension.startsWith(".")) {
169             throw new IllegalArgumentException JavaDoc("Extension should be a nonzero string not starting with a dot");
170         }
171         
172         if (extensions == null) {
173             extensions = new ArrayList JavaDoc<String JavaDoc>();
174         }
175         
176         assert extension.equals(extension.toLowerCase());
177
178         extensions.add(extension);
179     }
180
181     // XXX This is crying out for generics!
182
private Object JavaDoc createInstance(FileObject file) {
183         assert file.getExt().equals("instance"); // NOI18N
184
// Construct the service lazily using the instance cookie on the provided data object
185
try {
186             DataObject dobj = DataObject.find(file);
187             InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
188             return ic.instanceCreate();
189         } catch (ClassNotFoundException JavaDoc e) {
190             ErrorManager.getDefault().notify(e);
191         } catch (DataObjectNotFoundException e) {
192             ErrorManager.getDefault().notify(e);
193         } catch (IOException JavaDoc e) {
194             ErrorManager.getDefault().notify(e);
195         }
196         return null;
197     }
198     
199     public String JavaDoc toString() {
200         return mime + ":" + displayName;
201     }
202
203     public Completable getCompletionProvider() {
204         if (completionProvider == null && completionProviderFile != null) {
205             // Lazily construct completion provider
206
completionProvider = (Completable)createInstance(completionProviderFile);
207             if (completionProvider == null) {
208                 // Don't keep trying
209
completionProviderFile = null;
210             }
211         }
212         return completionProvider;
213     }
214
215     public void setCompletionProvider(Completable completionProvider) {
216         this.completionProvider = completionProvider;
217     }
218     
219     public void setCompletionProviderFile(FileObject completionProviderFile) {
220         this.completionProviderFile = completionProviderFile;
221     }
222
223     public InstantRenamer getInstantRenamer() {
224         if (renamer == null && renamerFile != null) {
225             renamer = (InstantRenamer)createInstance(renamerFile);
226             if (renamer == null) {
227                 // Don't keep trying
228
renamerFile = null;
229             }
230         }
231         return renamer;
232     }
233
234     public void setInstantRenamerFile(FileObject renamerFile) {
235         this.renamerFile = renamerFile;
236     }
237
238     public DeclarationFinder getDeclarationFinder() {
239         if (declarationFinder == null && declarationFinderFile != null) {
240             declarationFinder = (DeclarationFinder)createInstance(declarationFinderFile);
241             if (declarationFinder == null) {
242                 // Don't keep trying
243
declarationFinderFile = null;
244             }
245         }
246         return declarationFinder;
247     }
248
249     public void setDeclarationFinderFile(FileObject declarationFinderFile) {
250         this.declarationFinderFile = declarationFinderFile;
251     }
252
253     public Formatter getFormatter() {
254         if (formatter == null && formatterFile != null) {
255             formatter = (Formatter)createInstance(formatterFile);
256             if (formatter == null) {
257                 // Don't keep trying
258
formatterFile = null;
259             }
260         }
261         return formatter;
262     }
263
264     public void setFormatterFile(FileObject formatterFile) {
265         this.formatterFile = formatterFile;
266     }
267     
268     public BracketCompletion getBracketCompletion() {
269         if (bracketCompletion == null && bracketCompletionFile != null) {
270             bracketCompletion = (BracketCompletion)createInstance(bracketCompletionFile);
271             if (bracketCompletion == null) {
272                 // Don't keep trying
273
bracketCompletionFile = null;
274             }
275         }
276         return bracketCompletion;
277     }
278
279     public void setBracketCompletionFile(FileObject bracketCompletionFile) {
280         this.bracketCompletionFile = bracketCompletionFile;
281     }
282
283     public Indexer getIndexer() {
284         if (indexer == null && indexerFile != null) {
285             indexer = (Indexer)createInstance(indexerFile);
286             if (indexer == null) {
287                 // Don't keep trying
288
indexerFile = null;
289             }
290         }
291         return indexer;
292     }
293
294     public void setIndexerFile(FileObject indexerFile) {
295         this.indexerFile = indexerFile;
296     }
297
298     public StructureScanner getStructure() {
299         if (structure == null && structureFile != null) {
300             structure = (StructureScanner)createInstance(structureFile);
301             if (structure == null) {
302                 // Don't keep trying
303
structureFile = null;
304             }
305         }
306         return structure;
307     }
308
309     public void setStructureFile(FileObject structureFile) {
310         this.structureFile = structureFile;
311     }
312
313
314     
315     public PaletteController getPalette() {
316         if (palette == null && paletteFile != null) {
317             palette = (PaletteController)createInstance(paletteFile);
318             if (palette == null) {
319                 // Don't keep trying
320
paletteFile = null;
321             }
322         }
323         return palette;
324     }
325
326     public void setPaletteFile(FileObject paletteFile) {
327         this.paletteFile = paletteFile;
328     }
329 }
330
Popular Tags