KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > refactoring > ProjectRenameParticipant


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.refactoring;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.core.resources.IProject;
16
17 import org.eclipse.jdt.core.IJavaElement;
18 import org.eclipse.jdt.core.IJavaProject;
19 import org.eclipse.jdt.core.IPackageFragment;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.core.JavaCore;
22
23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
24
25 import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants;
26
27 public class ProjectRenameParticipant extends JUnitRenameParticipant {
28
29     private IJavaProject fProject;
30
31     private IJavaProject getNewJavaProject() {
32         IProject project= fProject.getProject().getWorkspace().getRoot().getProject(getNewName());
33         return getJavaProject(project);
34     }
35
36     protected IJavaProject getJavaProject(IProject project) {
37         return JavaCore.create(project);
38     }
39
40     protected boolean initialize(Object JavaDoc element) {
41         fProject= (IJavaProject) element;
42         return true;
43     }
44
45     public void createChangeForConfig(JUnitRenameParticipant.ChangeList changeList, LaunchConfigurationContainer config) throws CoreException {
46
47         changeList.addAttributeChangeIfNeeded(config, IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProject.getElementName(), getNewName());
48
49         IJavaProject newJavaProject= getNewJavaProject();
50
51         String JavaDoc container= config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, (String JavaDoc) null);
52         IJavaElement javaElement= getJavaElement(container);
53         if (javaElement == null)
54             return;
55         IJavaElement potentialMatch= javaElement;
56
57         // TODO: spike!
58
while (!potentialMatch.getHandleIdentifier().equals(fProject.getHandleIdentifier())) {
59             potentialMatch= potentialMatch.getParent();
60             if (potentialMatch == null)
61                 return;
62         }
63
64         String JavaDoc newHandle;
65         if (javaElement instanceof IPackageFragment) {
66             IPackageFragment fragment= (IPackageFragment) javaElement;
67             newHandle= newJavaProject.getPackageFragmentRoot(fragment.getParent().getElementName()).getPackageFragment(fragment.getElementName()).getHandleIdentifier();
68         } else if (javaElement instanceof IPackageFragmentRoot) {
69             IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement;
70             newHandle= newJavaProject.getPackageFragmentRoot(root.getElementName()).getHandleIdentifier();
71         } else if (javaElement instanceof IJavaProject) {
72             newHandle= newJavaProject.getHandleIdentifier();
73         } else {
74             // shouldn't happen, but if it does, we silently fail.
75
return;
76         }
77         changeList.addAttributeChange(config, JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, newHandle);
78
79         // rename change must come at the end
80
changeList.addRenameChangeIfNeeded(config, fProject.getElementName());
81     }
82
83     protected IJavaElement getJavaElement(String JavaDoc handle) {
84         return JavaCore.create(handle);
85     }
86 }
87
Popular Tags