Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4451ec320d8a7dc9f38121b72a0759fe2c95d35b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2009 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation9
 *******************************************************************************/

package org.eclipse.cdt.launch.internal.refactoring;

import java.util.Collection;
import java.util.Set;

import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;

/**
 * A rename participant for resource refactorings, that updates affected CDT
 * launch configurations.
 * 
 * @author Christian W. Damus (cdamus)
 * 
 * @since 6.0
 */
public class ResourceRenameParticipant extends RenameParticipant implements
		IExecutableExtension {

	private String name;

	private IResource resourceBeingRenamed;

	/**
	 * Initializes me.
	 */
	public ResourceRenameParticipant() {
		super();
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	protected boolean initialize(Object element) {
		if (element instanceof IResource) {
			resourceBeingRenamed = (IResource) element;
		} else if (element instanceof IAdaptable) {
			resourceBeingRenamed = (IResource) ((IAdaptable) element)
					.getAdapter(IResource.class);
		}

		return true;
	}

	@Override
	public Change createChange(IProgressMonitor pm) throws CoreException,
			OperationCanceledException {

		Change result = null;

		if (resourceBeingRenamed instanceof IProject) {
			String oldName = resourceBeingRenamed.getName();
			String newName = getArguments().getNewName();
			Collection<ILaunchConfigurationType> launchTypes = getCLaunchConfigTypes();

			if (!launchTypes.isEmpty()) {
				ILaunchManager mgr = DebugPlugin.getDefault()
						.getLaunchManager();

				for (ILaunchConfigurationType type : launchTypes) {
					ILaunchConfiguration[] launches = mgr
							.getLaunchConfigurations(type);

					for (ILaunchConfiguration next : launches) {
						if (next
								.getAttribute(
										ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME,
										"").equals(oldName)) { //$NON-NLS-1$

							result = AbstractLaunchConfigChange.append(result,
									new ProjectRenameChange(next, oldName,
											newName));
						}
					}
				}
			}
		}

		return result;
	}

	static Collection<ILaunchConfigurationType> getCLaunchConfigTypes() {
		Set<ILaunchConfigurationType> result = new java.util.HashSet<ILaunchConfigurationType>();

		ILaunchManager mgr = DebugPlugin.getDefault().getLaunchManager();
		for (ILaunchConfigurationType next : mgr.getLaunchConfigurationTypes()) {
			// is it a CDT launch type?
			if (next.getPluginIdentifier().startsWith("org.eclipse.cdt.")) { //$NON-NLS-1$
				result.add(next);
			}
		}

		return result;
	}

	@Override
	public RefactoringStatus checkConditions(IProgressMonitor pm,
			CheckConditionsContext context) throws OperationCanceledException {

		// I have no conditions to check. Updating the launches is trivial
		return new RefactoringStatus();
	}

	public void setInitializationData(IConfigurationElement config,
			String propertyName, Object data) throws CoreException {

		this.name = config.getAttribute("name"); //$NON-NLS-1$
	}

}

Back to the top