1 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 ; 31 import java.lang.ref.Reference ; 32 33 38 public final class SuggestionProviders { 39 40 42 private List providers = null; 43 private List docProviders = null; private Map providersByType = null; 45 46 private static Reference instance; 48 49 static Lookup lookup = Lookup.getDefault(); 51 52 private Lookup.Result lookupResult; 55 56 private SuggestionProviders() { 57 } 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 (scanner); 75 return scanner; 76 } 77 78 79 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 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 o1, Object 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 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 next = it.next(); 134 if (next instanceof DocumentSuggestionProvider) { 135 docProviders.add(next); 136 } 137 } 138 } 139 return docProviders; 140 } 141 142 146 public synchronized SuggestionProvider getProvider(SuggestionType type) { 147 if (providersByType == null) { 148 SuggestionTypes suggestionTypes = SuggestionTypes.getDefault(); 149 List providers = getProviders(); 151 providersByType = new HashMap(100); ListIterator it = providers.listIterator(); 156 while (it.hasNext()) { 157 SuggestionProvider provider = (SuggestionProvider) it.next(); 158 String typeName = provider.getType(); 159 if (typeName == null) { 160 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 private synchronized void invalidateCaches() { 174 providers = null; 175 docProviders = null; 176 providersByType = null; 177 } 178 179 180 } 181 | Popular Tags |