svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / console / jedit / TextUtilities.java @ 40561
History | View | Annotate | Download (5.26 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* 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, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.utils.console.jedit; |
25 |
/*
|
26 |
* TextUtilities.java - Utility functions used by the text area classes
|
27 |
* Copyright (C) 1999 Slava Pestov
|
28 |
*
|
29 |
* You may use and modify this package for any purpose. Redistribution is
|
30 |
* permitted, in both source and binary form, provided that this notice
|
31 |
* remains intact in all source distributions of this package.
|
32 |
*/
|
33 |
|
34 |
import javax.swing.text.BadLocationException; |
35 |
import javax.swing.text.Document; |
36 |
|
37 |
/**
|
38 |
* Class with several utility functions used by the text area component.
|
39 |
* @author Slava Pestov
|
40 |
* @version $Id$
|
41 |
*/
|
42 |
public class TextUtilities |
43 |
{ |
44 |
/**
|
45 |
* Returns the offset of the bracket matching the one at the
|
46 |
* specified offset of the document, or -1 if the bracket is
|
47 |
* unmatched (or if the character is not a bracket).
|
48 |
* @param doc The document
|
49 |
* @param offset The offset
|
50 |
* @exception BadLocationException If an out-of-bounds access
|
51 |
* was attempted on the document text
|
52 |
*/
|
53 |
public static int findMatchingBracket(Document doc, int offset) |
54 |
throws BadLocationException |
55 |
{ |
56 |
if(doc.getLength() == 0) |
57 |
return -1; |
58 |
char c = doc.getText(offset,1).charAt(0); |
59 |
char cprime; // c` - corresponding character |
60 |
boolean direction; // true = back, false = forward |
61 |
|
62 |
switch(c)
|
63 |
{ |
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 |
case '{': cprime = '}'; direction = false; break; |
69 |
case '}': cprime = '{'; direction = true; break; |
70 |
default: return -1; |
71 |
} |
72 |
|
73 |
int count;
|
74 |
|
75 |
// How to merge these two cases is left as an exercise
|
76 |
// for the reader.
|
77 |
|
78 |
// Go back or forward
|
79 |
if(direction)
|
80 |
{ |
81 |
// Count is 1 initially because we have already
|
82 |
// `found' one closing bracket
|
83 |
count = 1;
|
84 |
|
85 |
// Get text[0,offset-1];
|
86 |
String text = doc.getText(0,offset); |
87 |
|
88 |
// Scan backwards
|
89 |
for(int i = offset - 1; i >= 0; i--) |
90 |
{ |
91 |
// If text[i] == c, we have found another
|
92 |
// closing bracket, therefore we will need
|
93 |
// two opening brackets to complete the
|
94 |
// match.
|
95 |
char x = text.charAt(i);
|
96 |
if(x == c)
|
97 |
count++; |
98 |
|
99 |
// If text[i] == cprime, we have found a
|
100 |
// opening bracket, so we return i if
|
101 |
// --count == 0
|
102 |
else if(x == cprime) |
103 |
{ |
104 |
if(--count == 0) |
105 |
return i;
|
106 |
} |
107 |
} |
108 |
} |
109 |
else
|
110 |
{ |
111 |
// Count is 1 initially because we have already
|
112 |
// `found' one opening bracket
|
113 |
count = 1;
|
114 |
|
115 |
// So we don't have to + 1 in every loop
|
116 |
offset++; |
117 |
|
118 |
// Number of characters to check
|
119 |
int len = doc.getLength() - offset;
|
120 |
|
121 |
// Get text[offset+1,len];
|
122 |
String text = doc.getText(offset,len);
|
123 |
|
124 |
// Scan forwards
|
125 |
for(int i = 0; i < len; i++) |
126 |
{ |
127 |
// If text[i] == c, we have found another
|
128 |
// opening bracket, therefore we will need
|
129 |
// two closing brackets to complete the
|
130 |
// match.
|
131 |
char x = text.charAt(i);
|
132 |
|
133 |
if(x == c)
|
134 |
count++; |
135 |
|
136 |
// If text[i] == cprime, we have found an
|
137 |
// closing bracket, so we return i if
|
138 |
// --count == 0
|
139 |
else if(x == cprime) |
140 |
{ |
141 |
if(--count == 0) |
142 |
return i + offset;
|
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
// Nothing found
|
148 |
return -1; |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Locates the start of the word at the specified position.
|
153 |
* @param line The text
|
154 |
* @param pos The position
|
155 |
*/
|
156 |
public static int findWordStart(String line, int pos, String noWordSep) |
157 |
{ |
158 |
char ch = line.charAt(pos - 1); |
159 |
|
160 |
if(noWordSep == null) |
161 |
noWordSep = "";
|
162 |
boolean selectNoLetter = (!Character.isLetterOrDigit(ch) |
163 |
&& noWordSep.indexOf(ch) == -1);
|
164 |
|
165 |
int wordStart = 0; |
166 |
for(int i = pos - 1; i >= 0; i--) |
167 |
{ |
168 |
ch = line.charAt(i); |
169 |
if(selectNoLetter ^ (!Character.isLetterOrDigit(ch) && |
170 |
noWordSep.indexOf(ch) == -1))
|
171 |
{ |
172 |
wordStart = i + 1;
|
173 |
break;
|
174 |
} |
175 |
} |
176 |
|
177 |
return wordStart;
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Locates the end of the word at the specified position.
|
182 |
* @param line The text
|
183 |
* @param pos The position
|
184 |
*/
|
185 |
public static int findWordEnd(String line, int pos, String noWordSep) |
186 |
{ |
187 |
char ch = line.charAt(pos);
|
188 |
|
189 |
if(noWordSep == null) |
190 |
noWordSep = "";
|
191 |
boolean selectNoLetter = (!Character.isLetterOrDigit(ch) |
192 |
&& noWordSep.indexOf(ch) == -1);
|
193 |
|
194 |
int wordEnd = line.length();
|
195 |
for(int i = pos; i < line.length(); i++) |
196 |
{ |
197 |
ch = line.charAt(i); |
198 |
if(selectNoLetter ^ (!Character.isLetterOrDigit(ch) && |
199 |
noWordSep.indexOf(ch) == -1))
|
200 |
{ |
201 |
wordEnd = i; |
202 |
break;
|
203 |
} |
204 |
} |
205 |
return wordEnd;
|
206 |
} |
207 |
} |