KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > terminalemulator > WordDelineator


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 Terminal Emulator.
16  * The Initial Developer of the Original Software is Sun Microsystems, Inc..
17  * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Ivan Soleimanipour.
21  */

22
23 /*
24  * "WordDelineator.java"
25  * WordDelineator.java 1.6 01/07/26
26  */

27
28 package org.netbeans.lib.terminalemulator;
29
30 /*
31  * Class used by Term to find the boundaries of a <i>word</i>, the region
32  * of text that gets selected when you double-click.
33  *<p>
34  * Term has a default WordDelineator which can be changed by using this class
35  * as an adapter and overriding either charClass() or findLeft() and
36  * findRight() and assigning an object of the resulting class via
37  * Term.setWordDelineator().
38  */

39
40 public class WordDelineator {
41     /**
42      * Return the <i>character equivalence class</i> of 'c'.
43      *<p>
44      * This is used by findLeft() and findRight() which operate such that
45      * a <i>word</i> is bounded by a change in character class.
46      *<p>
47      * A character equivalence class is characterised by a number, any number,
48      * that is different from numbers for other character classes. For example,
49      * this implementation, which is used as the default WordDelineator for
50      * Term returns 1 for spaces and 0 for everything else.
51      */

52     protected int charClass(char c) {
53     if (Character.isWhitespace(c))
54         return 1;
55     else
56         return 0;
57     }
58
59     /**
60      * Return index of char at the beginning of the word.
61      */

62     protected int findLeft(StringBuffer JavaDoc buf, int start) {
63     int cclass = charClass(buf.charAt(start));
64
65     // go left until a character of differing class is found
66
int lx = start;
67     while (lx > 0 && charClass(buf.charAt(lx-1)) == cclass) {
68         lx--;
69     }
70     return lx;
71     }
72
73     /**
74      * Return index of char past the word.
75      */

76     protected int findRight(StringBuffer JavaDoc buf, int start) {
77     int cclass = charClass(buf.charAt(start));
78
79     // go right until a character of a differing class is found.
80
int rx = start;
81     while (rx < buf.length() && charClass(buf.charAt(rx)) == cclass) {
82         rx++;
83     }
84     rx--;
85     return rx;
86     }
87 }
88
89
Popular Tags