KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > JavaMigrationDelegate


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.launching;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate;
19 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.IType;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
24
25 /**
26  * Delegate for migrating Java launch configurations.
27  * The migration process involves a resource mapping being created such that launch configurations
28  * can be filtered from the launch configuration dialog based on resource availability
29  *
30  * @since 3.2
31  */

32 public class JavaMigrationDelegate implements ILaunchConfigurationMigrationDelegate {
33
34     /**
35      * represents the empty string
36      */

37     protected static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
38

39     /**
40      * Constructor needed for reflection
41      */

42     public JavaMigrationDelegate() {}
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate#isCandidate()
46      */

47     public boolean isCandidate(ILaunchConfiguration candidate) throws CoreException {
48         String JavaDoc pName = candidate.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING);
49         if(pName.equals(EMPTY_STRING)) {
50             return false;
51         }
52         if (!isAvailable(pName)) {
53             return false;
54         }
55         IResource[] mapped = candidate.getMappedResources();
56         IResource target = getResource(candidate);
57         if (target == null) {
58             return mapped != null;
59         } else {
60             if (mapped == null) {
61                 return true;
62             } else {
63                 if (mapped.length != 1) {
64                     return true;
65                 } else {
66                     return !target.equals(mapped[0]);
67                 }
68             }
69         }
70     }
71     
72     /**
73      * Returns whether the given project is available.
74      *
75      * @param projectName project name
76      * @return whether the project exists and is open
77      */

78     private boolean isAvailable(String JavaDoc projectName) {
79         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
80         return project.exists() && project.isOpen();
81     }
82
83     /**
84      * Returns the associated <code>IResource</code> for the specified launch configuration
85      * or <code>null</code> if none.
86      *
87      * @param candidate the candidate to get the backing resource for
88      * @return associated <code>IResource</code> or <code>null</code>
89      *
90      * @since 3.3
91      *
92      * @throws CoreException
93      */

94     public static IResource getResource(ILaunchConfiguration candidate) throws CoreException {
95         IResource resource = null;
96         String JavaDoc pname = candidate.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING);
97         if(!EMPTY_STRING.equals(pname)) {
98             IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(pname);
99             String JavaDoc tname = candidate.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, EMPTY_STRING);
100             if(!EMPTY_STRING.equals(tname)) {
101                 if(project != null && project.exists() && project.isOpen()) {
102                     IJavaProject jproject = JavaCore.create(project);
103                     if(jproject != null && jproject.exists()) {
104                         tname = tname.replace('$', '.');
105                         IType type = jproject.findType(tname);
106                         if(type != null) {
107                             resource = type.getUnderlyingResource();
108                             if(resource == null) {
109                                 resource = (IResource) type.getAdapter(IResource.class);
110                             }
111                         }
112                     }
113                 }
114             } else {
115                 return project;
116             }
117             if (resource == null) {
118                 resource = project;
119             }
120         }
121         return resource;
122     }
123     
124     /* (non-Javadoc)
125      * @see org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate#migrate(org.eclipse.debug.core.ILaunchConfiguration)
126      */

127     public void migrate(ILaunchConfiguration candidate) throws CoreException {
128         ILaunchConfigurationWorkingCopy wc = candidate.getWorkingCopy();
129         updateResourceMapping(wc);
130         wc.doSave();
131     }
132     
133     /**
134      * Updates the resource mapping for the given launch configuration.
135      *
136      * @param wc working copy
137      * @throws CoreException if an exception occurs updating resource mapping.
138      */

139     public static void updateResourceMapping(ILaunchConfigurationWorkingCopy wc) throws CoreException {
140         IResource resource = getResource(wc);
141         IResource[] resources = null;
142         if (resource != null) {
143             resources = new IResource[]{resource};
144         }
145         wc.setMappedResources(resources);
146     }
147
148 }
149
Popular Tags