Initial commit of newLISP.
[newlisp.git] / guiserver / java / TextAreaWidget.java
blobc0277a052b191fdae01a2c91fb1187c028827e02
1 //
2 // TextAreaWidget.java
3 // guiserver
4 //
5 // Created by Lutz Mueller on 5/16/07.
6 //
7 //
8 // Copyright (C) 2007 Lutz Mueller
9 //
10 // This program is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.awt.event.ActionEvent;
28 import java.util.*;
29 import java.io.*;
30 import javax.swing.*;
31 import javax.swing.event.*;
32 import javax.swing.text.*;
35 @SuppressWarnings("unchecked")
36 public class TextAreaWidget extends aTextWidget {
38 JTextArea textArea;
39 int lastCharCode = 65535;
40 int lastDot = 0;
41 Process process = null;
42 BufferedWriter stdOut;
43 BufferedReader stdInput;
44 BufferedReader stdError;
45 String command = "";
46 int historyIndex = 0;
47 Vector history;
48 String lastShellCommand;
50 public TextAreaWidget(StringTokenizer params)
52 id = params.nextToken();
53 action = params.nextToken();
55 textArea = new JTextArea();
56 textArea.setFont(new Font("SansSerif", Font.PLAIN, 12));
57 textArea.setLineWrap(true);
58 textArea.setWrapStyleWord(true);
60 areaScrollPane = new JScrollPane(textArea);
61 areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
63 container = areaScrollPane;
64 jcomponent = textArea;
65 component = areaScrollPane;
66 textcomp = textArea;
67 isScrollable = true;
69 if(params.hasMoreTokens())
70 areaScrollPane.setPreferredSize(
71 new Dimension(Integer.parseInt(params.nextToken()),Integer.parseInt(params.nextToken())));
73 gsObject.widgets.put(id, this);
75 KeyListener keyListener = new KeyAdapter() {
76 public void keyPressed(KeyEvent e)
78 Character chr = new Character(e.getKeyChar());
79 lastCharCode = chr.hashCode();
81 if(process != null)
83 // goto beggining of line
84 if(lastCharCode == 1)
85 textArea.setCaretPosition(lastDot);
87 // goto end of line
88 else if(lastCharCode == 5)
89 textArea.setCaretPosition(textArea.getText().length());
91 // clear screen with Ctrl-L
92 else if(lastCharCode == 12)
94 textArea.setText("");
95 lastDot = 0;
96 try {
97 stdOut.write(10);
98 stdOut.flush();
100 catch (IOException ioex) { System.out.println("clear screen:" + ioex); };
102 // get last command with Ctrl-P
103 else if(lastCharCode == 16) textArea.append(command.trim());
108 CaretListener caretListener = new CaretListener() {
109 public void caretUpdate(CaretEvent ce)
111 int mark = ce.getMark();
112 int dot = ce.getDot();
113 guiserver.out.println("(" + action + " \"" + id + "\" " + lastCharCode + " " + dot + " " + mark + ")");
114 guiserver.out.flush();
115 lastCharCode = 65535;
119 textArea.addCaretListener(caretListener);
120 textArea.addKeyListener(keyListener);
123 public void setTabSize(StringTokenizer tokens)
125 textArea.setTabSize(Integer.parseInt(tokens.nextToken()));
128 public void clearText(StringTokenizer params)
130 textcomp.setText("");
131 lastCharCode = 65535;
132 lastDot = 0;
133 if(process != null)
134 try {
135 stdOut.write(10);
136 stdOut.flush();
138 catch (IOException ioex) { System.out.println("clear screen:" + ioex); };
142 public void runShell(StringTokenizer tokens)
144 String command;
146 if(tokens == null)
148 command = lastShellCommand;
150 else
152 command = Base64Coder.decodeString(tokens.nextToken());
153 lastShellCommand = command;
156 history = new Vector();
157 historyIndex = 0;
159 if(process != null)
160 process.destroy();
162 InputMap im = textArea.getInputMap();
163 ActionMap am = textArea.getActionMap();
164 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "history-up");
165 am.put("history-up", new HistoryUpAction());
166 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "history-down");
167 am.put("history-down", new HistoryDownAction());
168 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "command");
169 am.put("command", new CommandAction());
172 textArea.setText("");
173 lastCharCode = 65535;
174 lastDot = 0;
175 try { setupShell(command); }
176 catch (IOException ioex) {
177 ErrorDialog.show("run-shell", "Could not start " + command);
178 process = null;
182 public void setupShell(String command) throws IOException
184 String s;
185 int chr;
186 process = Runtime.getRuntime().exec(command);
188 //System.out.println("set up:" + command);
190 if(guiserver.UTF8)
192 stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF8"));
193 stdError = new BufferedReader( new InputStreamReader(process.getErrorStream(), "UTF8"));
195 else
197 stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
198 stdError = new BufferedReader( new InputStreamReader(process.getErrorStream()));
201 stdOut = new BufferedWriter( new OutputStreamWriter(process.getOutputStream()));
203 new Thread(new stdinListener()).start();
204 new Thread(new stderrorListener()).start();
207 public void destroyShell(StringTokenizer params)
209 if(process != null) process.destroy();
210 process = null;
211 stdOut = null;
214 public void evalShell(StringTokenizer params)
216 String command = Base64Coder.decodeString(params.nextToken());
217 int retryCount = 0;
219 while(retryCount < 1)
221 if(process == null)
223 textArea.append("--- restarting shell ---");
224 runShell(null);
225 if(process == null) break;
227 else
229 try {
230 stdOut.write(command, 0, command.length());
231 stdOut.flush();
232 break;
234 catch (IOException ioex) {
235 textArea.append("--- eval shell: must restart shell ---");
236 process = null;
237 //++retryCount;
244 class stdinListener implements Runnable
246 int len;
247 char buff[];
248 String str;
249 String text;
251 stdinListener()
253 buff = new char[256];
256 public void run()
258 try {
259 while((len = stdInput.read(buff, 0, 256)) != -1)
261 str = new String(buff, 0, len);
262 text = textArea.getText();
263 if(text.length() > 100000)
264 textArea.setText(text.substring(50000));
266 textArea.append(str);
267 lastDot = textArea.getText().length();
268 textArea.setCaretPosition(lastDot);
271 catch (IOException ioex) {};
275 class stderrorListener implements Runnable
277 int len;
278 char buff[];
279 String str;
280 String text;
282 stderrorListener()
284 buff = new char[256];
287 public void run()
289 try {
290 while((len = stdError.read(buff, 0, 256)) != -1)
292 str = new String(buff, 0, len);
293 text = textArea.getText();
294 if(text.length() > 100000)
295 textArea.setText(text.substring(50000));
297 textArea.append(str);
298 lastDot = textArea.getText().length();
299 textArea.setCaretPosition(lastDot);
302 catch (IOException ioex) {};
307 private class HistoryUpAction extends AbstractAction {
308 public void actionPerformed(ActionEvent ev) {
309 textArea.setCaretPosition(lastDot);
310 textArea.moveCaretPosition(textArea.getText().length());
311 textArea.cut();
312 if(history.size() > 0)
314 textArea.append((String)history.elementAt(historyIndex));
315 if(historyIndex < (history.size() - 1))
316 historyIndex += 1;
321 private class HistoryDownAction extends AbstractAction {
322 public void actionPerformed(ActionEvent ev) {
323 textArea.setCaretPosition(lastDot);
324 textArea.moveCaretPosition(textArea.getText().length());
325 textArea.cut();
326 if(history.size() > 0)
328 textArea.append((String)history.elementAt(historyIndex));
329 if(historyIndex > 0)
330 historyIndex -= 1;
335 private class CommandAction extends AbstractAction {
336 public void actionPerformed(ActionEvent ev) {
337 String text = textArea.getText();
338 //System.out.println("lastDot:" + lastDot + " length:" + text.length());
339 //System.out.println("->" + command + "<-");
340 text = text.substring(lastDot);
342 if(guiserver.UTF8)
344 try {
345 byte[] utf8 = text.getBytes("UTF-8");
346 command = new String(utf8);
348 catch (UnsupportedEncodingException e)
350 System.out.println("errorconverting to UTF8");
351 command = text;
354 else
355 command = text;
357 textArea.append("\n");
358 try {
359 stdOut.write(command, 0, command.length());
360 stdOut.write(10);
361 stdOut.flush();
362 if(command.length() > 0)
363 history.insertElementAt(new String(text), 0);
364 historyIndex = 0;
366 catch (IOException ioex) {
367 textArea.append("--- cannot execute: restart shell ---");
375 Thread t = new Thread () {
376 public void run() {}
381 // eof //