KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > ProposalSorterRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006 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 package org.eclipse.jdt.internal.ui.text.java;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedHashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
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 /**
39  * @since 3.2
40  */

41 public final class ProposalSorterRegistry {
42     private static final String JavaDoc EXTENSION_POINT= "javaCompletionProposalSorters"; //$NON-NLS-1$
43
private static final String JavaDoc DEFAULT_ID= "org.eclipse.jdt.ui.RelevanceSorter"; //$NON-NLS-1$
44

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 JavaDoc fKey;
55
56     private Map JavaDoc fSorters= null;
57     private ProposalSorterHandle fDefaultSorter;
58
59     private ProposalSorterRegistry(final IPreferenceStore preferenceStore, final String JavaDoc 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 JavaDoc 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 JavaDoc sorters= new LinkedHashMap JavaDoc();
78         IExtensionRegistry registry= Platform.getExtensionRegistry();
79         List JavaDoc elements= new ArrayList JavaDoc(Arrays.asList(registry.getConfigurationElementsFor(JavaPlugin.getPluginId(), EXTENSION_POINT)));
80
81         for (Iterator JavaDoc iter= elements.iterator(); iter.hasNext();) {
82             IConfigurationElement element= (IConfigurationElement) iter.next();
83             
84             try {
85             
86                 ProposalSorterHandle handle= new ProposalSorterHandle(element);
87                 final String JavaDoc id= handle.getId();
88                 sorters.put(id, handle);
89                 if (DEFAULT_ID.equals(id))
90                     fDefaultSorter= handle;
91
92             } catch (InvalidRegistryObjectException x) {
93                 /*
94                  * Element is not valid any longer as the contributing plug-in was unloaded or for
95                  * some other reason. Do not include the extension in the list and inform the user
96                  * about it.
97                  */

98                 Object JavaDoc[] args= { element.toString() };
99                 String JavaDoc 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 JavaDoc title= JavaTextMessages.CompletionProposalComputerRegistry_error_dialog_title;
111         String JavaDoc message= status.getMessage();
112         MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), title, message);
113     }
114
115     public ProposalSorterHandle[] getSorters() {
116         ensureSortersRead();
117         Collection JavaDoc 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 JavaDoc id= handle.getId();
124         
125         fPreferenceStore.setValue(fKey, id);
126     }
127 }
128
Popular Tags