1 11 package org.eclipse.ltk.internal.core.refactoring.history; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IRegistryChangeEvent; 20 import org.eclipse.core.runtime.IRegistryChangeListener; 21 import org.eclipse.core.runtime.Platform; 22 23 import org.eclipse.ltk.core.refactoring.RefactoringContribution; 24 import org.eclipse.ltk.core.refactoring.RefactoringCore; 25 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 26 27 import org.eclipse.ltk.internal.core.refactoring.Messages; 28 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages; 29 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin; 30 31 36 public final class RefactoringContributionManager implements IRegistryChangeListener { 37 38 39 private static final String ATTRIBUTE_CLASS= "class"; 41 42 private static final String ATTRIBUTE_ID= "id"; 44 45 private static RefactoringContributionManager fInstance= null; 46 47 48 private static final String REFACTORING_CONTRIBUTIONS_EXTENSION_POINT= "refactoringContributions"; 50 55 public static RefactoringContributionManager getInstance() { 56 if (fInstance == null) 57 fInstance= new RefactoringContributionManager(); 58 return fInstance; 59 } 60 61 65 private Map fContributionCache= null; 66 67 73 private Map fIdCache= null; 74 75 78 private RefactoringContributionManager() { 79 } 81 82 85 public void connect() { 86 Platform.getExtensionRegistry().addRegistryChangeListener(this, RefactoringCore.ID_PLUGIN); 87 } 88 89 106 public RefactoringDescriptor createDescriptor(final String id, final String project, final String description, final String comment, final Map arguments, final int flags) { 107 Assert.isNotNull(id); 108 Assert.isNotNull(description); 109 Assert.isNotNull(arguments); 110 Assert.isLegal(flags >= RefactoringDescriptor.NONE); 111 final RefactoringContribution contribution= getRefactoringContribution(id); 112 if (contribution != null) 113 return contribution.createDescriptor(id, project, description, comment, arguments, flags); 114 return new DefaultRefactoringDescriptor(id, project, description, comment, arguments, flags); 115 } 116 117 120 public void disconnect() { 121 Platform.getExtensionRegistry().removeRegistryChangeListener(this); 122 } 123 124 134 public RefactoringContribution getRefactoringContribution(final String id) { 135 Assert.isNotNull(id); 136 Assert.isTrue(!"".equals(id)); populateCache(); 138 return (RefactoringContribution) fContributionCache.get(id); 139 } 140 141 150 public String getRefactoringId(final RefactoringContribution contribution) { 151 Assert.isNotNull(contribution); 152 populateCache(); 153 return (String ) fIdCache.get(contribution); 154 } 155 156 161 private void populateCache() { 162 if (fContributionCache == null || fIdCache == null) { 163 fContributionCache= new HashMap (32); 164 fIdCache= new HashMap (32); 165 final IConfigurationElement[] elements= Platform.getExtensionRegistry().getConfigurationElementsFor(RefactoringCore.ID_PLUGIN, REFACTORING_CONTRIBUTIONS_EXTENSION_POINT); 166 for (int index= 0; index < elements.length; index++) { 167 final IConfigurationElement element= elements[index]; 168 final String attributeId= element.getAttribute(ATTRIBUTE_ID); 169 final String point= RefactoringCore.ID_PLUGIN + "." + REFACTORING_CONTRIBUTIONS_EXTENSION_POINT; if (attributeId != null && !"".equals(attributeId)) { final String className= element.getAttribute(ATTRIBUTE_CLASS); 172 if (className != null && !"".equals(className)) { try { 174 final Object implementation= element.createExecutableExtension(ATTRIBUTE_CLASS); 175 if (implementation instanceof RefactoringContribution) { 176 if (fContributionCache.get(attributeId) != null) 177 RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_duplicate_warning, new String [] { attributeId, point})); 178 fContributionCache.put(attributeId, implementation); 179 fIdCache.put(implementation, attributeId); 180 } else 181 RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_creation_error, new String [] { point, attributeId})); 182 } catch (CoreException exception) { 183 RefactoringCorePlugin.log(exception); 184 } 185 } else 186 RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_missing_class_attribute, new String [] { point, attributeId, ATTRIBUTE_CLASS})); 187 } else 188 RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_missing_attribute, new String [] { point, ATTRIBUTE_ID})); 189 } 190 } 191 } 192 193 196 public void registryChanged(final IRegistryChangeEvent event) { 197 fContributionCache= null; 198 fIdCache= null; 199 } 200 } 201 | Popular Tags |