1 11 12 package org.eclipse.search.internal.core.text; 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.core.text.TextSearchEngine; 23 24 import org.eclipse.search.internal.ui.SearchMessages; 25 import org.eclipse.search.internal.ui.SearchPlugin; 26 import org.eclipse.search.internal.ui.SearchPreferencePage; 27 28 29 public class TextSearchEngineRegistry { 30 31 private static final String EXTENSION_POINT_ID= "org.eclipse.search.textSearchEngine"; private static final String ENGINE_NODE_NAME= "textSearchEngine"; private static final String ATTRIB_ID= "id"; private static final String ATTRIB_LABEL= "label"; private static final String ATTRIB_CLASS= "class"; 37 private TextSearchEngine fPreferredEngine; 38 private String fPreferredEngineId; 39 40 public TextSearchEngineRegistry() { 41 fPreferredEngineId= null; fPreferredEngine= null; 43 } 44 45 public TextSearchEngine getPreferred() { 46 String preferredId= getPreferredEngineID(); 47 if (!preferredId.equals(fPreferredEngineId)) { 48 updateEngine(preferredId); 49 } 50 return fPreferredEngine; 51 } 52 53 private void updateEngine(String preferredId) { 54 if (preferredId.length() != 0) { TextSearchEngine engine= createFromExtension(preferredId); 56 if (engine != null) { 57 fPreferredEngineId= preferredId; 58 fPreferredEngine= engine; 59 return; 60 } 61 setPreferredEngineID(""); } 64 fPreferredEngineId= ""; fPreferredEngine= TextSearchEngine.createDefault(); 66 } 67 68 private String getPreferredEngineID() { 69 Preferences prefs= SearchPlugin.getDefault().getPluginPreferences(); 70 String preferedEngine= prefs.getString(SearchPreferencePage.TEXT_SEARCH_ENGINE); 71 return preferedEngine; 72 } 73 74 private void setPreferredEngineID(String id) { 75 Preferences prefs= SearchPlugin.getDefault().getPluginPreferences(); 76 prefs.setValue(SearchPreferencePage.TEXT_SEARCH_ENGINE, id); 77 } 78 79 private TextSearchEngine createFromExtension(final String id) { 80 final TextSearchEngine[] res= new TextSearchEngine[] { null }; 81 82 SafeRunnable safe= new SafeRunnable() { 83 public void run() throws Exception { 84 IConfigurationElement[] extensions= Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID); 85 for (int i= 0; i < extensions.length; i++) { 86 IConfigurationElement curr= extensions[i]; 87 if (ENGINE_NODE_NAME.equals(curr.getName()) && id.equals(curr.getAttribute(ATTRIB_ID))) { 88 res[0]= (TextSearchEngine) curr.createExecutableExtension(ATTRIB_CLASS); 89 return; 90 } 91 } 92 } 93 public void handleException(Throwable e) { 94 SearchPlugin.log(e); 95 } 96 }; 97 SafeRunnable.run(safe); 98 return res[0]; 99 } 100 101 public String [][] getAvailableEngines() { 102 ArrayList res= new ArrayList (); 103 res.add(new String [] { SearchMessages.TextSearchEngineRegistry_defaulttextsearch_label, "" }); 105 IConfigurationElement[] extensions= Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID); 106 for (int i= 0; i < extensions.length; i++) { 107 IConfigurationElement engine= extensions[i]; 108 if (ENGINE_NODE_NAME.equals(engine.getName())) { 109 res.add(new String [] { engine.getAttribute(ATTRIB_LABEL), engine.getAttribute(ATTRIB_ID) }); 110 } 111 } 112 return (String [][]) res.toArray(new String [res.size()][]); 113 } 114 } 115 | Popular Tags |