1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 2000/04/27 08:59:23 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.terminal;
|
---|
22 |
|
---|
23 | public final class LineReaderTerminal implements TerminalListener {
|
---|
24 |
|
---|
25 | TerminalWin terminal;
|
---|
26 | StringBuffer readLineStr;
|
---|
27 | boolean echoStar;
|
---|
28 | boolean isReadingLine;
|
---|
29 |
|
---|
30 | ExternalMessageException extMsg;
|
---|
31 |
|
---|
32 | static public class ExternalMessageException extends Exception {
|
---|
33 | public ExternalMessageException(String msg) {
|
---|
34 | super(msg);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public LineReaderTerminal(TerminalWin terminal) {
|
---|
39 | this.terminal = terminal;
|
---|
40 | terminal.addTerminalListener(this);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void print(String str) {
|
---|
44 | if(terminal != null) {
|
---|
45 | terminal.write(str);
|
---|
46 | } else {
|
---|
47 | System.out.print(str);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void println(String str) {
|
---|
52 | if(terminal != null) {
|
---|
53 | terminal.write(str + "\n\r");
|
---|
54 | } else {
|
---|
55 | System.out.println(str);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void breakPromptLine(String msg) {
|
---|
60 | if(isReadingLine) {
|
---|
61 | synchronized(this) {
|
---|
62 | extMsg = new ExternalMessageException(msg);
|
---|
63 | this.notify();
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public String readLine(String defaultVal) {
|
---|
69 | synchronized(this) {
|
---|
70 | if(defaultVal != null) {
|
---|
71 | readLineStr = new StringBuffer(defaultVal);
|
---|
72 | terminal.write(defaultVal);
|
---|
73 | } else {
|
---|
74 | readLineStr = new StringBuffer();
|
---|
75 | }
|
---|
76 | isReadingLine = true;
|
---|
77 | try {
|
---|
78 | this.wait();
|
---|
79 | } catch (InterruptedException e) {
|
---|
80 | /* don't care */
|
---|
81 | }
|
---|
82 | isReadingLine = false;
|
---|
83 | }
|
---|
84 | return readLineStr.toString();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public String promptLine(String prompt, String defaultVal, boolean echoStar)
|
---|
88 | throws ExternalMessageException
|
---|
89 | {
|
---|
90 | String line = null;
|
---|
91 | if(terminal != null) {
|
---|
92 | terminal.setAttribute(Terminal.ATTR_BOLD, true);
|
---|
93 | terminal.write(prompt);
|
---|
94 | terminal.setAttribute(Terminal.ATTR_BOLD, false);
|
---|
95 | this.echoStar = echoStar;
|
---|
96 | line = readLine(defaultVal);
|
---|
97 | this.echoStar = false;
|
---|
98 | } /*
|
---|
99 | else {
|
---|
100 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
---|
101 | System.out.print(prompt);
|
---|
102 | line = br.readLine();
|
---|
103 | }
|
---|
104 | */
|
---|
105 | if(extMsg != null) {
|
---|
106 | ExternalMessageException msg = extMsg;
|
---|
107 | extMsg = null;
|
---|
108 | throw msg;
|
---|
109 | }
|
---|
110 | return line;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // TerminalListener interface
|
---|
114 | //
|
---|
115 | public synchronized void typedChar(char c) {
|
---|
116 | if(isReadingLine) {
|
---|
117 | if(c == (char)127 || c == (char)0x08) {
|
---|
118 | if(readLineStr.length() > 0) {
|
---|
119 | boolean ctrlChar = false;
|
---|
120 | if(readLineStr.charAt(readLineStr.length() - 1) < ' ') {
|
---|
121 | ctrlChar = true;
|
---|
122 | }
|
---|
123 | readLineStr.setLength(readLineStr.length() - 1);
|
---|
124 | terminal.write((char)8);
|
---|
125 | if(ctrlChar) terminal.write((char)8);
|
---|
126 | terminal.write(' ');
|
---|
127 | if(ctrlChar) terminal.write(' ');
|
---|
128 | terminal.write((char)8);
|
---|
129 | if(ctrlChar) terminal.write((char)8);
|
---|
130 | } else
|
---|
131 | terminal.doBell();
|
---|
132 | } else if(c == '\r') {
|
---|
133 | this.notify();
|
---|
134 | terminal.write("\n\r");
|
---|
135 | } else {
|
---|
136 | readLineStr.append(c);
|
---|
137 | if(echoStar)
|
---|
138 | terminal.write('*');
|
---|
139 | else
|
---|
140 | terminal.write(c);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void sendBytes(byte[] b) {
|
---|
146 | for(int i = 0; i < b.length; i++)
|
---|
147 | typedChar((char)b[i]);
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void signalWindowChanged(int rows, int cols, int vpixels, int hpixels) {
|
---|
151 | }
|
---|
152 | public void setSelection(String selection) {
|
---|
153 | }
|
---|
154 | public String getSelection() {
|
---|
155 | return null;
|
---|
156 | }
|
---|
157 | public void selectionAvailable(boolean val) {
|
---|
158 | }
|
---|
159 |
|
---|
160 | public static void main(String[] argv) {
|
---|
161 | java.awt.Frame frame = new java.awt.Frame();
|
---|
162 | TerminalWin terminal = new TerminalWin(frame, new TerminalXTerm());
|
---|
163 | LineReaderTerminal linereader = new LineReaderTerminal(terminal);
|
---|
164 |
|
---|
165 | frame.setLayout(new java.awt.BorderLayout());
|
---|
166 | frame.add(terminal.getPanelWithScrollbar(),
|
---|
167 | java.awt.BorderLayout.CENTER);
|
---|
168 |
|
---|
169 | frame.pack();
|
---|
170 | frame.show();
|
---|
171 |
|
---|
172 | linereader.println("Now entering lines...");
|
---|
173 | String line;
|
---|
174 | try {
|
---|
175 | while(true) {
|
---|
176 | line = linereader.promptLine("prompt> ", "", false);
|
---|
177 | System.out.println("line: " + line);
|
---|
178 | }
|
---|
179 | } catch (Exception e) {
|
---|
180 | System.out.println("Error: " + e);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | }
|
---|