001: /*
002: * @(#)JTextComponentConsoleUI.java 1.1 05/05/16
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.tools;
010:
011: import java.util.*;
012: import java.io.*;
013: import java.awt.Font;
014: import java.awt.event.KeyEvent;
015: import java.awt.event.KeyListener;
016: import javax.swing.*;
017: import javax.swing.text.*;
018: import javax.swing.event.DocumentEvent;
019: import javax.swing.event.DocumentListener;
020: import javax.swing.text.BadLocationException;
021: import javax.swing.text.Document;
022: import javax.swing.text.Segment;
023:
024: public class JTextComponentConsoleUI implements ConsoleUI {
025: private final static int defaultColumns = 80;
026: private final static int defaultRows = 24;
027:
028: JTextComponent textComponent;
029: Console model;
030: int mark;
031:
032: protected JTextComponentConsoleUI() {
033: setJTextComponent(createTextComponent());
034: }
035:
036: public void setModel(Console console) {
037: this .model = console;
038: }
039:
040: public Console getModel() {
041: return this .model;
042: }
043:
044: public JTextComponent getJTextComponent() {
045: return textComponent;
046: }
047:
048: public void setJTextComponent(JTextComponent c) {
049: this .textComponent = c;
050: }
051:
052: protected JTextComponent createTextComponent() {
053: JTextArea textarea = new JTextArea();
054: textarea.setLineWrap(true);
055: textarea.setFont(new Font("Monospaced", 0, 12));
056: textarea.setRows(defaultRows);
057: textarea.setColumns(defaultColumns);
058: return textarea;
059: }
060:
061: static void runCommand(Runnable command) {
062: if (!SwingUtilities.isEventDispatchThread()) {
063: SwingUtilities.invokeLater(command);
064: } else {
065: command.run();
066: }
067: }
068:
069: public void append(final String str) {
070: runCommand(new Runnable() {
071: public void run() {
072: insert(str, mark);
073: mark += str.length();
074: int pos = getLength();
075: setCursorPosition(pos);
076: }
077: });
078: }
079:
080: public void insert(final String str, final int mark) {
081: Document doc = textComponent.getDocument();
082: if (doc != null) {
083: try {
084: doc.insertString(mark, str, null);
085: } catch (BadLocationException e) {
086: throw new IllegalArgumentException(e.getMessage());
087: }
088: }
089: }
090:
091: public int getLength() {
092: return textComponent.getDocument().getLength();
093: }
094:
095: public void setCursorPosition(int pos) {
096: textComponent.select(pos, pos);
097: }
098:
099: public int getMarkPosition() {
100: return mark;
101: }
102:
103: public void setMarkPosition(int pos) {
104: mark = pos;
105: }
106:
107: synchronized void enter() {
108: try {
109: int len = getLength();
110: sendText(mark, len - mark);
111: } catch (IOException e) {
112: System.err.println(e);
113: }
114: }
115:
116: public void entered(String command) {
117: // skip
118: }
119:
120: void sendText(int start, int len) throws IOException {
121: Document doc = textComponent.getDocument();
122: int doclen = doc.getLength();
123: Segment segment = new Segment();
124: try {
125: doc.getText(mark, doclen - mark, segment);
126: } catch (BadLocationException ble) {
127: ble.printStackTrace();
128: }
129: entered(segment.toString());
130: char[] cbuf = segment.array;
131: int offset = segment.offset;
132: int count = segment.count;
133:
134: model.enter(cbuf, offset, count);
135:
136: insert("\n", getLength());
137: mark = doc.getLength();
138: }
139:
140: public void close() {
141: }
142: }
|