KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > text2 > TextSearchQueryProviderRegistry


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.search2.internal.ui.text2;
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.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 JavaDoc EXTENSION_POINT_ID= "org.eclipse.search.textSearchQueryProvider"; //$NON-NLS-1$
32
private static final String JavaDoc PROVIDER_NODE_NAME= "textSearchQueryProvider"; //$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 TextSearchQueryProvider fPreferredProvider;
38     private String JavaDoc fPreferredProviderId;
39
40     public TextSearchQueryProviderRegistry() {
41         fPreferredProviderId= null; // only null when not initialized
42
fPreferredProvider= null;
43     }
44
45     public TextSearchQueryProvider getPreferred() {
46         String JavaDoc preferredId= getPreferredEngineID();
47         if (!preferredId.equals(fPreferredProviderId)) {
48             updateProvider(preferredId);
49         }
50         return fPreferredProvider;
51     }
52
53     private void updateProvider(String JavaDoc preferredId) {
54         fPreferredProviderId= preferredId;
55         fPreferredProvider= null;
56         if (preferredId.length() != 0) { // empty string: default engine
57
fPreferredProvider= createFromExtension(preferredId);
58         }
59         if (fPreferredProvider == null) {
60             fPreferredProvider= new DefaultTextSearchQueryProvider();
61         }
62     }
63
64     private String JavaDoc getPreferredEngineID() {
65         Preferences prefs= SearchPlugin.getDefault().getPluginPreferences();
66         String JavaDoc preferedEngine= prefs.getString(SearchPreferencePage.TEXT_SEARCH_QUERY_PROVIDER);
67         return preferedEngine;
68     }
69
70 // private void setPreferredEngineID(String id) {
71
// Preferences prefs= SearchPlugin.getDefault().getPluginPreferences();
72
// prefs.setValue(SearchPreferencePage.TEXT_SEARCH_QUERY_PROVIDER, id);
73
// }
74

75     private TextSearchQueryProvider createFromExtension(final String JavaDoc id) {
76         final TextSearchQueryProvider[] res= new TextSearchQueryProvider[] { null };
77
78         SafeRunnable safe= new SafeRunnable() {
79             public void run() throws Exception JavaDoc {
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 JavaDoc e) {
90                 SearchPlugin.log(e);
91             }
92         };
93         SafeRunnable.run(safe);
94         return res[0];
95     }
96
97     public String JavaDoc[][] getAvailableProviders() {
98         ArrayList JavaDoc res= new ArrayList JavaDoc();
99         res.add(new String JavaDoc[] { SearchMessages.TextSearchQueryProviderRegistry_defaultProviderLabel, "" }); //$NON-NLS-1$
100

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