source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/quickfix/modifications/InlineRuleModification.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.1 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.quickfix.modifications;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.eclipse.core.runtime.NullProgressMonitor;
7import org.eclipse.emf.ecore.EObject;
8import org.eclipse.ltk.core.refactoring.Change;
9import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
10import org.eclipse.ltk.core.refactoring.RefactoringCore;
11import org.eclipse.ui.PlatformUI;
12import org.eclipse.xtext.parsetree.CompositeNode;
13import org.eclipse.xtext.parsetree.NodeAdapter;
14import org.eclipse.xtext.parsetree.NodeUtil;
15import org.eclipse.xtext.resource.IResourceDescriptions;
16import org.eclipse.xtext.ui.editor.XtextEditor;
17import org.eclipse.xtext.ui.editor.model.XtextDocument;
18import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
19import org.eclipse.xtext.ui.editor.model.edit.ISemanticModification;
20
21import de.ugoe.cs.swe.bnftools.ebnf.Rule;
22import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.InlineRulesProcessor;
23import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
24import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
25import de.ugoe.cs.swe.bnftools.utils.Utils;
26
27public class InlineRuleModification implements ISemanticModification {
28       
29        IResourceDescriptions resourceDescriptions;
30       
31        // ----------------------------------------------------------------------------------------------------
32
33        public InlineRuleModification(IResourceDescriptions resourceDescriptions) {
34                this.resourceDescriptions = resourceDescriptions;
35        }
36
37        // ----------------------------------------------------------------------------------------------------
38       
39        public void apply(EObject element, IModificationContext context)
40                        throws Exception {
41                XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
42                .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
43
44//TODO: better error handling           
45                if (!(editor.getDocument() instanceof XtextDocument)) {
46                        return;
47                }
48
49                if (!(element instanceof Rule))
50                        return;
51               
52                Rule rule = (Rule) element;
53               
54                XtextDocument doc = (XtextDocument) editor.getDocument();
55                EObject root = doc.readOnly(new RootEObjectFinder());
56                NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
57               
58                List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
59               
60                List<CompositeNode> references = Utils.findReferences(rule,
61                                resourceDescriptions, editor);
62               
63                nodePairs.add(new DeclarationReferencesPair(NodeUtil.getNodeAdapter(rule).getParserNode(), references));
64               
65                InlineRulesProcessor processor = new InlineRulesProcessor(editor, rootNode.getParserNode(), doc, nodePairs, true);
66
67                NullProgressMonitor pm = new NullProgressMonitor();
68
69                Change change;
70                try {
71                        change = processor.createChange(pm);
72                        change.initializeValidationData(pm);
73                        change.setEnabled(true);
74                        PerformChangeOperation operation = new PerformChangeOperation(
75                                        change);
76                        operation.setUndoManager(RefactoringCore.getUndoManager(),
77                                        processor.getProcessorName());
78                        operation.run(pm);
79                } catch (Exception e) {
80                        e.printStackTrace();
81                }
82
83        }
84
85}
Note: See TracBrowser for help on using the repository browser.