KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > swing > ATEAutoIndentation


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32
33 package org.antlr.works.ate.swing;
34
35 import org.antlr.works.ate.ATEPanel;
36
37 import javax.swing.*;
38 import javax.swing.text.BadLocationException JavaDoc;
39 import javax.swing.text.Document JavaDoc;
40
41 public class ATEAutoIndentation implements Runnable JavaDoc {
42
43     protected int offset;
44     protected int length;
45     protected boolean enabled = true;
46
47     protected ATEPanel textEditor;
48
49     public ATEAutoIndentation(ATEPanel textEditor) {
50         this.textEditor = textEditor;
51     }
52
53     public void setEnabled(boolean enable) {
54         this.enabled = enable;
55     }
56
57     public boolean enabled() {
58         return enabled;
59     }
60
61     public void indent(int offset, int length) {
62         this.offset = offset;
63         this.length = length;
64         if(enabled())
65             SwingUtilities.invokeLater(this);
66     }
67
68     public Document JavaDoc getDocument() {
69         return textEditor.getTextPane().getDocument();
70     }
71
72     public boolean autoIndentAfterReturn() throws BadLocationException JavaDoc {
73         String JavaDoc s = getDocument().getText(offset-1, length+1);
74         if(s.length() == 0)
75             return false;
76
77         if(s.length() == 1 || (s.charAt(0) != '\n' && s.charAt(1) == '\n')) {
78             // Find the beginning of the previous line
79
String JavaDoc t = getDocument().getText(0, offset);
80             for(int i = offset - 2; i >= 0; i--) {
81                 // Stop when a newline is found or the beginning of the text is reached
82
if(t.charAt(i) == '\n' || i == 0) {
83                     // Find the first non-white space/tab character;
84
if(i > 0) {
85                         // Reached \n, increment i to begin past it
86
i++;
87                     }
88                     // Go forward to see how many white-space or tab there are
89
// before reaching a character
90
int start = i;
91                     while((i < offset - 1) && (t.charAt(i) == ' ' || t.charAt(i) == '\t')) {
92                         i++;
93                     }
94
95                     // If we reached the end of the line without any other character
96
// we have to increment i in order to take into account the last
97
// white space or tab
98
if(i == offset - 1 && (t.charAt(i) == ' ' || t.charAt(i) == '\t')) {
99                         i++;
100                     }
101                     getDocument().insertString(offset+1, t.substring(start, i), null);
102                     return true;
103                 }
104             }
105         }
106         return false;
107     }
108
109
110     public void run() {
111         try {
112             if(!autoIndentAfterReturn()) {
113                 textEditor.ateAutoIndent(offset, length);
114             }
115         } catch (BadLocationException JavaDoc e) {
116             // ignore exception
117
}
118     }
119
120 }
121
Popular Tags