KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.checkstyle;
21
22
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import javax.swing.text.Element JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27
28 import org.netbeans.modules.tasklist.client.Suggestion;
29 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
30
31 import org.openide.ErrorManager;
32
33 /** Handles remembering (through changes as well) the line and column of the violation.
34  *
35  * @author <a HREF="mailto:mick@wever.org">Michael Semb Wever</a>
36  * @version $Id: AbstractSuggestionPerformer.java,v 1.3 2006/07/12 15:23:34 hair Exp $
37  */

38 public abstract class AbstractSuggestionPerformer implements SuggestionPerformer {
39
40     /** TODO comment me **/
41     protected final Document JavaDoc doc;
42
43     /** TODO comment me **/
44     protected final int lineno;
45
46     /** TODO comment me **/
47     protected String JavaDoc columnOnwards;
48     
49     protected String JavaDoc originalLine;
50
51     /**
52      * Creates a new instance of AbstractSuggestionPerformer
53      */

54     AbstractSuggestionPerformer(
55             final Document JavaDoc doc,
56             final int lineno,
57             final int column) {
58
59         this.doc = doc;
60         this.lineno = lineno;
61
62         // instead of remembering the column remember the string from the column to the end of the line.
63
final Element JavaDoc elm = getElement(doc, lineno - 1);
64         if (elm == null) {
65             ErrorManager.getDefault().log(ErrorManager.USER, "getElement was null");
66             return;
67         }
68         final int offset = elm.getStartOffset();
69         final int endOffset = elm.getEndOffset() - 1;
70         final int columnOffset = offset + Math.max(0, column) - 1;
71         try {
72
73             originalLine = doc.getText(columnOffset, endOffset);
74             columnOnwards = doc.getText(columnOffset, endOffset - columnOffset);
75
76         } catch (BadLocationException JavaDoc ex) {
77             ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
78         }
79     }
80
81     /** TODO comment me **/
82     public void perform(final Suggestion suggestion) {
83
84         if (!performOnAdjacentLine(lineno, false) && !performOnAdjacentLine(lineno, true)) {
85
86             ErrorManager.getDefault().log(ErrorManager.USER,
87                                     "Lost position of violation, no fix performed");
88         }
89     }
90
91     /** TODO comment me **/
92     protected abstract void performImpl(int docPosition) throws BadLocationException JavaDoc;
93
94     /** Such a simple operation there's no need to ask for confirmation.
95      * Also a little tricky to display whitespace being deleted!
96      **/

97     public Object JavaDoc getConfirmation(final Suggestion suggestion) {
98         return null;
99     }
100
101     /** TODO comment me **/
102     public boolean hasConfirmation() {
103         return false;
104     }
105
106     /** copied from ChangeCopyrightDatesPerformer **/
107     protected final static Element JavaDoc getElement(final Document JavaDoc d, final int linenumber) {
108         if (d == null) {
109             ErrorManager.getDefault().log(ErrorManager.USER, "d was null");
110             return null;
111         }
112
113         if (!(d instanceof StyledDocument JavaDoc)) {
114             ErrorManager.getDefault().log(ErrorManager.USER, "Not a styleddocument");
115             return null;
116         }
117
118         final StyledDocument JavaDoc doc = (StyledDocument JavaDoc) d;
119         Element JavaDoc e = doc.getParagraphElement(0).getParentElement();
120         if (e == null) {
121             // try default root (should work for text/plain)
122
e = doc.getDefaultRootElement();
123         }
124         final Element JavaDoc elm = e.getElement(linenumber);
125         return elm;
126     }
127
128     /** TODO comment me **/
129     private boolean performOnAdjacentLine(final int lNumber, final boolean incrementLine) {
130
131         boolean result = false;
132         if (lNumber > 0) {
133             final Element JavaDoc elm = getElement(doc, lNumber - 1);
134             if (elm == null) {
135                 ErrorManager.getDefault().log(ErrorManager.USER, "getElement was null");
136
137             } else {
138                 final int offset = elm.getStartOffset();
139                 final int endOffset = elm.getEndOffset() - 1;
140                 try {
141                     final String JavaDoc line = doc.getText(offset, endOffset - offset);
142                     final int idx = line.indexOf(columnOnwards);
143                     if (line.equals(originalLine) && idx >= 0) {
144                         performImpl(offset + idx);
145                         result = true;
146
147                     } else {
148                         // try preceding line
149
result = performOnAdjacentLine(incrementLine ? lNumber + 1 : lNumber - 1, incrementLine);
150                     }
151                 } catch (BadLocationException JavaDoc ex) {
152                     ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
153                 }
154             }
155         }
156         return result;
157     }
158 }
159
Popular Tags