1 package com.sshtools.ui.swing; 2 3 import java.awt.Canvas ; 4 import java.awt.Dimension ; 5 import java.awt.FontMetrics ; 6 import java.awt.Graphics ; 7 import java.util.Enumeration ; 8 import java.util.Vector ; 9 10 import javax.swing.JComponent ; 11 12 25 26 27 public class WrappingLabel extends JComponent 28 { 29 33 37 38 protected String text; 42 protected float m_nHAlign; 43 protected float m_nVAlign; 44 protected int baseline; 45 protected FontMetrics fm; 46 47 48 52 public WrappingLabel() 53 { 54 this(""); 55 } 56 57 public WrappingLabel(String s) 58 { 59 this(s, Canvas.LEFT_ALIGNMENT, Canvas.CENTER_ALIGNMENT); 60 } 61 62 public WrappingLabel(String s, float nHorizontal, float nVertical) 63 { 64 setText(s); 65 setHAlignStyle(nHorizontal); 66 setVAlignStyle(nVertical); 67 } 68 69 70 74 public float getHAlignStyle() { return m_nHAlign; } 75 public float getVAlignStyle() { return m_nVAlign; } 76 public String 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 s) 91 { 92 text = s; 93 repaint(); 94 } 95 96 97 101 public String paramString() 102 { 103 return ""; 104 } 105 106 public void paintComponent(Graphics g) 107 { 108 if (text != null) 109 { 110 Dimension d; 111 int currentY = 0; 112 Vector lines; 113 114 fm = getFontMetrics(getFont()); 116 baseline = fm.getMaxAscent(); 117 118 d = getSize(); 120 121 lines = breakIntoLines (text, d.width); 122 123 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 == Canvas.BOTTOM_ALIGNMENT) 131 { 132 currentY = d.height - ( lines.size() * fm.getHeight() ); 133 } 134 135 Enumeration elements = lines.elements(); 137 while (elements.hasMoreElements()) 138 { 139 drawAlignedString(g, 140 (String )(elements.nextElement()), 141 0, currentY, d.width); 142 currentY += fm.getHeight(); 143 } 144 145 fm = null; 147 } 148 } 149 150 151 protected Vector breakIntoLines (String s, int width) 152 { 153 int fromIndex = 0; 154 int pos = 0; 155 int bestpos; 156 String largestString; 157 Vector lines = new Vector (); 158 159 while (fromIndex != -1) 161 { 162 while (fromIndex < text.length() 164 && text.charAt(fromIndex) == ' ') 165 { 166 ++fromIndex; 167 if (fromIndex >= text.length()) break; 170 } 171 172 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 && ((spacePos == -1) || (spacePos != -1 && 186 newlinePos < spacePos))) 187 { 189 pos = newlinePos; 190 bHardNewline = true; 191 } 192 else 193 { 194 pos = spacePos; 195 bHardNewline = false; 196 } 197 198 if (pos == -1) 200 { 201 s = text.substring(fromIndex); 202 } 203 else 204 { 205 s = text.substring(fromIndex, pos); 206 } 207 208 if (fm.stringWidth(s) < width) 210 { 211 largestString = s; 212 bestpos = pos; 213 214 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 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 g, 262 String 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 |