001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings.text;
022:
023: import java.awt.Color;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026: import com.javujavu.javux.wings.Style;
027: import com.javujavu.javux.wings.WingFont;
028: import com.javujavu.javux.wings.WingTextPane;
029:
030: /**
031: * An abstract base class of all classes representing
032: * element of text document displayed in <code>WingTextPane</code>
033: * <br><br>
034: * <b>This class is not thread safe
035: * synchronization is provided by WingTextPane</b>
036: */
037: public abstract class TextPaneNode {
038: protected TextPaneNode parent;
039: public Style style;
040:
041: // protected int length;
042:
043: protected Rectangle bounds;
044: protected boolean valid;
045:
046: public TextPaneNode(Style style) {
047: this .style = style;
048: bounds = new Rectangle();
049: }
050:
051: public Style getStyle() {
052: if (style != null)
053: return style;
054: if (parent != null)
055: return parent.getStyle();
056: return null;
057: }
058:
059: public WingFont getFont() {
060: if (style != null && style.font != null)
061: return style.font;
062: if (parent != null)
063: return parent.getFont();
064: return null;
065: }
066:
067: public Color getForeground() {
068: if (style != null && style.foreground != null)
069: return style.foreground;
070: if (parent != null)
071: return parent.getForeground();
072: return null;
073: }
074:
075: public int getWidth() {
076: return bounds.width;
077: }
078:
079: public int getHeight() {
080: return bounds.height;
081: }
082:
083: public void setBounds(int x, int y, int width, int height) {
084: bounds.setBounds(x, y, width, height);
085: }
086:
087: public boolean breaksRow() {
088: return false;
089: }
090:
091: public int length() {
092: return 1;
093: }
094:
095: public boolean insert(int pos, TextPaneNode node) {
096: return false;
097: }
098:
099: public String getText(int start, int end) {
100: return "";
101: }
102:
103: public void remove(int start, int end) {
104: }
105:
106: protected void invalidate() {
107: valid = false;
108: if (parent != null)
109: parent.invalidate();
110: }
111:
112: public void layout(LayoutContext lc) {
113: valid = true;
114: }
115:
116: public abstract void paint(Graphics g, int x, int y,
117: int selectionStart, int selectionEnd, Style stSelected);
118:
119: public abstract Rectangle getCharBounds(int pos);
120:
121: public abstract int posAtPoint(int x, int y);
122:
123: public TextPaneNode nodeAt(int pos, boolean prefTextNode) {
124: return this ;
125: }
126:
127: public WingTextPane getOwner() {
128: return (parent != null) ? parent.getOwner() : null;
129: }
130:
131: public static int nearestX(Rectangle r, int x) {
132: if (x < r.x)
133: return r.x;
134: if (x >= r.x + r.width)
135: return r.width > 0 ? r.x + r.width - 1 : r.x;
136: return x;
137: }
138:
139: public static int nearestY(Rectangle r, int y) {
140: if (y < r.y)
141: return r.y;
142: if (y >= r.y + r.height)
143: return r.y + r.height - 1;
144: return y;
145: }
146:
147: // public int getChildPos(TextNode node) { return 0; }
148: public void replaceNode(TextPaneNode box, TextPaneNode[] r) {
149: }
150: }
|