KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.gsf;
20
21 import java.io.IOException JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.netbeans.api.java.classpath.ClassPath;
28
29 import org.netbeans.modules.gsf.Language;
30 import org.openide.ErrorManager;
31 import org.openide.actions.CopyAction;
32 import org.openide.actions.CutAction;
33 import org.openide.actions.DeleteAction;
34 import org.openide.actions.OpenAction;
35 import org.openide.actions.PasteAction;
36 import org.openide.actions.PropertiesAction;
37 import org.openide.actions.RenameAction;
38 import org.openide.actions.SaveAsTemplateAction;
39 import org.openide.actions.ToolsAction;
40 import org.openide.filesystems.FileObject;
41 import org.openide.loaders.DataObjectExistsException;
42 import org.openide.loaders.ExtensionList;
43 import org.openide.loaders.FileEntry;
44 import org.openide.loaders.MultiDataObject;
45 import org.openide.loaders.UniFileLoader;
46 import org.openide.util.Lookup;
47 import org.openide.util.MapFormat;
48 import org.openide.util.NbBundle;
49 import org.openide.util.actions.SystemAction;
50
51
52 /**
53  * Loader for recognizing languages handled by the generic scripting framework
54  *
55  * @author Tor Norbye
56  */

57 public class GsfDataLoader extends UniFileLoader {
58     boolean initialized;
59
60 // protected String actionsContext () {
61
// return "Loaders/text/x-java/Actions/"; // NOI18N
62
// }
63

64     public GsfDataLoader() {
65         super(GsfDataObject.class.getName());
66     }
67
68     @Override JavaDoc
69     protected void initialize() {
70         super.initialize();
71
72         ExtensionList list = getExtensions();
73
74         for (Language language : LanguageRegistry.getInstance()) {
75             list.addMimeType(language.getMimeType());
76
77             String JavaDoc[] extensions = language.getExtensions();
78
79             for (int i = 0; i < extensions.length; i++) {
80                 assert extensions[i].charAt(0) != '.' : "Extensions should exclude the .";
81                 list.addExtension(extensions[i]);
82             }
83         }
84
85         initialized = true;
86     }
87
88     @Override JavaDoc
89     protected MultiDataObject createMultiObject(FileObject primaryFile)
90         throws DataObjectExistsException, IOException JavaDoc {
91         Language language =
92             LanguageRegistry.getInstance().getLanguageByMimeType(primaryFile.getMIMEType());
93
94         return new GsfDataObject(primaryFile, this, language);
95     }
96
97     @Override JavaDoc
98     protected String JavaDoc defaultDisplayName() {
99         // Create a list of languages to include in the display
100
StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
101
102         for (Language language : LanguageRegistry.getInstance()) {
103             if (sb.length() > 0) {
104                 sb.append(", ");
105             }
106
107             sb.append(language.getDisplayName());
108         }
109
110         return NbBundle.getMessage(GsfDataLoader.class, "GenericLoaderName", sb.toString());
111     }
112
113     @Override JavaDoc
114     protected SystemAction[] defaultActions() {
115         return new SystemAction[] {
116             SystemAction.get(OpenAction.class),
117             null,
118             SystemAction.get(CutAction.class),
119             SystemAction.get(CopyAction.class),
120             SystemAction.get(PasteAction.class),
121             null,
122             SystemAction.get(DeleteAction.class),
123             SystemAction.get(RenameAction.class),
124             null,
125             SystemAction.get(SaveAsTemplateAction.class),
126             null,
127             SystemAction.get(ToolsAction.class),
128             SystemAction.get(PropertiesAction.class),
129         };
130     }
131
132     @Override JavaDoc
133     protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile) {
134         FileEntry.Format entry = new FileEntry.Format(obj, primaryFile) {
135             protected java.text.Format JavaDoc createFormat (FileObject target, String JavaDoc n, String JavaDoc e) {
136                 ClassPath cp = ClassPath.getClassPath(target, ClassPath.SOURCE);
137                 String JavaDoc resourcePath = "";
138                 if (cp != null) {
139                     resourcePath = cp.getResourceName(target);
140                 } else {
141                     ErrorManager.getDefault().log(ErrorManager.WARNING, "No classpath was found for folder: "+target);
142                 }
143                 Map JavaDoc m = new HashMap JavaDoc();
144                 m.put("NAME", n ); //NOI18N
145
String JavaDoc capitalizedName;
146                 if (n.length() > 1) {
147                     capitalizedName = Character.toUpperCase(n.charAt(0))+n.substring(1);
148                 } else {
149                     capitalizedName = ""+Character.toUpperCase(n.charAt(0));
150                 }
151                 m.put("CAPITALIZEDNAME", capitalizedName); //NOI18N
152
m.put("LOWERNAME", n.toLowerCase()); //NOI18N
153
m.put("UPPERNAME", n.toUpperCase()); //NOI18N
154
// Yes, this is package sans filename (target is a folder).
155
String JavaDoc packageName = resourcePath.replace('/', '.');
156                 m.put("PACKAGE", packageName); // NOI18N
157
String JavaDoc capitalizedPkgName;
158                 if (packageName == null || packageName.length() == 0) {
159                     packageName = "";
160                     capitalizedPkgName = "";
161                 } else if (packageName.length() > 1) {
162                     capitalizedPkgName = Character.toUpperCase(packageName.charAt(0))+packageName.substring(1);
163                 } else {
164                     capitalizedPkgName = ""+Character.toUpperCase(packageName.charAt(0));
165                 }
166                 m.put("CAPITALIZEDPACKAGE", capitalizedPkgName); // NOI18N
167
m.put("PACKAGE_SLASHES", resourcePath); // NOI18N
168
// Fully-qualified name:
169
if (target.isRoot ()) {
170                     m.put ("PACKAGE_AND_NAME", n); // NOI18N
171
m.put ("PACKAGE_AND_NAME_SLASHES", n); // NOI18N
172
} else {
173                     m.put ("PACKAGE_AND_NAME", resourcePath.replace('/', '.') + '.' + n); // NOI18N
174
m.put ("PACKAGE_AND_NAME_SLASHES", resourcePath + '/' + n); // NOI18N
175
}
176                 m.put("DATE", DateFormat.getDateInstance(DateFormat.LONG).format(new Date JavaDoc())); // NOI18N
177
m.put("TIME", DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date JavaDoc())); // NOI18N
178
MapFormat f = new MapFormat(m);
179                 f.setLeftBrace( "__" ); //NOI18N
180
f.setRightBrace( "__" ); //NOI18N
181

182                 return f;
183             }
184         };
185         return entry;
186     }
187 }
188
Popular Tags