1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.util.HashMap ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.ISafeRunnable; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.core.runtime.SafeRunner; 21 import org.eclipse.core.runtime.Status; 22 23 import org.eclipse.jdt.ui.JavaUI; 24 import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration; 25 26 import org.eclipse.jdt.internal.ui.JavaPlugin; 27 import org.eclipse.jdt.internal.ui.util.CoreUtility; 28 29 public class ClasspathAttributeConfigurationDescriptors { 30 31 private static class Descriptor { 32 33 private IConfigurationElement fConfigElement; 34 private ClasspathAttributeConfiguration fInstance; 35 36 private static final String ATT_NAME = "attributeName"; private static final String ATT_CLASS = "class"; 39 public Descriptor(IConfigurationElement configElement) throws CoreException { 40 fConfigElement = configElement; 41 fInstance= null; 42 43 String name = configElement.getAttribute(ATT_NAME); 44 String pageClassName = configElement.getAttribute(ATT_CLASS); 45 46 if (name == null) { 47 throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, 0, "Invalid extension (missing attributeName)", null)); } 49 if (pageClassName == null) { 50 throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, 0, "Invalid extension (missing class name): " + name, null)); } 52 } 53 54 public ClasspathAttributeConfiguration getInstance() throws CoreException { 55 if (fInstance == null) { 56 Object elem= CoreUtility.createExtension(fConfigElement, ATT_CLASS); 57 if (elem instanceof ClasspathAttributeConfiguration) { 58 fInstance= (ClasspathAttributeConfiguration) elem; 59 } else { 60 throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, 0, "Invalid extension (page not of type IClasspathContainerPage): " + getKey(), null)); } 62 } 63 return fInstance; 64 } 65 66 public String getKey() { 67 return fConfigElement.getAttribute(ATT_NAME); 68 } 69 } 70 71 private static final String ATT_EXTENSION = "classpathAttributeConfiguration"; 73 private HashMap fDescriptors; 74 75 public ClasspathAttributeConfigurationDescriptors() { 76 fDescriptors= null; 77 } 78 79 private HashMap getDescriptors() { 80 if (fDescriptors == null) { 81 fDescriptors= readExtensions(); 82 } 83 return fDescriptors; 84 } 85 86 public boolean containsKey(String attributeKey) { 87 return getDescriptors().containsKey(attributeKey); 88 } 89 90 public ClasspathAttributeConfiguration get(final String attributeKey) { 91 final Descriptor desc= (Descriptor) getDescriptors().get(attributeKey); 92 if (desc == null) { 93 return null; 94 } 95 final ClasspathAttributeConfiguration[] res= { null }; 96 SafeRunner.run(new ISafeRunnable() { 97 98 public void handleException(Throwable exception) { 99 JavaPlugin.log(exception); 100 getDescriptors().remove(attributeKey); } 102 103 public void run() throws Exception { 104 res[0]= desc.getInstance(); 105 } 106 }); 107 return res[0]; 108 } 109 110 private static HashMap readExtensions() { 111 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(JavaUI.ID_PLUGIN, ATT_EXTENSION); 112 HashMap descriptors= new HashMap (elements.length * 2); 113 for (int i= 0; i < elements.length; i++) { 114 try { 115 Descriptor curr= new Descriptor(elements[i]); 116 descriptors.put(curr.getKey(), curr); 117 } catch (CoreException e) { 118 JavaPlugin.log(e); 119 } 120 } 121 return descriptors; 122 } 123 124 } 125 | Popular Tags |