1 11 package org.eclipse.search2.internal.ui; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 ID_EXTENSION_POINT= "org.eclipse.search.searchResultViewPages"; public static final String ATTRIB_SEARCH_RESULT_CLASS= "searchResultClass"; public static final String ATTRIB_ID= "id"; 34 public static final String ATTRIB_LABEL= "label"; public static final String ATTRIB_ICON= "icon"; 37 private final Map fResultClassNameToExtension; 38 private final Map fExtensionToInstance; 39 private final IConfigurationElement[] fExtensions; 40 41 public SearchPageRegistry() { 42 fExtensionToInstance= new HashMap (); 43 fResultClassNameToExtension= new HashMap (); 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 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 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 findLabelForPageId(String 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 [] result= new Object [1]; 79 80 ISafeRunnable safeRunnable= new SafeRunnable(SearchMessages.SearchPageRegistry_error_creating_extensionpoint) { 81 public void run() throws Exception { 82 result[0]= configElement.createExecutableExtension("class"); } 84 public void handleException(Throwable e) { 85 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 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 resultClass) { 110 String className= resultClass.getName(); 111 IConfigurationElement configElement= (IConfigurationElement) fResultClassNameToExtension.get(className); 112 if (configElement != null) { 113 return configElement; 114 } 115 Class 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 [] 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 |