| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.mozilla.javascript.tools.shell; |
| 7 |
import java.io.*; |
| 8 |
import java.awt.*; |
| 9 |
import java.awt.event.*; |
| 10 |
import javax.swing.*; |
| 11 |
import javax.swing.event.*; |
| 12 |
import javax.swing.text.Document; |
| 13 |
import javax.swing.text.Segment; |
| 14 |
|
| 15 |
class ConsoleWrite implements Runnable { |
| 16 |
private ConsoleTextArea textArea; |
| 17 |
private String str; |
| 18 |
|
| 19 |
public ConsoleWrite(ConsoleTextArea textArea, String str) { |
| 20 |
this.textArea = textArea; |
| 21 |
this.str = str; |
| 22 |
} |
| 23 |
|
| 24 |
public void run() { |
| 25 |
textArea.write(str); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
class ConsoleWriter extends java.io.OutputStream { |
| 30 |
|
| 31 |
private ConsoleTextArea textArea; |
| 32 |
private StringBuffer buffer; |
| 33 |
|
| 34 |
public ConsoleWriter(ConsoleTextArea textArea) { |
| 35 |
this.textArea = textArea; |
| 36 |
buffer = new StringBuffer(); |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public synchronized void write(int ch) { |
| 41 |
buffer.append((char)ch); |
| 42 |
if(ch == '\n') { |
| 43 |
flushBuffer(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public synchronized void write (char[] data, int off, int len) { |
| 48 |
for(int i = off; i < len; i++) { |
| 49 |
buffer.append(data[i]); |
| 50 |
if(data[i] == '\n') { |
| 51 |
flushBuffer(); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public synchronized void flush() { |
| 58 |
if (buffer.length() > 0) { |
| 59 |
flushBuffer(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void close () { |
| 65 |
flush(); |
| 66 |
} |
| 67 |
|
| 68 |
private void flushBuffer() { |
| 69 |
String str = buffer.toString(); |
| 70 |
buffer.setLength(0); |
| 71 |
SwingUtilities.invokeLater(new ConsoleWrite(textArea, str)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public class ConsoleTextArea |
| 76 |
extends JTextArea implements KeyListener, DocumentListener |
| 77 |
{ |
| 78 |
static final long serialVersionUID = 8557083244830872961L; |
| 79 |
|
| 80 |
private ConsoleWriter console1; |
| 81 |
private ConsoleWriter console2; |
| 82 |
private PrintStream out; |
| 83 |
private PrintStream err; |
| 84 |
private PrintWriter inPipe; |
| 85 |
private PipedInputStream in; |
| 86 |
private java.util.List<String> history; |
| 87 |
private int historyIndex = -1; |
| 88 |
private int outputMark = 0; |
| 89 |
|
| 90 |
@Override |
| 91 |
public void select(int start, int end) { |
| 92 |
requestFocus(); |
| 93 |
super.select(start, end); |
| 94 |
} |
| 95 |
|
| 96 |
public ConsoleTextArea(String[] argv) { |
| 97 |
super(); |
| 98 |
history = new java.util.ArrayList<String>(); |
| 99 |
console1 = new ConsoleWriter(this); |
| 100 |
console2 = new ConsoleWriter(this); |
| 101 |
out = new PrintStream(console1, true); |
| 102 |
err = new PrintStream(console2, true); |
| 103 |
PipedOutputStream outPipe = new PipedOutputStream(); |
| 104 |
inPipe = new PrintWriter(outPipe); |
| 105 |
in = new PipedInputStream(); |
| 106 |
try { |
| 107 |
outPipe.connect(in); |
| 108 |
} catch(IOException exc) { |
| 109 |
exc.printStackTrace(); |
| 110 |
} |
| 111 |
getDocument().addDocumentListener(this); |
| 112 |
addKeyListener(this); |
| 113 |
setLineWrap(true); |
| 114 |
setFont(new Font("Monospaced", 0, 12)); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
synchronized void returnPressed() { |
| 119 |
Document doc = getDocument(); |
| 120 |
int len = doc.getLength(); |
| 121 |
Segment segment = new Segment(); |
| 122 |
try { |
| 123 |
doc.getText(outputMark, len - outputMark, segment); |
| 124 |
} catch(javax.swing.text.BadLocationException ignored) { |
| 125 |
ignored.printStackTrace(); |
| 126 |
} |
| 127 |
if(segment.count > 0) { |
| 128 |
history.add(segment.toString()); |
| 129 |
} |
| 130 |
historyIndex = history.size(); |
| 131 |
inPipe.write(segment.array, segment.offset, segment.count); |
| 132 |
append("\n"); |
| 133 |
outputMark = doc.getLength(); |
| 134 |
inPipe.write("\n"); |
| 135 |
inPipe.flush(); |
| 136 |
console1.flush(); |
| 137 |
} |
| 138 |
|
| 139 |
public void eval(String str) { |
| 140 |
inPipe.write(str); |
| 141 |
inPipe.write("\n"); |
| 142 |
inPipe.flush(); |
| 143 |
console1.flush(); |
| 144 |
} |
| 145 |
|
| 146 |
public void keyPressed(KeyEvent e) { |
| 147 |
int code = e.getKeyCode(); |
| 148 |
if(code == KeyEvent.VK_BACK_SPACE || code == KeyEvent.VK_LEFT) { |
| 149 |
if(outputMark == getCaretPosition()) { |
| 150 |
e.consume(); |
| 151 |
} |
| 152 |
} else if(code == KeyEvent.VK_HOME) { |
| 153 |
int caretPos = getCaretPosition(); |
| 154 |
if(caretPos == outputMark) { |
| 155 |
e.consume(); |
| 156 |
} else if(caretPos > outputMark) { |
| 157 |
if(!e.isControlDown()) { |
| 158 |
if(e.isShiftDown()) { |
| 159 |
moveCaretPosition(outputMark); |
| 160 |
} else { |
| 161 |
setCaretPosition(outputMark); |
| 162 |
} |
| 163 |
e.consume(); |
| 164 |
} |
| 165 |
} |
| 166 |
} else if(code == KeyEvent.VK_ENTER) { |
| 167 |
returnPressed(); |
| 168 |
e.consume(); |
| 169 |
} else if(code == KeyEvent.VK_UP) { |
| 170 |
historyIndex--; |
| 171 |
if(historyIndex >= 0) { |
| 172 |
if(historyIndex >= history.size()) { |
| 173 |
historyIndex = history.size() -1; |
| 174 |
} |
| 175 |
if(historyIndex >= 0) { |
| 176 |
String str = history.get(historyIndex); |
| 177 |
int len = getDocument().getLength(); |
| 178 |
replaceRange(str, outputMark, len); |
| 179 |
int caretPos = outputMark + str.length(); |
| 180 |
select(caretPos, caretPos); |
| 181 |
} else { |
| 182 |
historyIndex++; |
| 183 |
} |
| 184 |
} else { |
| 185 |
historyIndex++; |
| 186 |
} |
| 187 |
e.consume(); |
| 188 |
} else if(code == KeyEvent.VK_DOWN) { |
| 189 |
int caretPos = outputMark; |
| 190 |
if(history.size() > 0) { |
| 191 |
historyIndex++; |
| 192 |
if(historyIndex < 0) {historyIndex = 0;} |
| 193 |
int len = getDocument().getLength(); |
| 194 |
if (historyIndex < history.size()) { |
| 195 |
String str = history.get(historyIndex); |
| 196 |
replaceRange(str, outputMark, len); |
| 197 |
caretPos = outputMark + str.length(); |
| 198 |
} else { |
| 199 |
historyIndex = history.size(); |
| 200 |
replaceRange("", outputMark, len); |
| 201 |
} |
| 202 |
} |
| 203 |
select(caretPos, caretPos); |
| 204 |
e.consume(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
public void keyTyped(KeyEvent e) { |
| 209 |
int keyChar = e.getKeyChar(); |
| 210 |
if(keyChar == 0x8 ) { |
| 211 |
if(outputMark == getCaretPosition()) { |
| 212 |
e.consume(); |
| 213 |
} |
| 214 |
} else if(getCaretPosition() < outputMark) { |
| 215 |
setCaretPosition(outputMark); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public synchronized void keyReleased(KeyEvent e) { |
| 220 |
} |
| 221 |
|
| 222 |
public synchronized void write(String str) { |
| 223 |
insert(str, outputMark); |
| 224 |
int len = str.length(); |
| 225 |
outputMark += len; |
| 226 |
select(outputMark, outputMark); |
| 227 |
} |
| 228 |
|
| 229 |
public synchronized void insertUpdate(DocumentEvent e) { |
| 230 |
int len = e.getLength(); |
| 231 |
int off = e.getOffset(); |
| 232 |
if(outputMark > off) { |
| 233 |
outputMark += len; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
public synchronized void removeUpdate(DocumentEvent e) { |
| 238 |
int len = e.getLength(); |
| 239 |
int off = e.getOffset(); |
| 240 |
if(outputMark > off) { |
| 241 |
if(outputMark >= off + len) { |
| 242 |
outputMark -= len; |
| 243 |
} else { |
| 244 |
outputMark = off; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
public synchronized void postUpdateUI() { |
| 250 |
|
| 251 |
requestFocus(); |
| 252 |
setCaret(getCaret()); |
| 253 |
select(outputMark, outputMark); |
| 254 |
} |
| 255 |
|
| 256 |
public synchronized void changedUpdate(DocumentEvent e) { |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
public InputStream getIn() { |
| 261 |
return in; |
| 262 |
} |
| 263 |
|
| 264 |
public PrintStream getOut() { |
| 265 |
return out; |
| 266 |
} |
| 267 |
|
| 268 |
public PrintStream getErr() { |
| 269 |
return err; |
| 270 |
} |
| 271 |
|
| 272 |
} |