001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.swing;
023:
024: import java.awt.Graphics;
025: import java.awt.Insets;
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.StringTokenizer;
029:
030: /**
031: * Manages the console text and attributes
032: */
033: public class ConsoleBuffer {
034: private List rows = null;
035: private ConsoleAttribute defaultAttribute = null;
036: JConsole console = null;
037:
038: /**
039: * Create the console buffer
040: */
041: public ConsoleBuffer(JConsole console) {
042: /* Setup the row buffer */
043: clear();
044:
045: /* Set the parent console */
046: this .console = console;
047:
048: /* Create a default attribute */
049: defaultAttribute = new ConsoleAttribute();
050: }
051:
052: /**
053: * Append text to the buffer
054: */
055: public void append(String text) {
056: append(text, defaultAttribute);
057: }
058:
059: /**
060: * Append text with an attribute to the buffer
061: */
062: public void append(String text, ConsoleAttribute attribute) {
063: ConsoleRow row = (ConsoleRow) rows.get(rows.size() - 1);
064:
065: if (text.indexOf("\n") != -1 || text.indexOf("\b") != -1) {
066: StringTokenizer tokenizer = new StringTokenizer(text,
067: "\n\b", true);
068: while (tokenizer.hasMoreTokens()) {
069: String token = tokenizer.nextToken();
070:
071: if (token.equals("\n")) {
072: row = new ConsoleRow(this );
073: rows.add(row);
074: console.dirty = true;
075: } else if (token.equals("\b")) {
076: int fragment = row.getFragmentCount() - 1;
077: String fragmentText = row.getFragmentText(fragment);
078: row.setFragmentText(fragment, fragmentText
079: .substring(0, fragmentText.length() - 1));
080: } else {
081: row.appendFragment(token, attribute);
082: }
083: }
084: } else {
085: row.appendFragment(text, attribute);
086: }
087: console.updateScrollbar();
088: }
089:
090: /**
091: * Paint the buffer
092: */
093: void paintBuffer(Graphics g) {
094: Insets insets = console.getInsets();
095: int visibleRows = (console.height - insets.top - insets.bottom)
096: / console.rowHeight;
097: int max = rows.size() - console.offset;
098:
099: if (visibleRows > max)
100: visibleRows = max;
101:
102: for (int i = 0; i < visibleRows; i++) {
103: ConsoleRow row = ((ConsoleRow) rows.get(console.offset + i));
104:
105: if (console.dirty) {
106: row.paintRow(g, i * console.rowHeight + insets.top);
107: } else if (row.isDirty()) {
108: g.setColor(console.getBackground());
109: g.fillRect(0, i * console.rowHeight + insets.top,
110: console.width, console.rowHeight);
111: row.paintRow(g, i * console.rowHeight + insets.top);
112: }
113: }
114: }
115:
116: /**
117: * Return a row
118: */
119: public ConsoleRow getRow(int row) {
120: return (ConsoleRow) rows.get(row);
121: }
122:
123: /**
124: * Remove a row
125: */
126: public void removeRow(int row) {
127: rows.remove(row);
128: console.updateScrollbar();
129: }
130:
131: /**
132: * Get the row count
133: */
134: public int getRowCount() {
135: return rows.size();
136: }
137:
138: /**
139: * Clear the buffer
140: */
141: public void clear() {
142: rows = new ArrayList();
143: rows.add(new ConsoleRow(this ));
144: if (console != null) {
145: console.offset = 0;
146: console.setCursorPosition(0, 0);
147: console.updateScrollbar();
148: }
149: }
150: }
|