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.Graphics;
024: import java.awt.Rectangle;
025: import com.javujavu.javux.wings.Style;
026: import com.javujavu.javux.wings.WingFont;
027:
028: /**
029: * A class displaying single line portion of text with a specified <code>Style</code>
030: * in <code>WingTextPane</code>
031: * <br><br>
032: * <b>This class is not thread safe
033: * synchronization is provided by WingTextPane</b>
034: */
035: class TextNodeCell extends TextPaneNode {
036: private final int start;
037: private final int end;
038: private Rectangle rect;
039:
040: public int getHeight() {
041: return rect.height;
042: }
043:
044: public int getWidth() {
045: return rect.width;
046: }
047:
048: public int length() {
049: return end - start;
050: }
051:
052: public TextNodeCell(TextNode snake, int start, int end, int width,
053: int height) {
054: super (null);
055: this .parent = snake;
056: this .start = start;
057: this .end = end;
058: rect = new Rectangle(0, 0, width, height);
059: }
060:
061: public void setBounds(int x, int y, int width, int height) {
062: bounds.setBounds(x, y, width, height);
063: rect.setLocation(x, y + height - rect.height);
064: TextNode ps = ((TextNode) parent);
065: ps.unionBounds(bounds);
066: }
067:
068: public void paint(Graphics g, int x, int y, int selectionStart,
069: int selectionEnd, Style stSelected) {
070: // x+= bounds.x;
071: // y+= bounds.y;
072: Rectangle cb = g.getClipBounds();
073:
074: if (cb.x > x + bounds.x + bounds.width
075: || cb.y > y + bounds.y + bounds.height
076: || cb.x + cb.width < x + bounds.x
077: || cb.y + cb.height < y + bounds.y) {
078: return;
079: }
080: WingFont font = getFont();
081: g.setFont(font.getFont());
082: g.setColor(getForeground());
083:
084: x += rect.x;
085: y += rect.y;
086:
087: char[] c = new char[1];
088: TextNode ps = ((TextNode) parent);
089: boolean first = true;
090: for (int i = start; i < end && x < cb.x + cb.width; i++) {
091: if (x + ps.textWidth[i] >= cb.x) {
092: c[0] = ps.text.charAt(i);
093:
094: if (i - start >= selectionStart
095: && i - start < selectionEnd) {
096: if (stSelected.background != null) {
097: g.setColor(stSelected.background);
098: g.fillRect(x, y, ps.textWidth[i], rect.height);
099: }
100: g
101: .setColor((stSelected.foreground != null) ? stSelected.foreground
102: : getForeground());
103: font.drawChars(g, c, 0, 1, x, y + ps.ascent, first);
104: first = false;
105: } else {
106: if (parent.style != null
107: && parent.style.background != null) {
108: g.setColor(parent.style.background);
109: g.fillRect(x, y, ps.textWidth[i], rect.height);
110: }
111: g.setColor(getForeground());
112: font.drawChars(g, c, 0, 1, x, y + ps.ascent, first);
113: first = false;
114: }
115: }
116: x += ps.textWidth[i];
117: }
118: }
119:
120: public Rectangle getCharBounds(int pos) {
121: // if(pos>=length()) return null;
122:
123: TextNode ps = ((TextNode) parent);
124: Rectangle r = new Rectangle(bounds);
125: int i = start;
126: for (; i < start + pos; i++) {
127: r.x += ps.textWidth[i];
128: }
129: r.width = ps.textWidth[i];
130: return r;
131: }
132:
133: public int posAtPoint(int x, int y) {
134: if (!bounds.contains(x, y))
135: return -1;
136:
137: TextNode ps = ((TextNode) parent);
138: x -= bounds.x;
139:
140: int pos = 0;
141: for (int i = start; i < end; i++) {
142: if (x < ps.textWidth[i] / 2)
143: return pos;
144: x -= ps.textWidth[i];
145: pos++;
146: }
147: return -1;
148: }
149: }
|