Gridarta Editor
TextUtilities.java
Go to the documentation of this file.
1 /*
2  * TextUtilities.java - Utility functions used by the text area classes
3  * Copyright (C) 1999 Slava Pestov
4  * Copyright (C) 2000-2015 The Gridarta Developers.
5  *
6  * You may use and modify this package for any purpose. Redistribution is
7  * permitted, in both source and binary form, provided that this notice
8  * remains intact in all source distributions of this package.
9  */
10 
11 package net.sf.gridarta.textedit.textarea;
12 
13 import javax.swing.text.BadLocationException;
14 import javax.swing.text.Document;
15 
20 public class TextUtilities {
21 
25  private TextUtilities() {
26  }
27 
38  public static int findMatchingBracket(final Document doc, final int offset) throws BadLocationException {
39  if (doc.getLength() == 0) {
40  return -1;
41  }
42  final char c = doc.getText(offset, 1).charAt(0);
43  final char cPrime; // c` - corresponding character
44  final boolean direction; // true = back, false = forward
45 
46  switch (c) {
47  case '(':
48  cPrime = ')';
49  direction = false;
50  break;
51  case ')':
52  cPrime = '(';
53  direction = true;
54  break;
55  case '[':
56  cPrime = ']';
57  direction = false;
58  break;
59  case ']':
60  cPrime = '[';
61  direction = true;
62  break;
63  case '{':
64  cPrime = '}';
65  direction = false;
66  break;
67  case '}':
68  cPrime = '{';
69  direction = true;
70  break;
71  default:
72  return -1;
73  }
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  // Count is 1 initially because we have already
81  // `found' one closing bracket
82  int count = 1;
83 
84  // Get text[0,offset-1];
85  final CharSequence text = doc.getText(0, offset);
86 
87  // Scan backwards
88  for (int i = offset - 1; i >= 0; i--) {
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  final char x = text.charAt(i);
94  if (x == c) {
95  count++;
96  } else if (x == cPrime) {
97  // If text[i] == cPrime, we have found a
98  // opening bracket, so we return i if
99  // --count == 0
100  if (--count == 0) {
101  return i;
102  }
103  }
104  }
105  } else {
106  // Number of characters to check
107  final int len = doc.getLength() - (offset + 1);
108 
109  // Count is 1 initially because we have already
110  // `found' one opening bracket
111  int count = 1;
112 
113  // Get text[offset+1,len];
114  final CharSequence text = doc.getText(offset + 1, len);
115 
116  // Scan forwards
117  for (int i = 0; i < len; i++) {
118  // If text[i] == c, we have found another
119  // opening bracket, therefore we will need
120  // two closing brackets to complete the
121  // match.
122  final char x = text.charAt(i);
123 
124  if (x == c) {
125  count++;
126  } else if (x == cPrime) {
127  // If text[i] == cPrime, we have found an
128  // closing bracket, so we return i if
129  // --count == 0
130  if (--count == 0) {
131  return i + offset + 1;
132  }
133  }
134  }
135  }
136 
137  // Nothing found
138  return -1;
139  }
140 
149  public static int findWordStart(final CharSequence line, final int pos, final String noWordSep) {
150  char ch = line.charAt(pos - 1);
151 
152  final boolean selectNoLetter = selectNoLetter(ch, noWordSep);
153 
154  int wordStart = 0;
155  for (int i = pos - 1; i >= 0; i--) {
156  ch = line.charAt(i);
157  if (selectNoLetter ^ selectNoLetter(ch, noWordSep)) {
158  wordStart = i + 1;
159  break;
160  }
161  }
162 
163  return wordStart;
164  }
165 
174  public static int findWordEnd(final CharSequence line, final int pos, final String noWordSep) {
175  char ch = line.charAt(pos);
176 
177  final boolean selectNoLetter = selectNoLetter(ch, noWordSep);
178 
179  int wordEnd = line.length();
180  for (int i = pos; i < line.length(); i++) {
181  ch = line.charAt(i);
182  if (selectNoLetter ^ selectNoLetter(ch, noWordSep)) {
183  wordEnd = i;
184  break;
185  }
186  }
187  return wordEnd;
188  }
189 
197  private static boolean selectNoLetter(final char ch, final String noWordSep) {
198  return !Character.isLetterOrDigit(ch) && (noWordSep == null || noWordSep.indexOf(ch) == -1);
199  }
200 
201 }
static boolean selectNoLetter(final char ch, final String noWordSep)
Returns whether a character is not part of a word.
static int findMatchingBracket(final Document doc, final int offset)
Returns the offset of the bracket matching the one at the specified offset of the document...
static int findWordStart(final CharSequence line, final int pos, final String noWordSep)
Locates the start of the word at the specified position.
Class with several utility functions used by the text area component.
static int findWordEnd(final CharSequence line, final int pos, final String noWordSep)
Locates the end of the word at the specified position.