001: /*
002: * TextLine.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: TextLine.java,v 1.3 2002/10/03 15:23:55 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.UnsupportedEncodingException;
025:
026: public class TextLine extends AbstractLine implements Line {
027: private static final int SAVED = 0x0001;
028: private static final int NEW = 0x0002;
029:
030: private int flags;
031: private String text;
032: private String originalText;
033: private int bits;
034:
035: protected TextLine() {
036: }
037:
038: public TextLine(String s) {
039: text = s;
040: }
041:
042: protected final void init(String s) {
043: text = s;
044: }
045:
046: public final synchronized int flags() {
047: return flags;
048: }
049:
050: public final synchronized void setFlags(int flags) {
051: this .flags = flags;
052: }
053:
054: public final synchronized String getText() {
055: return text != null ? text : "";
056: }
057:
058: public final synchronized void setText(String s) {
059: if (originalText == null)
060: originalText = text;
061: text = s;
062: if (text != null && text.equals(originalText))
063: originalText = null;
064: bits &= ~SAVED;
065: }
066:
067: public final String getOriginalText() {
068: return originalText;
069: }
070:
071: public final void setOriginalText(String s) {
072: originalText = s;
073: }
074:
075: public final boolean isModified() {
076: return originalText != null || isNew();
077: }
078:
079: public final boolean isNew() {
080: return (bits & NEW) == NEW;
081: }
082:
083: public final void setNew(boolean b) {
084: if (b)
085: bits |= NEW;
086: else
087: bits &= ~NEW;
088: }
089:
090: public final boolean isSaved() {
091: return (bits & SAVED) == SAVED;
092: }
093:
094: public final void setSaved(boolean b) {
095: if (b)
096: bits |= SAVED;
097: else
098: bits &= ~SAVED;
099: }
100:
101: public final void unmodified() {
102: originalText = null;
103: bits &= (~SAVED & ~NEW);
104: }
105:
106: public final char charAt(int i) {
107: return getText().charAt(i);
108: }
109:
110: public final String substring(int beginIndex) {
111: return getText().substring(beginIndex);
112: }
113:
114: public final String substring(int beginIndex, int endIndex) {
115: return getText().substring(beginIndex, endIndex);
116: }
117:
118: public final String trim() {
119: return getText().trim();
120: }
121:
122: public final int length() {
123: return getText().length();
124: }
125:
126: public final int getWidth() {
127: return length() * Display.getCharWidth();
128: }
129:
130: public final byte[] getBytes(String encoding)
131: throws UnsupportedEncodingException {
132: byte[] bytes = getText().getBytes(encoding);
133: if (bytes.length >= 2) {
134: if ((bytes[0] == (byte) 0xfe && bytes[1] == (byte) 0xff)
135: || (bytes[0] == (byte) 0xff && bytes[1] == (byte) 0xfe)) {
136: // Get rid of byte order mark.
137: byte[] newBytes = new byte[bytes.length - 2];
138: for (int i = 0; i < newBytes.length; i++)
139: newBytes[i] = bytes[i + 2];
140: return newBytes;
141: }
142: }
143: return bytes;
144: }
145:
146: public final boolean isBlank() {
147: String s = getText();
148:
149: for (int i = s.length(); i-- > 0;)
150: if (!Character.isWhitespace(s.charAt(i)))
151: return false;
152:
153: return true;
154: }
155:
156: // Copies text, original text, and bit flags only.
157: public Line copy() {
158: TextLine line = new TextLine(text);
159: line.originalText = originalText;
160: line.bits = bits;
161: return line;
162: }
163:
164: // Copies text, original text, and bit flags only.
165: public void copy(Line line) {
166: if (line instanceof TextLine) {
167: TextLine textLine = (TextLine) line;
168: text = textLine.text;
169: originalText = textLine.originalText;
170: bits = textLine.bits;
171: } else
172: Debug.bug();
173: }
174: }
|