KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > buildpath > JUnitContainerInitializer


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.junit.buildpath;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Status;
20
21 import org.eclipse.jface.preference.IPreferenceStore;
22
23 import org.eclipse.jdt.core.ClasspathContainerInitializer;
24 import org.eclipse.jdt.core.IClasspathAttribute;
25 import org.eclipse.jdt.core.IClasspathContainer;
26 import org.eclipse.jdt.core.IClasspathEntry;
27 import org.eclipse.jdt.core.IJavaModel;
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.JavaCore;
30 import org.eclipse.jdt.core.JavaModelException;
31
32 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
33 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
34 import org.eclipse.jdt.internal.junit.ui.JUnitPreferencesConstants;
35
36 public class JUnitContainerInitializer extends ClasspathContainerInitializer {
37     
38     public static final String JavaDoc JUNIT_CONTAINER_ID= "org.eclipse.jdt.junit.JUNIT_CONTAINER"; //$NON-NLS-1$
39

40     private static final IStatus NOT_SUPPORTED= new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED, new String JavaDoc(), null);
41     private static final IStatus READ_ONLY= new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY, new String JavaDoc(), null);
42
43     /**
44      * @deprecated just for compatibility
45      */

46     private final static String JavaDoc JUNIT3_8_1= "3.8.1"; //$NON-NLS-1$
47
private final static String JavaDoc JUNIT3= "3"; //$NON-NLS-1$
48
private final static String JavaDoc JUNIT4= "4"; //$NON-NLS-1$
49

50     public final static IPath JUNIT3_PATH= new Path(JUNIT_CONTAINER_ID).append(JUNIT3);
51     public final static IPath JUNIT4_PATH= new Path(JUNIT_CONTAINER_ID).append(JUNIT4);
52     
53     private static class JUnitContainer implements IClasspathContainer {
54         
55         private final IClasspathEntry[] fEntries;
56         private final IPath fPath;
57
58         public JUnitContainer(IPath path, IClasspathEntry[] entries) {
59             fPath= path;
60             fEntries= entries;
61         }
62
63         public IClasspathEntry[] getClasspathEntries() {
64             return fEntries;
65         }
66
67         public String JavaDoc getDescription() {
68             if (JUNIT4_PATH.equals(fPath)) {
69                 return JUnitMessages.JUnitContainerInitializer_description_junit4;
70             }
71             return JUnitMessages.JUnitContainerInitializer_description_junit3;
72         }
73
74         public int getKind() {
75             return IClasspathContainer.K_APPLICATION;
76         }
77
78         public IPath getPath() {
79             return fPath;
80         }
81         
82     }
83     
84     
85     public JUnitContainerInitializer() {
86     }
87
88     public void initialize(IPath containerPath, IJavaProject project) throws CoreException {
89         if (isValidJUnitContainerPath(containerPath)) {
90             JUnitContainer container= getNewContainer(containerPath);
91             JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project }, new IClasspathContainer[] { container }, null);
92         }
93
94     }
95     
96     private static JUnitContainer getNewContainer(IPath containerPath) {
97         IClasspathEntry entry= null;
98         String JavaDoc version= containerPath.segment(1);
99         if (JUNIT3_8_1.equals(version) || JUNIT3.equals(version)) {
100             entry= BuildPathSupport.getJUnit3LibraryEntry();
101         } else if (JUNIT4.equals(version)) {
102             entry= BuildPathSupport.getJUnit4LibraryEntry();
103         }
104         
105         IClasspathEntry[] entries;
106         if (entry != null) {
107             entries= new IClasspathEntry[] { entry };
108         } else {
109             entries= new IClasspathEntry[] { };
110         }
111         return new JUnitContainer(containerPath, entries);
112     }
113     
114     
115     private static boolean isValidJUnitContainerPath(IPath path) {
116         return path != null && path.segmentCount() == 2 && JUNIT_CONTAINER_ID.equals(path.segment(0));
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#canUpdateClasspathContainer(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
121      */

122     public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) {
123         return true;
124     }
125     
126     /* (non-Javadoc)
127      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getAccessRulesStatus(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
128      */

129     public IStatus getAccessRulesStatus(IPath containerPath, IJavaProject project) {
130         return NOT_SUPPORTED;
131     }
132     
133     /* (non-Javadoc)
134      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getSourceAttachmentStatus(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
135      */

136     public IStatus getSourceAttachmentStatus(IPath containerPath, IJavaProject project) {
137         return READ_ONLY;
138     }
139     
140     /* (non-Javadoc)
141      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getAttributeStatus(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, java.lang.String)
142      */

143     public IStatus getAttributeStatus(IPath containerPath, IJavaProject project, String JavaDoc attributeKey) {
144         if (attributeKey.equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) {
145             return Status.OK_STATUS;
146         }
147         return NOT_SUPPORTED;
148     }
149     
150
151     /* (non-Javadoc)
152      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#requestClasspathContainerUpdate(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathContainer)
153      */

154     public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException {
155         IClasspathEntry[] entries= containerSuggestion.getClasspathEntries();
156         if (entries.length == 1 && isValidJUnitContainerPath(containerPath)) {
157             String JavaDoc version= containerPath.segment(1);
158             
159             // only modifiable entry in Javadoc location
160
IClasspathAttribute[] extraAttributes= entries[0].getExtraAttributes();
161             for (int i= 0; i < extraAttributes.length; i++) {
162                 IClasspathAttribute attrib= extraAttributes[i];
163                 if (attrib.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) {
164
165                     IPreferenceStore preferenceStore= JUnitPlugin.getDefault().getPreferenceStore();
166                     if (JUNIT3.equals(version)) {
167                         preferenceStore.setValue(JUnitPreferencesConstants.JUNIT3_JAVADOC, attrib.getValue());
168                     } else if (JUNIT4.equals(version)) {
169                         preferenceStore.setValue(JUnitPreferencesConstants.JUNIT4_JAVADOC, attrib.getValue());
170                     }
171                     break;
172                 }
173             }
174             rebindClasspathEntries(project.getJavaModel(), containerPath);
175         }
176     }
177     
178     private static void rebindClasspathEntries(IJavaModel model, IPath containerPath) throws JavaModelException {
179         ArrayList JavaDoc affectedProjects= new ArrayList JavaDoc();
180         
181         IJavaProject[] projects= model.getJavaProjects();
182         for (int i= 0; i < projects.length; i++) {
183             IJavaProject project= projects[i];
184             IClasspathEntry[] entries= project.getRawClasspath();
185             for (int k= 0; k < entries.length; k++) {
186                 IClasspathEntry curr= entries[k];
187                 if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER && containerPath.equals(curr.getPath())) {
188                     affectedProjects.add(project);
189                 }
190             }
191         }
192         if (!affectedProjects.isEmpty()) {
193             IJavaProject[] affected= (IJavaProject[]) affectedProjects.toArray(new IJavaProject[affectedProjects.size()]);
194             IClasspathContainer[] containers= new IClasspathContainer[affected.length];
195             for (int i= 0; i < containers.length; i++) {
196                 containers[i]= getNewContainer(containerPath);
197             }
198             JavaCore.setClasspathContainer(containerPath, affected, containers, null);
199         }
200     }
201
202     /**
203      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getDescription(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
204      */

205     public String JavaDoc getDescription(IPath containerPath, IJavaProject project) {
206         if (isValidJUnitContainerPath(containerPath)) {
207             String JavaDoc version= containerPath.segment(1);
208             if (JUNIT3_8_1.equals(version) || JUNIT3.equals(version)) {
209                 return JUnitMessages.JUnitContainerInitializer_description_initializer_junit3;
210             } else if (JUNIT4.equals(version)) {
211                 return JUnitMessages.JUnitContainerInitializer_description_initializer_junit4;
212             }
213         }
214         return JUnitMessages.JUnitContainerInitializer_description_initializer_unresolved;
215     }
216
217     /* (non-Javadoc)
218      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getComparisonID(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
219      */

220     public Object JavaDoc getComparisonID(IPath containerPath, IJavaProject project) {
221         return containerPath;
222     }
223     
224 }
225
Popular Tags