001: /*******************************************************************************
002: * Copyright (c) 2000, 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.folding;
011:
012: import java.util.Collections;
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.core.runtime.IExtensionRegistry;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.Platform;
022: import org.eclipse.core.runtime.Status;
023:
024: import org.eclipse.jface.preference.IPreferenceStore;
025:
026: import org.eclipse.jdt.internal.corext.util.Messages;
027:
028: import org.eclipse.jdt.ui.PreferenceConstants;
029: import org.eclipse.jdt.ui.text.folding.IJavaFoldingStructureProvider;
030:
031: import org.eclipse.jdt.internal.ui.JavaPlugin;
032:
033: /**
034: * @since 3.0
035: */
036: public class JavaFoldingStructureProviderRegistry {
037:
038: private static final String EXTENSION_POINT = "foldingStructureProviders"; //$NON-NLS-1$
039:
040: /** The map of descriptors, indexed by their identifiers. */
041: private Map fDescriptors;
042:
043: /**
044: * Creates a new instance.
045: */
046: public JavaFoldingStructureProviderRegistry() {
047: }
048:
049: /**
050: * Returns an array of <code>JavaFoldingStructureProviderDescriptor</code> describing
051: * all extension to the <code>foldingProviders</code> extension point.
052: *
053: * @return the list of extensions to the
054: * <code>quickDiffReferenceProvider</code> extension point
055: */
056: public JavaFoldingStructureProviderDescriptor[] getFoldingProviderDescriptors() {
057: synchronized (this ) {
058: ensureRegistered();
059: return (JavaFoldingStructureProviderDescriptor[]) fDescriptors
060: .values()
061: .toArray(
062: new JavaFoldingStructureProviderDescriptor[fDescriptors
063: .size()]);
064: }
065: }
066:
067: /**
068: * Returns the folding provider descriptor with identifier <code>id</code> or
069: * <code>null</code> if no such provider is registered.
070: *
071: * @param id the identifier for which a provider is wanted
072: * @return the corresponding provider descriptor, or <code>null</code> if none can be
073: * found
074: */
075: public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor(
076: String id) {
077: synchronized (this ) {
078: ensureRegistered();
079: return (JavaFoldingStructureProviderDescriptor) fDescriptors
080: .get(id);
081: }
082: }
083:
084: /**
085: * Instantiates and returns the provider that is currently configured in the
086: * preferences.
087: *
088: * @return the current provider according to the preferences
089: */
090: public IJavaFoldingStructureProvider getCurrentFoldingProvider() {
091: IPreferenceStore preferenceStore = JavaPlugin.getDefault()
092: .getPreferenceStore();
093: String currentProviderId = preferenceStore
094: .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
095: JavaFoldingStructureProviderDescriptor desc = getFoldingProviderDescriptor(currentProviderId);
096:
097: // Fallback to default if extension has gone
098: if (desc == null) {
099: String message = Messages
100: .format(
101: FoldingMessages.JavaFoldingStructureProviderRegistry_warning_providerNotFound_resetToDefault,
102: currentProviderId);
103: JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin
104: .getPluginId(), IStatus.OK, message, null));
105:
106: String defaultProviderId = preferenceStore
107: .getDefaultString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
108:
109: desc = getFoldingProviderDescriptor(defaultProviderId);
110: Assert.isNotNull(desc);
111:
112: preferenceStore
113: .setToDefault(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
114: }
115:
116: try {
117: return desc.createProvider();
118: } catch (CoreException e) {
119: JavaPlugin.log(e);
120: return null;
121: }
122: }
123:
124: /**
125: * Ensures that the extensions are read and stored in
126: * <code>fDescriptors</code>.
127: */
128: private void ensureRegistered() {
129: if (fDescriptors == null)
130: reloadExtensions();
131: }
132:
133: /**
134: * Reads all extensions.
135: * <p>
136: * This method can be called more than once in
137: * order to reload from a changed extension registry.
138: * </p>
139: */
140: public void reloadExtensions() {
141: IExtensionRegistry registry = Platform.getExtensionRegistry();
142: Map map = new HashMap();
143:
144: IConfigurationElement[] elements = registry
145: .getConfigurationElementsFor(JavaPlugin.getPluginId(),
146: EXTENSION_POINT);
147: for (int i = 0; i < elements.length; i++) {
148: JavaFoldingStructureProviderDescriptor desc = new JavaFoldingStructureProviderDescriptor(
149: elements[i]);
150: map.put(desc.getId(), desc);
151: }
152:
153: synchronized (this) {
154: fDescriptors = Collections.unmodifiableMap(map);
155: }
156: }
157:
158: }
|