-
Notifications
You must be signed in to change notification settings - Fork 23
/
RenameElementChange.java
60 lines (51 loc) · 2.18 KB
/
RenameElementChange.java
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
/*******************************************************************************
* Copyright (c) 2024 Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Martin Jobst - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.fordiac.ide.typemanagement.refactoring;
import java.util.Objects;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.fordiac.ide.model.NameRepository;
import org.eclipse.fordiac.ide.model.commands.change.ChangeNameCommand;
import org.eclipse.fordiac.ide.model.libraryElement.INamedElement;
import org.eclipse.fordiac.ide.typemanagement.Messages;
import org.eclipse.gef.commands.Command;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
public class RenameElementChange extends AbstractCommandChange<INamedElement> {
private final String newName;
private String oldName;
public RenameElementChange(final String name, final URI elementURI, final String newName) {
super(name, elementURI, INamedElement.class);
this.newName = newName;
}
@Override
protected Command createCommand(final INamedElement element) {
return ChangeNameCommand.forName(element, newName);
}
@Override
public void initializeValidationData(final INamedElement element, final IProgressMonitor pm) {
oldName = element.getName();
}
@Override
public RefactoringStatus isValid(final INamedElement element, final IProgressMonitor pm)
throws CoreException, OperationCanceledException {
final RefactoringStatus status = new RefactoringStatus();
if (!Objects.equals(element.getName(), oldName)) {
status.addFatalError(Messages.RenameElementChange_NameChanged);
} else if (!NameRepository.isValidName(element, newName)) {
status.addFatalError(Messages.RenameElementChange_InvalidName);
}
return status;
}
}