001: /*=============================================================================
002: * Copyright Texas Instruments 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 javax.swing.*;
022: import java.io.*;
023:
024: /**
025: * For testing the console.
026: *
027: * @author Rob Clark
028: * @version 0.1
029: */
030: public class Test {
031: private static ti.swing.ConsoleTextArea console;
032:
033: private static Region lastRegion = null;
034:
035: private static int attributeIdx = -1;
036: private static Attribute[] attributes = new Attribute[] {
037: InverseAttribute.INVERSE, FgColorAttribute.RED,
038: FgColorAttribute.GREEN, FgColorAttribute.BLUE, };
039:
040: public static void main(String[] args) {
041: try {
042: JFrame frame = new JFrame("test");
043: console = new ti.swing.ConsoleTextArea(125, 80);
044:
045: frame.getContentPane().add(console);
046:
047: frame.pack();
048: frame.setVisible(true);
049:
050: if (args.length == 0) {
051: while (true) {
052: Thread.sleep(1000);
053: for (int i = 0; i < 5; i++) {
054: append("i=" + i);
055: for (int j = 0; j < 5; j++)
056: append("j="
057: + j
058: + ", 1 2 3 4 5 6 7 8 9 0 a b c d e f g |");
059:
060: zap(5);
061: append("123\n");
062: }
063: }
064: } else {
065: BufferedReader in = new BufferedReader(console
066: .getReader());
067: PrintWriter out = new PrintWriter(console.getWriter());
068:
069: while (true) {
070: out.print("test> ");
071: out.flush();
072:
073: String input = in.readLine();
074:
075: if (input != null) {
076: System.err.println("input: \"" + input + "\"");
077: out.println(input);
078: }
079: }
080: }
081: } catch (Throwable t) {
082: t.printStackTrace();
083: }
084: }
085:
086: private static void zap(int num) {
087: console.getInputHandler().lock();
088: if (lastRegion != null) {
089: console.getInputHandler().removeRegion(lastRegion);
090: lastRegion = null;
091: }
092: console.getInputHandler().zap(num);
093: console.getInputHandler().unlock();
094: }
095:
096: private static void append(String buf) {
097: console.getInputHandler().lock();
098: if (lastRegion != null) {
099: console.getInputHandler().removeRegion(lastRegion);
100: lastRegion = null;
101: }
102: int start = console.getInputHandler().getOffset();
103: console.getInputHandler().append(buf.toCharArray(), 0,
104: buf.length());
105: int end = console.getInputHandler().getOffset();
106: console.getInputHandler().addRegion(
107: lastRegion = getAttribute().getRegion(start,
108: end - start));
109: console.getInputHandler().unlock();
110: }
111:
112: private static Attribute getAttribute() {
113: attributeIdx = ++attributeIdx % attributes.length;
114: return attributes[attributeIdx];
115: }
116: }
117:
118: /*
119: * Local Variables:
120: * tab-width: 2
121: * indent-tabs-mode: nil
122: * mode: java
123: * c-indentation-style: java
124: * c-basic-offset: 2
125: * eval: (c-set-offset 'substatement-open '0)
126: * eval: (c-set-offset 'case-label '+)
127: * eval: (c-set-offset 'inclass '+)
128: * eval: (c-set-offset 'inline-open '0)
129: * End:
130: */
|