KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > dataobject > MimeLookupInitializerImpl


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.languages.dataobject;
21
22 import org.netbeans.modules.languages.dataobject.LanguagesEditorKit;
23 import org.netbeans.api.languages.LanguagesManager;
24 import org.netbeans.modules.languages.features.CompletionProviderImpl;
25 import org.netbeans.spi.editor.mimelookup.MimeLookupInitializer;
26 import org.openide.util.Lookup;
27 import org.openide.util.lookup.InstanceContent;
28 import org.openide.util.lookup.Lookups;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32
33 /**
34  *
35  * @author Jan Jancura
36  */

37 public class MimeLookupInitializerImpl implements MimeLookupInitializer {
38
39     private String JavaDoc[] mimeTypes;
40     private Map JavaDoc<String JavaDoc[],Lookup.Result> children = new HashMap JavaDoc<String JavaDoc[],Lookup.Result> ();
41     
42     public MimeLookupInitializerImpl () {
43         this (new String JavaDoc [0]);
44     }
45     
46     public MimeLookupInitializerImpl (String JavaDoc[] mimeTypes) {
47         this.mimeTypes = mimeTypes;
48 // if (mimeTypes.length == 0)
49
// S ystem.out.println("new MimeLookupInitializerImpl []");
50
// else
51
// if (mimeTypes.length == 1)
52
// S ystem.out.println("new MimeLookupInitializerImpl " + mimeTypes [0]);
53
// else
54
// S ystem.out.println("new MimeLookupInitializerImpl " + mimeTypes);
55
}
56     
57     /**
58      * Lookup providing mime-type sensitive or global-level data
59      * depending on which level this initializer is defined.
60      *
61      * @return Lookup or null, if there are no lookup-able objects for mime or global level.
62      */

63     public Lookup lookup () {
64     return _lookup ();
65     }
66     
67     /**
68      * Retrieves a Lookup.Result of MimeLookupInitializers for the given sub-mimeType.
69      *
70      * @param mimeType mime-type string representation e.g. "text/x-java"
71      * @return non-null lookup result of MimeLookupInitializer(s).
72      * <br/>
73      * Typically there should be just one child initializer although if there
74      * will be more than one all of them will be taken into consideration.
75      * <br/>
76      * If there will be no specific initializers for the particular mime-type
77      * then an empty result should be returned.
78      */

79     public Lookup.Result child (String JavaDoc mimeType) {
80         synchronized (children){
81             String JavaDoc[] newMimeType = new String JavaDoc [mimeTypes.length + 1];
82             System.arraycopy (mimeTypes, 0, newMimeType, 0, mimeTypes.length);
83             newMimeType [mimeTypes.length] = mimeType;
84             Lookup.Result child = children.get (newMimeType);
85             if (child == null){
86                 child = Lookups.fixed (
87                     new Object JavaDoc[] {
88                         new MimeLookupInitializerImpl (newMimeType)
89                     }).<MimeLookupInitializerImpl>lookup (
90                         new Lookup.Template<MimeLookupInitializerImpl> (MimeLookupInitializerImpl.class)
91                     );
92                 children.put (newMimeType, child);
93             }
94             return child;
95         }
96     }
97     
98     private Lookup lookup;
99     private Lookup _lookup () {
100         if (lookup == null) {
101             if (mimeTypes.length != 1){
102                 lookup = Lookup.EMPTY;
103                 return lookup;
104             }
105             if (LanguagesManager.getDefault ().getSupportedMimeTypes ().
106                     contains (mimeTypes [0])
107             ) {
108                 lookup = Lookups.fixed (
109                     new Integer JavaDoc[] {
110                         new Integer JavaDoc (1),
111                         new Integer JavaDoc (2),
112 // new Integer (3),
113
// new Integer (4)
114
},
115                     new InstanceContent.Convertor<Integer JavaDoc,Object JavaDoc> () {
116                         public Object JavaDoc convert (Integer JavaDoc i) {
117                             switch (i.intValue ()) {
118                                 case 1:
119                                     //S ystem.out.println("get LanguagesEditorKit for " + mimeTypes [0]);
120
// MClassLoader mcl = new MClassLoader (getClass ().getClassLoader ());
121
// try {
122
// Class cl = mcl.loadClass (LanguagesEditorKit.class.getName () + "Virtual");
123
// Constructor constructor = cl.getConstructor (new Class[] {String.class});
124
// return constructor.newInstance (new Object[] {mimeTypes [0]});
125
// } catch (Exception ex) {
126
// ErrorManager.getDefault ().notify (ex);
127
// }
128
return new LanguagesEditorKit (mimeTypes [0]);
129                                 case 2:
130                                     return new CompletionProviderImpl ();
131 // case 2:
132
// S ystem.out.println("get LanguagesOptions for " + mimeTypes [0]);
133
// return new LanguagesOptions ();
134
// case 3:
135
// //S ystem.out.println("get LanguagesFoldManager.Factory for " + mimeTypes [0]);
136
// //return new LanguagesFoldManager.Factory (mimeTypes [0]);
137
// return null;
138
// case 4:
139
// //S ystem.out.println("get CodeFoldingSideBarFactory for " + mimeTypes [0]);
140
// return new CodeFoldingSideBarFactory ();
141
}
142                             return null;
143                         }
144                         public Class JavaDoc<? extends Object JavaDoc> type (Integer JavaDoc i) {
145                             switch (i.intValue ()) {
146                                 case 1:
147                                     return LanguagesEditorKit.class;
148                                 case 2:
149                                     return CompletionProviderImpl.class;
150 // case 2:
151
// return LanguagesOptions.class;
152
// case 3:
153
// return LanguagesFoldManager.Factory.class;
154
// case 4:
155
// return CodeFoldingSideBarFactory.class;
156
}
157                             return null;
158                         }
159                         public String JavaDoc id (Integer JavaDoc i) {
160                             return i.toString ();
161                         }
162                         public String JavaDoc displayName (Integer JavaDoc i) {
163                             return i.toString ();
164                         }
165                     }
166                 );
167             }
168         }
169         return lookup;
170     }
171     
172 // static class MClassLoader extends ClassLoader {
173
// MClassLoader (ClassLoader cl) {
174
// super (cl);
175
// ClassLoader cld = (ClassLoader) Lookup.getDefault ().lookup (ClassLoader.class);
176
// S ystem.out.println("ClassLoader " + cld);
177
// }
178
//
179
// public Class findClass (String name) throws ClassNotFoundException {
180
// if (!name.endsWith ("Virtual")) return null;
181
// String file = "/" + name.substring (0, name.length () - 7).replaceAll ("\\.", "/") + ".class";
182
// InputStream is = getClass ().getResourceAsStream (file);
183
// if (is == null) throw new ClassNotFoundException (name);
184
// byte[] b = new byte [4000];
185
// int off = 0;
186
// try {
187
// do {
188
// do {
189
// int l = is.read (b, off, b.length - off);
190
// if (l == -1) break;
191
// off += l;
192
// } while (off < b.length);
193
// if (off < b.length) {
194
// byte[] nb = new byte [off];
195
// System.arraycopy (b, 0, nb, 0, off);
196
// b = nb;
197
// break;
198
// }
199
// byte[] nb = new byte [b.length * 2];
200
// System.arraycopy (b, 0, nb, 0, off);
201
// b = nb;
202
// } while (true);
203
// return defineClass (name.substring (0, name.length () - 7), b, 0, b.length);
204
// } catch (IOException ex) {
205
// ex.printStackTrace ();
206
// return null;
207
// }
208
// }
209
// }
210
}
211
Popular Tags