KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > gsf > 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 package org.netbeans.modules.gsf;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.netbeans.modules.gsf.Language;
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
30
31 /**
32  * Listen for editor lookup requests for a particular mime type, and lazily
33  * initialize language support and construct an editor kit for the given
34  * mime type.
35  *
36  * Based on MimeLookupInitializer in the Schliemann prototype by Jan Jancura.
37  *
38  * @author Jan Jancura
39  * @author Tor Norbye
40  */

41 public class MimeLookupInitializerImpl implements MimeLookupInitializer {
42     private String JavaDoc[] mimeTypes;
43     private Map JavaDoc<String JavaDoc, Lookup.Result> children = new HashMap JavaDoc(); //<mimetype, child Lookup.Result>
44
private Lookup lookup;
45
46     public MimeLookupInitializerImpl() {
47         this(new String JavaDoc[0]);
48     }
49
50     public MimeLookupInitializerImpl(String JavaDoc[] mimeTypes) {
51         this.mimeTypes = mimeTypes;
52     }
53
54     /**
55      * Retrieves a Lookup.Result of MimeLookupInitializers for the given sub-mimeType.
56      *
57      * @param mimeType mime-type string representation e.g. "text/x-java"
58      * @return non-null lookup result of MimeLookupInitializer(s).
59      * <br/>
60      * Typically there should be just one child initializer although if there
61      * will be more than one all of them will be taken into consideration.
62      * <br/>
63      * If there will be no specific initializers for the particular mime-type
64      * then an empty result should be returned.
65      */

66     public Lookup.Result child(String JavaDoc mimeType) {
67         synchronized (children) {
68             String JavaDoc[] newMimeType = new String JavaDoc[mimeTypes.length + 1];
69             System.arraycopy(mimeTypes, 0, newMimeType, 0, mimeTypes.length);
70             newMimeType[mimeTypes.length] = mimeType;
71
72             Lookup.Result child = (Lookup.Result)children.get(mimeType);
73
74             if (child == null) {
75                 child = Lookups.fixed(new Object JavaDoc[] { new MimeLookupInitializerImpl(newMimeType) })
76                                .lookup(new Lookup.Template(MimeLookupInitializerImpl.class));
77                 children.put(mimeType, child);
78             }
79
80             return child;
81         }
82     }
83
84     /**
85      * Lookup providing mime-type sensitive or global-level data
86      * depending on which level this initializer is defined.
87      *
88      * @return Lookup or null, if there are no lookup-able objects for mime or global level.
89      */

90     public Lookup lookup() {
91         if (lookup == null) {
92             if (mimeTypes.length != 1) {
93                 lookup = Lookup.EMPTY;
94
95                 return lookup;
96             }
97
98             if (LanguageRegistry.getInstance().isSupported(mimeTypes[0])) {
99                 final Language language =
100                     LanguageRegistry.getInstance().getLanguageByMimeType(mimeTypes[0]);
101                 assert language != null;
102
103                 // TODO - finer granularity. What is really initialized here is layer stuff
104
// related to the language.
105
LanguageRegistry.getInstance().initializeLanguageForEditor(language);
106
107                 lookup =
108                     Lookups.fixed(new Integer JavaDoc[] { Integer.valueOf(1) },
109                         new InstanceContent.Convertor() {
110                             public Object JavaDoc convert(Object JavaDoc obj) {
111                                 switch (((Integer JavaDoc)obj).intValue()) {
112                                 case 1:
113
114                                     GsfEditorKitFactory outer =
115                                         new GsfEditorKitFactory(language);
116
117                                     return outer.kit();
118                                 }
119
120                                 return null;
121                             }
122
123                             public Class JavaDoc type(Object JavaDoc obj) {
124                                 switch (((Integer JavaDoc)obj).intValue()) {
125                                 case 1:
126                                     return GsfEditorKitFactory.GsfEditorKit.class;
127                                 }
128
129                                 return null;
130                             }
131
132                             public String JavaDoc id(Object JavaDoc obj) {
133                                 return obj.toString();
134                             }
135
136                             public String JavaDoc displayName(Object JavaDoc obj) {
137                                 return obj.toString();
138                             }
139                         });
140             }
141         }
142
143         return lookup;
144     }
145 }
146
Popular Tags