001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths;
011:
012: import java.util.HashMap;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.ISafeRunnable;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.core.runtime.SafeRunner;
020: import org.eclipse.core.runtime.Status;
021:
022: import org.eclipse.jdt.ui.JavaUI;
023: import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration;
024:
025: import org.eclipse.jdt.internal.ui.JavaPlugin;
026: import org.eclipse.jdt.internal.ui.util.CoreUtility;
027:
028: public class ClasspathAttributeConfigurationDescriptors {
029:
030: private static class Descriptor {
031:
032: private IConfigurationElement fConfigElement;
033: private ClasspathAttributeConfiguration fInstance;
034:
035: private static final String ATT_NAME = "attributeName"; //$NON-NLS-1$
036: private static final String ATT_CLASS = "class"; //$NON-NLS-1$
037:
038: public Descriptor(IConfigurationElement configElement)
039: throws CoreException {
040: fConfigElement = configElement;
041: fInstance = null;
042:
043: String name = configElement.getAttribute(ATT_NAME);
044: String pageClassName = configElement
045: .getAttribute(ATT_CLASS);
046:
047: if (name == null) {
048: throw new CoreException(
049: new Status(
050: IStatus.ERROR,
051: JavaUI.ID_PLUGIN,
052: 0,
053: "Invalid extension (missing attributeName)", null)); //$NON-NLS-1$
054: }
055: if (pageClassName == null) {
056: throw new CoreException(
057: new Status(
058: IStatus.ERROR,
059: JavaUI.ID_PLUGIN,
060: 0,
061: "Invalid extension (missing class name): " + name, null)); //$NON-NLS-1$
062: }
063: }
064:
065: public ClasspathAttributeConfiguration getInstance()
066: throws CoreException {
067: if (fInstance == null) {
068: Object elem = CoreUtility.createExtension(
069: fConfigElement, ATT_CLASS);
070: if (elem instanceof ClasspathAttributeConfiguration) {
071: fInstance = (ClasspathAttributeConfiguration) elem;
072: } else {
073: throw new CoreException(
074: new Status(
075: IStatus.ERROR,
076: JavaUI.ID_PLUGIN,
077: 0,
078: "Invalid extension (page not of type IClasspathContainerPage): " + getKey(), null)); //$NON-NLS-1$
079: }
080: }
081: return fInstance;
082: }
083:
084: public String getKey() {
085: return fConfigElement.getAttribute(ATT_NAME);
086: }
087: }
088:
089: private static final String ATT_EXTENSION = "classpathAttributeConfiguration"; //$NON-NLS-1$
090:
091: private HashMap fDescriptors;
092:
093: public ClasspathAttributeConfigurationDescriptors() {
094: fDescriptors = null;
095: }
096:
097: private HashMap getDescriptors() {
098: if (fDescriptors == null) {
099: fDescriptors = readExtensions();
100: }
101: return fDescriptors;
102: }
103:
104: public boolean containsKey(String attributeKey) {
105: return getDescriptors().containsKey(attributeKey);
106: }
107:
108: public ClasspathAttributeConfiguration get(final String attributeKey) {
109: final Descriptor desc = (Descriptor) getDescriptors().get(
110: attributeKey);
111: if (desc == null) {
112: return null;
113: }
114: final ClasspathAttributeConfiguration[] res = { null };
115: SafeRunner.run(new ISafeRunnable() {
116:
117: public void handleException(Throwable exception) {
118: JavaPlugin.log(exception);
119: getDescriptors().remove(attributeKey); // remove from list
120: }
121:
122: public void run() throws Exception {
123: res[0] = desc.getInstance();
124: }
125: });
126: return res[0];
127: }
128:
129: private static HashMap readExtensions() {
130: IConfigurationElement[] elements = Platform
131: .getExtensionRegistry().getConfigurationElementsFor(
132: JavaUI.ID_PLUGIN, ATT_EXTENSION);
133: HashMap descriptors = new HashMap(elements.length * 2);
134: for (int i = 0; i < elements.length; i++) {
135: try {
136: Descriptor curr = new Descriptor(elements[i]);
137: descriptors.put(curr.getKey(), curr);
138: } catch (CoreException e) {
139: JavaPlugin.log(e);
140: }
141: }
142: return descriptors;
143: }
144:
145: }
|