5 // Created by Lutz Mueller on 5/16/07.
8 // Copyright (C) 2007 Lutz Mueller
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/>.
26 import java
.awt
.event
.*;
27 import java
.awt
.event
.ActionEvent
;
31 import javax
.swing
.event
.*;
32 import javax
.swing
.text
.*;
35 @SuppressWarnings("unchecked")
36 public class TextAreaWidget
extends aTextWidget
{
39 int lastCharCode
= 65535;
41 Process process
= null;
42 BufferedWriter stdOut
;
43 BufferedReader stdInput
;
44 BufferedReader stdError
;
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
;
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();
83 // goto beggining of line
85 textArea
.setCaretPosition(lastDot
);
88 else if(lastCharCode
== 5)
89 textArea
.setCaretPosition(textArea
.getText().length());
91 // clear screen with Ctrl-L
92 else if(lastCharCode
== 12)
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;
138 catch (IOException ioex
) { System
.out
.println("clear screen:" + ioex
); };
142 public void runShell(StringTokenizer tokens
)
148 command
= lastShellCommand
;
152 command
= Base64Coder
.decodeString(tokens
.nextToken());
153 lastShellCommand
= command
;
156 history
= new Vector();
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;
175 try { setupShell(command
); }
176 catch (IOException ioex
) {
177 ErrorDialog
.show("run-shell", "Could not start " + command
);
182 public void setupShell(String command
) throws IOException
186 process
= Runtime
.getRuntime().exec(command
);
188 //System.out.println("set up:" + command);
192 stdInput
= new BufferedReader(new InputStreamReader(process
.getInputStream(), "UTF8"));
193 stdError
= new BufferedReader( new InputStreamReader(process
.getErrorStream(), "UTF8"));
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();
214 public void evalShell(StringTokenizer params
)
216 String command
= Base64Coder
.decodeString(params
.nextToken());
219 while(retryCount
< 1)
223 textArea
.append("--- restarting shell ---");
225 if(process
== null) break;
230 stdOut
.write(command
, 0, command
.length());
234 catch (IOException ioex
) {
235 textArea
.append("--- eval shell: must restart shell ---");
244 class stdinListener
implements Runnable
253 buff
= new char[256];
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
284 buff
= new char[256];
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());
312 if(history
.size() > 0)
314 textArea
.append((String
)history
.elementAt(historyIndex
));
315 if(historyIndex
< (history
.size() - 1))
321 private class HistoryDownAction
extends AbstractAction
{
322 public void actionPerformed(ActionEvent ev
) {
323 textArea
.setCaretPosition(lastDot
);
324 textArea
.moveCaretPosition(textArea
.getText().length());
326 if(history
.size() > 0)
328 textArea
.append((String
)history
.elementAt(historyIndex
));
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
);
345 byte[] utf8
= text
.getBytes("UTF-8");
346 command
= new String(utf8
);
348 catch (UnsupportedEncodingException e
)
350 System
.out
.println("errorconverting to UTF8");
357 textArea
.append("\n");
359 stdOut
.write(command
, 0, command
.length());
362 if(command
.length() > 0)
363 history
.insertElementAt(new String(text
), 0);
366 catch (IOException ioex
) {
367 textArea
.append("--- cannot execute: restart shell ---");
375 Thread t = new Thread () {