KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtensionPoint;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21
22 import org.eclipse.jdt.core.IClasspathEntry;
23
24 import org.eclipse.jdt.ui.JavaUI;
25 import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
26
27 import org.eclipse.jdt.internal.ui.JavaPlugin;
28 import org.eclipse.jdt.internal.ui.util.CoreUtility;
29
30 /**
31   */

32 public class ClasspathContainerDescriptor {
33
34     private IConfigurationElement fConfigElement;
35     private IClasspathContainerPage fPage;
36
37     private static final String JavaDoc ATT_EXTENSION = "classpathContainerPage"; //$NON-NLS-1$
38

39     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
40
private static final String JavaDoc ATT_NAME = "name"; //$NON-NLS-1$
41
private static final String JavaDoc ATT_PAGE_CLASS = "class"; //$NON-NLS-1$
42

43     public ClasspathContainerDescriptor(IConfigurationElement configElement) throws CoreException {
44         super();
45         fConfigElement = configElement;
46         fPage= null;
47
48         String JavaDoc id = fConfigElement.getAttribute(ATT_ID);
49         String JavaDoc name = configElement.getAttribute(ATT_NAME);
50         String JavaDoc pageClassName = configElement.getAttribute(ATT_PAGE_CLASS);
51
52         if (name == null) {
53             throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, 0, "Invalid extension (missing name): " + id, null)); //$NON-NLS-1$
54
}
55         if (pageClassName == null) {
56             throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, 0, "Invalid extension (missing page class name): " + id, null)); //$NON-NLS-1$
57
}
58     }
59
60     public IClasspathContainerPage createPage() throws CoreException {
61         if (fPage == null) {
62             Object JavaDoc elem= CoreUtility.createExtension(fConfigElement, ATT_PAGE_CLASS);
63             if (elem instanceof IClasspathContainerPage) {
64                 fPage= (IClasspathContainerPage) elem;
65             } else {
66                 String JavaDoc id= fConfigElement.getAttribute(ATT_ID);
67                 throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, 0, "Invalid extension (page not of type IClasspathContainerPage): " + id, null)); //$NON-NLS-1$
68
}
69         }
70         return fPage;
71     }
72     
73     public IClasspathContainerPage getPage() {
74         return fPage;
75     }
76     
77     public void setPage(IClasspathContainerPage page) {
78         fPage= page;
79     }
80     
81     public void dispose() {
82         if (fPage != null) {
83             fPage.dispose();
84             fPage= null;
85         }
86     }
87
88     public String JavaDoc getName() {
89         return fConfigElement.getAttribute(ATT_NAME);
90     }
91     
92     public String JavaDoc getPageClass() {
93         return fConfigElement.getAttribute(ATT_PAGE_CLASS);
94     }
95
96     public boolean canEdit(IClasspathEntry entry) {
97         String JavaDoc id = fConfigElement.getAttribute(ATT_ID);
98         if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
99             String JavaDoc type = entry.getPath().segment(0);
100             return id.equals(type);
101         }
102         return false;
103     }
104
105     public static ClasspathContainerDescriptor[] getDescriptors() {
106         ArrayList JavaDoc containers= new ArrayList JavaDoc();
107         
108         IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(JavaUI.ID_PLUGIN, ATT_EXTENSION);
109         if (extensionPoint != null) {
110             ClasspathContainerDescriptor defaultPage= null;
111             String JavaDoc defaultPageName= ClasspathContainerDefaultPage.class.getName();
112             
113             IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
114             for (int i = 0; i < elements.length; i++) {
115                 try {
116                     ClasspathContainerDescriptor curr= new ClasspathContainerDescriptor(elements[i]);
117                     if (defaultPageName.equals(curr.getPageClass())) {
118                         defaultPage= curr;
119                     } else {
120                         containers.add(curr);
121                     }
122                 } catch (CoreException e) {
123                     JavaPlugin.log(e);
124                 }
125             }
126             if (defaultPageName != null && containers.isEmpty()) {
127                 // default page only added of no other extensions found
128
containers.add(defaultPage);
129             }
130         }
131         return (ClasspathContainerDescriptor[]) containers.toArray(new ClasspathContainerDescriptor[containers.size()]);
132     }
133
134 }
135
Popular Tags