1 11 package org.eclipse.jdt.internal.ui.text.folding; 12 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.Map ; 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 39 public class JavaFoldingStructureProviderRegistry { 40 41 private static final String EXTENSION_POINT= "foldingStructureProviders"; 43 44 private Map fDescriptors; 45 46 49 public JavaFoldingStructureProviderRegistry() { 50 } 51 52 59 public JavaFoldingStructureProviderDescriptor[] getFoldingProviderDescriptors() { 60 synchronized (this) { 61 ensureRegistered(); 62 return (JavaFoldingStructureProviderDescriptor[]) fDescriptors.values().toArray(new JavaFoldingStructureProviderDescriptor[fDescriptors.size()]); 63 } 64 } 65 66 74 public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor(String id) { 75 synchronized (this) { 76 ensureRegistered(); 77 return (JavaFoldingStructureProviderDescriptor) fDescriptors.get(id); 78 } 79 } 80 81 87 public IJavaFoldingStructureProvider getCurrentFoldingProvider() { 88 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 89 String currentProviderId= preferenceStore.getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER); 90 JavaFoldingStructureProviderDescriptor desc= getFoldingProviderDescriptor(currentProviderId); 91 92 if (desc == null) { 94 String 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 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 117 private void ensureRegistered() { 118 if (fDescriptors == null) 119 reloadExtensions(); 120 } 121 122 129 public void reloadExtensions() { 130 IExtensionRegistry registry= Platform.getExtensionRegistry(); 131 Map map= new HashMap (); 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 |