Bug 12746 - refactoring support for launch configurations
diff --git a/org.eclipse.jdt.debug.ui/plugin.xml b/org.eclipse.jdt.debug.ui/plugin.xml
index a16b9d3..7f23e72 100644
--- a/org.eclipse.jdt.debug.ui/plugin.xml
+++ b/org.eclipse.jdt.debug.ui/plugin.xml
@@ -2249,4 +2249,78 @@
</activityPatternBinding>
</extension>
+<!-- refactoring -->
+
+ <extension point="org.eclipse.jdt.ui.renameParticipants">
+ <renameParticipant
+ class="org.eclipse.jdt.internal.debug.core.refactoring.LaunchConfigurationITypeRenameParticipant"
+ id="org.eclipse.jdt.debug.refactoring.launchConfigurationITypeRename">
+ <enablement>
+ <with variable="affectedProjects">
+ <iterate>
+ <test property="projectNature" value="org.eclipse.jdt.core.javanature"/>
+ </iterate>
+ </with>
+ <with variable="selection">
+ <count value="1"/>
+ <iterate>
+ <instanceof value="org.eclipse.jdt.core.IType"/>
+ </iterate>
+ </with>
+ </enablement>
+ </renameParticipant>
+ <renameParticipant
+ class="org.eclipse.jdt.internal.debug.core.refactoring.LaunchConfigurationIJavaProjectRenameParticipant"
+ id="org.eclipse.jdt.debug.refactoring.launchConfigurationIJavaProjectRename">
+ <enablement>
+ <with variable="affectedProjects">
+ <iterate>
+ <test property="projectNature" value="org.eclipse.jdt.core.javanature"/>
+ </iterate>
+ </with>
+ <with variable="selection">
+ <count value="1"/>
+ <iterate>
+ <instanceof value="org.eclipse.jdt.core.IJavaProject"/>
+ </iterate>
+ </with>
+ </enablement>
+ </renameParticipant>
+<!-- <renameParticipant
+ class="org.eclipse.jdt.internal.debug.core.refactoring.JavaBreakpointITypeRenameParticipant"
+ id="org.eclipse.jdt.debug.refactoring.javaBreakpointITypeRename">
+ <enablement>
+ <with variable="affectedProjects">
+ <iterate>
+ <test property="projectNature" value="org.eclipse.jdt.core.javanature"/>
+ </iterate>
+ </with>
+ <with variable="selection">
+ <count value="1"/>
+ <iterate>
+ <instanceof value="org.eclipse.jdt.core.IType"/>
+ </iterate>
+ </with>
+ </enablement>
+ </renameParticipant> -->
+ </extension>
+<!-- <extension point="org.eclipse.jdt.ui.moveParticipants">
+ <renameParticipant
+ class="org.eclipse.jdt.internal.debug.core.refactoring.LaunchConfigurationITypeMoveParticipant"
+ id="org.eclipse.jdt.debug.refactoring.launchConfigurationITypeMove">
+ <enablement>
+ <with variable="affectedProjects">
+ <iterate>
+ <test property="projectNature" value="org.eclipse.jdt.core.javanature"/>
+ </iterate>
+ </with>
+ <with variable="selection">
+ <count value="1"/>
+ <iterate>
+ <instanceof value="org.eclipse.jdt.core.IType"/>
+ </iterate>
+ </with>
+ </enablement>
+ </renameParticipant>
+ </extension> -->
</plugin>
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationIJavaProjectRenameParticipant.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationIJavaProjectRenameParticipant.java
new file mode 100644
index 0000000..de767d3
--- /dev/null
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationIJavaProjectRenameParticipant.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright (c) 2003 International Business Machines Corp. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v0.5
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v05.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jdt.internal.debug.core.refactoring;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.internal.corext.refactoring.base.IChange;
+import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatus;
+import org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringProcessor;
+import org.eclipse.jdt.internal.corext.refactoring.participants.RenameParticipant;
+
+/**
+ */
+public class LaunchConfigurationIJavaProjectRenameParticipant extends RenameParticipant {
+
+ private IJavaProject fJavaProject;
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#initialize(org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringProcessor, java.lang.Object)
+ */
+ public void initialize(IRefactoringProcessor processor, Object element) {
+ super.initialize(processor);
+ fJavaProject= (IJavaProject) element;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#isAvailable()
+ */
+ public boolean isAvailable() {
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#operatesOn(java.lang.Object)
+ */
+ public boolean operatesOn(Object element) {
+ return fJavaProject.equals(element);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#checkActivation()
+ */
+ public RefactoringStatus checkActivation() {
+ return new RefactoringStatus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#checkInput(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public RefactoringStatus checkInput(IProgressMonitor pm) {
+ return new RefactoringStatus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#createChange(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public IChange createChange(IProgressMonitor pm) throws CoreException {
+ return LaunchConfigurationProjectNameChange.createChangesFor(fJavaProject, getNewName());
+ }
+}
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationITypeRenameParticipant.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationITypeRenameParticipant.java
new file mode 100644
index 0000000..577c0c6
--- /dev/null
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationITypeRenameParticipant.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright (c) 2003 International Business Machines Corp. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v0.5
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v05.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jdt.internal.debug.core.refactoring;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.internal.corext.refactoring.base.IChange;
+import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatus;
+import org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringProcessor;
+import org.eclipse.jdt.internal.corext.refactoring.participants.RenameParticipant;
+
+/**
+ */
+public class LaunchConfigurationITypeRenameParticipant extends RenameParticipant {
+
+ private IType fType;
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#initialize(org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringProcessor, java.lang.Object)
+ */
+ public void initialize(IRefactoringProcessor processor, Object element) {
+ super.initialize(processor);
+ fType= (IType) element;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#isAvailable()
+ */
+ public boolean isAvailable() {
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#operatesOn(java.lang.Object)
+ */
+ public boolean operatesOn(Object element) {
+ return fType.equals(element);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#checkActivation()
+ */
+ public RefactoringStatus checkActivation() {
+ return new RefactoringStatus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#checkInput(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public RefactoringStatus checkInput(IProgressMonitor pm) {
+ return new RefactoringStatus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.participants.IRefactoringParticipant#createChange(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public IChange createChange(IProgressMonitor pm) throws CoreException {
+ return LaunchConfigurationMainTypeNameChange.createChangesFor(fType, getNewName());
+ }
+}
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationMainTypeNameChange.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationMainTypeNameChange.java
new file mode 100644
index 0000000..833c917
--- /dev/null
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationMainTypeNameChange.java
@@ -0,0 +1,168 @@
+/*******************************************************************************
+ * Copyright (c) 2003 International Business Machines Corp. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v0.5
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v05.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jdt.internal.debug.core.refactoring;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.ILaunchConfigurationListener;
+import org.eclipse.debug.core.ILaunchConfigurationType;
+import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
+import org.eclipse.debug.core.ILaunchManager;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.internal.corext.refactoring.CompositeChange;
+import org.eclipse.jdt.internal.corext.refactoring.base.Change;
+import org.eclipse.jdt.internal.corext.refactoring.base.ChangeAbortException;
+import org.eclipse.jdt.internal.corext.refactoring.base.ChangeContext;
+import org.eclipse.jdt.internal.corext.refactoring.base.IChange;
+import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
+
+public class LaunchConfigurationMainTypeNameChange extends Change {
+
+ public static IChange createChangesFor(IType type, String newName) throws CoreException {
+ List changes= new ArrayList();
+ ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager();
+ ILaunchConfigurationType configurationType= manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
+ ILaunchConfiguration configs[]= manager.getLaunchConfigurations(configurationType);
+ String typeName= type.getFullyQualifiedName();
+ for (int i= 0; i < configs.length; i++) {
+ ILaunchConfiguration launchConfiguration = configs[i];
+ String mainType= launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String)null);
+ if (typeName.equals(mainType)) {
+ changes.add(new LaunchConfigurationMainTypeNameChange(launchConfiguration, newName));
+ }
+ }
+ int nbChanges= changes.size();
+ if (nbChanges == 0) {
+ return null;
+ } else if (nbChanges == 1) {
+ return (IChange) changes.get(0);
+ } else {
+ return new CompositeChange("Launch configuration updates", (IChange[])changes.toArray(new IChange[changes.size()]));
+ }
+ }
+
+ private ILaunchConfiguration fLaunchConfiguration;
+
+ private String fOldName;
+
+ private String fNewTypeName;
+
+ private String fNewLaunchConfigurationName;
+
+ private LaunchConfigurationMainTypeNameChange fUndo;
+
+ /**
+ * New launch configuration object if the launch configuration has been renamed, the original
+ * launch configuration object otherwise.
+ */
+ protected ILaunchConfiguration fNewLaunchConfiguration;
+
+ /**
+ * launch configuration listener used to get the new ILaunchConfiguration object when a launch
+ * configuration is renamed.
+ */
+ private ILaunchConfigurationListener configurationListener= new ILaunchConfigurationListener() {
+ public void launchConfigurationAdded(final ILaunchConfiguration launchConfiguration) {
+ ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager();
+ final ILaunchConfiguration oldConfig= manager.getMovedFrom(launchConfiguration);
+ if (oldConfig != null && oldConfig == fLaunchConfiguration) {
+ fNewLaunchConfiguration= launchConfiguration;
+ }
+ }
+
+ public void launchConfigurationChanged(ILaunchConfiguration configuration) {
+ }
+
+ public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
+ }
+ };
+
+
+ /**
+ * LaunchConfigurationMainTypeNameChange constructor.
+ */
+ public LaunchConfigurationMainTypeNameChange(ILaunchConfiguration launchConfiguration, String newName) throws CoreException {
+ fLaunchConfiguration= launchConfiguration;
+ fNewLaunchConfiguration= launchConfiguration;
+ // generate the new type name
+ if (newName.endsWith(".java")) { //$NON-NLS-1$
+ newName= newName.substring(0, newName.length() - 5);
+ }
+ String current = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String) null);
+ int index = current.lastIndexOf('.');
+ if (index == -1) {
+ fNewTypeName = newName;
+ fOldName= current;
+ } else {
+ fNewTypeName = current.substring(0, index + 1) + newName;
+ fOldName= current.substring(index + 1);
+ }
+ // generate the new configuration name
+ String launchConfigurationName= fLaunchConfiguration.getName();
+ fNewLaunchConfigurationName= launchConfigurationName.replaceAll(fOldName, newName);
+ if (launchConfigurationName.equals(fNewLaunchConfigurationName) || DebugPlugin.getDefault().getLaunchManager().isExistingLaunchConfigurationName(fNewLaunchConfigurationName)) {
+ fNewLaunchConfigurationName= null;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#getName()
+ */
+ public String getName() {
+ if (fNewLaunchConfigurationName != null) {
+ return MessageFormat.format("Rename launch configuration \"{0}\" to \"{1}\", update main type", new String[] {fLaunchConfiguration.getName(), fNewLaunchConfigurationName});
+ } else {
+ return MessageFormat.format("Update main type of launch configuration \"{0}\"", new String[] {fLaunchConfiguration.getName()});
+ }
+ }
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#getModifiedLanguageElement()
+ */
+ public Object getModifiedLanguageElement() {
+ return fLaunchConfiguration;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#perform(org.eclipse.jdt.internal.corext.refactoring.base.ChangeContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public void perform(ChangeContext context, IProgressMonitor pm) throws JavaModelException, ChangeAbortException {
+ try {
+ // update the configuration
+ ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
+ launchManager.addLaunchConfigurationListener(configurationListener);
+ ILaunchConfigurationWorkingCopy copy = fLaunchConfiguration.getWorkingCopy();
+ copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fNewTypeName);
+ if (fNewLaunchConfigurationName != null) {
+ copy.rename(fNewLaunchConfigurationName);
+ }
+ copy.doSave();
+ launchManager.removeLaunchConfigurationListener(configurationListener);
+ // create the undo change
+ fUndo = new LaunchConfigurationMainTypeNameChange(fNewLaunchConfiguration, fOldName);
+ } catch (CoreException e) {
+ throw new JavaModelException(e);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#getUndoChange()
+ */
+ public IChange getUndoChange() {
+ return fUndo;
+ }
+}
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationProjectNameChange.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationProjectNameChange.java
new file mode 100644
index 0000000..518ba8b
--- /dev/null
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationProjectNameChange.java
@@ -0,0 +1,111 @@
+/*******************************************************************************
+ * Copyright (c) 2003 International Business Machines Corp. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v0.5
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v05.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jdt.internal.debug.core.refactoring;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.ILaunchConfigurationType;
+import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
+import org.eclipse.debug.core.ILaunchManager;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.internal.corext.refactoring.CompositeChange;
+import org.eclipse.jdt.internal.corext.refactoring.base.Change;
+import org.eclipse.jdt.internal.corext.refactoring.base.ChangeAbortException;
+import org.eclipse.jdt.internal.corext.refactoring.base.ChangeContext;
+import org.eclipse.jdt.internal.corext.refactoring.base.IChange;
+import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
+
+/**
+ */
+public class LaunchConfigurationProjectNameChange extends Change {
+
+ private ILaunchConfiguration fLaunchConfiguration;
+
+ private String fNewProjectName;
+
+ private IChange fUndo;
+
+ /**
+ * @param javaProject
+ * @param string
+ * @return
+ */
+ public static IChange createChangesFor(IJavaProject javaProject, String newProjectName) throws CoreException {
+ List changes= new ArrayList();
+ ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager();
+ ILaunchConfigurationType configurationType= manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
+ ILaunchConfiguration configs[]= manager.getLaunchConfigurations(configurationType);
+ String projectName= javaProject.getElementName();
+ for (int i= 0; i < configs.length; i++) {
+ ILaunchConfiguration launchConfiguration = configs[i];
+ String launchConfigurationProjectName= launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null);
+ if (projectName.equals(launchConfigurationProjectName)) {
+ changes.add(new LaunchConfigurationProjectNameChange(launchConfiguration, newProjectName));
+ }
+ }
+ int nbChanges= changes.size();
+ if (nbChanges == 0) {
+ return null;
+ } else if (nbChanges == 1) {
+ return (IChange) changes.get(0);
+ } else {
+ return new CompositeChange("Launch configuration updates", (IChange[])changes.toArray(new IChange[changes.size()]));
+ }
+ }
+
+ public LaunchConfigurationProjectNameChange(ILaunchConfiguration launchConfiguration, String newProjectName) {
+ fLaunchConfiguration= launchConfiguration;
+ fNewProjectName= newProjectName;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#perform(org.eclipse.jdt.internal.corext.refactoring.base.ChangeContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public void perform(ChangeContext context, IProgressMonitor pm) throws JavaModelException, ChangeAbortException {
+ try {
+ String currentProjectName= fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null);
+ ILaunchConfigurationWorkingCopy copy = fLaunchConfiguration.getWorkingCopy();
+ copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fNewProjectName);
+ copy.doSave();
+ fUndo= new LaunchConfigurationProjectNameChange(fLaunchConfiguration, currentProjectName);
+ } catch (CoreException e) {
+ throw new JavaModelException(e);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#getUndoChange()
+ */
+ public IChange getUndoChange() {
+ return fUndo;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#getModifiedLanguageElement()
+ */
+ public Object getModifiedLanguageElement() {
+ return fLaunchConfiguration;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#getName()
+ */
+ public String getName() {
+ return MessageFormat.format("Update project of launch configuration \"{0}\"", new String[] {fLaunchConfiguration.getName()});
+ }
+}