source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/FormatterProcessor.java @ 8

Last change on this file since 8 was 8, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 4.2 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.formatter;
2
3import org.eclipse.core.resources.IFile;
4import org.eclipse.core.runtime.CoreException;
5import org.eclipse.core.runtime.IProgressMonitor;
6import org.eclipse.core.runtime.OperationCanceledException;
7import org.eclipse.jface.text.BadLocationException;
8import org.eclipse.ltk.core.refactoring.Change;
9import org.eclipse.ltk.core.refactoring.CompositeChange;
10import org.eclipse.ltk.core.refactoring.RefactoringStatus;
11import org.eclipse.ltk.core.refactoring.TextFileChange;
12import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
13import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
14import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
15import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
16import org.eclipse.text.edits.MultiTextEdit;
17import org.eclipse.text.edits.ReplaceEdit;
18import org.eclipse.text.edits.TextEditGroup;
19import org.eclipse.xtext.parsetree.AbstractNode;
20import org.eclipse.xtext.parsetree.CompositeNode;
21import org.eclipse.xtext.ui.editor.XtextEditor;
22import org.eclipse.xtext.ui.editor.model.XtextDocument;
23
24import de.ugoe.cs.swe.bnftools.ebnf.Rule;
25
26public class FormatterProcessor extends RefactoringProcessor {
27        IFile file;
28        private CompositeNode rootNode;
29        private XtextDocument document;
30       
31        // ----------------------------------------------------------------------------------------------------
32
33        public FormatterProcessor(XtextEditor editor, CompositeNode root, XtextDocument doc) {
34                file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
35                this.rootNode = root;
36                this.document = doc;
37        }
38       
39        // ----------------------------------------------------------------------------------------------------
40
41        @Override
42        public Object[] getElements() {
43                return null;
44        }
45
46        // ----------------------------------------------------------------------------------------------------
47
48        @Override
49        public String getIdentifier() {
50                return "Formatter Processor";
51        }
52
53        // ----------------------------------------------------------------------------------------------------
54
55        @Override
56        public String getProcessorName() {
57                return "Formatter Processor";
58        }
59
60        // ----------------------------------------------------------------------------------------------------
61
62        @Override
63        public boolean isApplicable() throws CoreException {
64                return (rootNode != null);
65        }
66
67        // ----------------------------------------------------------------------------------------------------
68
69        @Override
70        public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
71                        throws CoreException, OperationCanceledException {
72               
73                RefactoringStatus status = new RefactoringStatus();
74
75                if (rootNode == null)
76                        status.addFatalError("Root node is null!");
77
78                return status;
79        }
80
81        // ----------------------------------------------------------------------------------------------------
82
83        @Override
84        public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
85                        CheckConditionsContext context) throws CoreException,
86                        OperationCanceledException {
87               
88                return checkInitialConditions(pm);
89        }
90
91        // ----------------------------------------------------------------------------------------------------
92
93        @Override
94        public Change createChange(IProgressMonitor pm) throws CoreException,
95                        OperationCanceledException {
96                CompositeChange compositeChange = new CompositeChange("Custom formatter");
97                pm.beginTask("Custom formatter", 2);
98               
99                MultiTextEdit multiEdit = new MultiTextEdit();
100                TextFileChange fileChange = new TextFileChange("Custom formatter text file change", file);
101                fileChange.setEdit(multiEdit);
102                fileChange.setTextType("bnf");
103                compositeChange.add(fileChange);
104               
105                String newText = "foobar";
106               
107                ReplaceEdit replaceEdit = new ReplaceEdit(0, document.getLength(), newText);
108                multiEdit.addChild(replaceEdit);
109               
110                return compositeChange;
111               
112        }
113
114        // ----------------------------------------------------------------------------------------------------
115
116        @Override
117        public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
118                        SharableParticipants sharedParticipants) throws CoreException {
119                return null;
120        }
121
122}
Note: See TracBrowser for help on using the repository browser.