KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > terminalemulator > Junk


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is Terminal Emulator.
16  * The Initial Developer of the Original Software is Sun Microsystems, Inc..
17  * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Ivan Soleimanipour.
21  */

22
23 /*
24  * "Line.java"
25  * Line.java 1.10 01/07/10
26  */

27
28 package org.netbeans.lib.terminalemulator;
29
30 class Line {
31     public int glyph_glyph;
32     public int glyph_rendition; // Background color for the whole line
33
// This is independent of per-character
34
// rendition.
35
private char buf[];
36     private int attr[];
37
38     // SHOULD use shorts?
39
private int capacity; // == buf.length == attr.length
40
private int length; // how much of buf and attr is filled
41

42
43     public Line() {
44     reset();
45     }
46
47     public void reset() {
48     length = 0;
49     capacity = 32;
50     buf = new char[capacity];
51     Attr = null;
52     glyph_glyph = 0;
53     glyph_rendition = 0;
54     wrapped = false;
55     about_to_wrap = false;
56     }
57
58
59     public int capacity() {
60     return Capacity;
61     }
62     public int length() {
63     return Length;
64     }
65
66     public boolean hasAttributes() {
67     return attr != null;
68     }
69
70     public void setWrapped(boolean wrapped) {
71     this.wrapped = wrapped;
72     }
73     public boolean isWrapped() {
74     return wrapped;
75     }
76     // SHOULD collapse wrapped with about_to_wrap into a bitfield
77
private boolean wrapped;
78
79
80
81     public boolean setAboutToWrap(boolean about_to_wrap) {
82     boolean old_about_to_wrap = about_to_wrap;
83     this.about_to_wrap = about_to_wrap;
84     return old_about_to_wrap;
85     }
86     public boolean isAboutToWrap() {
87     return about_to_wrap;
88     }
89     // Perhaps SHOULD have a state: normal, about-to-wrap, wrapped.
90
private boolean about_to_wrap;
91
92
93     /**
94      * Return true if we've already seen attributes for this line
95      * or 'a' is the first one, in which case we allocate the 'attr' array.
96      */

97     private boolean haveAttributes(int a) {
98     if (attr == null && a != 0) {
99         attr = new int[capacity];
100     }
101     return attr != null;
102     }
103
104
105     public Char [] charArray() {
106     return buf;
107     }
108     public int [] attrArray() {
109     return attr;
110     }
111
112
113     public StringBuffer JavaDoc stringBuffer() {
114     // only used for word finding
115
// Grrr, why don't we have: new StringBuffer(buf, 0, length);
116
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(length);
117     return sb.append(buf, 0, length);
118     }
119
120     /*
121      * Ensure that our capacity is min_capacity rounded up to 8.
122      */

123     private void ensureCapacity(int min_capacity) {
124
125     if (min_capacity <= capacity)
126         return; // nothing to do
127

128     /* OLD
129
130     // constant increments
131
132     int new_capacity = min_capacity;
133
134     // round up to 8
135     new_capacity &= ~0x7;
136     new_capacity += 8;
137     */

138
139     // doubling
140
int new_capacity = (length+1) * 2;
141     if (new_capacity < 0)
142         new_capacity = Integer.MAX_VALUE;
143     else if (min_capacity > new_capacity)
144         new_capacity = min_capacity;
145
146
147     char new_buf[] = new char[new_capacity];
148     System.arraycopy(buf, 0, new_buf, 0, length);
149     buf = new_buf;
150
151     if (attr != null) {
152         int new_attr[] = new int[new_capacity];
153         System.arraycopy(attr, 0, new_attr, 0, length);
154         attr = new_attr;
155     }
156
157     capacity = new_capacity;
158     }
159
160     /**
161      * Insert character and attribute at 'column' and shift everything
162      * past 'column' right.
163      */

164     public void insertCharAt(char c, int column, int a) {
165     int new_length = length + 1;
166     ensureCapacity(new_length);
167
168     System.arraycopy(buf, column, buf, column + 1, length - column);
169     buf[column] = c;
170
171     if (haveAttributes(a)) {
172         System.arraycopy(attr, column, attr, column + 1, length - column);
173         attr[column] = a;
174     }
175
176     length = new_length;
177     }
178
179     /*
180      * Generic addition and modification.
181      * Line will grow to accomodate column.
182      */

183     public void setCharAt(char c, int column, int a) {
184     if (column >= length) {
185         ensureCapacity(column+1);
186         length = column+1;
187     }
188     buf[column] = c;
189     if (haveAttributes(a)) {
190         attr[column] = a;
191     }
192     }
193
194     public void deleteCharAt(int column) {
195     if (column < 0 || column >= length)
196         return;
197     System.arraycopy(buf, column+1, buf, column, length-column-1);
198     buf[length-1] = 0;
199     if (attr != null) {
200         System.arraycopy(attr, column+1, attr, column, length-column-1);
201         attr[length-1] = 0;
202     }
203     // SHOULD move this up
204
length--;
205     }
206
207     public void clearToEndFrom(int col) {
208     ensureCapacity(col+1);
209
210     // Grrr, why is there a System.arrayCopy() but no System.arrayClear()?
211
for (int cx = col; cx < length; cx++)
212         buf[cx] = 0;
213     if (attr != null) {
214         for (int cx = col; cx < length; cx++)
215         attr[cx] = 0;
216     }
217     length = col;
218     }
219
220
221     /*
222      * Used for selections
223      * If the ecol is past the actual line length a "\n" is appended.
224      */

225     public String JavaDoc text(int bcol, int ecol) {
226     // System.out.println("Line.text(bcol " + bcol + " ecol " + ecol + ")");
227
// System.out.println("\tlength " + length);
228

229     String JavaDoc newline = "";
230
231     // this only happens for "empty" lines below the cursor.
232
if (length == 0)
233         return "";
234
235     if (ecol >= length) {
236         // The -1 snuffs out the newline.
237
ecol = length-1;
238         newline = "\n";
239
240         if (bcol >= length)
241         bcol = length-1;
242     }
243     return new String JavaDoc(buf, bcol, ecol-bcol+1) + newline;
244     }
245
246     public void setCharacterAttribute(int bcol, int ecol,
247                       int value, boolean on) {
248     // HACK: value is the ANSI code, haveAttributes takes out own
249
// compact encoding, but it only checks for 0 so it's OK.
250
if (!haveAttributes(value))
251         return;
252
253     if (on) {
254         for (int c = bcol; c <= ecol; c++)
255         attr[c] = Attr.setAttribute(attr[c], value);
256     } else {
257         for (int c = bcol; c <= ecol; c++)
258         attr[c] = Attr.unsetAttribute(attr[c], value);
259     }
260     }
261 }
262
Popular Tags