source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/quickfix/processors/RemoveUnusedRulesProcessor.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: 5.1 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.quickfix.processors;
2
3import java.util.List;
4
5import org.eclipse.core.resources.IFile;
6import org.eclipse.core.runtime.CoreException;
7import org.eclipse.core.runtime.IProgressMonitor;
8import org.eclipse.core.runtime.OperationCanceledException;
9import org.eclipse.ltk.core.refactoring.Change;
10import org.eclipse.ltk.core.refactoring.CompositeChange;
11import org.eclipse.ltk.core.refactoring.RefactoringStatus;
12import org.eclipse.ltk.core.refactoring.TextFileChange;
13import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
14import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
15import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
16import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
17import org.eclipse.text.edits.DeleteEdit;
18import org.eclipse.text.edits.MultiTextEdit;
19import org.eclipse.text.edits.TextEditGroup;
20import org.eclipse.xtext.parsetree.CompositeNode;
21import org.eclipse.xtext.ui.editor.XtextEditor;
22import org.eclipse.xtext.ui.editor.model.IXtextDocument;
23
24import de.ugoe.cs.swe.bnftools.utils.Utils;
25
26public class RemoveUnusedRulesProcessor extends RefactoringProcessor {
27        IFile file;
28        private List<CompositeNode> unusedRuleNodes;
29        private IXtextDocument document;
30       
31        // ----------------------------------------------------------------------------------------------------
32
33        public RemoveUnusedRulesProcessor(XtextEditor editor, IXtextDocument doc, List<CompositeNode> unusedRuleNodes) {
34                file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
35                this.unusedRuleNodes = unusedRuleNodes;
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 "Remove Unused Rules Processor";
51        }
52
53        // ----------------------------------------------------------------------------------------------------
54
55        @Override
56        public String getProcessorName() {
57                return "Remove Unused Rules Processor";
58        }
59
60        // ----------------------------------------------------------------------------------------------------
61
62        @Override
63        public boolean isApplicable() throws CoreException {
64                return (unusedRuleNodes != null) && (unusedRuleNodes.size() > 0);
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 (unusedRuleNodes == null) {
76                        status.addFatalError("No unused rule node!");
77                } else if (unusedRuleNodes.size() == 0) {
78                        status.addFatalError("No unused rule node!");
79                }
80                return status;
81        }
82
83        // ----------------------------------------------------------------------------------------------------
84
85        @Override
86        public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
87                        CheckConditionsContext context) throws CoreException,
88                        OperationCanceledException {
89               
90                return checkInitialConditions(pm);
91        }
92
93        // ----------------------------------------------------------------------------------------------------
94
95        @Override
96        public Change createChange(IProgressMonitor pm) throws CoreException,
97                        OperationCanceledException {
98                /*
99                 * Create a change, initialise the IProgressMonitor with 2 task:
100                 * 1. renaming the reference with the rule's right side
101                 * 2. renaming the whole rule with an empty string
102                 */
103                CompositeChange compositeChange = new CompositeChange("Remove Unused Rules");
104                pm.beginTask("Remove Unused Rules", unusedRuleNodes.size());
105               
106                //initialising the edit
107                MultiTextEdit multiEdit = new MultiTextEdit();
108                TextFileChange fileChange = new TextFileChange("Remove Unused Rule", file);
109                fileChange.setEdit(multiEdit);
110                fileChange.setTextType("bnf");
111                compositeChange.add(fileChange);
112               
113                for (int i=0; i < unusedRuleNodes.size(); i++) {
114                        CompositeNode declarationRule = unusedRuleNodes.get(i);
115
116                        // delete the declaration
117                        int offset = declarationRule.getOffset();
118                        int amount = Utils.stepBackNewlines(document, offset);
119                       
120                        DeleteEdit deleteEdit = new DeleteEdit(offset-amount, declarationRule.getLength()+amount);
121                        multiEdit.addChild(deleteEdit);
122                        TextEditGroup editGroup = new TextEditGroup("declaration removal", deleteEdit);
123                        fileChange.addTextEditGroup(editGroup);
124                        pm.worked(1);
125                }
126               
127                return compositeChange;
128               
129        }
130
131        // ----------------------------------------------------------------------------------------------------
132
133        @Override
134        public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
135                        SharableParticipants sharedParticipants) throws CoreException {
136                return null;
137        }
138
139        // ----------------------------------------------------------------------------------------------------
140
141       
142}
Note: See TracBrowser for help on using the repository browser.