001: /*=============================================================================
002: * Copyright Texas Instruments 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.swing.text;
022:
023: import javax.swing.text.*;
024: import java.awt.Graphics;
025:
026: /**
027: * Various utilities used by parts of the editor, but re-written in java for
028: * better performance. Perhaps as objectscript gets better at optimization
029: * some of this can be returned to script.
030: *
031: * @author Rob Clark (rob@ti.com)
032: * @version 1.10
033: */
034: public class ODEUtils {
035: // here for now: (should take CharSequence instead of string, so that
036: // we can later implement a CharSequence that is saveey about the gap
037: // in a GapContent...)
038: public static final int search(String str, int off, int start,
039: int end, Document doc, int inc) {
040: try {
041: return search(str, off, start, end, doc.getText(0, doc
042: .getLength()), inc);
043: } catch (BadLocationException e) {
044: e.printStackTrace(); // XXX should PackageScriptObjectException
045: return -1;
046: }
047: }
048:
049: public static final int search(String str, int off, int start,
050: int end, String data, int inc) {
051: int slen = str.length();
052: if (start < 0)
053: start = 0;
054: if (end >= data.length())
055: end = data.length() - 1;
056:
057: while ((start <= off) && (off < (end - slen))) {
058: if (str.equals(data.substring(off, off + slen)))
059: return off;
060: off += inc;
061: }
062:
063: return -1;
064: }
065:
066: public static final int search(String[] strs, int off, int start,
067: int end, Document doc, int inc) {
068: try {
069: return search(strs, off, start, end, doc.getText(0, doc
070: .getLength()), inc);
071: } catch (BadLocationException e) {
072: e.printStackTrace(); // XXX should PackageScriptObjectException
073: return -1;
074: }
075: }
076:
077: public static final int search(String[] strs, int off, int start,
078: int end, String data, int inc) {
079: int dlen = data.length();
080: if (start < 0)
081: start = 0;
082: if (end >= data.length())
083: end = data.length() - 1;
084:
085: while ((start <= off) && (off < end)) {
086: for (int i = 0; i < strs.length; i++)
087: if (strs[i].equals(data.substring(off, off
088: + strs[i].length())))
089: return off;
090: off += inc;
091: }
092:
093: return -1;
094: }
095:
096: public static final void renderLineNumbers(Graphics g,
097: int startLine, int endLine, int fh) {
098: for (int line = startLine; line < endLine; line++)
099: g.drawString(Integer.toString(line, 10), 0, line * fh);
100: }
101: }
102:
103: /*
104: * Local Variables:
105: * tab-width: 2
106: * indent-tabs-mode: nil
107: * mode: java
108: * c-indentation-style: java
109: * c-basic-offset: 2
110: * eval: (c-set-offset 'substatement-open '0)
111: * eval: (c-set-offset 'case-label '+)
112: * eval: (c-set-offset 'inclass '+)
113: * eval: (c-set-offset 'inline-open '0)
114: * End:
115: */
|