source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/refactoring/replacepassthroughrules/ReplaceAllPassthroughRulesRefactoringHandler.java @ 5

Last change on this file since 5 was 5, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 3.5 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.refactoring.replacepassthroughrules;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.eclipse.core.commands.AbstractHandler;
7import org.eclipse.core.commands.ExecutionEvent;
8import org.eclipse.core.commands.ExecutionException;
9import org.eclipse.emf.ecore.EObject;
10import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
11import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
12import org.eclipse.swt.widgets.Display;
13import org.eclipse.ui.PlatformUI;
14import org.eclipse.xtext.parsetree.AbstractNode;
15import org.eclipse.xtext.parsetree.CompositeNode;
16import org.eclipse.xtext.parsetree.NodeAdapter;
17import org.eclipse.xtext.parsetree.NodeUtil;
18import org.eclipse.xtext.resource.IResourceDescriptions;
19import org.eclipse.xtext.ui.editor.XtextEditor;
20import org.eclipse.xtext.ui.editor.model.XtextDocument;
21
22import com.google.inject.Inject;
23
24import de.ugoe.cs.swe.bnftools.analysis.EbnfAnalysisUtils;
25import de.ugoe.cs.swe.bnftools.ebnf.Rule;
26import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.ReplacePassthroughRuleProcessor;
27import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoring;
28import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoringWizard;
29import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
30import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
31import de.ugoe.cs.swe.bnftools.utils.Utils;
32
33public 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}
Note: See TracBrowser for help on using the repository browser.