| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 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; |
| 15 | import java.util.Collection; |
| 16 | import java.util.Collections; |
| 17 | import java.util.Comparator; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.LinkedHashMap; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.Map.Entry; |
| 26 | |
| 27 | import org.eclipse.core.resources.IMarker; |
| 28 | import org.eclipse.core.runtime.CoreException; |
| 29 | import org.eclipse.core.runtime.IConfigurationElement; |
| 30 | import org.eclipse.core.runtime.Platform; |
| 31 | import org.eclipse.ui.IMarkerHelpRegistry; |
| 32 | import org.eclipse.ui.IMarkerResolution; |
| 33 | import org.eclipse.ui.IMarkerResolutionGenerator; |
| 34 | import org.eclipse.ui.IMarkerResolutionGenerator2; |
| 35 | import org.eclipse.ui.internal.ide.Policy; |
| 36 | import org.osgi.framework.Bundle; |
| 37 | |
| 38 | /** |
| 39 | * This class is a registry for marker help contexts and resolutions. |
| 40 | */ |
| 41 | public class MarkerHelpRegistry implements IMarkerHelpRegistry { |
| 42 | /** |
| 43 | * Table of queries for marker F1 help. |
| 44 | */ |
| 45 | private Map helpQueries = new HashMap(); |
| 46 | |
| 47 | /** |
| 48 | * Sorted list of help queries. Used to ensure that the "most specific" |
| 49 | * query is tried first |
| 50 | */ |
| 51 | private List sortedHelpQueries; |
| 52 | |
| 53 | /** |
| 54 | * Table of queries for marker resolutions |
| 55 | */ |
| 56 | private Map resolutionQueries = new LinkedHashMap(); |
| 57 | |
| 58 | /** |
| 59 | * Help context id attribute in configuration element |
| 60 | */ |
| 61 | private static final String ATT_HELP = "helpContextId"; //$NON-NLS-1$ |
| 62 | |
| 63 | /** |
| 64 | * Resolution class attribute name in configuration element |
| 65 | */ |
| 66 | private static final String ATT_CLASS = "class"; //$NON-NLS-1$ |
| 67 | |
| 68 | private class QueryComparator implements Comparator { |
| 69 | /* |
| 70 | * (non-Javadoc) Method declared on Object. |
| 71 | */ |
| 72 | public boolean equals(Object o) { |
| 73 | if (!(o instanceof QueryComparator)) { |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * (non-Javadoc) Method declared on Comparator. |
| 81 | */ |
| 82 | public int compare(Object o1, Object o2) { |
| 83 | // more attribues come first |
| 84 | MarkerQuery q1 = (MarkerQuery) o1; |
| 85 | MarkerQuery q2 = (MarkerQuery) o2; |
| 86 | |
| 87 | int size1 = q1.getAttributes().length; |
| 88 | int size2 = q2.getAttributes().length; |
| 89 | |
| 90 | if (size1 > size2) { |
| 91 | return -1; |
| 92 | } |
| 93 | if (size1 == size2) { |
| 94 | return 0; |
| 95 | } |
| 96 | return 1; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * (non-Javadoc) Method declared on IMarkerHelpRegistry. |
| 102 | */ |
| 103 | public String getHelp(IMarker marker) { |
| 104 | if (sortedHelpQueries == null) { |
| 105 | Set set = helpQueries.keySet(); |
| 106 | sortedHelpQueries = new ArrayList(set.size()); |
| 107 | sortedHelpQueries.addAll(set); |
| 108 | Collections.sort(sortedHelpQueries, new QueryComparator()); |
| 109 | } |
| 110 | |
| 111 | // Return the first match (we assume there is only one) |
| 112 | for (Iterator iter = sortedHelpQueries.iterator(); iter.hasNext();) { |
| 113 | MarkerQuery query = (MarkerQuery) iter.next(); |
| 114 | MarkerQueryResult result = query.performQuery(marker); |
| 115 | if (result != null) { |
| 116 | // See if a matching result is registered |
| 117 | Map resultsTable = (Map) helpQueries.get(query); |
| 118 | |
| 119 | if (resultsTable.containsKey(result)) { |
| 120 | |
| 121 | Iterator elements = ((Collection) resultsTable.get(result)) |
| 122 | .iterator(); |
| 123 | while (elements.hasNext()) { |
| 124 | IConfigurationElement element = (IConfigurationElement) elements |
| 125 | .next(); |
| 126 | // We have a match so return the help context id |
| 127 | return element.getAttribute(ATT_HELP); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * (non-Javadoc) Method declared on IMarkerHelpRegistry. |
| 137 | */ |
| 138 | public boolean hasResolutions(IMarker marker) { |
| 139 | // Detect a match |
| 140 | for (Iterator iter = resolutionQueries.keySet().iterator(); iter |
| 141 | .hasNext();) { |
| 142 | MarkerQuery query = (MarkerQuery) iter.next(); |
| 143 | MarkerQueryResult result = query.performQuery(marker); |
| 144 | if (result != null) { |
| 145 | // See if a matching result is registered |
| 146 | Map resultsTable = (Map) resolutionQueries.get(query); |
| 147 | |
| 148 | if (resultsTable.containsKey(result)) { |
| 149 | |
| 150 | Iterator elements = ((Collection) resultsTable.get(result)) |
| 151 | .iterator(); |
| 152 | while (elements.hasNext()) { |
| 153 | IConfigurationElement element = (IConfigurationElement) elements |
| 154 | .next(); |
| 155 | |
| 156 | if (hasResolution(marker, element)) |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Return whether or not this configuration element has a resolution for the |
| 167 | * marker. |
| 168 | * |
| 169 | * @param marker |
| 170 | * @param element |
| 171 | * @return boolean <code>true</code> if there is a resolution. |
| 172 | */ |
| 173 | private boolean hasResolution(IMarker marker, IConfigurationElement element) { |
| 174 | IMarkerResolutionGenerator generator = null; |
| 175 | if (Platform.getBundle(element.getNamespace()).getState() == Bundle.ACTIVE) { |
| 176 | // The element's plugin is loaded so we instantiate |
| 177 | // the resolution |
| 178 | try { |
| 179 | generator = (IMarkerResolutionGenerator) element |
| 180 | .createExecutableExtension(ATT_CLASS); |
| 181 | } catch (CoreException e) { |
| 182 | Policy.handle(e); |
| 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 resolutions = new ArrayList(); |
| 214 | for (Iterator iter = resolutionQueries.entrySet().iterator(); iter |
| 215 | .hasNext();) { |
| 216 | Map.Entry entry = (Entry) iter.next(); |
| 217 | MarkerQuery query = (MarkerQuery) entry.getKey(); |
| 218 | MarkerQueryResult result = query.performQuery(marker); |
| 219 | if (result != null) { |
| 220 | // See if a matching result is registered |
| 221 | Map resultsTable = (Map) entry.getValue(); |
| 222 | |
| 223 | if (resultsTable.containsKey(result)) { |
| 224 | |
| 225 | Iterator elements = ((Collection) resultsTable.get(result)) |
| 226 | .iterator(); |
| 227 | while (elements.hasNext()) { |
| 228 | IConfigurationElement element = (IConfigurationElement) elements |
| 229 | .next(); |
| 230 | |
| 231 | IMarkerResolutionGenerator generator = null; |
| 232 | try { |
| 233 | generator = (IMarkerResolutionGenerator) element |
| 234 | .createExecutableExtension(ATT_CLASS); |
| 235 | } catch (CoreException e) { |
| 236 | Policy.handle(e); |
| 237 | } |
| 238 | if (generator != null) { |
| 239 | IMarkerResolution[] generatedResolutions = generator |
| 240 | .getResolutions(marker); |
| 241 | for (int i = 0; i < generatedResolutions.length; i++) { |
| 242 | resolutions.add(generatedResolutions[i]); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | return (IMarkerResolution[]) resolutions |
| 251 | .toArray(new IMarkerResolution[resolutions.size()]); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Adds a help query to the registry. |
| 256 | * |
| 257 | * @param query |
| 258 | * a marker query |
| 259 | * @param result |
| 260 | * a result for the given query |
| 261 | * @param element |
| 262 | * the configuration element defining the result |
| 263 | */ |
| 264 | public void addHelpQuery(MarkerQuery query, MarkerQueryResult result, |
| 265 | IConfigurationElement element) { |
| 266 | |
| 267 | addQuery(helpQueries, query, result, element); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Adds a resolution query to the registry. |
| 272 | * |
| 273 | * @param query |
| 274 | * a marker query |
| 275 | * @param result |
| 276 | * a result for the given query |
| 277 | * @param element |
| 278 | * the configuration element defining the result |
| 279 | */ |
| 280 | public void addResolutionQuery(MarkerQuery query, MarkerQueryResult result, |
| 281 | IConfigurationElement element) { |
| 282 | |
| 283 | addQuery(resolutionQueries, query, result, element); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Adds a query to the given table. |
| 288 | * |
| 289 | * @param table |
| 290 | * the table to which the query is added |
| 291 | * @param query |
| 292 | * a marker query |
| 293 | * @param result |
| 294 | * a result for the given query |
| 295 | * @param element |
| 296 | * the configuration element defining the result |
| 297 | */ |
| 298 | private void addQuery(Map table, MarkerQuery query, |
| 299 | MarkerQueryResult result, IConfigurationElement element) { |
| 300 | |
| 301 | // See if the query is already in the table |
| 302 | Map results = (Map) table.get(query); |
| 303 | if (results == null) { |
| 304 | // Create a new results table |
| 305 | results = new HashMap(); |
| 306 | |
| 307 | // Add the query to the table |
| 308 | table.put(query, results); |
| 309 | } |
| 310 | |
| 311 | if (results.containsKey(result)) { |
| 312 | Collection currentElements = (Collection) results.get(result); |
| 313 | currentElements.add(element); |
| 314 | } else { |
| 315 | Collection elements = new HashSet(); |
| 316 | elements.add(element); |
| 317 | |
| 318 | // Add the new result |
| 319 | results.put(result, elements); |
| 320 | } |
| 321 | } |
| 322 | } |