1 11 12 package org.eclipse.search2.internal.ui.text2; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.Platform; 18 import org.eclipse.core.runtime.Preferences; 19 20 import org.eclipse.jface.util.SafeRunnable; 21 22 import org.eclipse.search.ui.text.TextSearchQueryProvider; 23 24 import org.eclipse.search.internal.ui.SearchPlugin; 25 import org.eclipse.search.internal.ui.SearchPreferencePage; 26 27 import org.eclipse.search2.internal.ui.SearchMessages; 28 29 public class TextSearchQueryProviderRegistry { 30 31 private static final String EXTENSION_POINT_ID= "org.eclipse.search.textSearchQueryProvider"; private static final String PROVIDER_NODE_NAME= "textSearchQueryProvider"; private static final String ATTRIB_ID= "id"; private static final String ATTRIB_LABEL= "label"; private static final String ATTRIB_CLASS= "class"; 37 private TextSearchQueryProvider fPreferredProvider; 38 private String fPreferredProviderId; 39 40 public TextSearchQueryProviderRegistry() { 41 fPreferredProviderId= null; fPreferredProvider= null; 43 } 44 45 public TextSearchQueryProvider getPreferred() { 46 String preferredId= getPreferredEngineID(); 47 if (!preferredId.equals(fPreferredProviderId)) { 48 updateProvider(preferredId); 49 } 50 return fPreferredProvider; 51 } 52 53 private void updateProvider(String preferredId) { 54 fPreferredProviderId= preferredId; 55 fPreferredProvider= null; 56 if (preferredId.length() != 0) { fPreferredProvider= createFromExtension(preferredId); 58 } 59 if (fPreferredProvider == null) { 60 fPreferredProvider= new DefaultTextSearchQueryProvider(); 61 } 62 } 63 64 private String getPreferredEngineID() { 65 Preferences prefs= SearchPlugin.getDefault().getPluginPreferences(); 66 String preferedEngine= prefs.getString(SearchPreferencePage.TEXT_SEARCH_QUERY_PROVIDER); 67 return preferedEngine; 68 } 69 70 75 private TextSearchQueryProvider createFromExtension(final String id) { 76 final TextSearchQueryProvider[] res= new TextSearchQueryProvider[] { null }; 77 78 SafeRunnable safe= new SafeRunnable() { 79 public void run() throws Exception { 80 IConfigurationElement[] extensions= Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID); 81 for (int i= 0; i < extensions.length; i++) { 82 IConfigurationElement curr= extensions[i]; 83 if (PROVIDER_NODE_NAME.equals(curr.getName()) && id.equals(curr.getAttribute(ATTRIB_ID))) { 84 res[0]= (TextSearchQueryProvider) curr.createExecutableExtension(ATTRIB_CLASS); 85 return; 86 } 87 } 88 } 89 public void handleException(Throwable e) { 90 SearchPlugin.log(e); 91 } 92 }; 93 SafeRunnable.run(safe); 94 return res[0]; 95 } 96 97 public String [][] getAvailableProviders() { 98 ArrayList res= new ArrayList (); 99 res.add(new String [] { SearchMessages.TextSearchQueryProviderRegistry_defaultProviderLabel, "" }); 101 IConfigurationElement[] extensions= Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID); 102 for (int i= 0; i < extensions.length; i++) { 103 IConfigurationElement engine= extensions[i]; 104 if (PROVIDER_NODE_NAME.equals(engine.getName())) { 105 res.add(new String [] { engine.getAttribute(ATTRIB_LABEL), engine.getAttribute(ATTRIB_ID) }); 106 } 107 } 108 return (String [][]) res.toArray(new String [res.size()][]); 109 } 110 } 111 | Popular Tags |