KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > SearchPageRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search2.internal.ui;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.ISafeRunnable;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.SafeRunner;
20
21 import org.eclipse.jface.util.SafeRunnable;
22
23 import org.eclipse.search.ui.ISearchResult;
24 import org.eclipse.search.ui.ISearchResultPage;
25
26 import org.eclipse.search.internal.ui.SearchPlugin;
27
28 public class SearchPageRegistry {
29     
30     public static final String JavaDoc ID_EXTENSION_POINT= "org.eclipse.search.searchResultViewPages"; //$NON-NLS-1$
31
public static final String JavaDoc ATTRIB_SEARCH_RESULT_CLASS= "searchResultClass"; //$NON-NLS-1$
32
public static final String JavaDoc ATTRIB_ID= "id"; //$NON-NLS-1$
33

34     public static final String JavaDoc ATTRIB_LABEL= "label"; //$NON-NLS-1$
35
public static final String JavaDoc ATTRIB_ICON= "icon"; //$NON-NLS-1$
36

37     private final Map JavaDoc fResultClassNameToExtension;
38     private final Map JavaDoc fExtensionToInstance;
39     private final IConfigurationElement[] fExtensions;
40     
41     public SearchPageRegistry() {
42         fExtensionToInstance= new HashMap JavaDoc();
43         fResultClassNameToExtension= new HashMap JavaDoc();
44         fExtensions= Platform.getExtensionRegistry().getConfigurationElementsFor(ID_EXTENSION_POINT);
45         for (int i= 0; i < fExtensions.length; i++) {
46             fResultClassNameToExtension.put(fExtensions[i].getAttribute(ATTRIB_SEARCH_RESULT_CLASS), fExtensions[i]);
47         }
48     }
49
50     public ISearchResultPage findPageForSearchResult(ISearchResult result, boolean create) {
51         Class JavaDoc resultClass= result.getClass();
52         IConfigurationElement configElement= findConfigurationElement(resultClass);
53         if (configElement != null) {
54             return getSearchResultPage(configElement, create);
55         }
56         return null;
57     }
58     
59     public ISearchResultPage findPageForPageId(String JavaDoc pageId, boolean create) {
60         IConfigurationElement configElement= findConfigurationElement(pageId);
61         if (configElement != null) {
62             return getSearchResultPage(configElement, create);
63         }
64         return null;
65     }
66
67     public String JavaDoc findLabelForPageId(String JavaDoc pageId) {
68         IConfigurationElement configElement= findConfigurationElement(pageId);
69         if (configElement != null) {
70             return configElement.getAttribute(ATTRIB_LABEL);
71         }
72         return null;
73     }
74
75     private ISearchResultPage getSearchResultPage(final IConfigurationElement configElement, boolean create) {
76         ISearchResultPage instance= (ISearchResultPage) fExtensionToInstance.get(configElement);
77         if (instance == null && create) {
78             final Object JavaDoc[] result= new Object JavaDoc[1];
79
80             ISafeRunnable safeRunnable= new SafeRunnable(SearchMessages.SearchPageRegistry_error_creating_extensionpoint) {
81                 public void run() throws Exception JavaDoc {
82                     result[0]= configElement.createExecutableExtension("class"); //$NON-NLS-1$
83
}
84                 public void handleException(Throwable JavaDoc e) {
85                     // invalid contribution
86
SearchPlugin.log(e);
87                 }
88             };
89             SafeRunner.run(safeRunnable);
90             if (result[0] instanceof ISearchResultPage) {
91                 instance= (ISearchResultPage) result[0];
92                 instance.setID(configElement.getAttribute(ATTRIB_ID));
93                 fExtensionToInstance.put(configElement, instance);
94             }
95         }
96         return instance;
97     }
98     
99     private IConfigurationElement findConfigurationElement(String JavaDoc pageId) {
100         for (int i= 0; i < fExtensions.length; i++) {
101             IConfigurationElement curr= fExtensions[i];
102             if (pageId.equals(curr.getAttribute(ATTRIB_ID))) {
103                 return curr;
104             }
105         }
106         return null;
107     }
108     
109     private IConfigurationElement findConfigurationElement(Class JavaDoc resultClass) {
110         String JavaDoc className= resultClass.getName();
111         IConfigurationElement configElement= (IConfigurationElement) fResultClassNameToExtension.get(className);
112         if (configElement != null) {
113             return configElement;
114         }
115         Class JavaDoc superclass= resultClass.getSuperclass();
116         if (superclass != null) {
117             IConfigurationElement foundExtension= findConfigurationElement(superclass);
118             if (foundExtension != null) {
119                 fResultClassNameToExtension.put(className, configElement);
120                 return foundExtension;
121             }
122         }
123             
124         Class JavaDoc[] interfaces= resultClass.getInterfaces();
125         for (int i= 0; i < interfaces.length; i++) {
126             IConfigurationElement foundExtension= findConfigurationElement(interfaces[i]);
127             if (foundExtension != null) {
128                 fResultClassNameToExtension.put(className, configElement);
129                 return foundExtension;
130             }
131         }
132         return null;
133     }
134
135
136
137 }
138
Popular Tags