1 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 35 public class LaunchConfigurationProjectMainTypeChange extends Change { 36 37 private ILaunchConfiguration fLaunchConfiguration; 38 private String fNewMainTypeName; 39 private String fNewProjectName; 40 private String fNewLaunchConfigurationName; 41 private String fOldMainTypeName; 42 private String fOldProjectName; 43 private String fNewConfigContainerName; 44 45 51 public LaunchConfigurationProjectMainTypeChange(ILaunchConfiguration launchConfiguration, String newMainTypeName, String newProjectName) throws CoreException { 52 fLaunchConfiguration = launchConfiguration; 53 fNewMainTypeName = newMainTypeName; 54 fNewProjectName = newProjectName; 55 fOldMainTypeName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String ) null); 56 fOldProjectName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String ) null); 57 if (fNewMainTypeName != null) { 58 String oldName = Signature.getSimpleName(fOldMainTypeName); 60 String newName = Signature.getSimpleName(fNewMainTypeName); 61 String 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 72 public Object getModifiedElement() { 73 return fLaunchConfiguration; 74 } 75 76 79 public String getName() { 80 if (fNewLaunchConfigurationName != null) { 81 return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_0, new String [] {fLaunchConfiguration.getName(), fNewLaunchConfigurationName}); 82 } 83 if (fNewProjectName == null) { 84 return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_1, new String [] {fLaunchConfiguration.getName()}); 85 } 86 if (fNewMainTypeName == null) { 87 return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_2, new String [] {fLaunchConfiguration.getName()}); 88 } 89 return MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_3, new String [] {fLaunchConfiguration.getName()}); 90 } 91 92 95 public void initializeValidationData(IProgressMonitor pm) {} 96 97 100 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { 101 if (fLaunchConfiguration.exists()) { 102 String typeName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 103 String projectName = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )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 [] {fLaunchConfiguration.getName(), fOldProjectName})); 110 } 111 return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_5, new String [] {fLaunchConfiguration.getName(), fOldMainTypeName})); 112 } 113 else { 114 if (fOldProjectName.equals(projectName)) { 116 return new RefactoringStatus(); 117 } 118 return RefactoringStatus.createWarningStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_4, new String [] {fLaunchConfiguration.getName(), fOldProjectName})); 119 } 120 } 121 return RefactoringStatus.createFatalErrorStatus(MessageFormat.format(RefactoringMessages.LaunchConfigurationProjectMainTypeChange_6, new String [] {fLaunchConfiguration.getName()})); 122 } 123 124 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 oldMainTypeName; 137 String 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 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 return new LaunchConfigurationProjectMainTypeChange(wc, oldMainTypeName, oldProjectName); 162 } 163 164 168 public void setNewContainerName(String newContainerName) { 169 fNewConfigContainerName = newContainerName; 170 } 171 } 172 | Popular Tags |