KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > checkstyle > TrailingSpacesSuggestionPerformer


1 /*
2  * TrailingSpacesSuggestionPerformer.java
3  *
4  * Created on December 30, 2005, 9:06 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.tasklist.checkstyle;
11
12
13 import javax.swing.text.BadLocationException JavaDoc;
14 import javax.swing.text.Document JavaDoc;
15 import javax.swing.text.Element JavaDoc;
16 import javax.swing.text.StyledDocument JavaDoc;
17
18 import org.netbeans.modules.tasklist.client.Suggestion;
19 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
20 import org.netbeans.modules.tasklist.providers.SuggestionContext;
21
22 import org.openide.ErrorManager;
23
24 /** Removes Trailing spaces from line.
25  *
26  * @author hair
27  */

28 public final class TrailingSpacesSuggestionPerformer extends AbstractSuggestionPerformer{
29     
30     /** Creates a new instance of TrailingSpacesSuggestionPerformer */
31     TrailingSpacesSuggestionPerformer(
32             final Document JavaDoc doc,
33             final int lineno) {
34         
35         super(doc,lineno,0);
36     }
37
38     public void perform(final Suggestion suggestion) {
39         
40         final Element JavaDoc elm = getElement(doc, lineno-1);
41         if (elm == null) {
42             ErrorManager.getDefault().log(ErrorManager.USER, "getElement was null");
43             return;
44         }
45         final int offset = elm.getStartOffset();
46         final int endOffset = elm.getEndOffset()-2; // lines finished with '\n' & ''
47
int wsStart = endOffset;
48         try {
49             while( doc.getText(wsStart,1).charAt(0) == ' ' ){
50                 --wsStart;
51             }
52             doc.remove(wsStart+1,endOffset-wsStart);
53         } catch (BadLocationException JavaDoc ex) {
54             ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
55         }
56     }
57    
58     protected void performImpl(int docPosition) throws BadLocationException JavaDoc {
59         throw new UnsupportedOperationException JavaDoc("TrailingSpacesSuggestionProvider overrides perform(..) directly.");
60     }
61 }
62
Popular Tags