01: /*
02: * Beryl - A web platform based on XML, XSLT and Java
03: * This file is part of the Beryl XML GUI
04: *
05: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11:
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
20: */
21:
22: package org.beryl.gui.swing;
23:
24: import java.awt.Dimension;
25: import java.awt.FontMetrics;
26: import java.awt.Graphics;
27: import java.awt.Insets;
28: import java.util.StringTokenizer;
29:
30: import javax.swing.JComponent;
31: import javax.swing.UIManager;
32:
33: public class JBreakingLabel extends JComponent {
34: private String text = null;
35:
36: public JBreakingLabel() {
37: this ("");
38: }
39:
40: public JBreakingLabel(String text) {
41: setForeground(UIManager.getColor("Label.foreground"));
42: setBackground(UIManager.getColor("Label.background"));
43: setText(text);
44: }
45:
46: public String getText() {
47: return text;
48: }
49:
50: public void setText(String text) {
51: this .text = text;
52: repaint();
53: }
54:
55: public Dimension getMaximumSize() {
56: return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
57: }
58:
59: public Dimension getPreferredSize() {
60: return super .getPreferredSize();
61: }
62:
63: public Dimension getMinimumSize() {
64: return new Dimension(0, 0);
65: }
66:
67: public void paintComponent(Graphics g) {
68: Insets insets = getInsets();
69:
70: int width = getWidth() - insets.left - insets.right;
71: int height = getHeight() - insets.top - insets.bottom - 1;
72:
73: g.setColor(getBackground());
74: g.fillRect(insets.left, insets.top, width, height);
75: g.setFont(getFont());
76: g.setColor(getForeground());
77:
78: FontMetrics metrics = g.getFontMetrics(getFont());
79:
80: int xoffset = 0;
81: int yoffset = metrics.getHeight();
82:
83: StringTokenizer tokenizer = new StringTokenizer(text, " ", true);
84: while (tokenizer.hasMoreTokens()) {
85: String token = tokenizer.nextToken();
86: int tokenWidth = metrics.stringWidth(token);
87: if (xoffset + tokenWidth > width) {
88: yoffset += metrics.getHeight();
89: xoffset = 0;
90: }
91: g.drawString(token, xoffset + insets.left, yoffset
92: + insets.bottom);
93: xoffset += tokenWidth;
94: }
95: }
96: }
|