package jas2.swingstudio;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
/**
* An OutputStream implementation that places it's
* output in a swing text model (Document). The
* Document can be either a plain text or styled
* document implementation. If styled, the attributes
* assigned to the output stream will be used in
* the display of the output.
*
* @author Peter Armstrong
*
* Heavily based on (i.e. a modified subset of):
* http://java.sun.com/products/jfc/tsc/text/console/DocumentOutputStream.java
* @author Timothy Prinzing
* @version 1.1 02/05/99
*/
public class JASConsoleDocumentOutputStream extends OutputStream {
public JASConsoleDocumentOutputStream(Document doc, AttributeSet a,
String s, JASConsoleTabbedPane j) {
theDocument = doc;
theAttributeSet = a;
theScrollPaneName = s;
theTabbedPane = j;
}
public void write(int b) throws IOException {
one[0] = (byte) b;
write(one, 0, 1);
}
public void write(byte b[], int off, int len) throws IOException {
try {
int theTabIndex = theTabbedPane.indexOfTab(theScrollPaneName);
//I still have to use the tab index to check if the tab
//with a given name is in theTabbedPane--the difference is now
//I look up the index instead of stupidly storing it!
if (theTabIndex != -1) {
theTabbedPane.setSelectedIndex(theTabIndex);
} else {
//make a new JASConsoleScrollPane
JASConsoleScrollPane theScrollPane =
theTabbedPane.getSelectedConsoleScrollPane();
//now make a new scroll pane, add it to the tabbed pane and set selected
//when the tab is added theTabbedPane's listener takes care of the
//commandProcessor stuff for us
JASConsoleScrollPane theNewScrollPane =
new JASConsoleScrollPane(theScrollPaneName, theTabbedPane);
theTabbedPane.addTab(theScrollPaneName,theNewScrollPane);
theTabIndex = theTabbedPane.indexOfTab(theScrollPaneName);
theTabbedPane.setSelectedIndex(theTabIndex);
//the JASConsoleScrollPane constructor inits the JASConsoleTextPane
//so now I can set the Document!
theNewScrollPane.getTheTextArea().setDocument(theDocument);
theNewScrollPane.setOutputAttributes(theAttributeSet);
}
//now that we're here, the correct scroll pane exist and be focused
//so write the string
String s = new String(b,off,len);
theDocument.insertString(theDocument.getLength(), s, theAttributeSet);
//and optionally append it to the log file also
appendToLog(s);
//and make sure that our Document hasn't gotten too big
JASConsoleDocumentUtilities.trimDoc(theDocument);
theTabbedPane.getSelectedConsoleScrollPane().scrollToEnd();
} catch (BadLocationException ble) {
throw new IOException(ble.getMessage());
}
}
private void appendToLog(String s) {
JASConsoleScrollPane theScrollPane = theTabbedPane.getSelectedConsoleScrollPane();
if (theScrollPane != null && ((JASConsoleTextArea)theScrollPane.getTheTextArea()).isLoggingOn()) {
try {
((JASConsoleTextArea)theScrollPane.getTheTextArea()).getLogWriter().write(s);
((JASConsoleTextArea)theScrollPane.getTheTextArea()).getLogWriter().flush();
} catch (IOException eek) {
}
}
}
private String theScrollPaneName;
private byte[] one = new byte[1];
private Document theDocument;
private AttributeSet theAttributeSet;
private JASConsoleTabbedPane theTabbedPane;
}
Ads help maintain this website.