001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud.ui;
023:
024: import java.awt.Font;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.util.StringTokenizer;
028:
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031:
032: /**
033: * DOCUMENT ME!
034: *
035: * @author $author$
036: */
037: public class MultilineLabel extends JPanel {
038: private GridBagConstraints constraints;
039: private String text;
040:
041: /**
042: * Creates a new MultilineLabel object.
043: */
044: public MultilineLabel() {
045: this ("");
046: }
047:
048: /**
049: * Creates a new MultilineLabel object.
050: *
051: * @param text DOCUMENT ME!
052: */
053: public MultilineLabel(String text) {
054: super (new GridBagLayout());
055: constraints = new GridBagConstraints();
056: constraints.anchor = GridBagConstraints.NORTHWEST;
057: constraints.fill = GridBagConstraints.NONE;
058: setText(text);
059: }
060:
061: /**
062: * DOCUMENT ME!
063: *
064: * @param f DOCUMENT ME!
065: */
066: public void setFont(Font f) {
067: super .setFont(f);
068:
069: for (int i = 0; i < getComponentCount(); i++) {
070: getComponent(i).setFont(f);
071: }
072: }
073:
074: /**
075: * DOCUMENT ME!
076: *
077: * @param text DOCUMENT ME!
078: */
079: public void setText(String text) {
080: this .text = text;
081: removeAll();
082:
083: StringTokenizer tok = new StringTokenizer(text, "\n");
084: constraints.weighty = 0.0;
085: constraints.weightx = 1.0;
086:
087: while (tok.hasMoreTokens()) {
088: String t = tok.nextToken();
089:
090: if (!tok.hasMoreTokens()) {
091: constraints.weighty = 1.0;
092:
093: }
094: UIUtil.jGridBagAdd(this , new JLabel(t), constraints,
095: GridBagConstraints.REMAINDER);
096: }
097:
098: revalidate();
099: repaint();
100: }
101:
102: /**
103: * DOCUMENT ME!
104: *
105: * @return DOCUMENT ME!
106: */
107: public String getText() {
108: return text;
109: }
110: }
|