1 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.List ; 22 import java.util.Map ; 23 import java.util.Set ; 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 39 public class MarkerHelpRegistry implements IMarkerHelpRegistry { 40 43 private Map helpQueries = new HashMap (); 44 45 49 private List sortedHelpQueries; 50 51 54 private Map resolutionQueries = new HashMap (); 55 56 59 private static final String ATT_HELP = "helpContextId"; 61 64 private static final String ATT_CLASS = "class"; 66 private class QueryComparator implements Comparator { 67 70 public boolean equals(Object o) { 71 if (!(o instanceof QueryComparator)) { 72 return false; 73 } 74 return true; 75 } 76 77 80 public int compare(Object o1, Object o2) { 81 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 101 public String getHelp(IMarker marker) { 102 if (sortedHelpQueries == null) { 103 Set set = helpQueries.keySet(); 104 sortedHelpQueries = new ArrayList (set.size()); 105 sortedHelpQueries.addAll(set); 106 Collections.sort(sortedHelpQueries, new QueryComparator()); 107 } 108 109 for (Iterator iter = sortedHelpQueries.iterator(); iter.hasNext();) { 111 MarkerQuery query = (MarkerQuery) iter.next(); 112 MarkerQueryResult result = query.performQuery(marker); 113 if (result != null) { 114 Map resultsTable = (Map ) helpQueries.get(query); 116 117 if (resultsTable.containsKey(result)) { 118 119 Iterator elements = ((Collection ) resultsTable.get(result)) 120 .iterator(); 121 while (elements.hasNext()) { 122 IConfigurationElement element = (IConfigurationElement) elements 123 .next(); 124 return element.getAttribute(ATT_HELP); 126 } 127 } 128 } 129 } 130 return null; 131 } 132 133 136 public boolean hasResolutions(IMarker marker) { 137 for (Iterator iter = resolutionQueries.keySet().iterator(); iter 139 .hasNext();) { 140 MarkerQuery query = (MarkerQuery) iter.next(); 141 MarkerQueryResult result = query.performQuery(marker); 142 if (result != null) { 143 Map resultsTable = (Map ) resolutionQueries.get(query); 145 146 if (resultsTable.containsKey(result)) { 147 148 Iterator elements = ((Collection ) 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 171 private boolean hasResolution(IMarker marker, IConfigurationElement element) { 172 IMarkerResolutionGenerator generator = null; 173 if (Platform.getBundle(element.getNamespace()).getState() == Bundle.ACTIVE) { 174 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()); } 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 return true; 196 } 197 } 198 } 199 } else { 200 return true; 203 } 204 return false; 205 } 206 207 208 211 public IMarkerResolution[] getResolutions(IMarker marker) { 212 ArrayList resolutions = new ArrayList (); 214 for (Iterator iter = resolutionQueries.keySet().iterator(); iter 215 .hasNext();) { 216 MarkerQuery query = (MarkerQuery) iter.next(); 217 MarkerQueryResult result = query.performQuery(marker); 218 if (result != null) { 219 Map resultsTable = (Map ) resolutionQueries.get(query); 221 222 if (resultsTable.containsKey(result)) { 223 224 Iterator elements = ((Collection ) 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()); } 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 265 public void addHelpQuery(MarkerQuery query, MarkerQueryResult result, 266 IConfigurationElement element) { 267 268 addQuery(helpQueries, query, result, element); 269 } 270 271 281 public void addResolutionQuery(MarkerQuery query, MarkerQueryResult result, 282 IConfigurationElement element) { 283 284 addQuery(resolutionQueries, query, result, element); 285 } 286 287 299 private void addQuery(Map table, MarkerQuery query, 300 MarkerQueryResult result, IConfigurationElement element) { 301 302 Map results = (Map ) table.get(query); 304 if (results == null) { 305 results = new HashMap (); 307 308 table.put(query, results); 310 } 311 312 if (results.containsKey(result)) { 313 Collection currentElements = (Collection ) results.get(result); 314 currentElements.add(element); 315 } else { 316 Collection elements = new HashSet (); 317 elements.add(element); 318 319 results.put(result, elements); 321 } 322 } 323 } 324 | Popular Tags |