[5] | 1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.inlinesinglereferencedrules;
|
---|
| 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.ebnf.Rule;
|
---|
| 25 | import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.InlineRulesProcessor;
|
---|
| 26 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoring;
|
---|
| 27 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoringWizard;
|
---|
| 28 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
| 29 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
| 30 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
| 31 |
|
---|
| 32 | public class InlineSingleReferencedRulesRefactoringHandler extends
|
---|
| 33 | AbstractHandler {
|
---|
| 34 |
|
---|
| 35 | @Inject
|
---|
| 36 | IResourceDescriptions resourceDescriptions;
|
---|
| 37 |
|
---|
| 38 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 39 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 40 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 41 |
|
---|
| 42 | //TODO: better error handling
|
---|
| 43 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 44 | return null;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 48 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 49 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 50 |
|
---|
| 51 | List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
|
---|
| 52 | for (int i=0; i < rootNode.getParserNode().getChildren().size(); i++) {
|
---|
| 53 | AbstractNode currentNode = rootNode.getParserNode().getChildren().get(i);
|
---|
| 54 | if (currentNode.getElement() instanceof Rule) {
|
---|
| 55 | Rule rule = (Rule) currentNode.getElement();
|
---|
| 56 | NodeAdapter declarationNode = NodeUtil.getNodeAdapter(rule);
|
---|
| 57 | List<CompositeNode> references = Utils.findReferences(rule,
|
---|
| 58 | resourceDescriptions, editor);
|
---|
| 59 |
|
---|
| 60 | if (references.size() == 1) {
|
---|
| 61 | DeclarationReferencesPair nodePair = new DeclarationReferencesPair(declarationNode.getParserNode(), references);
|
---|
| 62 | nodePairs.add(nodePair);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | InlineRulesProcessor processor = new InlineRulesProcessor(editor, rootNode.getParserNode(), doc, nodePairs, false);
|
---|
| 68 |
|
---|
| 69 | GenericRefactoring refactoring = new GenericRefactoring(processor);
|
---|
| 70 |
|
---|
| 71 | GenericRefactoringWizard wizard = new GenericRefactoringWizard(
|
---|
| 72 | refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
|
---|
| 73 |
|
---|
| 74 | RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
|
---|
| 75 | wizard);
|
---|
| 76 |
|
---|
| 77 | try {
|
---|
| 78 | openOperation.run(Display.getCurrent().getActiveShell(),
|
---|
| 79 | "Refactoring not possible!");
|
---|
| 80 | } catch (InterruptedException e1) {
|
---|
| 81 | e1.printStackTrace();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return null;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | }
|
---|