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.util.ArrayList;
026:
027: /**
028: * This class represents one row in the console
029: */
030: public class ConsoleRow {
031: private ArrayList fragments = null;
032: private ConsoleBuffer buffer = null;
033: private boolean dirty = false;
034:
035: private static class Fragment {
036: public String text = null;
037: public ConsoleAttribute attribute = null;
038:
039: public Fragment(String text, ConsoleAttribute attribute) {
040: this .text = text;
041: this .attribute = attribute;
042: }
043:
044: public void insert(int offset, String string) {
045: text = new StringBuffer(text).insert(offset, string)
046: .toString();
047: }
048: };
049:
050: /**
051: * Create a new row
052: */
053: public ConsoleRow(ConsoleBuffer buffer) {
054: this .buffer = buffer;
055: fragments = new ArrayList();
056: }
057:
058: /**
059: * Append a fragment to the row. If there is already a fragment with
060: * the same attributes, the two fragments are joined
061: */
062: public void appendFragment(String text, ConsoleAttribute attribute) {
063: if (fragments.size() == 0) {
064: fragments.add(new Fragment(text, attribute));
065: } else {
066: Fragment last = (Fragment) fragments
067: .get(fragments.size() - 1);
068: if (last.attribute == attribute)
069: last.text += text;
070: else
071: fragments.add(new Fragment(text, attribute));
072: }
073: dirty = true;
074: }
075:
076: /**
077: * Get the number of fragments in this row
078: */
079: public int getFragmentCount() {
080: return fragments.size();
081: }
082:
083: /**
084: * Get the text content of a fragment
085: */
086: public String getFragmentText(int index) {
087: return ((Fragment) fragments.get(index)).text;
088: }
089:
090: /**
091: * Set the text content of a fragment
092: */
093: public void setFragmentText(int index, String text) {
094: ((Fragment) fragments.get(index)).text = text;
095: dirty = true;
096: }
097:
098: /**
099: * Get the attribute content of a fragment
100: */
101: public ConsoleAttribute getFragmentAttribute(int index) {
102: return ((Fragment) fragments.get(index)).attribute;
103: }
104:
105: /**
106: * Remove a fragment from the row
107: */
108: public void removeFragment(int index) {
109: fragments.remove(index);
110: dirty = true;
111: }
112:
113: /**
114: * Check whether this row needs repainting
115: */
116: public boolean isDirty() {
117: return dirty;
118: }
119:
120: /**
121: * Mark this row to be repainted
122: */
123: public void markDirty() {
124: dirty = true;
125: }
126:
127: /**
128: * Paint the row
129: */
130: void paintRow(Graphics g, int y) {
131: int x = buffer.console.getInsets().left;
132:
133: for (int i = 0, size = fragments.size(); i < size; i++) {
134: Fragment fragment = (Fragment) fragments.get(i);
135: ConsoleAttribute attribute = fragment.attribute;
136: int width = buffer.console.columnWidth
137: * fragment.text.length();
138:
139: if (attribute.isBold())
140: g.setFont(buffer.console.boldFont);
141: else
142: g.setFont(buffer.console.plainFont);
143: g.setColor(attribute.getBackgroundColor());
144: g.fillRect(x, y, width, buffer.console.rowHeight);
145: g.setColor(attribute.getForegroundColor());
146: g.drawString(fragment.text, x, y + buffer.console.rowHeight
147: - buffer.console.descent);
148:
149: x += width;
150: }
151: dirty = false;
152: }
153: };
|