1 11 package org.eclipse.jdt.internal.junit.buildpath; 12 13 import java.util.ArrayList ; 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 JUNIT_CONTAINER_ID= "org.eclipse.jdt.junit.JUNIT_CONTAINER"; 40 private static final IStatus NOT_SUPPORTED= new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED, new String (), null); 41 private static final IStatus READ_ONLY= new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY, new String (), null); 42 43 46 private final static String JUNIT3_8_1= "3.8.1"; private final static String JUNIT3= "3"; private final static String JUNIT4= "4"; 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 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 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 122 public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) { 123 return true; 124 } 125 126 129 public IStatus getAccessRulesStatus(IPath containerPath, IJavaProject project) { 130 return NOT_SUPPORTED; 131 } 132 133 136 public IStatus getSourceAttachmentStatus(IPath containerPath, IJavaProject project) { 137 return READ_ONLY; 138 } 139 140 143 public IStatus getAttributeStatus(IPath containerPath, IJavaProject project, String attributeKey) { 144 if (attributeKey.equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) { 145 return Status.OK_STATUS; 146 } 147 return NOT_SUPPORTED; 148 } 149 150 151 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 version= containerPath.segment(1); 158 159 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 affectedProjects= new ArrayList (); 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 205 public String getDescription(IPath containerPath, IJavaProject project) { 206 if (isValidJUnitContainerPath(containerPath)) { 207 String 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 220 public Object getComparisonID(IPath containerPath, IJavaProject project) { 221 return containerPath; 222 } 223 224 } 225 | Popular Tags |