1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.ui;
27
28 import java.awt.Font;
29 import java.awt.GridBagConstraints;
30 import java.awt.GridBagLayout;
31
32 import java.util.StringTokenizer;
33
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36
37
38 /**
39 *
40 *
41 * @author $author$
42 * @version $Revision: 1.13 $
43 */
44 public class MultilineLabel extends JPanel {
45 // Private instance variables
46 private GridBagConstraints constraints;
47 private String text;
48
49 /**
50 * Creates a new MultilineLabel object.
51 */
52 public MultilineLabel() {
53 this("");
54 }
55
56 /**
57 * Creates a new MultilineLabel object.
58 *
59 * @param text
60 */
61 public MultilineLabel(String text) {
62 super(new GridBagLayout());
63 constraints = new GridBagConstraints();
64 constraints.anchor = GridBagConstraints.NORTHWEST;
65 constraints.fill = GridBagConstraints.NONE;
66 setText(text);
67 }
68
69 /**
70 *
71 *
72 * @param f
73 */
74 public void setFont(Font f) {
75 super.setFont(f);
76
77 for (int i = 0; i < getComponentCount(); i++) {
78 getComponent(i).setFont(f);
79 }
80 }
81
82 /**
83 *
84 *
85 * @param text
86 */
87 public void setText(String text) {
88 this.text = text;
89 removeAll();
90
91 StringTokenizer tok = new StringTokenizer(text, "\n");
92 constraints.weighty = 0.0;
93 constraints.weightx = 1.0;
94
95 while (tok.hasMoreTokens()) {
96 String t = tok.nextToken();
97
98 if (!tok.hasMoreTokens()) {
99 constraints.weighty = 1.0;
100 }
101
102 UIUtil.jGridBagAdd(this, new JLabel(t), constraints,
103 GridBagConstraints.REMAINDER);
104 }
105
106 revalidate();
107 repaint();
108 }
109
110 /**
111 *
112 *
113 * @return
114 */
115 public String getText() {
116 return text;
117 }
118 }