1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.StringTokenizer ; 24 25 import org.eclipse.core.runtime.IConfigurationElement; 26 import org.eclipse.core.runtime.IContributor; 27 import org.eclipse.core.runtime.IExtensionRegistry; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.InvalidRegistryObjectException; 30 import org.eclipse.core.runtime.Platform; 31 import org.eclipse.core.runtime.Status; 32 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.SelectionAdapter; 35 import org.eclipse.swt.events.SelectionEvent; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 import org.eclipse.swt.widgets.Link; 40 41 import org.eclipse.jface.dialogs.IDialogConstants; 42 import org.eclipse.jface.dialogs.MessageDialog; 43 import org.eclipse.jface.preference.IPreferenceStore; 44 45 import org.eclipse.ui.dialogs.PreferencesUtil; 46 47 import org.eclipse.jdt.internal.corext.util.Messages; 48 49 import org.eclipse.jdt.ui.PreferenceConstants; 50 51 import org.eclipse.jdt.internal.ui.JavaPlugin; 52 53 60 public final class CompletionProposalComputerRegistry { 61 62 private static final String EXTENSION_POINT= "javaCompletionProposalComputer"; 64 65 private static CompletionProposalComputerRegistry fgSingleton= null; 66 67 75 public static synchronized CompletionProposalComputerRegistry getDefault() { 76 if (fgSingleton == null) { 77 fgSingleton= new CompletionProposalComputerRegistry(); 78 } 79 80 return fgSingleton; 81 } 82 83 88 private final Map fDescriptorsByPartition= new HashMap (); 89 95 private final Map fPublicDescriptorsByPartition= new HashMap (); 96 100 private final List fDescriptors= new ArrayList (); 101 104 private final List fPublicDescriptors= Collections.unmodifiableList(fDescriptors); 105 106 private final List fCategories= new ArrayList (); 107 private final List fPublicCategories= Collections.unmodifiableList(fCategories); 108 111 private boolean fLoaded= false; 112 113 116 public CompletionProposalComputerRegistry() { 117 } 118 119 143 List getProposalComputerDescriptors(String partition) { 144 ensureExtensionPointRead(); 145 List result= (List ) fPublicDescriptorsByPartition.get(partition); 146 return result != null ? result : Collections.EMPTY_LIST; 147 } 148 149 164 List getProposalComputerDescriptors() { 165 ensureExtensionPointRead(); 166 return fPublicDescriptors; 167 } 168 169 183 public List getProposalCategories() { 184 ensureExtensionPointRead(); 185 return fPublicCategories; 186 } 187 188 192 private void ensureExtensionPointRead() { 193 boolean reload; 194 synchronized (this) { 195 reload= !fLoaded; 196 fLoaded= true; 197 } 198 if (reload) 199 reload(); 200 } 201 202 209 public void reload() { 210 IExtensionRegistry registry= Platform.getExtensionRegistry(); 211 List elements= new ArrayList (Arrays.asList(registry.getConfigurationElementsFor(JavaPlugin.getPluginId(), EXTENSION_POINT))); 212 213 Map map= new HashMap (); 214 List all= new ArrayList (); 215 216 List categories= getCategories(elements); 217 for (Iterator iter= elements.iterator(); iter.hasNext();) { 218 IConfigurationElement element= (IConfigurationElement) iter.next(); 219 try { 220 CompletionProposalComputerDescriptor desc= new CompletionProposalComputerDescriptor(element, this, categories); 221 Set partitions= desc.getPartitions(); 222 for (Iterator it= partitions.iterator(); it.hasNext();) { 223 String partition= (String ) it.next(); 224 List list= (List ) map.get(partition); 225 if (list == null) { 226 list= new ArrayList (); 227 map.put(partition, list); 228 } 229 list.add(desc); 230 } 231 all.add(desc); 232 233 } catch (InvalidRegistryObjectException x) { 234 239 Object [] args= {element.toString()}; 240 String message= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_invalid_message, args); 241 IStatus status= new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, x); 242 informUser(status); 243 } 244 } 245 246 synchronized (this) { 247 fCategories.clear(); 248 fCategories.addAll(categories); 249 250 Set partitions= map.keySet(); 251 fDescriptorsByPartition.keySet().retainAll(partitions); 252 fPublicDescriptorsByPartition.keySet().retainAll(partitions); 253 for (Iterator it= partitions.iterator(); it.hasNext();) { 254 String partition= (String ) it.next(); 255 List old= (List ) fDescriptorsByPartition.get(partition); 256 List current= (List ) map.get(partition); 257 if (old != null) { 258 old.clear(); 259 old.addAll(current); 260 } else { 261 fDescriptorsByPartition.put(partition, current); 262 fPublicDescriptorsByPartition.put(partition, Collections.unmodifiableList(current)); 263 } 264 } 265 266 fDescriptors.clear(); 267 fDescriptors.addAll(all); 268 } 269 } 270 271 private List getCategories(List elements) { 272 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 273 String preference= store.getString(PreferenceConstants.CODEASSIST_EXCLUDED_CATEGORIES); 274 Set disabled= new HashSet (); 275 StringTokenizer tok= new StringTokenizer (preference, "\0"); while (tok.hasMoreTokens()) 277 disabled.add(tok.nextToken()); 278 Map ordered= new HashMap (); 279 preference= store.getString(PreferenceConstants.CODEASSIST_CATEGORY_ORDER); 280 tok= new StringTokenizer (preference, "\0"); while (tok.hasMoreTokens()) { 282 StringTokenizer inner= new StringTokenizer (tok.nextToken(), ":"); String id= inner.nextToken(); 284 int rank= Integer.parseInt(inner.nextToken()); 285 ordered.put(id, new Integer (rank)); 286 } 287 288 List categories= new ArrayList (); 289 for (Iterator iter= elements.iterator(); iter.hasNext();) { 290 IConfigurationElement element= (IConfigurationElement) iter.next(); 291 try { 292 if (element.getName().equals("proposalCategory")) { iter.remove(); 295 CompletionProposalCategory category= new CompletionProposalCategory(element, this); 296 categories.add(category); 297 category.setIncluded(!disabled.contains(category.getId())); 298 Integer rank= (Integer ) ordered.get(category.getId()); 299 if (rank != null) { 300 int r= rank.intValue(); 301 boolean separate= r < 0xffff; 302 category.setSeparateCommand(separate); 303 category.setSortOrder(r); 304 } 305 } 306 } catch (InvalidRegistryObjectException x) { 307 312 Object [] args= {element.toString()}; 313 String message= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_invalid_message, args); 314 IStatus status= new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, x); 315 informUser(status); 316 } 317 } 318 return categories; 319 } 320 321 327 void informUser(CompletionProposalComputerDescriptor descriptor, IStatus status) { 328 JavaPlugin.log(status); 329 String title= JavaTextMessages.CompletionProposalComputerRegistry_error_dialog_title; 330 CompletionProposalCategory category= descriptor.getCategory(); 331 IContributor culprit= descriptor.getContributor(); 332 Set affectedPlugins= getAffectedContributors(category, culprit); 333 334 final String avoidHint; 335 final String culpritName= culprit == null ? null : culprit.getName(); 336 if (affectedPlugins.isEmpty()) 337 avoidHint= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_messageAvoidanceHint, new Object [] {culpritName, category.getDisplayName()}); 338 else 339 avoidHint= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_messageAvoidanceHintWithWarning, new Object [] {culpritName, category.getDisplayName(), toString(affectedPlugins)}); 340 341 String message= status.getMessage(); 342 MessageDialog dialog = new MessageDialog(JavaPlugin.getActiveWorkbenchShell(), title, null , message, MessageDialog.ERROR, new String [] { IDialogConstants.OK_LABEL }, 0) { 344 protected Control createCustomArea(Composite parent) { 345 Link link= new Link(parent, SWT.NONE); 346 link.setText(avoidHint); 347 link.addSelectionListener(new SelectionAdapter() { 348 public void widgetSelected(SelectionEvent e) { 349 PreferencesUtil.createPreferenceDialogOn(getShell(), "org.eclipse.jdt.ui.preferences.CodeAssistPreferenceAdvanced", null, null).open(); } 351 }); 352 GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false); 353 gridData.widthHint= this.getMinimumMessageWidth(); 354 link.setLayoutData(gridData); 355 return link; 356 } 357 }; 358 dialog.open(); 359 } 360 361 368 private Set getAffectedContributors(CompletionProposalCategory category, IContributor culprit) { 369 Set affectedPlugins= new HashSet (); 370 for (Iterator it= getProposalComputerDescriptors().iterator(); it.hasNext();) { 371 CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next(); 372 CompletionProposalCategory cat= desc.getCategory(); 373 if (cat.equals(category)) { 374 IContributor contributor= desc.getContributor(); 375 if (contributor != null && !culprit.equals(contributor)) 376 affectedPlugins.add(contributor.getName()); 377 } 378 } 379 return affectedPlugins; 380 } 381 382 private Object toString(Collection collection) { 383 String string= collection.toString(); 385 return string.substring(1, string.length() - 1); 386 } 387 388 private void informUser(IStatus status) { 389 JavaPlugin.log(status); 390 String title= JavaTextMessages.CompletionProposalComputerRegistry_error_dialog_title; 391 String message= status.getMessage(); 392 MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), title, message); 393 } 394 } 395 | Popular Tags |