source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/refactoring/removeunusedrules/RemoveUnusedRulesRefactoringHandler.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.2 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.refactoring.removeunusedrules;
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.ebnf.Rule;
25import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.RemoveUnusedRulesProcessor;
26import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoring;
27import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoringWizard;
28import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
29import de.ugoe.cs.swe.bnftools.utils.Utils;
30
31public class RemoveUnusedRulesRefactoringHandler extends AbstractHandler {
32
33        @Inject
34        IResourceDescriptions resourceDescriptions;
35
36        public Object execute(ExecutionEvent event) throws ExecutionException {
37                XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
38                .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
39
40//TODO: better error handling           
41                if (!(editor.getDocument() instanceof XtextDocument)) {
42                        return null;
43                }
44               
45                XtextDocument doc = (XtextDocument) editor.getDocument();
46                EObject root = doc.readOnly(new RootEObjectFinder());
47                NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
48
49                List<CompositeNode> unusedRules = new ArrayList<CompositeNode>();
50               
51                int counter = 0;
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() == 0) && (counter > 0)){
61                                        unusedRules.add(declarationNode.getParserNode());
62                                }
63                                counter++;
64                        }
65                }
66
67                RemoveUnusedRulesProcessor processor = new RemoveUnusedRulesProcessor(
68                                editor, editor.getDocument(), unusedRules);
69               
70                GenericRefactoring refactoring = new GenericRefactoring(processor);
71               
72                GenericRefactoringWizard wizard = new GenericRefactoringWizard(
73                                refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
74
75                RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
76                                wizard);
77
78                try {
79                        openOperation.run(Display.getCurrent().getActiveShell(),
80                                        "Refactoring not possible!");
81                } catch (InterruptedException e1) {
82                        e1.printStackTrace();
83                }
84
85               
86                return null;
87        }
88
89}
Note: See TracBrowser for help on using the repository browser.