05438248282b023790965825fb2e4a37e6d4a934
1 package org.inkscape.script;
3 import java.awt.BorderLayout;
4 import javax.swing.JFrame;
5 import javax.swing.JPanel;
6 import javax.swing.JTextPane;
7 import javax.swing.JScrollPane;
8 import javax.swing.text.DefaultCaret;
9 import javax.swing.text.Document;
10 import javax.swing.text.BadLocationException;
11 import javax.swing.text.StyleConstants;
12 import javax.swing.text.SimpleAttributeSet;
13 import java.awt.Color;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.KeyListener;
16 import java.awt.Font;
18 import java.io.ByteArrayOutputStream;
19 import java.io.PrintStream;
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.io.PrintWriter;
24 public class Terminal extends JPanel
25 implements KeyListener
26 {
28 SimpleAttributeSet inTextAttr;
29 SimpleAttributeSet outTextAttr;
30 SimpleAttributeSet errTextAttr;
32 StringBuffer buf = new StringBuffer();
33 JTextPane textPane;
35 void err(String msg)
36 {
37 System.out.println("Terminal err: " + msg);
38 }
40 void trace(String msg)
41 {
42 System.out.println("Terminal: " + msg);
43 }
46 void processInputLine(String txt)
47 {
48 ScriptConsole cons = ScriptConsole.getInstance();
49 if (cons != null)
50 cons.doRunCmd(txt);
51 }
55 class OutWriter extends Writer
56 {
58 public void write(char[] cbuf, int off, int len)
59 {
60 String s = new String(cbuf, off, len);
61 output(s);
62 }
64 public void flush()
65 {
66 }
68 public void close()
69 {
70 }
72 }
76 PrintWriter outWriter;
78 public Writer getOutWriter()
79 {
80 if (outWriter == null)
81 outWriter = new PrintWriter(new OutWriter());
82 return outWriter;
83 }
86 public void output(String txt)
87 {
88 Document doc = textPane.getDocument();
89 try
90 {
91 doc.insertString(doc.getLength(), txt, outTextAttr);
92 textPane.setCaretPosition(doc.getLength());
93 }
94 catch (BadLocationException e)
95 {
96 }
98 }
101 public void outputf(String fmt, Object... args)
102 {
103 ByteArrayOutputStream baos = new ByteArrayOutputStream();
104 PrintStream out = new PrintStream(baos);
105 out.printf(fmt, args);
106 out.close();
107 String s = baos.toString();
108 output(s);
109 }
113 class ErrWriter extends Writer
114 {
116 public void write(char[] cbuf, int off, int len)
117 {
118 String s = new String(cbuf, off, len);
119 error(s);
120 }
122 public void flush()
123 {
124 }
126 public void close()
127 {
128 }
130 }
134 PrintWriter errWriter;
136 public Writer getErrWriter()
137 {
138 if (errWriter == null)
139 errWriter = new PrintWriter(new ErrWriter());
140 return errWriter;
141 }
144 public void error(String txt)
145 {
146 Document doc = textPane.getDocument();
147 try
148 {
149 doc.insertString(doc.getLength(), txt, errTextAttr);
150 textPane.setCaretPosition(doc.getLength());
151 }
152 catch (BadLocationException e)
153 {
154 }
156 }
158 public void errorf(String fmt, Object... args)
159 {
160 ByteArrayOutputStream baos = new ByteArrayOutputStream();
161 PrintStream out = new PrintStream(baos);
162 out.printf(fmt, args);
163 out.close();
164 String s = baos.toString();
165 error(s);
166 }
169 public void keyPressed(KeyEvent evt)
170 {
171 }
173 public void keyReleased(KeyEvent evt)
174 {
175 }
177 public void keyTyped(KeyEvent evt)
178 {
179 Document doc = textPane.getDocument();
180 char ch = evt.getKeyChar();
181 if (ch == 127)
182 {
183 }
184 else if (ch == '\b')
185 {
186 if (buf.length() == 0)
187 return;
188 try
189 {
190 buf.delete(buf.length()-1, buf.length());
191 doc.remove(doc.getLength()-1, 1);
192 textPane.setCaretPosition(doc.getLength());
193 }
194 catch (BadLocationException e)
195 {
196 err("keyTyped:" + e);
197 }
198 }
199 else
200 {
201 try
202 {
203 buf.append(ch);
204 doc.insertString(doc.getLength(), "" + ch, inTextAttr);
205 textPane.setCaretPosition(doc.getLength());
206 }
207 catch (BadLocationException e)
208 {
209 }
210 if (ch == '\n' || ch == '\r')
211 {
212 String txt = buf.toString();
213 buf.delete(0, buf.length());
214 txt = txt.trim();
215 processInputLine(txt);
216 }
217 }
221 }
225 void setup()
226 {
227 setLayout(new BorderLayout());
228 textPane = new JTextPane();
229 add(new JScrollPane(textPane), BorderLayout.CENTER);
230 textPane.setEditable(false);
231 textPane.setBackground(Color.BLACK);
232 textPane.setCaretColor(Color.WHITE);
233 textPane.setCaret(new DefaultCaret());
234 textPane.getCaret().setVisible(true);
235 textPane.getCaret().setBlinkRate(500);
236 Font currentFont = textPane.getFont();
237 textPane.setFont(new Font("Monospaced", currentFont.getStyle(), currentFont.getSize()));
238 textPane.addKeyListener(this);
240 inTextAttr = new SimpleAttributeSet();
241 StyleConstants.setForeground(inTextAttr, Color.YELLOW);
242 outTextAttr = new SimpleAttributeSet();
243 StyleConstants.setForeground(outTextAttr, Color.GREEN);
244 errTextAttr = new SimpleAttributeSet();
245 StyleConstants.setForeground(errTextAttr, Color.RED);
248 }
255 public Terminal()
256 {
257 super();
258 setup();
259 }
263 public static void main(String argv[])
264 {
265 Terminal t = new Terminal();
266 JFrame par = new JFrame("Terminal Test");
267 par.setSize(500, 350);
268 par.getContentPane().add(t);
269 par.setVisible(true);
270 }
272 }