KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > WrappingLabel


1 package com.sshtools.ui.awt;
2
3 import java.awt.Canvas JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.FontMetrics JavaDoc;
6 import java.awt.Graphics JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 /**
12  * VWrappingLabel is based on Symantec's class WrappingLabel; however, this
13  * class can format the text vertically, too. It also wraps text at newlines
14  * embedded in the label's text.
15  *
16  * @see symantec.awt.WrappingLabel
17  * @author Paul F. Williams (mailto:paul@criterioninc.com) Criterion, Inc.
18  * (http://www.criterioninc.com)
19  * @author Kyle Morris (mailto:morriskg@nexusfusion.com)
20  *
21  */

22
23 public class WrappingLabel extends Canvas JavaDoc {
24     // --------------------------------------------------
25
// constants
26
// --------------------------------------------------
27

28     // --------------------------------------------------
29
// class variables
30
// --------------------------------------------------
31

32     // --------------------------------------------------
33
// member variables
34
// --------------------------------------------------
35
protected String JavaDoc text;
36     protected float m_nHAlign;
37     protected float m_nVAlign;
38     protected int baseline;
39     protected FontMetrics JavaDoc fm;
40
41     // --------------------------------------------------
42
// constructors
43
// --------------------------------------------------
44

45     public WrappingLabel() {
46         this(""); //$NON-NLS-1$
47
}
48
49     public WrappingLabel(String JavaDoc s) {
50         this(s, Canvas.LEFT_ALIGNMENT, Canvas.CENTER_ALIGNMENT);
51     }
52
53     public WrappingLabel(String JavaDoc s, float nHorizontal, float nVertical) {
54         super();
55         setText(s);
56         setHAlignStyle(nHorizontal);
57         setVAlignStyle(nVertical);
58     }
59
60     // --------------------------------------------------
61
// accessor members
62
// --------------------------------------------------
63

64     public float getHAlignStyle() {
65         return m_nHAlign;
66     }
67
68     public float getVAlignStyle() {
69         return m_nVAlign;
70     }
71
72     public String JavaDoc getText() {
73         return text;
74     }
75
76     public void setHAlignStyle(float a) {
77         m_nHAlign = a;
78         invalidate();
79     }
80
81     public void setVAlignStyle(float a) {
82         m_nVAlign = a;
83         invalidate();
84     }
85
86     public void setText(String JavaDoc s) {
87         text = s;
88         repaint();
89     }
90
91     // --------------------------------------------------
92
// member methods
93
// --------------------------------------------------
94

95     public String JavaDoc paramString() {
96         return ""; //$NON-NLS-1$
97
}
98
99     public Dimension JavaDoc getMinimumSize() {
100         fm = getFontMetrics(getFont());
101         return new Dimension JavaDoc(9999, breakIntoLines(text, 9999).size() * fm.getHeight());
102     }
103
104     public Dimension JavaDoc getPreferredSize() {
105         return new Dimension JavaDoc(super.getPreferredSize().width, getMinimumSize().height);
106     }
107
108     public void paint(Graphics JavaDoc g) {
109         if (text != null) {
110             Dimension JavaDoc d;
111             int currentY = 0;
112             Vector JavaDoc lines;
113
114             // Set up some class variables
115
fm = getFontMetrics(getFont());
116             baseline = fm.getMaxAscent();
117
118             // Get the maximum height and width of the current control
119
d = getSize();
120
121             lines = breakIntoLines(text, d.width);
122
123             // if (m_nVAlign == V_ALIGN_CENTER)
124
if (m_nVAlign == Canvas.CENTER_ALIGNMENT) {
125                 int center = (d.height / 2);
126                 currentY = center - ((lines.size() / 2) * fm.getHeight());
127             }
128             // else if (m_nVAlign == V_ALIGN_BOTTOM)
129
else if (m_nVAlign == Canvas.BOTTOM_ALIGNMENT) {
130                 currentY = d.height - (lines.size() * fm.getHeight());
131             }
132
133             // now we have broken into substrings, print them
134
Enumeration JavaDoc elements = lines.elements();
135             while (elements.hasMoreElements()) {
136                 drawAlignedString(g, (String JavaDoc) (elements.nextElement()), 0, currentY, d.width);
137                 currentY += fm.getHeight();
138             }
139
140             // We're done with the font metrics...
141
fm = null;
142         }
143     }
144
145 // public Dimension getSize() {
146
// if (text != null) {
147
// Dimension d;
148
// int currentY = 0;
149
// Vector lines;
150
//
151
// // Set up some class variables
152
// fm = getFontMetrics(getFont());
153
// baseline = fm.getMaxAscent();
154
//
155
// // Get the maximum height and width of the current control
156
// d = getSize();
157
//
158
// lines = breakIntoLines(text, d.width);
159
//
160
// // if (m_nVAlign == V_ALIGN_CENTER)
161
// if (m_nVAlign == Canvas.CENTER_ALIGNMENT) {
162
// int center = (d.height / 2);
163
// currentY = center - ((lines.size() / 2) * fm.getHeight());
164
// }
165
// // else if (m_nVAlign == V_ALIGN_BOTTOM)
166
// else if (m_nVAlign == Canvas.BOTTOM_ALIGNMENT) {
167
// currentY = d.height - (lines.size() * fm.getHeight());
168
// }
169
//
170
// // now we have broken into substrings, print them
171
// Enumeration elements = lines.elements();
172
// while (elements.hasMoreElements()) {
173
// currentY += fm.getHeight();
174
// }
175
// }
176
// else {
177
// return super.getSize();
178
// }
179
// }
180

181     protected Vector JavaDoc breakIntoLines(String JavaDoc s, int width) {
182         int fromIndex = 0;
183         int pos = 0;
184         int bestpos;
185         String JavaDoc largestString;
186         Vector JavaDoc lines = new Vector JavaDoc();
187
188         // while we haven't run past the end of the string...
189
while (fromIndex != -1) {
190             // Automatically skip any spaces at the beginning of the line
191
while (fromIndex < text.length() && text.charAt(fromIndex) == ' ') {
192                 ++fromIndex;
193                 // If we hit the end of line
194
// while skipping spaces, we're done.
195
if (fromIndex >= text.length())
196                     break;
197             }
198
199             // fromIndex represents the beginning of the line
200
pos = fromIndex;
201             bestpos = -1;
202             largestString = null;
203
204             while (pos >= fromIndex) {
205                 boolean bHardNewline = false;
206                 int newlinePos = text.indexOf('\n', pos);
207                 int spacePos = text.indexOf(' ', pos);
208
209                 if (newlinePos != -1 && // there is a newline and either
210
((spacePos == -1) || // 1. there is no space,
211
// or
212
(spacePos != -1 && newlinePos < spacePos)))
213                 // 2. the newline is first
214
{
215                     pos = newlinePos;
216                     bHardNewline = true;
217                 } else {
218                     pos = spacePos;
219                     bHardNewline = false;
220                 }
221
222                 // Couldn't find another space?
223
if (pos == -1) {
224                     s = text.substring(fromIndex);
225                 } else {
226                     s = text.substring(fromIndex, pos);
227                 }
228
229                 // If the string fits, keep track of it.
230
if (fm.stringWidth(s) < width) {
231                     largestString = s;
232                     bestpos = pos;
233
234                     // If we've hit the end of the
235
// string or a newline, use it.
236
if (bHardNewline)
237                         bestpos++;
238                     if (pos == -1 || bHardNewline)
239                         break;
240                 } else {
241                     break;
242                 }
243
244                 ++pos;
245             }
246
247             if (largestString == null) {
248                 // Couldn't wrap at a space, so find the largest line
249
// that fits and print that. Note that this will be
250
// slightly off -- the width of a string will not necessarily
251
// be the sum of the width of its characters, due to kerning.
252
int totalWidth = 0;
253                 int oneCharWidth = 0;
254
255                 pos = fromIndex;
256
257                 while (pos < text.length()) {
258                     oneCharWidth = fm.charWidth(text.charAt(pos));
259                     if ((totalWidth + oneCharWidth) >= width)
260                         break;
261                     totalWidth += oneCharWidth;
262                     ++pos;
263                 }
264
265                 lines.addElement(text.substring(fromIndex, pos));
266                 fromIndex = pos;
267             } else {
268                 lines.addElement(largestString);
269                 fromIndex = bestpos;
270             }
271         }
272
273         return lines;
274     }
275
276     protected void drawAlignedString(Graphics JavaDoc g, String JavaDoc s, int x, int y, int width) {
277         int drawx;
278         int drawy;
279
280         drawx = x;
281         drawy = y + baseline;
282
283         if (m_nHAlign != Canvas.LEFT_ALIGNMENT) {
284             int sw;
285
286             sw = fm.stringWidth(s);
287
288             if (m_nHAlign == Canvas.CENTER_ALIGNMENT) {
289                 drawx += (width - sw) / 2;
290             } else if (m_nHAlign == Canvas.RIGHT_ALIGNMENT) {
291                 drawx = drawx + width - sw;
292             }
293         }
294
295         g.drawString(s, drawx, drawy);
296     }
297 }
Popular Tags