KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sshtools.ui.swing;
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.Vector JavaDoc;
9
10 import javax.swing.JComponent JavaDoc;
11
12 /**
13  * VWrappingLabel is based on Symantec's class WrappingLabel; however, this
14 class
15  * can format the text vertically, too. It also wraps text at newlines
16 embedded
17  * in the label's text.
18  *
19  * @see symantec.awt.WrappingLabel
20  * @author Paul F. Williams (mailto:paul@criterioninc.com)
21  * Criterion, Inc. (http://www.criterioninc.com)
22  * @author Kyle Morris (mailto:morriskg@nexusfusion.com)
23  *
24  */

25
26
27 public class WrappingLabel extends JComponent JavaDoc
28 {
29     //--------------------------------------------------
30
// constants
31
//--------------------------------------------------
32

33     //--------------------------------------------------
34
// class variables
35
//--------------------------------------------------
36

37
38     //--------------------------------------------------
39
// member variables
40
//--------------------------------------------------
41
protected String JavaDoc text;
42     protected float m_nHAlign;
43     protected float m_nVAlign;
44     protected int baseline;
45     protected FontMetrics JavaDoc fm;
46
47
48     //--------------------------------------------------
49
// constructors
50
//--------------------------------------------------
51

52     public WrappingLabel()
53     {
54         this("");
55     }
56
57     public WrappingLabel(String JavaDoc s)
58     {
59         this(s, Canvas.LEFT_ALIGNMENT, Canvas.CENTER_ALIGNMENT);
60     }
61
62     public WrappingLabel(String JavaDoc s, float nHorizontal, float nVertical)
63     {
64         setText(s);
65         setHAlignStyle(nHorizontal);
66         setVAlignStyle(nVertical);
67     }
68
69
70     //--------------------------------------------------
71
// accessor members
72
//--------------------------------------------------
73

74     public float getHAlignStyle() { return m_nHAlign; }
75     public float getVAlignStyle() { return m_nVAlign; }
76     public String JavaDoc getText() { return text; }
77
78     public void setHAlignStyle(float a)
79     {
80         m_nHAlign = a;
81         invalidate();
82     }
83
84     public void setVAlignStyle(float a)
85     {
86         m_nVAlign = a;
87         invalidate();
88     }
89
90     public void setText(String JavaDoc s)
91     {
92         text = s;
93         repaint();
94     }
95
96
97     //--------------------------------------------------
98
// member methods
99
//--------------------------------------------------
100

101     public String JavaDoc paramString()
102     {
103         return "";
104     }
105
106     public void paintComponent(Graphics JavaDoc g)
107     {
108         if (text != null)
109         {
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             {
126                 int center = (d.height / 2);
127                 currentY = center - ( (lines.size() / 2) * fm.getHeight() );
128             }
129             //else if (m_nVAlign == V_ALIGN_BOTTOM)
130
else if (m_nVAlign == Canvas.BOTTOM_ALIGNMENT)
131             {
132                 currentY = d.height - ( lines.size() * fm.getHeight() );
133             }
134
135             // now we have broken into substrings, print them
136
Enumeration JavaDoc elements = lines.elements();
137             while (elements.hasMoreElements())
138             {
139                 drawAlignedString(g,
140                   (String JavaDoc)(elements.nextElement()),
141                   0, currentY, d.width);
142                 currentY += fm.getHeight();
143             }
144
145             // We're done with the font metrics...
146
fm = null;
147         }
148     }
149
150
151     protected Vector JavaDoc breakIntoLines (String JavaDoc s, int width)
152     {
153         int fromIndex = 0;
154         int pos = 0;
155         int bestpos;
156         String JavaDoc largestString;
157         Vector JavaDoc lines = new Vector JavaDoc();
158
159         // while we haven't run past the end of the string...
160
while (fromIndex != -1)
161         {
162             // Automatically skip any spaces at the beginning of the line
163
while (fromIndex < text.length()
164                    && text.charAt(fromIndex) == ' ')
165             {
166                 ++fromIndex;
167                 // If we hit the end of line
168
// while skipping spaces, we're done.
169
if (fromIndex >= text.length()) break;
170             }
171
172             // fromIndex represents the beginning of the line
173
pos = fromIndex;
174             bestpos = -1;
175             largestString = null;
176
177             while (pos >= fromIndex)
178             {
179                 boolean bHardNewline = false;
180                 int newlinePos = text.indexOf('\n', pos);
181                 int spacePos = text.indexOf(' ', pos);
182
183                 if (newlinePos != -1 && // there is a newline and either
184
((spacePos == -1) || // 1. there is no space, or
185
(spacePos != -1 &&
186                         newlinePos < spacePos)))
187                         // 2. the newline is first
188
{
189                     pos = newlinePos;
190                     bHardNewline = true;
191                 }
192                 else
193                 {
194                     pos = spacePos;
195                     bHardNewline = false;
196                 }
197
198                 // Couldn't find another space?
199
if (pos == -1)
200                 {
201                     s = text.substring(fromIndex);
202                 }
203                 else
204                 {
205                     s = text.substring(fromIndex, pos);
206                 }
207
208                 // If the string fits, keep track of it.
209
if (fm.stringWidth(s) < width)
210                 {
211                     largestString = s;
212                     bestpos = pos;
213
214                     // If we've hit the end of the
215
// string or a newline, use it.
216
if (bHardNewline)
217                         bestpos++;
218                     if (pos == -1 || bHardNewline) break;
219                 }
220                 else
221                 {
222                     break;
223                 }
224
225                 ++pos;
226             }
227
228             if (largestString == null)
229             {
230                 // Couldn't wrap at a space, so find the largest line
231
// that fits and print that. Note that this will be
232
// slightly off -- the width of a string will not necessarily
233
// be the sum of the width of its characters, due to kerning.
234
int totalWidth = 0;
235                 int oneCharWidth = 0;
236
237                 pos = fromIndex;
238
239                 while (pos < text.length())
240                 {
241                     oneCharWidth = fm.charWidth(text.charAt(pos));
242                     if ((totalWidth + oneCharWidth) >= width) break;
243                     totalWidth += oneCharWidth;
244                     ++pos;
245                 }
246
247                 lines.addElement (text.substring(fromIndex, pos));
248                 fromIndex = pos;
249             }
250             else
251             {
252                 lines.addElement (largestString);
253                 fromIndex = bestpos;
254             }
255            }
256
257            return lines;
258        }
259
260
261     protected void drawAlignedString(Graphics JavaDoc g,
262                    String JavaDoc s, int x, int y, int width)
263     {
264         int drawx;
265         int drawy;
266
267         drawx = x;
268         drawy = y + baseline;
269
270         if (m_nHAlign != Canvas.LEFT_ALIGNMENT)
271         {
272             int sw;
273
274             sw = fm.stringWidth(s);
275
276             if (m_nHAlign == Canvas.CENTER_ALIGNMENT)
277             {
278                 drawx += (width - sw) / 2;
279             }
280             else if (m_nHAlign == Canvas.RIGHT_ALIGNMENT)
281             {
282                 drawx = drawx + width - sw;
283             }
284         }
285
286         g.drawString(s, drawx, drawy);
287     }
288 }
Popular Tags