samer@0: /* samer@0: * Console.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util.swing; samer@0: import samer.core.*; samer@0: import java.awt.*; samer@0: import java.io.*; samer@0: import javax.swing.*; samer@0: samer@0: /** samer@0:

samer@0: A class which can display messages in a scrolling text samer@0: area, which can be put in another container or its own frame. samer@0: There is a static reference to the current Console so samer@0: that anyone can display messages without worrying about samer@0: who created the Console. samer@0: samer@0:

samer@0: Associated with the Console are a Writer and an OutputStream samer@0: so that any method which writes to a Java IO stream can be samer@0: redirected to the current console. samer@0: samer@0: */ samer@0: samer@0: public class Console extends JTextArea implements Agent samer@0: { samer@0: private Writer writer=null; samer@0: private OutputStream ostream=null; samer@0: samer@0: public Console() { this(true); } samer@0: public Console(boolean setAsDefault) samer@0: { samer@0: super("",16,40); samer@0: samer@0: // setBackground( X.color(Shell.datum("swing.output.console.background"), getBackground())); samer@0: // setForeground( X.color(Shell.datum("swing.output.console.foreground"), getForeground())); samer@0: Font fnt=X.font(Shell.datum("swing.output.console.font"),null); samer@0: if (fnt!=null) setFont(fnt); samer@0: samer@0: setEditable(false); samer@0: setLineWrap(true); samer@0: samer@0: writer = new Writer(); samer@0: ostream = new OutputStream(); samer@0: } samer@0: samer@0: public String toString() { return "samer.core.util.swing.Console"; } samer@0: public void write( String s) { append(s); } samer@0: public void getCommands(Agent.Registry r) { r.add("clear").add("setfont"); } samer@0: public void execute(String cmd, Environment env) throws Exception samer@0: { samer@0: if (cmd.equals("clear")) setText(""); samer@0: else if (cmd.equals("setfont")) samer@0: setFont(X.font(env.datum("font"),null)); samer@0: } samer@0: samer@0: public Writer getWriter() { samer@0: if (writer==null) writer = new Writer(); samer@0: return writer; samer@0: } samer@0: samer@0: public OutputStream getStream() { samer@0: if (ostream==null) ostream = new OutputStream(); samer@0: return ostream; samer@0: } samer@0: samer@0: class Writer extends java.io.Writer samer@0: { samer@0: StringBuffer str; samer@0: samer@0: Writer() { str = new StringBuffer(); } samer@0: samer@0: public void write( char buf[], int off, int len) { samer@0: str.append( buf, off, len); // indentation samer@0: } samer@0: samer@0: public void flush() { samer@0: // append( str.toString()); samer@0: try { append( str.toString()); } samer@0: catch(IOException ex) { /* eh? */} samer@0: str.setLength(0); samer@0: } samer@0: public void close() { } samer@0: } samer@0: samer@0: class OutputStream extends java.io.OutputStream samer@0: { samer@0: StringBuffer str; samer@0: samer@0: OutputStream() { str = new StringBuffer(); } samer@0: samer@0: public void write( int b) { str.append( (char)b); } samer@0: public void flush() { append( str.toString()); str.setLength(0); } samer@0: public void close() { } samer@0: } samer@0: } samer@0: