Skip to main content
summaryrefslogtreecommitdiffstats
blob: 24ee9ceb26cbc2e37371f30181f834eaa2f66d96 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
 * Copyright (c) 2011 BestSolution.at 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:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 ******************************************************************************/
package org.eclipse.e4.tools.emf.editor3x;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.e4.tools.emf.editor3x.extension.Util;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;

public class ModelRenameParticipant extends
		org.eclipse.ltk.core.refactoring.participants.RenameParticipant {
	private IType _type;
	private IPackageFragment _pckage;
	private IFile _file;
	private IFolder _folder;

	@Override
	protected boolean initialize(Object element) {

		if (element instanceof IType) {
			_type = (IType) element;
			return true;
		}

		if (element instanceof IPackageFragment) {
			_pckage = (IPackageFragment) element;
			return true;
		}

		if (element instanceof IFile) {
			_file = (IFile) element;
			return true;
		}

		if (element instanceof IFolder) {
			_folder = (IFolder) element;
			return true;
		}
		return false;
	}

	@Override
	public String getName() {
		return "Workbench Model Contribution Participant";
	}

	@Override
	public RefactoringStatus checkConditions(IProgressMonitor pm,
			CheckConditionsContext context) throws OperationCanceledException {
		return new RefactoringStatus();
	}

	@Override
	public Change createChange(IProgressMonitor pm) throws CoreException,
			OperationCanceledException {
		if (_type != null) {
			return createClassChange(pm, _type);
		}

		if (_pckage != null) {
			return createPackageChange(pm, _pckage);
		}

		if (_file != null) {
			return createFileChange(pm, _file);
		}

		if (_folder != null) {
			return createFolderChange(pm, _folder);
		}

		return null;
	}

	private Change createFolderChange(IProgressMonitor pm, IFolder folder)
			throws CoreException {

		String SPLIT = "/";
		if (folder.getParent().getFullPath().segmentCount() == 1) {
			SPLIT = "";
		}

		String newUrl = "platform:/plugin/" + folder.getProject().getName() + "/"
				+ folder.getParent().getProjectRelativePath().toString()
				+ SPLIT + getArguments().getNewName();

		String oldUrl = "platform:/plugin" + folder.getFullPath();

		return RefactorParticipantDelegate.createChange(pm, this, oldUrl,
				newUrl);
	}

	private Change createFileChange(IProgressMonitor pm, IFile file)
			throws CoreException {

		String SPLIT = "/";
		if (file.getParent().getFullPath().segmentCount() == 1) {
			SPLIT = "";
		}
		String newUrl = "platform:/plugin/" + file.getProject().getName() + "/"
				+ file.getParent().getProjectRelativePath().toString() + SPLIT
				+ getArguments().getNewName();

		String oldUrl = "platform:/plugin" + file.getFullPath();

		return RefactorParticipantDelegate.createChange(pm, this, oldUrl,
				newUrl);
	}

	private Change createPackageChange(IProgressMonitor pm,
			IPackageFragment pckage) throws CoreException {
		String bundle = Util.getBundleSymbolicName(pckage.getJavaProject()
				.getProject());

		final String newUrl = "bundleclass://" + bundle + "/"
				+ getArguments().getNewName();

		String oldUrl = "bundleclass://" + bundle + "/"
				+ pckage.getElementName();

		return RefactorParticipantDelegate.createChange(pm, this, oldUrl,
				newUrl);
	}

	private Change createClassChange(IProgressMonitor pm, IType type)
			throws CoreException {
		String bundle = Util.getBundleSymbolicName(type.getJavaProject()
				.getProject());

		final String newUrl = "bundleclass://"
				+ bundle
				+ "/"
				+ (type.getPackageFragment().getElementName().length() == 0 ? getArguments()
						.getNewName() : type.getPackageFragment()
						.getElementName() + "." + getArguments().getNewName());
		String oldUrl = "bundleclass://" + bundle + "/"
				+ type.getFullyQualifiedName().replace(".", "\\.");

		return RefactorParticipantDelegate.createChange(pm, this, oldUrl,
				newUrl);
	}

}

Back to the top