KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > syntax > jedit > TextUtilities


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * TextUtilities.java
28  *
29  */

30
31 package org.syntax.jedit;
32
33 import javax.swing.text.*;
34
35 /**
36  * Class with several utility functions used by the text area component.
37  * @author Slava Pestov
38  * @version $Id: TextUtilities.java 932 2006-10-20 09:32:45Z gtoffoli $
39  */

40 public class TextUtilities
41 {
42     /**
43      * Returns the offset of the bracket matching the one at the
44      * specified offset of the document, or -1 if the bracket is
45      * unmatched (or if the character is not a bracket).
46      * @param doc The document
47      * @param offset The offset
48      * @exception BadLocationException If an out-of-bounds access
49      * was attempted on the document text
50      */

51     public static int findMatchingBracket(Document doc, int offset)
52         throws BadLocationException
53     {
54         if(doc.getLength() == 0)
55             return -1;
56         char c = doc.getText(offset,1).charAt(0);
57         char cprime; // c` - corresponding character
58
boolean direction; // true = back, false = forward
59

60         switch(c)
61         {
62         case '(': cprime = ')'; direction = false; break;
63         case ')': cprime = '('; direction = true; break;
64         case '[': cprime = ']'; direction = false; break;
65         case ']': cprime = '['; direction = true; break;
66         case '{': cprime = '}'; direction = false; break;
67         case '}': cprime = '{'; direction = true; break;
68         default: return -1;
69         }
70
71         int count;
72
73         // How to merge these two cases is left as an exercise
74
// for the reader.
75

76         // Go back or forward
77
if(direction)
78         {
79             // Count is 1 initially because we have already
80
// `found' one closing bracket
81
count = 1;
82
83             // Get text[0,offset-1];
84
String JavaDoc text = doc.getText(0,offset);
85
86             // Scan backwards
87
for(int i = offset - 1; i >= 0; i--)
88             {
89                 // If text[i] == c, we have found another
90
// closing bracket, therefore we will need
91
// two opening brackets to complete the
92
// match.
93
char x = text.charAt(i);
94                 if(x == c)
95                     count++;
96
97                 // If text[i] == cprime, we have found a
98
// opening bracket, so we return i if
99
// --count == 0
100
else if(x == cprime)
101                 {
102                     if(--count == 0)
103                         return i;
104                 }
105             }
106         }
107         else
108         {
109             // Count is 1 initially because we have already
110
// `found' one opening bracket
111
count = 1;
112
113             // So we don't have to + 1 in every loop
114
offset++;
115
116             // Number of characters to check
117
int len = doc.getLength() - offset;
118
119             // Get text[offset+1,len];
120
String JavaDoc text = doc.getText(offset,len);
121
122             // Scan forwards
123
for(int i = 0; i < len; i++)
124             {
125                 // If text[i] == c, we have found another
126
// opening bracket, therefore we will need
127
// two closing brackets to complete the
128
// match.
129
char x = text.charAt(i);
130
131                 if(x == c)
132                     count++;
133
134                 // If text[i] == cprime, we have found an
135
// closing bracket, so we return i if
136
// --count == 0
137
else if(x == cprime)
138                 {
139                     if(--count == 0)
140                         return i + offset;
141                 }
142             }
143         }
144
145         // Nothing found
146
return -1;
147     }
148
149     /**
150      * Locates the start of the word at the specified position.
151      * @param line The text
152      * @param pos The position
153      */

154     public static int findWordStart(String JavaDoc line, int pos, String JavaDoc noWordSep)
155     {
156         char ch = line.charAt(pos - 1);
157
158         if(noWordSep == null)
159             noWordSep = "";
160         boolean selectNoLetter = (!Character.isLetterOrDigit(ch)
161             && noWordSep.indexOf(ch) == -1);
162
163         int wordStart = 0;
164         for(int i = pos - 1; i >= 0; i--)
165         {
166             ch = line.charAt(i);
167             if(selectNoLetter ^ (!Character.isLetterOrDigit(ch) &&
168                 noWordSep.indexOf(ch) == -1))
169             {
170                 wordStart = i + 1;
171                 break;
172             }
173         }
174
175         return wordStart;
176     }
177
178     /**
179      * Locates the end of the word at the specified position.
180      * @param line The text
181      * @param pos The position
182      */

183     public static int findWordEnd(String JavaDoc line, int pos, String JavaDoc noWordSep)
184     {
185         char ch = line.charAt(pos);
186
187         if(noWordSep == null)
188             noWordSep = "";
189         boolean selectNoLetter = (!Character.isLetterOrDigit(ch)
190             && noWordSep.indexOf(ch) == -1);
191
192         int wordEnd = line.length();
193         for(int i = pos; i < line.length(); i++)
194         {
195             ch = line.charAt(i);
196             if(selectNoLetter ^ (!Character.isLetterOrDigit(ch) &&
197                 noWordSep.indexOf(ch) == -1))
198             {
199                 wordEnd = i;
200                 break;
201             }
202         }
203         return wordEnd;
204     }
205 }
206
Popular Tags