1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.replacepassthroughrules;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import org.eclipse.core.commands.AbstractHandler;
|
---|
7 | import org.eclipse.core.commands.ExecutionEvent;
|
---|
8 | import org.eclipse.core.commands.ExecutionException;
|
---|
9 | import org.eclipse.emf.ecore.EObject;
|
---|
10 | import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
|
---|
11 | import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
|
---|
12 | import org.eclipse.swt.widgets.Display;
|
---|
13 | import org.eclipse.ui.PlatformUI;
|
---|
14 | import org.eclipse.xtext.parsetree.AbstractNode;
|
---|
15 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
16 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
17 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
18 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
19 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
20 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
21 |
|
---|
22 | import com.google.inject.Inject;
|
---|
23 |
|
---|
24 | import de.ugoe.cs.swe.bnftools.analysis.EbnfAnalysisUtils;
|
---|
25 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
26 | import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.ReplacePassthroughRuleProcessor;
|
---|
27 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoring;
|
---|
28 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoringWizard;
|
---|
29 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
30 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
31 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
32 |
|
---|
33 | public class ReplaceAllPassthroughRulesRefactoringHandler extends
|
---|
34 | AbstractHandler {
|
---|
35 |
|
---|
36 | @Inject
|
---|
37 | IResourceDescriptions resourceDescriptions;
|
---|
38 |
|
---|
39 | // ----------------------------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
42 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
43 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
44 |
|
---|
45 | //TODO: better error handling
|
---|
46 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
47 | return null;
|
---|
48 | }
|
---|
49 |
|
---|
50 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
51 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
52 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
53 |
|
---|
54 | List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
|
---|
55 | for (int i=0; i < rootNode.getParserNode().getChildren().size(); i++) {
|
---|
56 | AbstractNode currentNode = rootNode.getParserNode().getChildren().get(i);
|
---|
57 | if (currentNode.getElement() instanceof Rule) {
|
---|
58 | Rule rule = (Rule) currentNode.getElement();
|
---|
59 | NodeAdapter declarationNode = NodeUtil.getNodeAdapter(rule);
|
---|
60 | if (EbnfAnalysisUtils.isPassthroughRule(rule)) {
|
---|
61 | List<CompositeNode> references = Utils.findReferences(rule,
|
---|
62 | resourceDescriptions, editor);
|
---|
63 |
|
---|
64 | DeclarationReferencesPair nodePair = new DeclarationReferencesPair(declarationNode.getParserNode(), references);
|
---|
65 | nodePairs.add(nodePair);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | ReplacePassthroughRuleProcessor processor = new ReplacePassthroughRuleProcessor(
|
---|
71 | editor, editor.getDocument(), nodePairs, false);
|
---|
72 |
|
---|
73 | GenericRefactoring refactoring = new GenericRefactoring(processor);
|
---|
74 |
|
---|
75 | GenericRefactoringWizard wizard = new GenericRefactoringWizard(
|
---|
76 | refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
|
---|
77 |
|
---|
78 | RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
|
---|
79 | wizard);
|
---|
80 |
|
---|
81 | try {
|
---|
82 | openOperation.run(Display.getCurrent().getActiveShell(),
|
---|
83 | "Refactoring not possible!");
|
---|
84 | } catch (InterruptedException e1) {
|
---|
85 | e1.printStackTrace();
|
---|
86 | }
|
---|
87 |
|
---|
88 | return null;
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|