1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.rename;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import org.eclipse.core.resources.IFile;
|
---|
6 | import org.eclipse.core.runtime.CoreException;
|
---|
7 | import org.eclipse.core.runtime.IProgressMonitor;
|
---|
8 | import org.eclipse.core.runtime.OperationCanceledException;
|
---|
9 | import org.eclipse.jface.text.ITextSelection;
|
---|
10 | import org.eclipse.ltk.core.refactoring.Change;
|
---|
11 | import org.eclipse.ltk.core.refactoring.CompositeChange;
|
---|
12 | import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
---|
13 | import org.eclipse.ltk.core.refactoring.TextFileChange;
|
---|
14 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
|
---|
15 | import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
|
---|
16 | import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
|
---|
17 | import org.eclipse.text.edits.MultiTextEdit;
|
---|
18 | import org.eclipse.text.edits.ReplaceEdit;
|
---|
19 | import org.eclipse.text.edits.TextEditGroup;
|
---|
20 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
21 | import org.eclipse.xtext.resource.IEObjectDescription;
|
---|
22 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
23 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
24 |
|
---|
25 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
26 | import de.ugoe.cs.swe.bnftools.utils.EObjectSelectionResolver;
|
---|
27 | import de.ugoe.cs.swe.bnftools.utils.URIFragmentResolver;
|
---|
28 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
29 |
|
---|
30 | public class RenameProcessor extends
|
---|
31 | org.eclipse.ltk.core.refactoring.participants.RenameProcessor {
|
---|
32 |
|
---|
33 | private List<CompositeNode> references = null;
|
---|
34 | private String currentName;
|
---|
35 | private CompositeNode declaration;
|
---|
36 | private IFile file;
|
---|
37 |
|
---|
38 | // --------------------------------------------------------------------------------
|
---|
39 |
|
---|
40 | public RenameProcessor(XtextEditor editor,
|
---|
41 | IResourceDescriptions resourceDescriptions) {
|
---|
42 |
|
---|
43 | final ITextSelection selection = (ITextSelection) editor
|
---|
44 | .getSelectionProvider().getSelection();
|
---|
45 |
|
---|
46 | final IEObjectDescription eObjectDescription = editor.getDocument()
|
---|
47 | .readOnly(new EObjectSelectionResolver(selection, resourceDescriptions));
|
---|
48 |
|
---|
49 | declaration = editor.getDocument().readOnly(
|
---|
50 | new URIFragmentResolver(eObjectDescription.getEObjectURI()
|
---|
51 | .fragment()));
|
---|
52 |
|
---|
53 | if (declaration.getElement() instanceof Rule) {
|
---|
54 | Rule r = (Rule) declaration.getElement();
|
---|
55 | currentName = r.getName();
|
---|
56 | }
|
---|
57 |
|
---|
58 | file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
|
---|
59 |
|
---|
60 | if (eObjectDescription != null) {
|
---|
61 | references = Utils.findReferenceDescriptions(eObjectDescription, resourceDescriptions, editor);
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|
65 |
|
---|
66 | // --------------------------------------------------------------------------------
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public Object[] getElements() {
|
---|
70 | return null;
|
---|
71 | }
|
---|
72 |
|
---|
73 | // --------------------------------------------------------------------------------
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public String getIdentifier() {
|
---|
77 | return "Rename Processor Identifier";
|
---|
78 | }
|
---|
79 |
|
---|
80 | // --------------------------------------------------------------------------------
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public String getProcessorName() {
|
---|
84 | return "Rename Processor";
|
---|
85 | }
|
---|
86 |
|
---|
87 | // --------------------------------------------------------------------------------
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public boolean isApplicable() throws CoreException {
|
---|
91 | return (references != null);
|
---|
92 | }
|
---|
93 |
|
---|
94 | // --------------------------------------------------------------------------------
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
|
---|
98 | throws CoreException, OperationCanceledException {
|
---|
99 | RefactoringStatus status = new RefactoringStatus();
|
---|
100 |
|
---|
101 | if (references == null)
|
---|
102 | status.addFatalError("Could not obtain references!");
|
---|
103 |
|
---|
104 | return status;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // --------------------------------------------------------------------------------
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
|
---|
111 | CheckConditionsContext context) throws CoreException,
|
---|
112 | OperationCanceledException {
|
---|
113 | RefactoringStatus status = new RefactoringStatus();
|
---|
114 | RenameRefactoring refactoring = (RenameRefactoring) getRefactoring();
|
---|
115 | if (currentName.equals(refactoring.getRenameText())) {
|
---|
116 | status.addFatalError("Name unchanged!");
|
---|
117 | } else if (refactoring.getRenameText().length() <= 0) {
|
---|
118 | status.addFatalError("name must not be empty!");
|
---|
119 | }
|
---|
120 |
|
---|
121 | return status;
|
---|
122 | }
|
---|
123 |
|
---|
124 | // --------------------------------------------------------------------------------
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public Change createChange(IProgressMonitor pm) throws CoreException,
|
---|
128 | OperationCanceledException {
|
---|
129 | CompositeChange compositeChange = new CompositeChange("Rename");
|
---|
130 | pm.beginTask("Rename Refactoring", references.size());
|
---|
131 |
|
---|
132 | MultiTextEdit multiEdit = new MultiTextEdit();
|
---|
133 | TextFileChange fileChange = new TextFileChange("Declaraction Renaming", file);
|
---|
134 |
|
---|
135 | fileChange.setEdit(multiEdit);
|
---|
136 | fileChange.setTextType("bnf");
|
---|
137 | compositeChange.add(fileChange);
|
---|
138 |
|
---|
139 | RenameRefactoring refactoring = (RenameRefactoring) getRefactoring();
|
---|
140 | String replaceText = refactoring.getRenameText();
|
---|
141 |
|
---|
142 | ReplaceEdit replaceEdit = new ReplaceEdit(declaration.getOffset(), ((Rule)declaration.getElement()).getName().length(), replaceText);
|
---|
143 | multiEdit.addChild(replaceEdit);
|
---|
144 |
|
---|
145 | TextEditGroup editGroup = new TextEditGroup("declaration update", replaceEdit);
|
---|
146 | fileChange.addTextEditGroup(editGroup);
|
---|
147 | pm.worked(1);
|
---|
148 |
|
---|
149 |
|
---|
150 | for (int i=0; i < references.size(); i++) {
|
---|
151 | CompositeNode reference = references.get(i);
|
---|
152 |
|
---|
153 | replaceEdit = new ReplaceEdit(reference.getOffset(), ((Rule)declaration.getElement()).getName().length(), replaceText);
|
---|
154 | multiEdit.addChild(replaceEdit);
|
---|
155 | editGroup = new TextEditGroup("reference update", replaceEdit);
|
---|
156 | fileChange.addTextEditGroup(editGroup);
|
---|
157 |
|
---|
158 | pm.worked(1);
|
---|
159 | }
|
---|
160 |
|
---|
161 | return compositeChange;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --------------------------------------------------------------------------------
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
|
---|
168 | SharableParticipants sharedParticipants) throws CoreException {
|
---|
169 | return null;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // --------------------------------------------------------------------------------
|
---|
173 |
|
---|
174 |
|
---|
175 | }
|
---|