KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > TextLine


1 /*
2  * TextLine.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: TextLine.java,v 1.3 2002/10/03 15:23:55 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.UnsupportedEncodingException JavaDoc;
25
26 public class TextLine extends AbstractLine implements Line
27 {
28     private static final int SAVED = 0x0001;
29     private static final int NEW = 0x0002;
30
31     private int flags;
32     private String JavaDoc text;
33     private String JavaDoc originalText;
34     private int bits;
35
36     protected TextLine()
37     {
38     }
39
40     public TextLine(String JavaDoc s)
41     {
42         text = s;
43     }
44
45     protected final void init(String JavaDoc s)
46     {
47         text = s;
48     }
49
50     public final synchronized int flags()
51     {
52         return flags;
53     }
54
55     public final synchronized void setFlags(int flags)
56     {
57         this.flags = flags;
58     }
59
60     public final synchronized String JavaDoc getText()
61     {
62         return text != null ? text : "";
63     }
64
65     public final synchronized void setText(String JavaDoc s)
66     {
67         if (originalText == null)
68             originalText = text;
69         text = s;
70         if (text != null && text.equals(originalText))
71             originalText = null;
72         bits &= ~SAVED;
73     }
74
75     public final String JavaDoc getOriginalText()
76     {
77         return originalText;
78     }
79
80     public final void setOriginalText(String JavaDoc s)
81     {
82         originalText = s;
83     }
84
85     public final boolean isModified()
86     {
87         return originalText != null || isNew();
88     }
89
90     public final boolean isNew()
91     {
92         return (bits & NEW) == NEW;
93     }
94
95     public final void setNew(boolean b)
96     {
97         if (b)
98             bits |= NEW;
99         else
100             bits &= ~NEW;
101     }
102
103     public final boolean isSaved()
104     {
105         return (bits & SAVED) == SAVED;
106     }
107
108     public final void setSaved(boolean b)
109     {
110         if (b)
111             bits |= SAVED;
112         else
113             bits &= ~SAVED;
114     }
115
116     public final void unmodified()
117     {
118         originalText = null;
119         bits &= (~SAVED & ~NEW);
120     }
121
122     public final char charAt(int i)
123     {
124         return getText().charAt(i);
125     }
126
127     public final String JavaDoc substring(int beginIndex)
128     {
129         return getText().substring(beginIndex);
130     }
131
132     public final String JavaDoc substring(int beginIndex, int endIndex)
133     {
134         return getText().substring(beginIndex, endIndex);
135     }
136
137     public final String JavaDoc trim()
138     {
139         return getText().trim();
140     }
141
142     public final int length()
143     {
144         return getText().length();
145     }
146
147     public final int getWidth()
148     {
149         return length() * Display.getCharWidth();
150     }
151
152     public final byte[] getBytes(String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc
153     {
154         byte[] bytes = getText().getBytes(encoding);
155         if (bytes.length >= 2) {
156             if ((bytes[0] == (byte) 0xfe && bytes[1] == (byte) 0xff) ||
157                 (bytes[0] == (byte) 0xff && bytes[1] == (byte) 0xfe)) {
158                 // Get rid of byte order mark.
159
byte[] newBytes = new byte[bytes.length-2];
160                 for (int i = 0; i < newBytes.length; i++)
161                     newBytes[i] = bytes[i+2];
162                 return newBytes;
163             }
164         }
165         return bytes;
166     }
167
168     public final boolean isBlank()
169     {
170         String JavaDoc s = getText();
171
172         for (int i = s.length(); i-- > 0;)
173             if (!Character.isWhitespace(s.charAt(i)))
174                 return false;
175
176         return true;
177     }
178
179     // Copies text, original text, and bit flags only.
180
public Line copy()
181     {
182         TextLine line = new TextLine(text);
183         line.originalText = originalText;
184         line.bits = bits;
185         return line;
186     }
187
188     // Copies text, original text, and bit flags only.
189
public void copy(Line line)
190     {
191         if (line instanceof TextLine) {
192             TextLine textLine = (TextLine) line;
193             text = textLine.text;
194             originalText = textLine.originalText;
195             bits = textLine.bits;
196         } else
197             Debug.bug();
198     }
199 }
200
Popular Tags