KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > SuggestionProviders


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.tasklist.suggestions;
21
22 import org.openide.util.Lookup;
23 import org.openide.util.LookupListener;
24 import org.openide.util.LookupEvent;
25 import org.openide.ErrorManager;
26 import org.netbeans.modules.tasklist.providers.SuggestionProvider;
27 import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider;
28
29 import java.util.*;
30 import java.lang.ref.WeakReference JavaDoc;
31 import java.lang.ref.Reference JavaDoc;
32
33 /**
34  * Registry of suggestion providers. Wraps default lookup.
35  *
36  * @author Petr Kuzel
37  */

38 public final class SuggestionProviders {
39
40     // Providers registry we use
41

42     private List providers = null;
43     private List docProviders = null; // subset of elements in providers; these implement DocumentSuggestionProvider
44
private Map providersByType = null;
45
46     // keep default (until client exists)
47
private static Reference JavaDoc instance;
48
49     // package private so tests can change
50
static Lookup lookup = Lookup.getDefault();
51
52     // results must be strongly referenced otherwise it get collected and
53
// will not fire change events
54
private Lookup.Result lookupResult;
55
56     private SuggestionProviders() {
57         // lazy init
58
}
59
60     public synchronized static SuggestionProviders getDefault() {
61         if (instance == null) {
62             return createDefault();
63         }
64         SuggestionProviders scanner = (SuggestionProviders) instance.get();
65         if (scanner == null) {
66             return createDefault();
67         } else {
68             return scanner;
69         }
70     }
71
72     private static SuggestionProviders createDefault() {
73         SuggestionProviders scanner = new SuggestionProviders();
74         instance = new WeakReference JavaDoc(scanner);
75         return scanner;
76     }
77
78
79     /**
80      * Return a list of the providers registered
81      *
82      * @todo Filter out disabled providers
83      */

84     public synchronized List getProviders() {
85         if (providers == null) {
86             providers = new ArrayList(20);
87             Lookup.Template template =
88                     new Lookup.Template(SuggestionProvider.class);
89             lookupResult = lookup.lookup(template);
90             lookupResult.addLookupListener(new LookupListener() {
91                 public void resultChanged(LookupEvent ev) {
92                     invalidateCaches();
93                 }
94             });
95
96             Iterator it = lookupResult.allInstances().iterator();
97             // Two stage process so we can sort by priority
98

99             ArrayList provList = new ArrayList(20);
100             while (it.hasNext()) {
101                 SuggestionProvider sp = (SuggestionProvider) it.next();
102                 provList.add(sp);
103             }
104             SuggestionProvider[] provA =
105                     (SuggestionProvider[]) provList.toArray(new SuggestionProvider[provList.size()]);
106             final SuggestionTypes types = SuggestionTypes.getDefault();
107             Arrays.sort(provA, new Comparator() {
108                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
109                     SuggestionProvider a = (SuggestionProvider) o1;
110                     SuggestionProvider b = (SuggestionProvider) o2;
111                     try {
112                         SuggestionType at = types.getType(a.getType());
113                         SuggestionType bt = types.getType(b.getType());
114                         return at.getPosition() - bt.getPosition();
115                     } catch (Exception JavaDoc e) {
116                         return -1;
117                     }
118                 }
119             });
120             for (int i = 0; i < provA.length; i++) {
121                 SuggestionProvider sp = provA[i];
122                 providers.add(sp);
123             }
124         }
125         return providers;
126     }
127
128     public synchronized List getDocProviders() {
129         if (docProviders == null) {
130             docProviders = new ArrayList(20);
131             Iterator it = getProviders().iterator();
132             while (it.hasNext()) {
133                 Object JavaDoc next = it.next();
134                 if (next instanceof DocumentSuggestionProvider) {
135                     docProviders.add(next);
136                 }
137             }
138         }
139         return docProviders;
140     }
141
142     /**
143      * @return The SuggestionProvider responsible for providing suggestions
144      * of a particular type
145      */

146     public synchronized SuggestionProvider getProvider(SuggestionType type) {
147         if (providersByType == null) {
148             SuggestionTypes suggestionTypes = SuggestionTypes.getDefault();
149             //Collection types = suggestionTypes.getAllTypes();
150
List providers = getProviders();
151             providersByType = new HashMap(100); // XXXXX ?<??
152
// Perhaps use suggestionTypes.getAllTypes()*2 as a seed?
153
// Note, this includes suggestion types that do not have
154
// providers
155
ListIterator it = providers.listIterator();
156             while (it.hasNext()) {
157                 SuggestionProvider provider = (SuggestionProvider) it.next();
158                 String JavaDoc typeName = provider.getType();
159                 if (typeName == null) {
160                     // Should I just let a NullPointerException occur instead?
161
// After all, non null is required for correct operation.
162
ErrorManager.getDefault().log("SuggestionProvider " + provider + " provides null value to getTypes()");
163                     continue;
164                 }
165                 SuggestionType tp = suggestionTypes.getType(typeName);
166                 providersByType.put(tp, provider);
167             }
168         }
169         return (SuggestionProvider) providersByType.get(type);
170     }
171
172     // can come from random thread
173
private synchronized void invalidateCaches() {
174         providers = null;
175         docProviders = null;
176         providersByType = null;
177     }
178
179
180 }
181
Popular Tags