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

Last change on this file since 31 was 18, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 3.9 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.BnfEntry;
26import de.ugoe.cs.swe.bnftools.ebnf.Rule;
27import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.ReplacePassthroughRuleProcessor;
28import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoring;
29import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoringWizard;
30import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
31import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
32import de.ugoe.cs.swe.bnftools.utils.Utils;
33
34public class ReplaceAllPassthroughRulesRefactoringHandler extends
35                AbstractHandler {
36
37        @Inject
38        IResourceDescriptions resourceDescriptions;
39
40        // ----------------------------------------------------------------------------------------------------
41
42        public Object execute(ExecutionEvent event) throws ExecutionException {
43                XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
44                .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
45
46//TODO: better error handling           
47                if (!(editor.getDocument() instanceof XtextDocument)) {
48                        return null;
49                }
50               
51                XtextDocument doc = (XtextDocument) editor.getDocument();
52                EObject root = doc.readOnly(new RootEObjectFinder());
53                NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
54               
55                List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
56                for (int i=0; i < rootNode.getParserNode().getChildren().size(); i++) {
57                        AbstractNode currentNode = rootNode.getParserNode().getChildren().get(i);
58                       
59                        EObject element = currentNode.getElement();
60                        if (!(element instanceof BnfEntry))
61                                continue;
62                        BnfEntry bnfEntry = (BnfEntry) element;
63                        if (bnfEntry.getRule() == null)
64                                continue;
65                       
66                        currentNode = NodeUtil.getNodeAdapter(bnfEntry.getRule()).getParserNode();
67                       
68                        if (currentNode.getElement() instanceof Rule) {
69                                Rule rule = (Rule) currentNode.getElement();
70                                NodeAdapter declarationNode = NodeUtil.getNodeAdapter(rule);
71                                if (EbnfAnalysisUtils.isPassthroughRule(rule)) {
72                                        List<CompositeNode> references = Utils.findReferences(rule,
73                                                        resourceDescriptions, editor);
74                                       
75                                        DeclarationReferencesPair nodePair = new DeclarationReferencesPair(declarationNode.getParserNode(), references);
76                                        nodePairs.add(nodePair);
77                                }
78                        }
79                }
80
81                ReplacePassthroughRuleProcessor processor = new ReplacePassthroughRuleProcessor(
82                                editor, editor.getDocument(), nodePairs, false);
83               
84                GenericRefactoring refactoring = new GenericRefactoring(processor);
85               
86                GenericRefactoringWizard wizard = new GenericRefactoringWizard(
87                                refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
88
89                RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
90                                wizard);
91
92                try {
93                        openOperation.run(Display.getCurrent().getActiveShell(),
94                                        "Refactoring not possible!");
95                } catch (InterruptedException e1) {
96                        e1.printStackTrace();
97                }
98
99                return null;
100        }
101
102}
Note: See TracBrowser for help on using the repository browser.