import java.io.FileReader;
import java.io.Reader;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
// The following can be commented out for version 1.3
import pre13.AsyncBoxView;
/**
* This example demonstrates changing the StyledEditorKit
* to use the AsyncBoxView instead of a BoxView for the
* vertical arrangement of the lines/paragraphs.
*
* @author Timothy Prinzing
* @version 1.2 07/19/99
*/
public class StyleExample {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("give filename argument");
System.exit(1);
}
try {
EditorKit kit = new AsyncStyledEditorKit();
JTextPane pane = new JTextPane();
pane.setEditorKit(kit);
JScrollPane scroller = new JScrollPane();
JViewport vp = scroller.getViewport();
vp.add(pane);
vp.setBackingStoreEnabled(true);
JFrame f = new JFrame();
f.getContentPane().add(scroller);
f.pack();
f.setSize(600, 600);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Thread loader = new AsyncLoader(pane.getDocument(), new FileReader(args[0]));
loader.start();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
static class AsyncLoader extends Thread {
AsyncLoader(Document doc, Reader in) {
super("document-loader");
setPriority(Thread.MIN_PRIORITY);
this.in = in;
this.doc = doc;
}
public void run() {
try {
char[] buff = new char[4096];
int nch;
while ((nch = in.read(buff, 0, buff.length)) != -1) {
doc.insertString(doc.getLength(), new String(buff, 0, nch), null);
}
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
Reader in;
Document doc;
}
/**
* A slightly modified version of the javax.swing.text.StyledEditorKit
* that replaces the view of the section element with one that does asynchronous
* layout.
*/
public static class AsyncStyledEditorKit extends StyledEditorKit implements ViewFactory {
/**
* Fetches a factory that is suitable for producing
* views of any models that are produced by this
* kit. This is implemented to return View implementations
* for the following kinds of elements:
*