01: /*
02: * Javu WingS - Lightweight Java Component Set
03: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
04: * e-mail: ksadlocha@programics.com
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20:
21: package com.javujavu.javux.wings.text;
22:
23: import java.util.Vector;
24: import com.javujavu.javux.wings.WingConst;
25:
26: /**
27: * A class used to layout text elements in <code>WingTextPane</code>
28: * <br><br>
29: * <b>This class is not thread safe
30: * synchronization is provided by WingTextPane</b>
31: */
32: public class LayoutContext {
33: public boolean revalidate;
34: public int prefWidth;
35: public int realWidth;
36: public int realHeight;
37: public int dy;
38: public int dx;
39: public int alignment;
40: public boolean fixed;
41: public int wrap;
42:
43: protected int width;
44: protected Vector row;
45:
46: public LayoutContext(int prefWidth, int dx, int dy, int alignment,
47: boolean fixed, int wrap, boolean revalidate) {
48: if (prefWidth < 1)
49: prefWidth = 1;
50: this .prefWidth = prefWidth;
51: this .dx = dx;
52: this .dy = dy;
53: this .alignment = alignment;
54: this .fixed = fixed;
55: this .wrap = wrap;
56: this .revalidate = revalidate;
57: row = new Vector();
58: }
59:
60: public void add(TextPaneNode n, boolean allowBreakRow) {
61: if (row == null)
62: row = new Vector();
63: if (allowBreakRow && row.size() > 0
64: && width + n.getWidth() > prefWidth) {
65: endRow();
66: }
67: row.addElement(n);
68: width += n.getWidth();
69: }
70:
71: public void endRow() {
72: int h = 0, w = 0;
73: for (int i = 0; i < row.size(); i++) {
74: TextPaneNode n = (TextPaneNode) row.elementAt(i);
75: if (n.getHeight() > h)
76: h = n.getHeight();
77: w += n.getWidth();
78: }
79: int x = 0;
80: if (fixed && alignment == WingConst.CENTER)
81: x = (prefWidth - w) / 2;
82: else if (fixed && alignment == WingConst.RIGHT)
83: x = prefWidth - w;
84:
85: for (int i = 0; i < row.size(); i++) {
86: TextPaneNode n = (TextPaneNode) row.elementAt(i);
87: n.setBounds(dx + x, dy + realHeight, n.getWidth(), h);
88: x += n.getWidth();
89: }
90:
91: realHeight += h;
92: if (width > realWidth)
93: realWidth = width;
94: row.removeAllElements();
95: width = 0;
96: }
97: }
|