1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is Rhino JavaScript Debugger code, released
19
* The Initial Developer of the Original Code is
20
* See Beyond Corporation.
21
* Portions created by the Initial Developer are Copyright (C) 2000
22
* the Initial Developer. All Rights Reserved.
27
* Alternatively, the contents of this file may be used under the terms of
28
* the GNU General Public License Version 2 or later (the "GPL"), in which
29
* case the provisions of the GPL are applicable instead of those above. If
30
* you wish to allow use of your version of this file only under the terms of
31
* the GPL and not to allow others to use your version of this file under the
32
* MPL, indicate your decision by deleting the provisions above and replacing
33
* them with the notice and other provisions required by the GPL. If you do
34
* not delete the provisions above, a recipient may use your version of this
35
* file under either the MPL or the GPL.
37
* ***** END LICENSE BLOCK ***** */
38
package org.mozilla.javascript.tools.shell;
41
import java.awt.event.*;
43
import javax.swing.event.*;
44
import javax.swing.text.Document;
45
import javax.swing.text.Segment;
47
class ConsoleWrite implements Runnable {
48
private ConsoleTextArea textArea;
51
public ConsoleWrite(ConsoleTextArea textArea, String str) {
52
this.textArea = textArea;
61
class ConsoleWriter extends java.io.OutputStream {
63
private ConsoleTextArea textArea;
64
private StringBuffer buffer;
66
public ConsoleWriter(ConsoleTextArea textArea) {
67
this.textArea = textArea;
68
buffer = new StringBuffer();
71
public synchronized void write(int ch) {
72
buffer.append((char)ch);
78
public synchronized void write (char[] data, int off, int len) {
79
for(int i = off; i < len; i++) {
80
buffer.append(data[i]);
87
public synchronized void flush() {
88
if (buffer.length() > 0) {
93
public void close () {
97
private void flushBuffer() {
98
String str = buffer.toString();
100
SwingUtilities.invokeLater(new ConsoleWrite(textArea, str));
104
public class ConsoleTextArea
105
extends JTextArea implements KeyListener, DocumentListener
107
static final long serialVersionUID = 8557083244830872961L;
109
private ConsoleWriter console1;
110
private ConsoleWriter console2;
111
private PrintStream out;
112
private PrintStream err;
113
private PrintWriter inPipe;
114
private PipedInputStream in;
115
private java.util.Vector history;
116
private int historyIndex = -1;
117
private int outputMark = 0;
119
public void select(int start, int end) {
121
super.select(start, end);
124
public ConsoleTextArea(String[] argv) {
126
history = new java.util.Vector();
127
console1 = new ConsoleWriter(this);
128
console2 = new ConsoleWriter(this);
129
out = new PrintStream(console1);
130
err = new PrintStream(console2);
131
PipedOutputStream outPipe = new PipedOutputStream();
132
inPipe = new PrintWriter(outPipe);
133
in = new PipedInputStream();
136
} catch(IOException exc) {
137
exc.printStackTrace();
139
getDocument().addDocumentListener(this);
140
addKeyListener(this);
142
setFont(new Font("Monospaced", 0, 12));
146
synchronized void returnPressed() {
147
Document doc = getDocument();
148
int len = doc.getLength();
149
Segment segment = new Segment();
151
doc.getText(outputMark, len - outputMark, segment);
152
} catch(javax.swing.text.BadLocationException ignored) {
153
ignored.printStackTrace();
155
if(segment.count > 0) {
156
history.addElement(segment.toString());
158
historyIndex = history.size();
159
inPipe.write(segment.array, segment.offset, segment.count);
161
outputMark = doc.getLength();
167
public void eval(String str) {
174
public void keyPressed(KeyEvent e) {
175
int code = e.getKeyCode();
176
if(code == KeyEvent.VK_BACK_SPACE || code == KeyEvent.VK_LEFT) {
177
if(outputMark == getCaretPosition()) {
180
} else if(code == KeyEvent.VK_HOME) {
181
int caretPos = getCaretPosition();
182
if(caretPos == outputMark) {
184
} else if(caretPos > outputMark) {
185
if(!e.isControlDown()) {
186
if(e.isShiftDown()) {
187
moveCaretPosition(outputMark);
189
setCaretPosition(outputMark);
194
} else if(code == KeyEvent.VK_ENTER) {
197
} else if(code == KeyEvent.VK_UP) {
199
if(historyIndex >= 0) {
200
if(historyIndex >= history.size()) {
201
historyIndex = history.size() -1;
203
if(historyIndex >= 0) {
204
String str = (String)history.elementAt(historyIndex);
205
int len = getDocument().getLength();
206
replaceRange(str, outputMark, len);
207
int caretPos = outputMark + str.length();
208
select(caretPos, caretPos);
216
} else if(code == KeyEvent.VK_DOWN) {
217
int caretPos = outputMark;
218
if(history.size() > 0) {
220
if(historyIndex < 0) {historyIndex = 0;}
221
int len = getDocument().getLength();
222
if(historyIndex < history.size()) {
223
String str = (String)history.elementAt(historyIndex);
224
replaceRange(str, outputMark, len);
225
caretPos = outputMark + str.length();
227
historyIndex = history.size();
228
replaceRange("", outputMark, len);
231
select(caretPos, caretPos);
236
public void keyTyped(KeyEvent e) {
237
int keyChar = e.getKeyChar();
238
if(keyChar == 0x8 /* KeyEvent.VK_BACK_SPACE */) {
239
if(outputMark == getCaretPosition()) {
242
} else if(getCaretPosition() < outputMark) {
243
setCaretPosition(outputMark);
247
public synchronized void keyReleased(KeyEvent e) {
250
public synchronized void write(String str) {
251
insert(str, outputMark);
252
int len = str.length();
254
select(outputMark, outputMark);
257
public synchronized void insertUpdate(DocumentEvent e) {
258
int len = e.getLength();
259
int off = e.getOffset();
260
if(outputMark > off) {
265
public synchronized void removeUpdate(DocumentEvent e) {
266
int len = e.getLength();
267
int off = e.getOffset();
268
if(outputMark > off) {
269
if(outputMark >= off + len) {
277
public synchronized void postUpdateUI() {
278
// this attempts to cleanup the damage done by updateComponentTreeUI
280
setCaret(getCaret());
281
select(outputMark, outputMark);
284
public synchronized void changedUpdate(DocumentEvent e) {
288
public InputStream getIn() {
292
public PrintStream getOut() {
296
public PrintStream getErr() {