KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > JUnitMigrationDelegate


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.launcher;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.Path;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate;
21 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
22
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IJavaModel;
25 import org.eclipse.jdt.core.IJavaProject;
26 import org.eclipse.jdt.core.JavaCore;
27
28 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
29
30 public class JUnitMigrationDelegate implements ILaunchConfigurationMigrationDelegate {
31
32     protected static final String JavaDoc EMPTY_STRING= ""; //$NON-NLS-1$
33

34     public JUnitMigrationDelegate() {
35
36     }
37
38     /*
39      * (non-Javadoc)
40      *
41      * @see org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate#isCandidate()
42      */

43     public boolean isCandidate(ILaunchConfiguration candidate) throws CoreException {
44         IResource[] mapped = candidate.getMappedResources();
45         IResource target = getResource(candidate);
46         if (target == null) {
47             return mapped == null;
48         } else {
49             if (mapped == null) {
50                 return true;
51             } else {
52                 if (mapped.length != 1) {
53                     return true;
54                 } else {
55                     return !target.equals(mapped[0]);
56                 }
57             }
58         }
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate#migrate(org.eclipse.debug.core.ILaunchConfiguration)
65      */

66     public void migrate(ILaunchConfiguration candidate) throws CoreException {
67         ILaunchConfigurationWorkingCopy wc= candidate.getWorkingCopy();
68         mapResources(wc);
69         wc.doSave();
70     }
71     
72     /**
73      * Maps a resource for the given launch configuration.
74      *
75      * @param config working copy
76      * @throws CoreException if an exception occurs mapping resource
77      */

78     public static void mapResources(ILaunchConfigurationWorkingCopy config) throws CoreException {
79         IResource resource = getResource(config);
80         if (resource == null) {
81             config.setMappedResources(null);
82         } else {
83             config.setMappedResources(new IResource[]{resource});
84         }
85     }
86     
87     /**
88      * Returns a resource mapping for the given launch configuration, or <code>null</code>
89      * if none.
90      *
91      * @param config working copy
92      * @returns resource or <code>null</code>
93      * @throws CoreException if an exception occurs mapping resource
94      */

95     private static IResource getResource(ILaunchConfiguration config) throws CoreException {
96         String JavaDoc projName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
97         String JavaDoc containerHandle = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, (String JavaDoc)null);
98         String JavaDoc typeName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
99         IJavaElement element = null;
100         if (projName != null && Path.ROOT.isValidSegment(projName)) {
101             IJavaProject javaProject = getJavaModel().getJavaProject(projName);
102             if (javaProject.exists()) {
103                 if (typeName != null) {
104                     element = javaProject.findType(typeName);
105                 }
106                 if (element == null) {
107                     element = javaProject;
108                 }
109             }
110         } else if (containerHandle != null) {
111             element = JavaCore.create(containerHandle);
112         }
113         IResource resource = null;
114         if (element != null) {
115             resource = element.getResource();
116         }
117         return resource;
118     }
119     
120     /*
121      * Convenience method to get access to the java model.
122      */

123     private static IJavaModel getJavaModel() {
124         return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
125     }
126
127 }
128
Popular Tags