001: /*=============================================================================
002: * Copyright Texas Instruments, Inc., 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (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
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.swing.console;
020:
021: import ti.exceptions.ProgrammingErrorException;
022:
023: /**
024: * Row is a utility class used by {@link ConsoleBuffer} to managed data for a
025: * particular row. A row can track whether it needs to be redrawn (see
026: * {@link #needsRedraw}, and be edited using a similar interface as the console
027: * itself ({@link #append}, {@link #zap}). The <code>ConsoleBuffer</code> is
028: * responsible for handling line breaks, as the <code>Row</code> has no
029: * knowledge of the max number of columns per row.
030: * <p>
031: * Nothing in this class is synchronized, because everything that could
032: * potentially need synchronization should be called from the protection
033: * of synchronized code in {@link ConsoleBuffer}.
034: *
035: * @author Rob Clark
036: * @version 0.0
037: */
038: class Row implements java.io.Serializable {
039: /* Internally the data is cached as a string, but converted to StringBuffer
040: * when it needs editing...
041: */
042: private String str = "";
043: private StringBuffer sb = null;
044:
045: private final StringBuffer getStringBuffer() {
046: if (sb == null)
047: sb = new StringBuffer(str);
048: return sb;
049: }
050:
051: /**
052: * @see #getOffset
053: */
054: private int offset;
055:
056: /**
057: * @see #needsRedraw
058: */
059: private boolean dirty = true;
060: private boolean visible = true;
061:
062: /**
063: * Class Constructor.
064: *
065: * @param offset offset of this row within the console character
066: * stream
067: */
068: Row(int offset) {
069: this .offset = offset;
070: }
071:
072: /**
073: * The offset of the beginning of this row within the document stream.
074: * The offset<sub>i+1</sub> (offset of row i+1) is offset<sub>i</sub> +
075: * length<sub>i</sub>
076: *
077: * @return the offset
078: */
079: final int getOffset() {
080: return offset;
081: }
082:
083: /**
084: * Get the number of characters in this row.
085: */
086: final int getLength() {
087: if (sb != null)
088: return sb.length();
089: else
090: return str.length();
091: }
092:
093: /**
094: * Get the row data.
095: * @return a string
096: */
097: final String getData() {
098: if (sb != null) {
099: str = sb.toString();
100: sb = null;
101: }
102:
103: return str;
104: }
105:
106: /**
107: * Does this row need to be re-rendered? It is automatically marked dirty
108: * if the contents are changed (see {@link #append} and {@link #zap}, or
109: * can be marked dirty (for example if a new region is mapped over a part
110: * of this row) by calling {@link #markDirty}.
111: *
112: * @return should this row be re-rendered?
113: * @see #append
114: * @see #zap
115: * @see #markDirty
116: */
117: final boolean needsRedraw() {
118: return dirty && visible;
119: }
120:
121: /**
122: * Mark this row as needing to be repainted.
123: */
124: final void markDirty() {
125: dirty = true;
126: }
127:
128: /**
129: * Mark this row as not needing repaint.
130: */
131: final void markClean() {
132: dirty = false;
133: }
134:
135: /**
136: * Indicate whether this row is visible, as in not hidden based on the
137: * current clip-rect.
138: */
139: final void setVisible(boolean visible) {
140: this .visible = visible;
141: }
142:
143: /**
144: * Append characters to the end of this row.
145: *
146: * @param cbuf the character buffer
147: * @param off the offset into cbuf to first character to append
148: * @param len the number of characters to append
149: */
150: final void append(char[] cbuf, int off, int len) {
151: if ((off + len) > cbuf.length)
152: throw new ProgrammingErrorException("cbuf too short");
153:
154: getStringBuffer().append(cbuf, off, len);
155: markDirty();
156: }
157:
158: /**
159: * Delete characters from end of this row.
160: *
161: * @param num the number of characters to delete
162: */
163: final void zap(int num) {
164: int len = getLength();
165:
166: if (num > len)
167: throw new ProgrammingErrorException("num > len");
168:
169: getStringBuffer().delete(len - num, len);
170: markDirty();
171: }
172: }
173:
174: /*
175: * Local Variables:
176: * tab-width: 2
177: * indent-tabs-mode: nil
178: * mode: java
179: * c-indentation-style: java
180: * c-basic-offset: 2
181: * eval: (c-set-offset 'substatement-open '0)
182: * eval: (c-set-offset 'case-label '+)
183: * eval: (c-set-offset 'inclass '+)
184: * eval: (c-set-offset 'inline-open '0)
185: * End:
186: */
|