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.Image;
025: import java.awt.Rectangle;
026: import java.awt.image.ImageObserver;
027: import com.javujavu.javux.wings.Style;
028: import com.javujavu.javux.wings.WingImage;
029: import com.javujavu.javux.wings.WingTextPane;
030:
031: /**
032: * A class displaying image in <code>WingTextPane</code>
033: * <br><br>
034: * <b>This class is not thread safe
035: * synchronization is provided by WingTextPane</b>
036: */
037: //TODO display alt text
038: public class ImageNode extends TextPaneNode implements ImageObserver {
039: protected String alt;
040: protected WingImage img;
041: protected Rectangle rect;
042:
043: public ImageNode(WingImage img, String alt, Style style) {
044: super (style);
045: this .img = img;
046: if (alt == null)
047: alt = "";
048: this .alt = alt;
049: rect = new Rectangle();
050: }
051:
052: public int getWidth() {
053: return img != null ? img.getWidth() : 0;
054: }
055:
056: public int getHeight() {
057: return img != null ? img.getHeight() : 0;
058: }
059:
060: public int length() {
061: return 1;
062: }
063:
064: public String getText(int start, int end) {
065: return alt;
066: }
067:
068: public void layout(LayoutContext lc) {
069: lc.add(this , true);
070: valid = true;
071: }
072:
073: public void setBounds(int x, int y, int width, int height) {
074: bounds.setBounds(x, y, width, height);
075: rect.setBounds(x, y + height - getHeight(), width, getHeight());
076: }
077:
078: public void paint(Graphics g, int x, int y, int selectionStart,
079: int selectionEnd, Style stSelected) {
080: Rectangle cb = g.getClipBounds();
081: if (cb.x > x + bounds.x + bounds.width
082: || cb.y > y + bounds.y + bounds.height
083: || cb.x + cb.width < x + bounds.x
084: || cb.y + cb.height < y + bounds.y) {
085: return;
086: }
087:
088: x += rect.x;
089: y += rect.y;
090:
091: if (selectionStart <= 0 && selectionEnd > 0) {
092: g.setColor(stSelected.background);
093: g.fillRect(x, y, rect.width, rect.height);
094: }
095: WingImage img = this .img;
096: if (img != null)
097: img.drawImage(g, x, y, rect.width, rect.height, this );
098: }
099:
100: public Rectangle getCharBounds(int pos) {
101: if (!valid)
102: return null;
103: return new Rectangle(bounds);
104: }
105:
106: public int posAtPoint(int x, int y) {
107: if (!bounds.contains(x, y))
108: return -1;
109: if (x < bounds.x + bounds.width / 2)
110: return 0;
111: return -1;
112: }
113:
114: public boolean imageUpdate(Image img, int flags, int x, int y,
115: int width, int height) {
116: if ((flags & ABORT) != 0)
117: return false;
118: Rectangle r = new Rectangle(rect);
119: for (TextPaneNode n = parent; n != null; n = n.parent) {
120: r.x += n.bounds.x;
121: r.y += n.bounds.y;
122: }
123: WingTextPane o = getOwner();
124: if (o == null || !o.isShowing()
125: || !o.repaintVisible(r.x, r.y, r.width, r.height))
126: return false;
127: return (flags & (ALLBITS | ABORT)) == 0;
128: }
129:
130: }
|