KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > MimePathLookup


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.editor.mimelookup;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.netbeans.api.editor.mimelookup.MimePath;
25 import org.netbeans.spi.editor.mimelookup.MimeDataProvider;
26 import org.netbeans.spi.editor.mimelookup.MimeLookupInitializer;
27 import org.openide.util.Lookup;
28 import org.openide.util.LookupEvent;
29 import org.openide.util.LookupListener;
30 import org.openide.util.WeakListeners;
31 import org.openide.util.lookup.ProxyLookup;
32
33 /**
34  *
35  * @author vita
36  */

37 public final class MimePathLookup extends ProxyLookup implements LookupListener {
38     
39     private MimePath mimePath;
40     private Lookup.Result<MimeDataProvider> dataProviders;
41     private Lookup.Result<MimeLookupInitializer> mimeInitializers; // This is supported for backwards compatibility only.
42

43     /** Creates a new instance of MimePathLookup */
44     public MimePathLookup(MimePath mimePath) {
45         super();
46         
47         if (mimePath == null) {
48             throw new NullPointerException JavaDoc("Mime path can't be null."); //NOI18N
49
}
50         
51         this.mimePath = mimePath;
52
53         dataProviders = Lookup.getDefault().lookup(new Lookup.Template<MimeDataProvider>(MimeDataProvider.class));
54         dataProviders.addLookupListener((LookupListener) WeakListeners.create(LookupListener.class, this, dataProviders));
55
56         mimeInitializers = Lookup.getDefault().lookup(new Lookup.Template<MimeLookupInitializer>(MimeLookupInitializer.class));
57         mimeInitializers.addLookupListener((LookupListener) WeakListeners.create(LookupListener.class, this, mimeInitializers));
58         
59         rebuild();
60     }
61
62     public MimePath getMimePath() {
63         return mimePath;
64     }
65     
66     private void rebuild() {
67         ArrayList JavaDoc<Lookup> lookups = new ArrayList JavaDoc<Lookup>();
68         
69         for (MimeDataProvider provider : dataProviders.allInstances()) {
70             Lookup mimePathLookup = provider.getLookup(mimePath);
71             if (mimePathLookup != null) {
72                 lookups.add(mimePathLookup);
73             }
74         }
75         
76         for (MimeLookupInitializer initializer : mimeInitializers.allInstances()) {
77             for (int j = 0; j < mimePath.size(); j++) {
78                 Lookup.Result children = initializer.child(mimePath.getMimeType(j));
79                 for (Iterator JavaDoc k = children.allInstances().iterator(); k.hasNext(); ) {
80                     MimeLookupInitializer mli = (MimeLookupInitializer) k.next();
81                     Lookup mimePathLookup = mli.lookup();
82                     if (mimePathLookup != null) {
83                         lookups.add(mimePathLookup);
84                     }
85                 }
86             }
87         }
88         
89         setLookups((Lookup [])lookups.toArray(new Lookup[lookups.size()]));
90     }
91     
92     //-------------------------------------------------------------
93
// LookupListener implementation
94
//-------------------------------------------------------------
95

96     public void resultChanged(LookupEvent ev) {
97         rebuild();
98     }
99     
100 }
101
Popular Tags