KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > folding > JavaFoldingStructureProviderRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.folding;
12
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.Status;
24
25 import org.eclipse.jface.preference.IPreferenceStore;
26
27
28 import org.eclipse.jdt.internal.corext.util.Messages;
29
30 import org.eclipse.jdt.ui.PreferenceConstants;
31 import org.eclipse.jdt.ui.text.folding.IJavaFoldingStructureProvider;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34
35
36 /**
37  * @since 3.0
38  */

39 public class JavaFoldingStructureProviderRegistry {
40
41     private static final String JavaDoc EXTENSION_POINT= "foldingStructureProviders"; //$NON-NLS-1$
42

43     /** The map of descriptors, indexed by their identifiers. */
44     private Map JavaDoc fDescriptors;
45
46     /**
47      * Creates a new instance.
48      */

49     public JavaFoldingStructureProviderRegistry() {
50     }
51
52     /**
53      * Returns an array of <code>JavaFoldingStructureProviderDescriptor</code> describing
54      * all extension to the <code>foldingProviders</code> extension point.
55      *
56      * @return the list of extensions to the
57      * <code>quickDiffReferenceProvider</code> extension point
58      */

59     public JavaFoldingStructureProviderDescriptor[] getFoldingProviderDescriptors() {
60         synchronized (this) {
61             ensureRegistered();
62             return (JavaFoldingStructureProviderDescriptor[]) fDescriptors.values().toArray(new JavaFoldingStructureProviderDescriptor[fDescriptors.size()]);
63         }
64     }
65
66     /**
67      * Returns the folding provider descriptor with identifier <code>id</code> or
68      * <code>null</code> if no such provider is registered.
69      *
70      * @param id the identifier for which a provider is wanted
71      * @return the corresponding provider descriptor, or <code>null</code> if none can be
72      * found
73      */

74     public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor(String JavaDoc id) {
75         synchronized (this) {
76             ensureRegistered();
77             return (JavaFoldingStructureProviderDescriptor) fDescriptors.get(id);
78         }
79     }
80
81     /**
82      * Instantiates and returns the provider that is currently configured in the
83      * preferences.
84      *
85      * @return the current provider according to the preferences
86      */

87     public IJavaFoldingStructureProvider getCurrentFoldingProvider() {
88         IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
89         String JavaDoc currentProviderId= preferenceStore.getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
90         JavaFoldingStructureProviderDescriptor desc= getFoldingProviderDescriptor(currentProviderId);
91         
92         // Fallback to default if extension has gone
93
if (desc == null) {
94             String JavaDoc message= Messages.format(FoldingMessages.JavaFoldingStructureProviderRegistry_warning_providerNotFound_resetToDefault, currentProviderId);
95             JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, null));
96             
97             String JavaDoc defaultProviderId= preferenceStore.getDefaultString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
98             
99             desc= getFoldingProviderDescriptor(defaultProviderId);
100             Assert.isNotNull(desc);
101             
102             preferenceStore.setToDefault(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
103         }
104
105         try {
106             return desc.createProvider();
107         } catch (CoreException e) {
108             JavaPlugin.log(e);
109             return null;
110         }
111     }
112
113     /**
114      * Ensures that the extensions are read and stored in
115      * <code>fDescriptors</code>.
116      */

117     private void ensureRegistered() {
118         if (fDescriptors == null)
119             reloadExtensions();
120     }
121
122     /**
123      * Reads all extensions.
124      * <p>
125      * This method can be called more than once in
126      * order to reload from a changed extension registry.
127      * </p>
128      */

129     public void reloadExtensions() {
130         IExtensionRegistry registry= Platform.getExtensionRegistry();
131         Map JavaDoc map= new HashMap JavaDoc();
132
133         IConfigurationElement[] elements= registry.getConfigurationElementsFor(JavaPlugin.getPluginId(), EXTENSION_POINT);
134         for (int i= 0; i < elements.length; i++) {
135             JavaFoldingStructureProviderDescriptor desc= new JavaFoldingStructureProviderDescriptor(elements[i]);
136             map.put(desc.getId(), desc);
137         }
138
139         synchronized(this) {
140             fDescriptors= Collections.unmodifiableMap(map);
141         }
142     }
143
144 }
145
Popular Tags