comparison src/samer/core_/util/swing/Console.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * Console.java
3 *
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
5 * All rights reserved.
6 *
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 */
11
12 package samer.core.util.swing;
13 import samer.core.*;
14 import java.awt.*;
15 import java.io.*;
16 import javax.swing.*;
17
18 /**
19 <p>
20 A class which can display messages in a scrolling text
21 area, which can be put in another container or its own frame.
22 There is a static reference to the current Console so
23 that anyone can display messages without worrying about
24 who created the Console.
25
26 <p>
27 Associated with the Console are a Writer and an OutputStream
28 so that any method which writes to a Java IO stream can be
29 redirected to the current console.
30
31 */
32
33 public class Console extends JTextArea implements Agent
34 {
35 private Writer writer=null;
36 private OutputStream ostream=null;
37
38 public Console() { this(true); }
39 public Console(boolean setAsDefault)
40 {
41 super("",16,40);
42
43 // setBackground( X.color(Shell.datum("swing.output.console.background"), getBackground()));
44 // setForeground( X.color(Shell.datum("swing.output.console.foreground"), getForeground()));
45 Font fnt=X.font(Shell.datum("swing.output.console.font"),null);
46 if (fnt!=null) setFont(fnt);
47
48 setEditable(false);
49 setLineWrap(true);
50
51 writer = new Writer();
52 ostream = new OutputStream();
53 }
54
55 public String toString() { return "samer.core.util.swing.Console"; }
56 public void write( String s) { append(s); }
57 public void getCommands(Agent.Registry r) { r.add("clear").add("setfont"); }
58 public void execute(String cmd, Environment env) throws Exception
59 {
60 if (cmd.equals("clear")) setText("");
61 else if (cmd.equals("setfont"))
62 setFont(X.font(env.datum("font"),null));
63 }
64
65 public Writer getWriter() {
66 if (writer==null) writer = new Writer();
67 return writer;
68 }
69
70 public OutputStream getStream() {
71 if (ostream==null) ostream = new OutputStream();
72 return ostream;
73 }
74
75 class Writer extends java.io.Writer
76 {
77 StringBuffer str;
78
79 Writer() { str = new StringBuffer(); }
80
81 public void write( char buf[], int off, int len) {
82 str.append( buf, off, len); // indentation
83 }
84
85 public void flush() {
86 // append( str.toString());
87 try { append( str.toString()); }
88 catch(IOException ex) { /* eh? */}
89 str.setLength(0);
90 }
91 public void close() { }
92 }
93
94 class OutputStream extends java.io.OutputStream
95 {
96 StringBuffer str;
97
98 OutputStream() { str = new StringBuffer(); }
99
100 public void write( int b) { str.append( (char)b); }
101 public void flush() { append( str.toString()); str.setLength(0); }
102 public void close() { }
103 }
104 }
105