1 package com.sshtools.ui.awt; 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.StringTokenizer ; 9 import java.util.Vector ; 10 11 22 23 public class WrappingLabel extends Canvas { 24 28 32 protected String text; 36 protected float m_nHAlign; 37 protected float m_nVAlign; 38 protected int baseline; 39 protected FontMetrics fm; 40 41 45 public WrappingLabel() { 46 this(""); } 48 49 public WrappingLabel(String s) { 50 this(s, Canvas.LEFT_ALIGNMENT, Canvas.CENTER_ALIGNMENT); 51 } 52 53 public WrappingLabel(String s, float nHorizontal, float nVertical) { 54 super(); 55 setText(s); 56 setHAlignStyle(nHorizontal); 57 setVAlignStyle(nVertical); 58 } 59 60 64 public float getHAlignStyle() { 65 return m_nHAlign; 66 } 67 68 public float getVAlignStyle() { 69 return m_nVAlign; 70 } 71 72 public String 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 s) { 87 text = s; 88 repaint(); 89 } 90 91 95 public String paramString() { 96 return ""; } 98 99 public Dimension getMinimumSize() { 100 fm = getFontMetrics(getFont()); 101 return new Dimension (9999, breakIntoLines(text, 9999).size() * fm.getHeight()); 102 } 103 104 public Dimension getPreferredSize() { 105 return new Dimension (super.getPreferredSize().width, getMinimumSize().height); 106 } 107 108 public void paint(Graphics g) { 109 if (text != null) { 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 int center = (d.height / 2); 126 currentY = center - ((lines.size() / 2) * fm.getHeight()); 127 } 128 else if (m_nVAlign == Canvas.BOTTOM_ALIGNMENT) { 130 currentY = d.height - (lines.size() * fm.getHeight()); 131 } 132 133 Enumeration elements = lines.elements(); 135 while (elements.hasMoreElements()) { 136 drawAlignedString(g, (String ) (elements.nextElement()), 0, currentY, d.width); 137 currentY += fm.getHeight(); 138 } 139 140 fm = null; 142 } 143 } 144 145 181 protected Vector breakIntoLines(String s, int width) { 182 int fromIndex = 0; 183 int pos = 0; 184 int bestpos; 185 String largestString; 186 Vector lines = new Vector (); 187 188 while (fromIndex != -1) { 190 while (fromIndex < text.length() && text.charAt(fromIndex) == ' ') { 192 ++fromIndex; 193 if (fromIndex >= text.length()) 196 break; 197 } 198 199 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 && ((spacePos == -1) || (spacePos != -1 && newlinePos < spacePos))) 213 { 215 pos = newlinePos; 216 bHardNewline = true; 217 } else { 218 pos = spacePos; 219 bHardNewline = false; 220 } 221 222 if (pos == -1) { 224 s = text.substring(fromIndex); 225 } else { 226 s = text.substring(fromIndex, pos); 227 } 228 229 if (fm.stringWidth(s) < width) { 231 largestString = s; 232 bestpos = pos; 233 234 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 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 g, String 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 |