1 /**
2 * This is a simple mechanism to bind Inkscape to Java, and thence
3 * to all of the nice things that can be layered upon that.
4 *
5 * Authors:
6 * Bob Jamison
7 *
8 * Copyright (C) 2007-2008 Bob Jamison
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 3 of the License, or (at your option) any later version.
14 *
15 * This library 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 GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
25 package org.inkscape.script;
27 import java.awt.BorderLayout;
28 import javax.swing.JFrame;
29 import javax.swing.JPanel;
30 import javax.swing.JTextPane;
31 import javax.swing.JScrollPane;
32 import javax.swing.text.DefaultCaret;
33 import javax.swing.text.Document;
34 import javax.swing.text.BadLocationException;
35 import javax.swing.text.StyleConstants;
36 import javax.swing.text.SimpleAttributeSet;
37 import java.awt.Color;
38 import java.awt.event.KeyEvent;
39 import java.awt.event.KeyListener;
40 import java.awt.Font;
42 import java.io.ByteArrayOutputStream;
43 import java.io.PrintStream;
44 import java.io.IOException;
45 import java.io.Writer;
46 import java.io.PrintWriter;
48 public class Terminal extends JPanel
49 implements KeyListener
50 {
52 SimpleAttributeSet inTextAttr;
53 SimpleAttributeSet outTextAttr;
54 SimpleAttributeSet errTextAttr;
56 StringBuffer buf = new StringBuffer();
57 JTextPane textPane;
59 void err(String msg)
60 {
61 System.out.println("Terminal err: " + msg);
62 }
64 void trace(String msg)
65 {
66 System.out.println("Terminal: " + msg);
67 }
70 void processInputLine(String txt)
71 {
72 ScriptConsole cons = ScriptConsole.getInstance();
73 if (cons != null)
74 cons.doRunCmd(txt);
75 }
79 class OutWriter extends Writer
80 {
82 public void write(char[] cbuf, int off, int len)
83 {
84 String s = new String(cbuf, off, len);
85 output(s);
86 }
88 public void flush()
89 {
90 }
92 public void close()
93 {
94 }
96 }
100 PrintWriter outWriter;
102 public Writer getOutWriter()
103 {
104 if (outWriter == null)
105 outWriter = new PrintWriter(new OutWriter());
106 return outWriter;
107 }
110 public void output(String txt)
111 {
112 Document doc = textPane.getDocument();
113 try
114 {
115 doc.insertString(doc.getLength(), txt, outTextAttr);
116 textPane.setCaretPosition(doc.getLength());
117 }
118 catch (BadLocationException e)
119 {
120 }
122 }
125 public void outputf(String fmt, Object... args)
126 {
127 ByteArrayOutputStream baos = new ByteArrayOutputStream();
128 PrintStream out = new PrintStream(baos);
129 out.printf(fmt, args);
130 out.close();
131 String s = baos.toString();
132 output(s);
133 }
137 class ErrWriter extends Writer
138 {
140 public void write(char[] cbuf, int off, int len)
141 {
142 String s = new String(cbuf, off, len);
143 error(s);
144 }
146 public void flush()
147 {
148 }
150 public void close()
151 {
152 }
154 }
158 PrintWriter errWriter;
160 public Writer getErrWriter()
161 {
162 if (errWriter == null)
163 errWriter = new PrintWriter(new ErrWriter());
164 return errWriter;
165 }
168 public void error(String txt)
169 {
170 Document doc = textPane.getDocument();
171 try
172 {
173 doc.insertString(doc.getLength(), txt, errTextAttr);
174 textPane.setCaretPosition(doc.getLength());
175 }
176 catch (BadLocationException e)
177 {
178 }
180 }
182 public void errorf(String fmt, Object... args)
183 {
184 ByteArrayOutputStream baos = new ByteArrayOutputStream();
185 PrintStream out = new PrintStream(baos);
186 out.printf(fmt, args);
187 out.close();
188 String s = baos.toString();
189 error(s);
190 }
193 public void keyPressed(KeyEvent evt)
194 {
195 }
197 public void keyReleased(KeyEvent evt)
198 {
199 }
201 public void keyTyped(KeyEvent evt)
202 {
203 Document doc = textPane.getDocument();
204 char ch = evt.getKeyChar();
205 if (ch == 127)
206 {
207 }
208 else if (ch == '\b')
209 {
210 if (buf.length() == 0)
211 return;
212 try
213 {
214 buf.delete(buf.length()-1, buf.length());
215 doc.remove(doc.getLength()-1, 1);
216 textPane.setCaretPosition(doc.getLength());
217 }
218 catch (BadLocationException e)
219 {
220 err("keyTyped:" + e);
221 }
222 }
223 else
224 {
225 try
226 {
227 buf.append(ch);
228 doc.insertString(doc.getLength(), "" + ch, inTextAttr);
229 textPane.setCaretPosition(doc.getLength());
230 }
231 catch (BadLocationException e)
232 {
233 }
234 if (ch == '\n' || ch == '\r')
235 {
236 String txt = buf.toString();
237 buf.delete(0, buf.length());
238 txt = txt.trim();
239 processInputLine(txt);
240 }
241 }
245 }
249 void setup()
250 {
251 setLayout(new BorderLayout());
252 textPane = new JTextPane();
253 add(new JScrollPane(textPane), BorderLayout.CENTER);
254 textPane.setEditable(false);
255 textPane.setBackground(Color.BLACK);
256 textPane.setCaretColor(Color.WHITE);
257 textPane.setCaret(new DefaultCaret());
258 textPane.getCaret().setVisible(true);
259 textPane.getCaret().setBlinkRate(500);
260 Font currentFont = textPane.getFont();
261 textPane.setFont(new Font("Monospaced", currentFont.getStyle(), currentFont.getSize()));
262 textPane.addKeyListener(this);
264 inTextAttr = new SimpleAttributeSet();
265 StyleConstants.setForeground(inTextAttr, Color.YELLOW);
266 outTextAttr = new SimpleAttributeSet();
267 StyleConstants.setForeground(outTextAttr, Color.GREEN);
268 errTextAttr = new SimpleAttributeSet();
269 StyleConstants.setForeground(errTextAttr, Color.RED);
272 }
279 public Terminal()
280 {
281 super();
282 setup();
283 }
287 public static void main(String argv[])
288 {
289 Terminal t = new Terminal();
290 JFrame par = new JFrame("Terminal Test");
291 par.setSize(500, 350);
292 par.getContentPane().add(t);
293 par.setVisible(true);
294 }
296 }