1 11 package org.eclipse.ant.internal.ui.refactoring; 12 13 import java.io.File ; 14 import com.ibm.icu.text.MessageFormat; 15 import java.util.ArrayList ; 16 import java.util.List ; 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 fNewBuildfileLocation; 51 private String fNewProjectName; 52 private String fNewLaunchConfigurationName; 53 private String fOldBuildfileLocation; 54 private String fOldProjectName; 55 private ILaunchConfigurationWorkingCopy fNewLaunchConfiguration; 56 private String fNewConfigContainerName; 57 58 61 public static Change createChangesForProjectRename(IProject project, String newProjectName) throws CoreException { 62 String projectName= project.getDescription().getName(); 63 ILaunchConfiguration[] configs = getAntLaunchConfigurations(); 64 List changes= createChangesForProjectRename(project, configs, projectName, newProjectName); 65 return createChangeFromList(changes, RefactoringMessages.LaunchConfigurationBuildfileChange_7); 66 } 67 68 71 public static Change createChangesForBuildfileRename(IFile file, String newBuildfileName) throws CoreException { 72 IContentDescription description= null; 73 try { 74 description= file.getContentDescription(); 75 } catch (CoreException e) { 76 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; } 86 ILaunchConfiguration[] configs = getAntLaunchConfigurations(); 87 List 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 ILaunchConfigurationType configurationType= manager.getLaunchConfigurationType(IAntLaunchConfigurationConstants.ID_ANT_LAUNCH_CONFIGURATION_TYPE); 95 ILaunchConfiguration[] configs= manager.getLaunchConfigurations(configurationType); 96 return configs; 97 } 98 99 102 private static Change createChangeFromList(List changes, String 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 117 private static List createChangesForProjectRename(IProject project, ILaunchConfiguration[] configs, String projectName, String newProjectName) throws CoreException { 118 List changes= new ArrayList (); 119 for (int i= 0; i < configs.length; i++) { 120 ILaunchConfiguration launchConfiguration = configs[i]; 121 String launchConfigurationProjectName= launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )null); 122 if (projectName.equals(launchConfigurationProjectName)) { 123 LaunchConfigurationBuildfileChange change = new LaunchConfigurationBuildfileChange(launchConfiguration, null, null, newProjectName, false); 124 String 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 139 private static List createChangesForBuildfileRename(IFile buildfile, ILaunchConfiguration[] configs, String projectName, String newBuildfileName) throws CoreException { 140 List changes= new ArrayList (); 141 for (int i= 0; i < configs.length; i++) { 142 ILaunchConfiguration launchConfiguration = configs[i]; 143 String launchConfigurationProjectName= launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )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 newContainerName) { 153 fNewConfigContainerName = newContainerName; 154 } 155 156 private static String computeNewContainerName(IProject project, ILaunchConfiguration launchConfiguration) { 157 IPath currentLocation = launchConfiguration.getLocation(); 158 IPath projectLocation = project.getLocation(); 159 if (projectLocation.isPrefixOf(currentLocation)) { 160 String projectFile = new File (projectLocation.toOSString()).getAbsolutePath(); 161 String configDir = new File (currentLocation.toOSString()).getParent(); 162 return new String (configDir.substring(projectFile.length())); 163 } 164 return null; 165 } 166 167 protected LaunchConfigurationBuildfileChange(ILaunchConfiguration launchConfiguration, String oldBuildFileName, String newBuildfileName, String 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 ) null); 174 if (fNewBuildfileLocation != null) { 175 String 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 187 public String getName() { 188 if (fNewLaunchConfigurationName != null) { 189 return MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_0, new String [] {fLaunchConfiguration.getName(), fNewLaunchConfigurationName}); 190 } 191 if (fNewProjectName == null) { 192 return MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_1, new String [] {fLaunchConfiguration.getName()}); 193 } 194 return MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_2, new String [] {fLaunchConfiguration.getName()}); 195 } 196 197 200 public void initializeValidationData(IProgressMonitor pm) { 201 } 202 203 206 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { 207 if (fLaunchConfiguration.exists()) { 208 String buildFileLocation= fLaunchConfiguration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); if (fOldBuildfileLocation == null || (buildFileLocation.endsWith(fOldBuildfileLocation + '}') || buildFileLocation.endsWith(fOldBuildfileLocation))) { 210 String projectName= fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )null); 211 if (fOldProjectName.equals(projectName)) { 212 return new RefactoringStatus(); 213 } 214 return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_4, new String [] {fLaunchConfiguration.getName(), fOldProjectName})); 215 } 216 return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_5, new String [] {fLaunchConfiguration.getName(), fOldBuildfileLocation})); 217 } 218 return RefactoringStatus.createFatalErrorStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationBuildfileChange_6, new String [] {fLaunchConfiguration.getName()})); 219 } 220 221 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 oldBuildfileLocation= fNewLaunchConfiguration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); String oldProjectName; 235 if (fNewBuildfileLocation != null) { 236 String 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 newBuildFileLocation= oldBuildfileLocation.replaceFirst(oldProjectName, fNewProjectName); 244 fNewLaunchConfiguration.setAttribute(IExternalToolConstants.ATTR_LOCATION, newBuildFileLocation); 245 fNewLaunchConfiguration.setMappedResources(new IResource[] {getAssociatedFile(newBuildFileLocation)}); 246 String 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 return new LaunchConfigurationBuildfileChange(fNewLaunchConfiguration, fNewBuildfileLocation, fOldBuildfileLocation, oldProjectName, true); 262 } 263 264 267 public Object getModifiedElement() { 268 return fLaunchConfiguration; 269 } 270 271 private IFile getAssociatedFile(String location) { 272 IFile file= null; 273 IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 274 try { 275 String 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 |