KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > core > text > TextSearchEngineRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006 Wind River Systems and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Markus Schorn - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.search.internal.core.text;
13
14 import java.util.ArrayList JavaDoc;
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 JavaDoc EXTENSION_POINT_ID= "org.eclipse.search.textSearchEngine"; //$NON-NLS-1$
32
private static final String JavaDoc ENGINE_NODE_NAME= "textSearchEngine"; //$NON-NLS-1$
33
private static final String JavaDoc ATTRIB_ID= "id"; //$NON-NLS-1$
34
private static final String JavaDoc ATTRIB_LABEL= "label"; //$NON-NLS-1$
35
private static final String JavaDoc ATTRIB_CLASS= "class"; //$NON-NLS-1$
36

37     private TextSearchEngine fPreferredEngine;
38     private String JavaDoc fPreferredEngineId;
39
40     public TextSearchEngineRegistry() {
41         fPreferredEngineId= null; // only null when not initialized
42
fPreferredEngine= null;
43     }
44
45     public TextSearchEngine getPreferred() {
46         String JavaDoc preferredId= getPreferredEngineID();
47         if (!preferredId.equals(fPreferredEngineId)) {
48             updateEngine(preferredId);
49         }
50         return fPreferredEngine;
51     }
52
53     private void updateEngine(String JavaDoc preferredId) {
54         if (preferredId.length() != 0) { // empty string: default engine
55
TextSearchEngine engine= createFromExtension(preferredId);
56             if (engine != null) {
57                 fPreferredEngineId= preferredId;
58                 fPreferredEngine= engine;
59                 return;
60             }
61             // creation failed, clear preference
62
setPreferredEngineID(""); // set to default //$NON-NLS-1$
63
}
64         fPreferredEngineId= ""; //$NON-NLS-1$
65
fPreferredEngine= TextSearchEngine.createDefault();
66     }
67
68     private String JavaDoc getPreferredEngineID() {
69         Preferences prefs= SearchPlugin.getDefault().getPluginPreferences();
70         String JavaDoc preferedEngine= prefs.getString(SearchPreferencePage.TEXT_SEARCH_ENGINE);
71         return preferedEngine;
72     }
73
74     private void setPreferredEngineID(String JavaDoc id) {
75         Preferences prefs= SearchPlugin.getDefault().getPluginPreferences();
76         prefs.setValue(SearchPreferencePage.TEXT_SEARCH_ENGINE, id);
77     }
78
79     private TextSearchEngine createFromExtension(final String JavaDoc id) {
80         final TextSearchEngine[] res= new TextSearchEngine[] { null };
81
82         SafeRunnable safe= new SafeRunnable() {
83             public void run() throws Exception JavaDoc {
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 JavaDoc e) {
94                 SearchPlugin.log(e);
95             }
96         };
97         SafeRunnable.run(safe);
98         return res[0];
99     }
100
101     public String JavaDoc[][] getAvailableEngines() {
102         ArrayList JavaDoc res= new ArrayList JavaDoc();
103         res.add(new String JavaDoc[] { SearchMessages.TextSearchEngineRegistry_defaulttextsearch_label, "" }); //$NON-NLS-1$
104

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 JavaDoc[] { engine.getAttribute(ATTRIB_LABEL), engine.getAttribute(ATTRIB_ID) });
110             }
111         }
112         return (String JavaDoc[][]) res.toArray(new String JavaDoc[res.size()][]);
113     }
114 }
115
Popular Tags