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