]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/text/java/ProposalSorterRegistry.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / text / java / ProposalSorterRegistry.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2006, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.text.java;
12
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.Collection;
16import java.util.Iterator;
17import java.util.LinkedHashMap;
18import java.util.List;
19import java.util.Map;
20
21import org.eclipse.core.runtime.Assert;
22import org.eclipse.core.runtime.CoreException;
23import org.eclipse.core.runtime.IConfigurationElement;
24import org.eclipse.core.runtime.IExtensionRegistry;
25import org.eclipse.core.runtime.IStatus;
26import org.eclipse.core.runtime.InvalidRegistryObjectException;
27import org.eclipse.core.runtime.Platform;
28import org.eclipse.core.runtime.Status;
29
30import org.eclipse.jface.dialogs.MessageDialog;
31import org.eclipse.jface.preference.IPreferenceStore;
32
33import org.eclipse.jdt.internal.corext.util.Messages;
34
35import org.eclipse.jdt.ui.PreferenceConstants;
36
37import org.eclipse.jdt.internal.ui.JavaPlugin;
38
39/**
40 * @since 3.2
41 */
42public final class ProposalSorterRegistry {
43 private static final String EXTENSION_POINT= "javaCompletionProposalSorters"; //$NON-NLS-1$
44 private static final String DEFAULT_ID= "org.eclipse.jdt.ui.RelevanceSorter"; //$NON-NLS-1$
45
46 private static ProposalSorterRegistry fInstance;
47
48 public static synchronized ProposalSorterRegistry getDefault() {
49 if (fInstance == null)
50 fInstance= new ProposalSorterRegistry(JavaPlugin.getDefault().getPreferenceStore(), PreferenceConstants.CODEASSIST_SORTER);
51 return fInstance;
52 }
53
54 private final IPreferenceStore fPreferenceStore;
55 private final String fKey;
56
57 private Map<String, ProposalSorterHandle> fSorters= null;
58 private ProposalSorterHandle fDefaultSorter;
59
60 private ProposalSorterRegistry(final IPreferenceStore preferenceStore, final String key) {
61 Assert.isTrue(preferenceStore != null);
62 Assert.isTrue(key != null);
63 fPreferenceStore= preferenceStore;
64 fKey= key;
65 }
66
67 public ProposalSorterHandle getCurrentSorter() {
68 ensureSortersRead();
69 String id= fPreferenceStore.getString(fKey);
70 ProposalSorterHandle sorter= fSorters.get(id);
71 return sorter != null ? sorter : fDefaultSorter;
72 }
73
74 private synchronized void ensureSortersRead() {
75 if (fSorters != null)
76 return;
77
78 Map<String, ProposalSorterHandle> sorters= new LinkedHashMap<String, ProposalSorterHandle>();
79 IExtensionRegistry registry= Platform.getExtensionRegistry();
80 List<IConfigurationElement> elements= new ArrayList<IConfigurationElement>(Arrays.asList(registry.getConfigurationElementsFor(JavaPlugin.getPluginId(), EXTENSION_POINT)));
81
82 for (Iterator<IConfigurationElement> iter= elements.iterator(); iter.hasNext();) {
83 IConfigurationElement element= iter.next();
84
85 try {
86
87 ProposalSorterHandle handle= new ProposalSorterHandle(element);
88 final String id= handle.getId();
89 sorters.put(id, handle);
90 if (DEFAULT_ID.equals(id))
91 fDefaultSorter= handle;
92
93 } catch (InvalidRegistryObjectException x) {
94 /*
95 * Element is not valid any longer as the contributing plug-in was unloaded or for
96 * some other reason. Do not include the extension in the list and inform the user
97 * about it.
98 */
99 Object[] args= { element.toString() };
100 String message= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_invalid_message, args);
101 IStatus status= new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, x);
102 informUser(status);
103 } catch (CoreException x) {
104 informUser(x.getStatus());
105 }
106 }
107
108 fSorters= sorters;
109 }
110
111 private void informUser(IStatus status) {
112 JavaPlugin.log(status);
113 String title= JavaTextMessages.CompletionProposalComputerRegistry_error_dialog_title;
114 String message= status.getMessage();
115 MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), title, message);
116 }
117
118 public ProposalSorterHandle[] getSorters() {
119 ensureSortersRead();
120 Collection<ProposalSorterHandle> sorters= fSorters.values();
121 return sorters.toArray(new ProposalSorterHandle[sorters.size()]);
122 }
123
124 public void select(ProposalSorterHandle handle) {
125 Assert.isTrue(handle != null);
126 String id= handle.getId();
127
128 fPreferenceStore.setValue(fKey, id);
129 }
130}