Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05b49c99037477052454e0861e173d673b5968fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.search2.internal.ui;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;

import org.eclipse.jface.util.SafeRunnable;

import org.eclipse.search.internal.ui.SearchPlugin;
import org.eclipse.search.ui.ISearchResult;
import org.eclipse.search.ui.ISearchResultPage;

public class SearchPageRegistry {

	public static final String ID_EXTENSION_POINT= "org.eclipse.search.searchResultViewPages"; //$NON-NLS-1$
	public static final String ATTRIB_SEARCH_RESULT_CLASS= "searchResultClass"; //$NON-NLS-1$
	public static final String ATTRIB_ID= "id"; //$NON-NLS-1$

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

	public static final String ATTRIB_HELP_CONTEXT= "helpContextId"; //$NON-NLS-1$

	private final Map<String, IConfigurationElement> fResultClassNameToExtension;
	private final Map<IConfigurationElement, ISearchResultPage> fExtensionToInstance;
	private final IConfigurationElement[] fExtensions;

	public SearchPageRegistry() {
		fExtensionToInstance= new HashMap<>();
		fResultClassNameToExtension= new HashMap<>();
		fExtensions= Platform.getExtensionRegistry().getConfigurationElementsFor(ID_EXTENSION_POINT);
		for (IConfigurationElement fExtension : fExtensions) {
			fResultClassNameToExtension.put(fExtension.getAttribute(ATTRIB_SEARCH_RESULT_CLASS), fExtension);
		}
	}

	public ISearchResultPage findPageForSearchResult(ISearchResult result, boolean create) {
		Class<? extends ISearchResult> resultClass= result.getClass();
		IConfigurationElement configElement= findConfigurationElement(resultClass);
		if (configElement != null) {
			return getSearchResultPage(configElement, create);
		}
		return null;
	}

	public ISearchResultPage findPageForPageId(String pageId, boolean create) {
		IConfigurationElement configElement= findConfigurationElement(pageId);
		if (configElement != null) {
			return getSearchResultPage(configElement, create);
		}
		return null;
	}

	public String findLabelForPageId(String pageId) {
		IConfigurationElement configElement= findConfigurationElement(pageId);
		if (configElement != null) {
			return configElement.getAttribute(ATTRIB_LABEL);
		}
		return null;
	}

	public String getHelpContextId(String pageId) {
		IConfigurationElement configElement= findConfigurationElement(pageId);
		if (configElement != null) {
			return configElement.getAttribute(ATTRIB_HELP_CONTEXT);
		}
		return null;
	}

	private ISearchResultPage getSearchResultPage(final IConfigurationElement configElement, boolean create) {
		ISearchResultPage instance= fExtensionToInstance.get(configElement);
		if (instance == null && create) {
			final Object[] result= new Object[1];

			ISafeRunnable safeRunnable= new SafeRunnable(SearchMessages.SearchPageRegistry_error_creating_extensionpoint) {
				@Override
				public void run() throws Exception {
					result[0]= configElement.createExecutableExtension("class"); //$NON-NLS-1$
				}
				@Override
				public void handleException(Throwable e) {
					// invalid contribution
					SearchPlugin.log(e);
				}
			};
			SafeRunner.run(safeRunnable);
			if (result[0] instanceof ISearchResultPage) {
				instance= (ISearchResultPage) result[0];
				instance.setID(configElement.getAttribute(ATTRIB_ID));
				fExtensionToInstance.put(configElement, instance);
			}
		}
		return instance;
	}

	private IConfigurationElement findConfigurationElement(String pageId) {
		for (IConfigurationElement curr : fExtensions) {
			if (pageId.equals(curr.getAttribute(ATTRIB_ID))) {
				return curr;
			}
		}
		return null;
	}

	private IConfigurationElement findConfigurationElement(Class<?> resultClass) {
		String className= resultClass.getName();
		IConfigurationElement configElement= fResultClassNameToExtension.get(className);
		if (configElement != null) {
			return configElement;
		}
		Class<?> superclass= resultClass.getSuperclass();
		if (superclass != null) {
			IConfigurationElement foundExtension= findConfigurationElement(superclass);
			if (foundExtension != null) {
				fResultClassNameToExtension.put(className, configElement);
				return foundExtension;
			}
		}

		Class<?>[] interfaces= resultClass.getInterfaces();
		for (Class<?> interface1 : interfaces) {
			IConfigurationElement foundExtension= findConfigurationElement(interface1);
			if (foundExtension != null) {
				fResultClassNameToExtension.put(className, configElement);
				return foundExtension;
			}
		}
		return null;
	}



}

Back to the top