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.Iterator ; 17 import java.util.LinkedHashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.IConfigurationElement; 23 import org.eclipse.core.runtime.IExtensionRegistry; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.InvalidRegistryObjectException; 26 import org.eclipse.core.runtime.Platform; 27 import org.eclipse.core.runtime.Status; 28 29 import org.eclipse.jface.dialogs.MessageDialog; 30 import org.eclipse.jface.preference.IPreferenceStore; 31 32 import org.eclipse.jdt.internal.corext.util.Messages; 33 34 import org.eclipse.jdt.ui.PreferenceConstants; 35 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 38 41 public final class ProposalSorterRegistry { 42 private static final String EXTENSION_POINT= "javaCompletionProposalSorters"; private static final String DEFAULT_ID= "org.eclipse.jdt.ui.RelevanceSorter"; 45 private static ProposalSorterRegistry fInstance; 46 47 public static synchronized ProposalSorterRegistry getDefault() { 48 if (fInstance == null) 49 fInstance= new ProposalSorterRegistry(JavaPlugin.getDefault().getPreferenceStore(), PreferenceConstants.CODEASSIST_SORTER); 50 return fInstance; 51 } 52 53 private final IPreferenceStore fPreferenceStore; 54 private final String fKey; 55 56 private Map fSorters= null; 57 private ProposalSorterHandle fDefaultSorter; 58 59 private ProposalSorterRegistry(final IPreferenceStore preferenceStore, final String key) { 60 Assert.isTrue(preferenceStore != null); 61 Assert.isTrue(key != null); 62 fPreferenceStore= preferenceStore; 63 fKey= key; 64 } 65 66 public ProposalSorterHandle getCurrentSorter() { 67 ensureSortersRead(); 68 String id= fPreferenceStore.getString(fKey); 69 ProposalSorterHandle sorter= (ProposalSorterHandle) fSorters.get(id); 70 return sorter != null ? sorter : fDefaultSorter; 71 } 72 73 private synchronized void ensureSortersRead() { 74 if (fSorters != null) 75 return; 76 77 Map sorters= new LinkedHashMap (); 78 IExtensionRegistry registry= Platform.getExtensionRegistry(); 79 List elements= new ArrayList (Arrays.asList(registry.getConfigurationElementsFor(JavaPlugin.getPluginId(), EXTENSION_POINT))); 80 81 for (Iterator iter= elements.iterator(); iter.hasNext();) { 82 IConfigurationElement element= (IConfigurationElement) iter.next(); 83 84 try { 85 86 ProposalSorterHandle handle= new ProposalSorterHandle(element); 87 final String id= handle.getId(); 88 sorters.put(id, handle); 89 if (DEFAULT_ID.equals(id)) 90 fDefaultSorter= handle; 91 92 } catch (InvalidRegistryObjectException x) { 93 98 Object [] args= { element.toString() }; 99 String message= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_invalid_message, args); 100 IStatus status= new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, x); 101 informUser(status); 102 } 103 } 104 105 fSorters= sorters; 106 } 107 108 private void informUser(IStatus status) { 109 JavaPlugin.log(status); 110 String title= JavaTextMessages.CompletionProposalComputerRegistry_error_dialog_title; 111 String message= status.getMessage(); 112 MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), title, message); 113 } 114 115 public ProposalSorterHandle[] getSorters() { 116 ensureSortersRead(); 117 Collection sorters= fSorters.values(); 118 return (ProposalSorterHandle[]) sorters.toArray(new ProposalSorterHandle[sorters.size()]); 119 } 120 121 public void select(ProposalSorterHandle handle) { 122 Assert.isTrue(handle != null); 123 String id= handle.getId(); 124 125 fPreferenceStore.setValue(fKey, id); 126 } 127 } 128 | Popular Tags |