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 */ JavaFoldingStructureProviderRegistry()39 public JavaFoldingStructureProviderRegistry() { 40 } 41 42 /** 43 * Returns an array of <code>IJavaFoldingProviderDescriptor</code> 44 * describing all extension to the <code>foldingProviders</code> extension 45 * point. 46 * 47 * @return the list of extensions to the 48 * <code>quickDiffReferenceProvider</code> extension point. 49 */ getFoldingProviderDescriptors()50 public JavaFoldingStructureProviderDescriptor[] getFoldingProviderDescriptors() { 51 synchronized (this) { 52 ensureRegistered(); 53 return (JavaFoldingStructureProviderDescriptor[]) fDescriptors 54 .values() 55 .toArray( 56 new JavaFoldingStructureProviderDescriptor[fDescriptors 57 .size()]); 58 } 59 } 60 61 /** 62 * Returns the folding provider with identifier <code>id</code> or 63 * <code>null</code> if no such provider is registered. 64 * 65 * @param id 66 * the identifier for which a provider is wanted 67 * @return the corresponding provider, or <code>null</code> if none can be 68 * found 69 */ getFoldingProviderDescriptor( String id)70 public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor( 71 String id) { 72 synchronized (this) { 73 ensureRegistered(); 74 return (JavaFoldingStructureProviderDescriptor) fDescriptors 75 .get(id); 76 } 77 } 78 79 /** 80 * Instantiates and returns the provider that is currently configured in the 81 * preferences. 82 * 83 * @return the current provider according to the preferences 84 */ getCurrentFoldingProvider()85 public IJavaFoldingStructureProvider getCurrentFoldingProvider() { 86 String id = PHPeclipsePlugin.getDefault().getPreferenceStore() 87 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER); 88 JavaFoldingStructureProviderDescriptor desc = getFoldingProviderDescriptor(id); 89 if (desc != null) { 90 try { 91 return desc.createProvider(); 92 } catch (CoreException e) { 93 PHPeclipsePlugin.log(e); 94 } 95 } 96 return null; 97 } 98 99 /** 100 * Ensures that the extensions are read and stored in 101 * <code>fDescriptors</code>. 102 */ ensureRegistered()103 private void ensureRegistered() { 104 if (fDescriptors == null) 105 reloadExtensions(); 106 } 107 108 /** 109 * Reads all extensions. 110 * <p> 111 * This method can be called more than once in order to reload from a 112 * changed extension registry. 113 * </p> 114 */ reloadExtensions()115 public void reloadExtensions() { 116 IExtensionRegistry registry = Platform.getExtensionRegistry(); 117 Map map = new HashMap(); 118 119 IConfigurationElement[] elements = registry 120 .getConfigurationElementsFor(PHPeclipsePlugin.getPluginId(), 121 EXTENSION_POINT); 122 for (int i = 0; i < elements.length; i++) { 123 JavaFoldingStructureProviderDescriptor desc = new JavaFoldingStructureProviderDescriptor( 124 elements[i]); 125 map.put(desc.getId(), desc); 126 } 127 128 synchronized (this) { 129 fDescriptors = Collections.unmodifiableMap(map); 130 } 131 } 132 133 } 134