1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import java.util.Collections ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.InvalidRegistryObjectException; 21 import org.eclipse.core.runtime.PerformanceStats; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.Status; 24 25 26 import org.eclipse.jdt.internal.corext.util.Messages; 27 28 import org.eclipse.jdt.ui.text.java.AbstractProposalSorter; 29 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; 30 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 33 import org.osgi.framework.Bundle; 34 35 42 public final class ProposalSorterHandle { 43 44 private static final String ID= "id"; 46 private static final String NAME= "name"; 48 private static final String CLASS= "class"; 50 private static final String ACTIVATE= "activate"; 52 private static final String PERFORMANCE_EVENT= JavaPlugin.getPluginId() + "/perf/content_assist_sorters/extensions"; 57 private static final boolean MEASURE_PERFORMANCE= PerformanceStats.isEnabled(PERFORMANCE_EVENT); 58 59 private static final String SORT= "sort"; 61 62 private final String fId; 63 64 private final String fName; 65 66 private final String fClass; 67 68 private final boolean fActivate; 69 70 private final IConfigurationElement fElement; 71 72 private AbstractProposalSorter fSorter; 73 74 81 ProposalSorterHandle(IConfigurationElement element) throws InvalidRegistryObjectException { 82 Assert.isLegal(element != null); 83 84 fElement= element; 85 fId= element.getAttribute(ID); 86 checkNotNull(fId, ID); 87 88 String name= element.getAttribute(NAME); 89 if (name == null) 90 fName= fId; 91 else 92 fName= name; 93 94 String activateAttribute= element.getAttribute(ACTIVATE); 95 fActivate= Boolean.valueOf(activateAttribute).booleanValue(); 96 97 fClass= element.getAttribute(CLASS); 98 checkNotNull(fClass, CLASS); 99 } 100 101 107 private void checkNotNull(Object obj, String attribute) throws InvalidRegistryObjectException { 108 if (obj == null) { 109 Object [] args= { getId(), fElement.getContributor().getName(), attribute }; 110 String message= Messages.format(JavaTextMessages.CompletionProposalComputerDescriptor_illegal_attribute_message, args); 111 IStatus status= new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, null); 112 JavaPlugin.log(status); 113 throw new InvalidRegistryObjectException(); 114 } 115 } 116 117 122 public String getId() { 123 return fId; 124 } 125 126 131 public String getName() { 132 return fName; 133 } 134 135 144 private synchronized AbstractProposalSorter getSorter() throws CoreException, InvalidRegistryObjectException { 145 if (fSorter == null && (fActivate || isPluginLoaded())) 146 fSorter= createSorter(); 147 return fSorter; 148 } 149 150 private boolean isPluginLoaded() throws InvalidRegistryObjectException { 151 Bundle bundle= getBundle(); 152 return bundle != null && bundle.getState() == Bundle.ACTIVE; 153 } 154 155 private Bundle getBundle() throws InvalidRegistryObjectException { 156 String symbolicName= fElement.getContributor().getName(); 157 Bundle bundle= Platform.getBundle(symbolicName); 158 return bundle; 159 } 160 161 171 private AbstractProposalSorter createSorter() throws CoreException, InvalidRegistryObjectException { 172 return (AbstractProposalSorter) fElement.createExecutableExtension(CLASS); 173 } 174 175 184 public void sortProposals(ContentAssistInvocationContext context, List proposals) { 185 IStatus status; 186 try { 187 AbstractProposalSorter sorter= getSorter(); 188 189 PerformanceStats stats= startMeter(SORT, sorter); 190 191 sorter.beginSorting(context); 192 Collections.sort(proposals, sorter); 193 sorter.endSorting(); 194 195 status= stopMeter(stats, SORT); 196 197 if (status == null) 199 return; 200 201 status= createAPIViolationStatus(SORT); 202 203 } catch (InvalidRegistryObjectException x) { 204 status= createExceptionStatus(x); 205 } catch (CoreException x) { 206 status= createExceptionStatus(x); 207 } catch (RuntimeException x) { 208 status= createExceptionStatus(x); 209 } 210 211 JavaPlugin.log(status); 212 return; 213 } 214 215 private IStatus stopMeter(final PerformanceStats stats, String operation) { 216 if (MEASURE_PERFORMANCE) { 217 stats.endRun(); 218 if (stats.isFailure()) 219 return createPerformanceStatus(operation); 220 } 221 return null; 222 } 223 224 private PerformanceStats startMeter(String context, AbstractProposalSorter sorter) { 225 final PerformanceStats stats; 226 if (MEASURE_PERFORMANCE) { 227 stats= PerformanceStats.getStats(PERFORMANCE_EVENT, sorter); 228 stats.startRun(context); 229 } else { 230 stats= null; 231 } 232 return stats; 233 } 234 235 private Status createExceptionStatus(InvalidRegistryObjectException x) { 236 String disable= createBlameMessage(); 238 String reason= JavaTextMessages.CompletionProposalComputerDescriptor_reason_invalid; 239 return new Status(IStatus.INFO, JavaPlugin.getPluginId(), IStatus.OK, disable + " " + reason, x); } 241 242 private Status createExceptionStatus(CoreException x) { 243 String disable= createBlameMessage(); 245 String reason= JavaTextMessages.CompletionProposalComputerDescriptor_reason_instantiation; 246 return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, disable + " " + reason, x); } 248 249 private Status createExceptionStatus(RuntimeException x) { 250 String disable= createBlameMessage(); 252 String reason= JavaTextMessages.CompletionProposalComputerDescriptor_reason_runtime_ex; 253 return new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, disable + " " + reason, x); } 255 256 private Status createAPIViolationStatus(String operation) { 257 String disable= createBlameMessage(); 258 Object [] args= {operation}; 259 String reason= Messages.format(JavaTextMessages.CompletionProposalComputerDescriptor_reason_API, args); 260 return new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, disable + " " + reason, null); } 262 263 private Status createPerformanceStatus(String operation) { 264 String disable= createBlameMessage(); 265 Object [] args= {operation}; 266 String reason= Messages.format(JavaTextMessages.CompletionProposalComputerDescriptor_reason_performance, args); 267 return new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, disable + " " + reason, null); } 269 270 private String createBlameMessage() { 271 Object [] args= { getName(), getId() }; 272 String disable= Messages.format(JavaTextMessages.ProposalSorterHandle_blame, args); 273 return disable; 274 } 275 276 281 public String getErrorMessage() { 282 return null; 283 } 284 285 } 286 | Popular Tags |