KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > refactoring > LaunchConfigurationBuildfileChange


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.ant.internal.ui.refactoring;
12
13 import java.io.File JavaDoc;
14 import com.ibm.icu.text.MessageFormat;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.ant.core.AntCorePlugin;
19 import org.eclipse.ant.internal.ui.AntUtil;
20 import org.eclipse.ant.internal.ui.launchConfigurations.IAntLaunchConfigurationConstants;
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IWorkspace;
26 import org.eclipse.core.resources.IWorkspaceRoot;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.OperationCanceledException;
32 import org.eclipse.core.runtime.content.IContentDescription;
33 import org.eclipse.core.runtime.content.IContentType;
34 import org.eclipse.core.variables.IStringVariableManager;
35 import org.eclipse.core.variables.VariablesPlugin;
36 import org.eclipse.debug.core.DebugPlugin;
37 import org.eclipse.debug.core.ILaunchConfiguration;
38 import org.eclipse.debug.core.ILaunchConfigurationType;
39 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
40 import org.eclipse.debug.core.ILaunchManager;
41 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
42 import org.eclipse.ltk.core.refactoring.Change;
43 import org.eclipse.ltk.core.refactoring.CompositeChange;
44 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
45 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
46
47 public class LaunchConfigurationBuildfileChange extends Change {
48     
49     private ILaunchConfiguration fLaunchConfiguration;
50     private String JavaDoc fNewBuildfileLocation;
51     private String JavaDoc fNewProjectName;
52     private String JavaDoc fNewLaunchConfigurationName;
53     private String JavaDoc fOldBuildfileLocation;
54     private String JavaDoc fOldProjectName;
55     private ILaunchConfigurationWorkingCopy fNewLaunchConfiguration;
56     private String JavaDoc fNewConfigContainerName;
57
58     /**
59      * Create a change for each launch configuration which needs to be updated for this IJavaProject rename.
60      */

61     public static Change createChangesForProjectRename(IProject project, String JavaDoc newProjectName) throws CoreException {
62         String JavaDoc projectName= project.getDescription().getName();
63         ILaunchConfiguration[] configs = getAntLaunchConfigurations();
64         List JavaDoc changes= createChangesForProjectRename(project, configs, projectName, newProjectName);
65         return createChangeFromList(changes, RefactoringMessages.LaunchConfigurationBuildfileChange_7);
66     }
67     
68     /**
69      * Create a change for each launch configuration which needs to be updated for this buildfile rename.
70      */

71     public static Change createChangesForBuildfileRename(IFile file, String JavaDoc newBuildfileName) throws CoreException {
72         IContentDescription description= null;
73         try {
74             description= file.getContentDescription();
75         } catch (CoreException e) {
76             //missing file
77
return null;
78         }
79         if (description == null) {
80             return null;
81         }
82         IContentType contentType= description.getContentType();
83         if (contentType == null || !AntCorePlugin.ANT_BUILDFILE_CONTENT_TYPE.equals(contentType.getId())) {
84             return null; //not an Ant buildfile
85
}
86         ILaunchConfiguration[] configs = getAntLaunchConfigurations();
87         List JavaDoc changes= createChangesForBuildfileRename(file, configs, file.getProject().getName(), newBuildfileName);
88         return createChangeFromList(changes, RefactoringMessages.LaunchConfigurationBuildfileChange_7);
89     }
90
91     private static ILaunchConfiguration[] getAntLaunchConfigurations() throws CoreException {
92         ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager();
93         // Ant launch configurations
94
ILaunchConfigurationType configurationType= manager.getLaunchConfigurationType(IAntLaunchConfigurationConstants.ID_ANT_LAUNCH_CONFIGURATION_TYPE);
95         ILaunchConfiguration[] configs= manager.getLaunchConfigurations(configurationType);
96         return configs;
97     }
98     
99     /**
100      * Take a list of Changes, and return a unique Change, a CompositeChange, or null.
101      */

102     private static Change createChangeFromList(List JavaDoc changes, String JavaDoc changeLabel) {
103         int nbChanges= changes.size();
104         if (nbChanges == 0) {
105             return null;
106         } else if (nbChanges == 1) {
107             return (Change) changes.get(0);
108         } else {
109             return new CompositeChange(changeLabel, (Change[])changes.toArray(new Change[changes.size()]));
110         }
111     }
112         
113     /**
114      * Create a change for each launch configuration from the given list which needs
115      * to be updated for this IProject rename.
116      */

117     private static List JavaDoc createChangesForProjectRename(IProject project, ILaunchConfiguration[] configs, String JavaDoc projectName, String JavaDoc newProjectName) throws CoreException {
118         List JavaDoc changes= new ArrayList JavaDoc();
119         for (int i= 0; i < configs.length; i++) {
120             ILaunchConfiguration launchConfiguration = configs[i];
121             String JavaDoc launchConfigurationProjectName= launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
122             if (projectName.equals(launchConfigurationProjectName)) {
123                 LaunchConfigurationBuildfileChange change = new LaunchConfigurationBuildfileChange(launchConfiguration, null, null, newProjectName, false);
124                 String JavaDoc newContainerName = computeNewContainerName(project, launchConfiguration);
125                 if (newContainerName != null) {
126                     change.setNewContainerName(newContainerName);
127                 }
128
129                 changes.add(change);
130             }
131         }
132         return changes;
133     }
134     
135     /**
136      * Create a change for each launch configuration from the given list which needs
137      * to be updated for this buildfile rename.
138      */

139     private static List JavaDoc createChangesForBuildfileRename(IFile buildfile, ILaunchConfiguration[] configs, String JavaDoc projectName, String JavaDoc newBuildfileName) throws CoreException {
140         List JavaDoc changes= new ArrayList JavaDoc();
141         for (int i= 0; i < configs.length; i++) {
142             ILaunchConfiguration launchConfiguration = configs[i];
143             String JavaDoc launchConfigurationProjectName= launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
144             if (projectName.equals(launchConfigurationProjectName)) {
145                 LaunchConfigurationBuildfileChange change = new LaunchConfigurationBuildfileChange(launchConfiguration, buildfile.getName(), newBuildfileName, null, false);
146                 changes.add(change);
147             }
148         }
149         return changes;
150     }
151
152     protected void setNewContainerName(String JavaDoc newContainerName) {
153         fNewConfigContainerName = newContainerName;
154     }
155
156     private static String JavaDoc computeNewContainerName(IProject project, ILaunchConfiguration launchConfiguration) {
157         IPath currentLocation = launchConfiguration.getLocation();
158         IPath projectLocation = project.getLocation();
159         if (projectLocation.isPrefixOf(currentLocation)) {
160             String JavaDoc projectFile = new File JavaDoc(projectLocation.toOSString()).getAbsolutePath();
161             String JavaDoc configDir = new File JavaDoc(currentLocation.toOSString()).getParent();
162             return new String JavaDoc(configDir.substring(projectFile.length()));
163         }
164         return null;
165     }
166     
167     protected LaunchConfigurationBuildfileChange(ILaunchConfiguration launchConfiguration, String JavaDoc oldBuildFileName, String JavaDoc newBuildfileName, String JavaDoc newProjectName, boolean undo) throws CoreException {
168         fLaunchConfiguration= launchConfiguration;
169         fNewLaunchConfiguration= launchConfiguration.getWorkingCopy();
170         fNewBuildfileLocation= newBuildfileName;
171         fNewProjectName= newProjectName;
172         fOldBuildfileLocation= oldBuildFileName;
173         fOldProjectName= fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc) null);
174         if (fNewBuildfileLocation != null) {
175             // generate the new configuration name
176
String JavaDoc launchConfigurationName= fLaunchConfiguration.getName();
177             fNewLaunchConfigurationName= launchConfigurationName.replaceAll(oldBuildFileName, newBuildfileName);
178             if (launchConfigurationName.equals(fNewLaunchConfigurationName) || (!undo && DebugPlugin.getDefault().getLaunchManager().isExistingLaunchConfigurationName(fNewLaunchConfigurationName))) {
179                 fNewLaunchConfigurationName= null;
180             }
181         }
182     }
183
184     /* (non-Javadoc)
185      * @see org.eclipse.ltk.core.refactoring.Change#getName()
186      */

187     public String JavaDoc getName() {
188         if (fNewLaunchConfigurationName != null) {
189             return MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_0, new String JavaDoc[] {fLaunchConfiguration.getName(), fNewLaunchConfigurationName});
190         }
191         if (fNewProjectName == null) {
192             return MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_1, new String JavaDoc[] {fLaunchConfiguration.getName()});
193         }
194         return MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_2, new String JavaDoc[] {fLaunchConfiguration.getName()});
195     }
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
199      */

200     public void initializeValidationData(IProgressMonitor pm) {
201     }
202     
203     /* (non-Javadoc)
204      * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
205      */

206     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException {
207         if (fLaunchConfiguration.exists()) {
208             String JavaDoc buildFileLocation= fLaunchConfiguration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); //$NON-NLS-1$
209
if (fOldBuildfileLocation == null || (buildFileLocation.endsWith(fOldBuildfileLocation + '}') || buildFileLocation.endsWith(fOldBuildfileLocation))) {
210                 String JavaDoc projectName= fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
211                 if (fOldProjectName.equals(projectName)) {
212                     return new RefactoringStatus();
213                 }
214                 return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_4, new String JavaDoc[] {fLaunchConfiguration.getName(), fOldProjectName}));
215             }
216             return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_5, new String JavaDoc[] {fLaunchConfiguration.getName(), fOldBuildfileLocation}));
217         }
218         return RefactoringStatus.createFatalErrorStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_6, new String JavaDoc[] {fLaunchConfiguration.getName()}));
219     }
220     
221     /* (non-Javadoc)
222      * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
223      */

224     public Change perform(IProgressMonitor pm) throws CoreException {
225         if (fNewConfigContainerName != null) {
226             IWorkspace workspace = ResourcesPlugin.getWorkspace();
227             IWorkspaceRoot root = workspace.getRoot();
228             IProject project = root.getProject(fNewProjectName);
229             IContainer container = (IContainer) project.findMember(fNewConfigContainerName);
230             fNewLaunchConfiguration.setContainer(container);
231         }
232
233         String JavaDoc oldBuildfileLocation= fNewLaunchConfiguration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); //$NON-NLS-1$
234
String JavaDoc oldProjectName;
235         if (fNewBuildfileLocation != null) {
236             String JavaDoc newBuildFileLocation= oldBuildfileLocation.replaceFirst(fOldBuildfileLocation, fNewBuildfileLocation);
237             fNewLaunchConfiguration.setAttribute(IExternalToolConstants.ATTR_LOCATION, newBuildFileLocation);
238             fNewLaunchConfiguration.setMappedResources(new IResource[] {getAssociatedFile(newBuildFileLocation)});
239         }
240         if (fNewProjectName != null) {
241             oldProjectName= fOldProjectName;
242             fNewLaunchConfiguration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fNewProjectName);
243             String JavaDoc newBuildFileLocation= oldBuildfileLocation.replaceFirst(oldProjectName, fNewProjectName);
244             fNewLaunchConfiguration.setAttribute(IExternalToolConstants.ATTR_LOCATION, newBuildFileLocation);
245             fNewLaunchConfiguration.setMappedResources(new IResource[] {getAssociatedFile(newBuildFileLocation)});
246             String JavaDoc launchConfigurationName= fLaunchConfiguration.getName();
247             fNewLaunchConfigurationName= launchConfigurationName.replaceFirst(oldProjectName, fNewProjectName);
248             if (launchConfigurationName.equals(fNewLaunchConfigurationName) || DebugPlugin.getDefault().getLaunchManager().isExistingLaunchConfigurationName(fNewLaunchConfigurationName)) {
249                 fNewLaunchConfigurationName= null;
250             }
251         } else {
252             oldProjectName= null;
253         }
254         if (fNewLaunchConfigurationName != null) {
255             fNewLaunchConfiguration.rename(fNewLaunchConfigurationName);
256         }
257
258         fNewLaunchConfiguration.doSave();
259
260         // create the undo change
261
return new LaunchConfigurationBuildfileChange(fNewLaunchConfiguration, fNewBuildfileLocation, fOldBuildfileLocation, oldProjectName, true);
262     }
263     
264     /* (non-Javadoc)
265      * @see org.eclipse.ltk.core.refactoring.Change#getModifiedElement()
266      */

267     public Object JavaDoc getModifiedElement() {
268         return fLaunchConfiguration;
269     }
270     
271     private IFile getAssociatedFile(String JavaDoc location) {
272         IFile file= null;
273         IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
274         try {
275             String JavaDoc expandedLocation= manager.performStringSubstitution(location);
276             if (expandedLocation != null) {
277                 file= AntUtil.getFileForLocation(expandedLocation, null);
278             }
279         } catch (CoreException e) {
280         }
281         return file;
282     }
283 }
Popular Tags