KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > registry > MarkerHelpRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.ui.internal.ide.registry;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.eclipse.core.resources.IMarker;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IConfigurationElement;
28 import org.eclipse.core.runtime.Platform;
29 import org.eclipse.ui.IMarkerHelpRegistry;
30 import org.eclipse.ui.IMarkerResolution;
31 import org.eclipse.ui.IMarkerResolutionGenerator;
32 import org.eclipse.ui.IMarkerResolutionGenerator2;
33 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
34 import org.osgi.framework.Bundle;
35
36 /**
37  * This class is a registry for marker help contexts and resolutions.
38  */

39 public class MarkerHelpRegistry implements IMarkerHelpRegistry {
40     /**
41      * Table of queries for marker F1 help.
42      */

43     private Map JavaDoc helpQueries = new HashMap JavaDoc();
44
45     /**
46      * Sorted list of help queries. Used to ensure that the "most specific"
47      * query is tried first
48      */

49     private List JavaDoc sortedHelpQueries;
50
51     /**
52      * Table of queries for marker resolutions
53      */

54     private Map JavaDoc resolutionQueries = new HashMap JavaDoc();
55
56     /**
57      * Help context id attribute in configuration element
58      */

59     private static final String JavaDoc ATT_HELP = "helpContextId"; //$NON-NLS-1$
60

61     /**
62      * Resolution class attribute name in configuration element
63      */

64     private static final String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
65

66     private class QueryComparator implements Comparator JavaDoc {
67         /*
68          * (non-Javadoc) Method declared on Object.
69          */

70         public boolean equals(Object JavaDoc o) {
71             if (!(o instanceof QueryComparator)) {
72                 return false;
73             }
74             return true;
75         }
76
77         /*
78          * (non-Javadoc) Method declared on Comparator.
79          */

80         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
81             // more attribues come first
82
MarkerQuery q1 = (MarkerQuery) o1;
83             MarkerQuery q2 = (MarkerQuery) o2;
84
85             int size1 = q1.getAttributes().length;
86             int size2 = q2.getAttributes().length;
87
88             if (size1 > size2) {
89                 return -1;
90             }
91             if (size1 == size2) {
92                 return 0;
93             }
94             return 1;
95         }
96     }
97
98     /*
99      * (non-Javadoc) Method declared on IMarkerHelpRegistry.
100      */

101     public String JavaDoc getHelp(IMarker marker) {
102         if (sortedHelpQueries == null) {
103             Set JavaDoc set = helpQueries.keySet();
104             sortedHelpQueries = new ArrayList JavaDoc(set.size());
105             sortedHelpQueries.addAll(set);
106             Collections.sort(sortedHelpQueries, new QueryComparator());
107         }
108
109         // Return the first match (we assume there is only one)
110
for (Iterator JavaDoc iter = sortedHelpQueries.iterator(); iter.hasNext();) {
111             MarkerQuery query = (MarkerQuery) iter.next();
112             MarkerQueryResult result = query.performQuery(marker);
113             if (result != null) {
114                 // See if a matching result is registered
115
Map JavaDoc resultsTable = (Map JavaDoc) helpQueries.get(query);
116
117                 if (resultsTable.containsKey(result)) {
118
119                     Iterator JavaDoc elements = ((Collection JavaDoc) resultsTable.get(result))
120                             .iterator();
121                     while (elements.hasNext()) {
122                         IConfigurationElement element = (IConfigurationElement) elements
123                                 .next();
124                         // We have a match so return the help context id
125
return element.getAttribute(ATT_HELP);
126                     }
127                 }
128             }
129         }
130         return null;
131     }
132
133     /*
134      * (non-Javadoc) Method declared on IMarkerHelpRegistry.
135      */

136     public boolean hasResolutions(IMarker marker) {
137         // Detect a match
138
for (Iterator JavaDoc iter = resolutionQueries.keySet().iterator(); iter
139                 .hasNext();) {
140             MarkerQuery query = (MarkerQuery) iter.next();
141             MarkerQueryResult result = query.performQuery(marker);
142             if (result != null) {
143                 // See if a matching result is registered
144
Map JavaDoc resultsTable = (Map JavaDoc) resolutionQueries.get(query);
145
146                 if (resultsTable.containsKey(result)) {
147
148                     Iterator JavaDoc elements = ((Collection JavaDoc) resultsTable.get(result))
149                             .iterator();
150                     while (elements.hasNext()) {
151                         IConfigurationElement element = (IConfigurationElement) elements
152                                 .next();
153
154                         if (hasResolution(marker, element))
155                             return true;
156                     }
157                 }
158             }
159         }
160         return false;
161     }
162
163     /**
164      * Return whether or not this configuration element has a resolution for the
165      * marker.
166      *
167      * @param marker
168      * @param element
169      * @return boolean <code>true</code> if there is a resolution.
170      */

171     private boolean hasResolution(IMarker marker, IConfigurationElement element) {
172         IMarkerResolutionGenerator generator = null;
173         if (Platform.getBundle(element.getNamespace()).getState() == Bundle.ACTIVE) {
174             // The element's plugin is loaded so we instantiate
175
// the resolution
176
try {
177                 generator = (IMarkerResolutionGenerator) element
178                         .createExecutableExtension(ATT_CLASS);
179             } catch (CoreException e) {
180                 IDEWorkbenchPlugin
181                         .log(
182                                 "Unable to instantiate resolution generator", e.getStatus()); //$NON-NLS-1$
183
}
184             if (generator != null) {
185                 if (generator instanceof IMarkerResolutionGenerator2) {
186                     if (((IMarkerResolutionGenerator2) generator)
187                             .hasResolutions(marker)) {
188                         return true;
189                     }
190                 } else {
191                     IMarkerResolution[] resolutions = generator
192                             .getResolutions(marker);
193                     if (resolutions.length > 0) {
194                         // there is at least one resolution
195
return true;
196                     }
197                 }
198             }
199         } else {
200             // The element's plugin in not loaded so we assume
201
// the generator will produce resolutions for the marker
202
return true;
203         }
204         return false;
205     }
206
207
208     /* (non-Javadoc)
209      * @see org.eclipse.ui.IMarkerHelpRegistry#getResolutions(org.eclipse.core.resources.IMarker)
210      */

211     public IMarkerResolution[] getResolutions(IMarker marker) {
212         // Collect all matches
213
ArrayList JavaDoc resolutions = new ArrayList JavaDoc();
214         for (Iterator JavaDoc iter = resolutionQueries.keySet().iterator(); iter
215                 .hasNext();) {
216             MarkerQuery query = (MarkerQuery) iter.next();
217             MarkerQueryResult result = query.performQuery(marker);
218             if (result != null) {
219                 // See if a matching result is registered
220
Map JavaDoc resultsTable = (Map JavaDoc) resolutionQueries.get(query);
221
222                 if (resultsTable.containsKey(result)) {
223
224                     Iterator JavaDoc elements = ((Collection JavaDoc) resultsTable.get(result))
225                             .iterator();
226                     while (elements.hasNext()) {
227                         IConfigurationElement element = (IConfigurationElement) elements
228                                 .next();
229
230                         IMarkerResolutionGenerator generator = null;
231                         try {
232                             generator = (IMarkerResolutionGenerator) element
233                                     .createExecutableExtension(ATT_CLASS);
234                         } catch (CoreException e) {
235                             IDEWorkbenchPlugin
236                                     .log(
237                                             "Unable to instantiate resolution generator", e.getStatus()); //$NON-NLS-1$
238
}
239                         if (generator != null) {
240                             IMarkerResolution[] generatedResolutions = generator
241                                     .getResolutions(marker);
242                             for (int i = 0; i < generatedResolutions.length; i++) {
243                                 resolutions.add(generatedResolutions[i]);
244                             }
245                         }
246
247                     }
248                 }
249             }
250         }
251         return (IMarkerResolution[]) resolutions
252                 .toArray(new IMarkerResolution[resolutions.size()]);
253     }
254
255     /**
256      * Adds a help query to the registry.
257      *
258      * @param query
259      * a marker query
260      * @param result
261      * a result for the given query
262      * @param element
263      * the configuration element defining the result
264      */

265     public void addHelpQuery(MarkerQuery query, MarkerQueryResult result,
266             IConfigurationElement element) {
267
268         addQuery(helpQueries, query, result, element);
269     }
270
271     /**
272      * Adds a resolution query to the registry.
273      *
274      * @param query
275      * a marker query
276      * @param result
277      * a result for the given query
278      * @param element
279      * the configuration element defining the result
280      */

281     public void addResolutionQuery(MarkerQuery query, MarkerQueryResult result,
282             IConfigurationElement element) {
283
284         addQuery(resolutionQueries, query, result, element);
285     }
286
287     /**
288      * Adds a query to the given table.
289      *
290      * @param table
291      * the table to which the query is added
292      * @param query
293      * a marker query
294      * @param result
295      * a result for the given query
296      * @param element
297      * the configuration element defining the result
298      */

299     private void addQuery(Map JavaDoc table, MarkerQuery query,
300             MarkerQueryResult result, IConfigurationElement element) {
301
302         // See if the query is already in the table
303
Map JavaDoc results = (Map JavaDoc) table.get(query);
304         if (results == null) {
305             // Create a new results table
306
results = new HashMap JavaDoc();
307
308             // Add the query to the table
309
table.put(query, results);
310         }
311
312         if (results.containsKey(result)) {
313             Collection JavaDoc currentElements = (Collection JavaDoc) results.get(result);
314             currentElements.add(element);
315         } else {
316             Collection JavaDoc elements = new HashSet JavaDoc();
317             elements.add(element);
318
319             // Add the new result
320
results.put(result, elements);
321         }
322     }
323 }
324
Popular Tags