3m9 compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / folding / JavaFoldingStructureProviderRegistry.java
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
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtensionRegistry;
24 import org.eclipse.core.runtime.Platform;
25
26 /**
27  * @since 3.0
28  */
29 public class JavaFoldingStructureProviderRegistry {
30         
31         private static final String EXTENSION_POINT= "foldingStructureProviders"; //$NON-NLS-1$
32         
33         /** The map of descriptors, indexed by their identifiers. */
34         private Map fDescriptors;
35
36         /**
37          * Creates a new instance. 
38          */
39         public JavaFoldingStructureProviderRegistry() {
40         }
41         
42         /**
43          * Returns an array of <code>IJavaFoldingProviderDescriptor</code> describing
44          * all extension to the <code>foldingProviders</code> extension point.
45          * 
46          * @return the list of extensions to the
47          *         <code>quickDiffReferenceProvider</code> extension point.
48          */
49         public JavaFoldingStructureProviderDescriptor[] getFoldingProviderDescriptors() {
50                 synchronized (this) {
51                         ensureRegistered();
52                         return (JavaFoldingStructureProviderDescriptor[]) fDescriptors.values().toArray(new JavaFoldingStructureProviderDescriptor[fDescriptors.size()]);
53                 }
54         }
55         
56         /**
57          * Returns the folding provider with identifier <code>id</code> or
58          * <code>null</code> if no such provider is registered.
59          * 
60          * @param id the identifier for which a provider is wanted
61          * @return the corresponding provider, or <code>null</code> if none can be
62          *         found
63          */
64         public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor(String id) {
65                 synchronized (this) {
66                         ensureRegistered();
67                         return (JavaFoldingStructureProviderDescriptor) fDescriptors.get(id);
68                 }
69         }
70         
71         /**
72          * Instantiates and returns the provider that is currently configured in the
73          * preferences.
74          * 
75          * @return the current provider according to the preferences
76          */
77         public IJavaFoldingStructureProvider getCurrentFoldingProvider() {
78                 String id= PHPeclipsePlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
79                 JavaFoldingStructureProviderDescriptor desc= getFoldingProviderDescriptor(id);
80                 if (desc != null) {
81                         try {
82                                 return desc.createProvider();
83                         } catch (CoreException e) {
84                           PHPeclipsePlugin.log(e);
85                         }
86                 }
87                 return null;
88         }
89         
90         /**
91          * Ensures that the extensions are read and stored in
92          * <code>fDescriptors</code>.
93          */
94         private void ensureRegistered() {
95                 if (fDescriptors == null)
96                         reloadExtensions();
97         }
98
99         /**
100          * Reads all extensions.
101          * <p>
102          * This method can be called more than once in
103          * order to reload from a changed extension registry.
104          * </p>
105          */
106         public void reloadExtensions() {
107                 IExtensionRegistry registry= Platform.getExtensionRegistry();
108                 Map map= new HashMap();
109
110                 IConfigurationElement[] elements= registry.getConfigurationElementsFor(PHPeclipsePlugin.getPluginId(), EXTENSION_POINT);
111                 for (int i= 0; i < elements.length; i++) {
112                         JavaFoldingStructureProviderDescriptor desc= new JavaFoldingStructureProviderDescriptor(elements[i]);
113                         map.put(desc.getId(), desc);
114                 }
115                 
116                 synchronized(this) {
117                         fDescriptors= Collections.unmodifiableMap(map);
118                 }
119         }
120
121 }