KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > refactoring > LaunchConfigurationProjectMainTypeChange


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.debug.core.refactoring;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IWorkspace;
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
24 import org.eclipse.jdt.core.Signature;
25 import org.eclipse.jdt.internal.launching.JavaMigrationDelegate;
26 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
27 import org.eclipse.ltk.core.refactoring.Change;
28 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
29
30 import com.ibm.icu.text.MessageFormat;
31
32 /**
33  * The change for the main type project change of a launch configuration
34  */

35 public class LaunchConfigurationProjectMainTypeChange extends Change {
36     
37     private ILaunchConfiguration fLaunchConfiguration;
38     private String JavaDoc fNewMainTypeName;
39     private String JavaDoc fNewProjectName;
40     private String JavaDoc fNewLaunchConfigurationName;
41     private String JavaDoc fOldMainTypeName;
42     private String JavaDoc fOldProjectName;
43     private String JavaDoc fNewConfigContainerName;
44     
45     /**
46      * LaunchConfigurationProjectMainTypeChange constructor.
47      * @param launchConfiguration the launch configuration to modify
48      * @param newMainTypeName the name of the new main type, or <code>null</code> if not modified.
49      * @param newProjectName the name of the project, or <code>null</code> if not modified.
50      */

51     public LaunchConfigurationProjectMainTypeChange(ILaunchConfiguration launchConfiguration, String JavaDoc newMainTypeName, String JavaDoc newProjectName) throws CoreException {
52         fLaunchConfiguration = launchConfiguration;
53         fNewMainTypeName = newMainTypeName;
54         fNewProjectName = newProjectName;
55         fOldMainTypeName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc) null);
56         fOldProjectName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc) null);
57         if (fNewMainTypeName != null) {
58             // generate the new configuration name
59
String JavaDoc oldName = Signature.getSimpleName(fOldMainTypeName);
60             String JavaDoc newName = Signature.getSimpleName(fNewMainTypeName);
61             String JavaDoc lcname = fLaunchConfiguration.getName();
62             fNewLaunchConfigurationName = lcname.replaceAll(oldName, newName);
63             if (lcname.equals(fNewLaunchConfigurationName) || DebugPlugin.getDefault().getLaunchManager().isExistingLaunchConfigurationName(fNewLaunchConfigurationName)) {
64                 fNewLaunchConfigurationName = null;
65             }
66         }
67     }
68
69     /* (non-Javadoc)
70      * @see org.eclipse.ltk.core.refactoring.Change#getModifiedElement()
71      */

72     public Object JavaDoc getModifiedElement() {
73         return fLaunchConfiguration;
74     }
75     
76     /* (non-Javadoc)
77      * @see org.eclipse.ltk.core.refactoring.Change#getName()
78      */

79     public String JavaDoc getName() {
80         if (fNewLaunchConfigurationName != null) {
81             return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_0, new String JavaDoc[] {fLaunchConfiguration.getName(), fNewLaunchConfigurationName});
82         }
83         if (fNewProjectName == null) {
84             return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_1, new String JavaDoc[] {fLaunchConfiguration.getName()});
85         }
86         if (fNewMainTypeName == null) {
87             return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_2, new String JavaDoc[] {fLaunchConfiguration.getName()});
88         }
89         return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_3, new String JavaDoc[] {fLaunchConfiguration.getName()});
90     }
91     
92     /* (non-Javadoc)
93      * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
94      */

95     public void initializeValidationData(IProgressMonitor pm) {}
96     
97     /* (non-Javadoc)
98      * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
99      */

100     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException {
101         if (fLaunchConfiguration.exists()) {
102             String JavaDoc typeName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
103             String JavaDoc projectName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
104             if(fOldMainTypeName != null) {
105                 if (fOldMainTypeName.equals(typeName)) {
106                     if (fOldProjectName.equals(projectName)) {
107                         return new RefactoringStatus();
108                     }
109                     return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_4, new String JavaDoc[] {fLaunchConfiguration.getName(), fOldProjectName}));
110                 }
111                 return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_5, new String JavaDoc[] {fLaunchConfiguration.getName(), fOldMainTypeName}));
112             }
113             else {
114                 //need to catch the case for remote java LC's, they have no maintype
115
if (fOldProjectName.equals(projectName)) {
116                     return new RefactoringStatus();
117                 }
118                 return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_4, new String JavaDoc[] {fLaunchConfiguration.getName(), fOldProjectName}));
119             }
120         }
121         return RefactoringStatus.createFatalErrorStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_6, new String JavaDoc[] {fLaunchConfiguration.getName()}));
122     }
123     
124     /* (non-Javadoc)
125      * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
126      */

127     public Change perform(IProgressMonitor pm) throws CoreException {
128         final ILaunchConfigurationWorkingCopy wc = fLaunchConfiguration.getWorkingCopy();
129         if (fNewConfigContainerName != null) {
130             IWorkspace workspace = ResourcesPlugin.getWorkspace();
131             IWorkspaceRoot root = workspace.getRoot();
132             IProject project = root.getProject(fNewProjectName);
133             IContainer container = (IContainer) project.findMember(fNewConfigContainerName);
134             wc.setContainer(container);
135         }
136         String JavaDoc oldMainTypeName;
137         String JavaDoc oldProjectName;
138         if (fNewMainTypeName != null) {
139             oldMainTypeName = fOldMainTypeName;
140             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fNewMainTypeName);
141         }
142         else {
143             oldMainTypeName = null;
144         }
145         if (fNewProjectName != null) {
146             oldProjectName = fOldProjectName;
147             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fNewProjectName);
148             //CONTEXTLAUNCHING
149
JavaMigrationDelegate.updateResourceMapping(wc);
150         }
151         else {
152             oldProjectName = null;
153         }
154         if (fNewLaunchConfigurationName != null) {
155             wc.rename(fNewLaunchConfigurationName);
156         }
157         if (wc.isDirty()) {
158             fLaunchConfiguration = wc.doSave();
159         }
160         // create the undo change
161
return new LaunchConfigurationProjectMainTypeChange(wc, oldMainTypeName, oldProjectName);
162     }
163     
164     /**
165      * Sets the new container name
166      * @param newContainerName the new name for the container
167      */

168     public void setNewContainerName(String JavaDoc newContainerName) {
169         fNewConfigContainerName = newContainerName;
170     }
171 }
172
Popular Tags