KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > ClasspathAttributeConfigurationDescriptors


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12
13 import java.util.HashMap JavaDoc;
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 JavaDoc ATT_NAME = "attributeName"; //$NON-NLS-1$
37
private static final String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
38

39         public Descriptor(IConfigurationElement configElement) throws CoreException {
40             fConfigElement = configElement;
41             fInstance= null;
42     
43             String JavaDoc name = configElement.getAttribute(ATT_NAME);
44             String JavaDoc 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)); //$NON-NLS-1$
48
}
49             if (pageClassName == null) {
50                 throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, 0, "Invalid extension (missing class name): " + name, null)); //$NON-NLS-1$
51
}
52         }
53     
54         public ClasspathAttributeConfiguration getInstance() throws CoreException {
55             if (fInstance == null) {
56                 Object JavaDoc 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)); //$NON-NLS-1$
61
}
62             }
63             return fInstance;
64         }
65         
66         public String JavaDoc getKey() {
67             return fConfigElement.getAttribute(ATT_NAME);
68         }
69     }
70     
71     private static final String JavaDoc ATT_EXTENSION = "classpathAttributeConfiguration"; //$NON-NLS-1$
72

73     private HashMap JavaDoc fDescriptors;
74     
75     public ClasspathAttributeConfigurationDescriptors() {
76         fDescriptors= null;
77     }
78     
79     private HashMap JavaDoc getDescriptors() {
80         if (fDescriptors == null) {
81             fDescriptors= readExtensions();
82         }
83         return fDescriptors;
84     }
85     
86     public boolean containsKey(String JavaDoc attributeKey) {
87         return getDescriptors().containsKey(attributeKey);
88     }
89     
90     public ClasspathAttributeConfiguration get(final String JavaDoc 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 JavaDoc exception) {
99                 JavaPlugin.log(exception);
100                 getDescriptors().remove(attributeKey); // remove from list
101
}
102
103             public void run() throws Exception JavaDoc {
104                 res[0]= desc.getInstance();
105             }
106         });
107         return res[0];
108     }
109     
110     private static HashMap JavaDoc readExtensions() {
111         IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(JavaUI.ID_PLUGIN, ATT_EXTENSION);
112         HashMap JavaDoc descriptors= new HashMap JavaDoc(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