001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: * Contributor(s): Ivan Soleimanipour.
042: */
043:
044: /*
045: * "WordDelineator.java"
046: * WordDelineator.java 1.6 01/07/26
047: */
048:
049: package org.netbeans.lib.terminalemulator;
050:
051: /*
052: * Class used by Term to find the boundaries of a <i>word</i>, the region
053: * of text that gets selected when you double-click.
054: *<p>
055: * Term has a default WordDelineator which can be changed by using this class
056: * as an adapter and overriding either charClass() or findLeft() and
057: * findRight() and assigning an object of the resulting class via
058: * Term.setWordDelineator().
059: */
060:
061: public class WordDelineator {
062: /**
063: * Return the <i>character equivalence class</i> of 'c'.
064: *<p>
065: * This is used by findLeft() and findRight() which operate such that
066: * a <i>word</i> is bounded by a change in character class.
067: *<p>
068: * A character equivalence class is characterised by a number, any number,
069: * that is different from numbers for other character classes. For example,
070: * this implementation, which is used as the default WordDelineator for
071: * Term returns 1 for spaces and 0 for everything else.
072: */
073: protected int charClass(char c) {
074: if (Character.isWhitespace(c))
075: return 1;
076: else
077: return 0;
078: }
079:
080: /**
081: * Return index of char at the beginning of the word.
082: */
083: protected int findLeft(StringBuffer buf, int start) {
084: int cclass = charClass(buf.charAt(start));
085:
086: // go left until a character of differing class is found
087: int lx = start;
088: while (lx > 0 && charClass(buf.charAt(lx - 1)) == cclass) {
089: lx--;
090: }
091: return lx;
092: }
093:
094: /**
095: * Return index of char past the word.
096: */
097: protected int findRight(StringBuffer buf, int start) {
098: int cclass = charClass(buf.charAt(start));
099:
100: // go right until a character of a differing class is found.
101: int rx = start;
102: while (rx < buf.length() && charClass(buf.charAt(rx)) == cclass) {
103: rx++;
104: }
105: rx--;
106: return rx;
107: }
108: }
|