001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.text.java;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.Collection;
015: import java.util.Iterator;
016: import java.util.LinkedHashMap;
017: import java.util.List;
018: import java.util.Map;
019:
020: import org.eclipse.core.runtime.Assert;
021: import org.eclipse.core.runtime.IConfigurationElement;
022: import org.eclipse.core.runtime.IExtensionRegistry;
023: import org.eclipse.core.runtime.IStatus;
024: import org.eclipse.core.runtime.InvalidRegistryObjectException;
025: import org.eclipse.core.runtime.Platform;
026: import org.eclipse.core.runtime.Status;
027:
028: import org.eclipse.jface.dialogs.MessageDialog;
029: import org.eclipse.jface.preference.IPreferenceStore;
030:
031: import org.eclipse.jdt.internal.corext.util.Messages;
032:
033: import org.eclipse.jdt.ui.PreferenceConstants;
034:
035: import org.eclipse.jdt.internal.ui.JavaPlugin;
036:
037: /**
038: * @since 3.2
039: */
040: public final class ProposalSorterRegistry {
041: private static final String EXTENSION_POINT = "javaCompletionProposalSorters"; //$NON-NLS-1$
042: private static final String DEFAULT_ID = "org.eclipse.jdt.ui.RelevanceSorter"; //$NON-NLS-1$
043:
044: private static ProposalSorterRegistry fInstance;
045:
046: public static synchronized ProposalSorterRegistry getDefault() {
047: if (fInstance == null)
048: fInstance = new ProposalSorterRegistry(JavaPlugin
049: .getDefault().getPreferenceStore(),
050: PreferenceConstants.CODEASSIST_SORTER);
051: return fInstance;
052: }
053:
054: private final IPreferenceStore fPreferenceStore;
055: private final String fKey;
056:
057: private Map fSorters = null;
058: private ProposalSorterHandle fDefaultSorter;
059:
060: private ProposalSorterRegistry(
061: final IPreferenceStore preferenceStore, final String key) {
062: Assert.isTrue(preferenceStore != null);
063: Assert.isTrue(key != null);
064: fPreferenceStore = preferenceStore;
065: fKey = key;
066: }
067:
068: public ProposalSorterHandle getCurrentSorter() {
069: ensureSortersRead();
070: String id = fPreferenceStore.getString(fKey);
071: ProposalSorterHandle sorter = (ProposalSorterHandle) fSorters
072: .get(id);
073: return sorter != null ? sorter : fDefaultSorter;
074: }
075:
076: private synchronized void ensureSortersRead() {
077: if (fSorters != null)
078: return;
079:
080: Map sorters = new LinkedHashMap();
081: IExtensionRegistry registry = Platform.getExtensionRegistry();
082: List elements = new ArrayList(Arrays.asList(registry
083: .getConfigurationElementsFor(JavaPlugin.getPluginId(),
084: EXTENSION_POINT)));
085:
086: for (Iterator iter = elements.iterator(); iter.hasNext();) {
087: IConfigurationElement element = (IConfigurationElement) iter
088: .next();
089:
090: try {
091:
092: ProposalSorterHandle handle = new ProposalSorterHandle(
093: element);
094: final String id = handle.getId();
095: sorters.put(id, handle);
096: if (DEFAULT_ID.equals(id))
097: fDefaultSorter = handle;
098:
099: } catch (InvalidRegistryObjectException x) {
100: /*
101: * Element is not valid any longer as the contributing plug-in was unloaded or for
102: * some other reason. Do not include the extension in the list and inform the user
103: * about it.
104: */
105: Object[] args = { element.toString() };
106: String message = Messages
107: .format(
108: JavaTextMessages.CompletionProposalComputerRegistry_invalid_message,
109: args);
110: IStatus status = new Status(IStatus.WARNING, JavaPlugin
111: .getPluginId(), IStatus.OK, message, x);
112: informUser(status);
113: }
114: }
115:
116: fSorters = sorters;
117: }
118:
119: private void informUser(IStatus status) {
120: JavaPlugin.log(status);
121: String title = JavaTextMessages.CompletionProposalComputerRegistry_error_dialog_title;
122: String message = status.getMessage();
123: MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(),
124: title, message);
125: }
126:
127: public ProposalSorterHandle[] getSorters() {
128: ensureSortersRead();
129: Collection sorters = fSorters.values();
130: return (ProposalSorterHandle[]) sorters
131: .toArray(new ProposalSorterHandle[sorters.size()]);
132: }
133:
134: public void select(ProposalSorterHandle handle) {
135: Assert.isTrue(handle != null);
136: String id = handle.getId();
137:
138: fPreferenceStore.setValue(fKey, id);
139: }
140: }
|