2228c68c34ab4f26d1c17975946f904e65bdad6f
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.folding;
12
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import net.sourceforge.phpdt.ui.PreferenceConstants;
18 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingStructureProvider;
19 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.ui.WebUI;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExtensionRegistry;
26 import org.eclipse.core.runtime.Platform;
27
28 /**
29  * @since 3.0
30  */
31 public class JavaFoldingStructureProviderRegistry {
32
33         private static final String EXTENSION_POINT = "foldingStructureProviders"; //$NON-NLS-1$
34
35         /** The map of descriptors, indexed by their identifiers. */
36         private Map fDescriptors;
37
38         /**
39          * Creates a new instance.
40          */
41         public JavaFoldingStructureProviderRegistry() {
42         }
43
44         /**
45          * Returns an array of <code>IJavaFoldingProviderDescriptor</code>
46          * describing all extension to the <code>foldingProviders</code> extension
47          * point.
48          * 
49          * @return the list of extensions to the
50          *         <code>quickDiffReferenceProvider</code> extension point.
51          */
52         public JavaFoldingStructureProviderDescriptor[] getFoldingProviderDescriptors() {
53                 synchronized (this) {
54                         ensureRegistered();
55                         return (JavaFoldingStructureProviderDescriptor[]) fDescriptors
56                                         .values()
57                                         .toArray(
58                                                         new JavaFoldingStructureProviderDescriptor[fDescriptors
59                                                                         .size()]);
60                 }
61         }
62
63         /**
64          * Returns the folding provider with identifier <code>id</code> or
65          * <code>null</code> if no such provider is registered.
66          * 
67          * @param id
68          *            the identifier for which a provider is wanted
69          * @return the corresponding provider, or <code>null</code> if none can be
70          *         found
71          */
72         public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor(
73                         String id) {
74                 synchronized (this) {
75                         ensureRegistered();
76                         return (JavaFoldingStructureProviderDescriptor) fDescriptors
77                                         .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                 String id = WebUI.getDefault().getPreferenceStore()
89                                 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
90                 JavaFoldingStructureProviderDescriptor desc = getFoldingProviderDescriptor(id);
91                 if (desc != null) {
92                         try {
93                                 return desc.createProvider();
94                         } catch (CoreException e) {
95                                 WebUI.log(e);
96                         }
97                 }
98                 return null;
99         }
100
101         /**
102          * Ensures that the extensions are read and stored in
103          * <code>fDescriptors</code>.
104          */
105         private void ensureRegistered() {
106                 if (fDescriptors == null)
107                         reloadExtensions();
108         }
109
110         /**
111          * Reads all extensions.
112          * <p>
113          * This method can be called more than once in order to reload from a
114          * changed extension registry.
115          * </p>
116          */
117         public void reloadExtensions() {
118                 IExtensionRegistry registry = Platform.getExtensionRegistry();
119                 Map map = new HashMap();
120
121                 IConfigurationElement[] elements = registry
122                                 .getConfigurationElementsFor(PHPeclipsePlugin.getPluginId(),
123                                                 EXTENSION_POINT);
124                 for (int i = 0; i < elements.length; i++) {
125                         JavaFoldingStructureProviderDescriptor desc = new JavaFoldingStructureProviderDescriptor(
126                                         elements[i]);
127                         map.put(desc.getId(), desc);
128                 }
129
130                 synchronized (this) {
131                         fDescriptors = Collections.unmodifiableMap(map);
132                 }
133         }
134
135 }