1 21 22 package com.izforge.izpack.util; 23 24 import java.awt.Color ; 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.FontMetrics ; 28 import java.awt.Graphics ; 29 import java.util.Vector ; 30 31 import javax.swing.JComponent ; 32 33 34 53 59 public class MultiLineLabel extends JComponent 60 { 61 62 65 private static final long serialVersionUID = 4051045255031894837L; 66 67 public static final int LEFT = 0; 69 public static final int CENTER = 1; 70 71 public static final int RIGHT = 2; 72 73 public static final int DEFAULT_MARGIN = 10; 74 75 public static final int DEFAULT_ALIGN = LEFT; 76 77 public static final int LEAST_ALLOWED = 200; 79 81 private static final int FOUND = 0; 83 private static final int NOT_FOUND = 1; 84 85 private static final int NOT_DONE = 0; 86 87 private static final int DONE = 1; 88 89 private static final char[] WHITE_SPACE = { ' ', '\n', '\t'}; 90 91 private static final char[] SPACES = { ' ', '\t'}; 92 93 private static final char NEW_LINE = '\n'; 94 95 protected Vector line = new Vector (); 97 protected String labelText; 99 protected int numLines; 101 protected int marginHeight; 103 protected int marginWidth; 105 protected int lineHeight; 107 protected int lineAscent; 109 protected int lineDescent; 111 protected int[] lineWidth; 113 protected int maxWidth; 115 private int maxAllowed = LEAST_ALLOWED; 117 private boolean maxAllowedSet = false; 119 121 protected int alignment = LEFT; 123 124 133 136 public MultiLineLabel(String text, int horMargin, int vertMargin, int maxWidth, int justify) 137 { 138 this.labelText = text; 139 this.marginWidth = horMargin; 140 this.marginHeight = vertMargin; 141 this.maxAllowed = maxWidth; 142 this.maxAllowedSet = true; 143 this.alignment = justify; 144 } 145 146 147 154 157 public MultiLineLabel(String label, int marginWidth, int marginHeight) 158 { 159 this.labelText = label; 160 this.marginWidth = marginWidth; 161 this.marginHeight = marginHeight; 162 } 163 164 165 171 174 public MultiLineLabel(String label, int alignment) 175 { 176 this.labelText = label; 177 this.alignment = alignment; 178 } 179 180 181 186 189 public MultiLineLabel(String label) 190 { 191 this.labelText = label; 192 } 193 194 195 208 211 int getPosition(String target, int start, char[] source, int mode) 212 { 213 int status; 214 int position; 215 int scan; 216 int targetEnd; 217 int sourceLength; 218 char temp; 219 220 targetEnd = (target.length() - 1); 221 sourceLength = source.length; 222 position = start; 223 224 if (mode == FOUND) 225 { 226 status = NOT_DONE; 227 while (status != DONE) 228 { 229 position++; 230 if (!(position < targetEnd)) { return (targetEnd); 234 } 235 temp = target.charAt(position); 236 for (scan = 0; scan < sourceLength; scan++) { if (source[scan] == temp) 240 { 241 status = DONE; 242 } 243 } 244 } 245 return (position); 246 } 247 else if (mode == NOT_FOUND) 248 { 249 status = NOT_DONE; 250 while (status != DONE) 251 { 252 position++; 253 if (!(position < targetEnd)) { return (targetEnd); 257 } 258 temp = target.charAt(position); 259 status = DONE; 260 for (scan = 0; scan < sourceLength; scan++) { if (source[scan] == temp) 264 { 265 status = NOT_DONE; 266 } 267 } 268 } 269 return (position); 270 } 271 return (0); 272 } 273 274 275 283 286 int breakWord(String word, FontMetrics fm) 287 { 288 int width; 289 int currentPos; 290 int endPos; 291 292 width = 0; 293 currentPos = 0; 294 endPos = word.length() - 1; 295 296 if (endPos <= 0) { return (currentPos); } 298 while ((width < maxAllowed) && (currentPos < endPos)) 300 { 301 currentPos++; 302 width = fm.stringWidth(labelText.substring(0, currentPos)); 303 } 304 if (currentPos != endPos) 307 { 308 currentPos--; 309 } 310 return (currentPos); 311 } 312 313 314 320 321 private void divideLabel() 322 { 323 int width; 324 int startPos; 325 int currentPos; 326 int lastPos; 327 int endPos; 328 329 line.clear(); 330 FontMetrics fm = this.getFontMetrics(this.getFont()); 331 332 startPos = 0; 333 currentPos = startPos; 334 lastPos = currentPos; 335 endPos = (labelText.length() - 1); 336 337 while (currentPos < endPos) 338 { 339 width = 0; 340 while ((width < maxAllowed) && (currentPos < endPos) 346 && (labelText.charAt(currentPos) != NEW_LINE)) 347 { 348 lastPos = currentPos; 349 currentPos = getPosition(labelText, currentPos, WHITE_SPACE, FOUND); 350 width = fm.stringWidth(labelText.substring(startPos, currentPos)); 351 } 352 if (labelText.charAt(currentPos) == NEW_LINE) 357 { 358 lastPos = currentPos; 359 } 360 if (currentPos == endPos && width <= maxAllowed) 370 { 371 lastPos = currentPos; 372 String s = labelText.substring(startPos); 373 line.addElement(s); 374 } 375 else 381 { 382 if (lastPos == startPos) 388 { 389 lastPos = startPos + breakWord(labelText.substring(startPos, currentPos), fm); 390 } 391 String s = labelText.substring(startPos, lastPos); 392 line.addElement(s); 393 } 394 395 startPos = getPosition(labelText, lastPos, SPACES, NOT_FOUND); 401 currentPos = startPos; 402 } 403 404 numLines = line.size(); 405 lineWidth = new int[numLines]; 406 } 407 408 409 413 414 protected void measure() 415 { 416 if (!maxAllowedSet) 417 { 418 maxAllowed = getParent().getSize().width; 419 } 420 421 if (maxAllowed < (20)) { return; } 423 424 FontMetrics fm = this.getFontMetrics(this.getFont()); 425 426 if (fm == null) { return; } 428 429 divideLabel(); 430 431 this.lineHeight = fm.getHeight(); 432 this.lineDescent = fm.getDescent(); 433 this.maxWidth = 0; 434 435 for (int i = 0; i < numLines; i++) 436 { 437 this.lineWidth[i] = fm.stringWidth((String ) this.line.elementAt(i)); 438 if (this.lineWidth[i] > this.maxWidth) 439 { 440 this.maxWidth = this.lineWidth[i]; 441 } 442 } 443 } 444 445 446 451 452 public void paint(Graphics graphics) 453 { 454 int x; 455 int y; 456 457 measure(); 458 Dimension d = this.getSize(); 459 460 y = lineAscent + (d.height - (numLines * lineHeight)) / 2; 461 462 for (int i = 0; i < numLines; i++) 463 { 464 y += lineHeight; 465 switch (alignment) 466 { 467 case LEFT: 468 x = marginWidth; 469 break; 470 case CENTER: 471 x = (d.width - lineWidth[i]) / 2; 472 break; 473 case RIGHT: 474 x = d.width - marginWidth - lineWidth[i]; 475 break; 476 default: 477 x = (d.width - lineWidth[i]) / 2; 478 } 479 graphics.drawString((String ) line.elementAt(i), x, y); 480 } 481 } 482 483 484 489 490 public void setText(String labelText) 491 { 492 this.labelText = labelText; 493 repaint(); 494 } 495 496 497 502 503 public void setFont(Font font) 504 { 505 super.setFont(font); 506 repaint(); 507 } 508 509 510 515 516 public void setColor(Color color) 517 { 518 super.setForeground(color); 519 repaint(); 520 } 521 522 523 528 529 public void setJustify(int alignment) 530 { 531 this.alignment = alignment; 532 repaint(); 533 } 534 535 536 541 542 public void setMaxWidth(int width) 543 { 544 this.maxAllowed = width; 545 this.maxAllowedSet = true; 546 repaint(); 547 } 548 549 550 555 556 public void setMarginWidth(int margin) 557 { 558 this.marginWidth = margin; 559 repaint(); 560 } 561 562 563 568 569 public void setMarginHeight(int margin) 570 { 571 this.marginHeight = margin; 572 repaint(); 573 } 574 575 576 586 587 public void setBounds(int x, int y, int width, int height) 588 { 589 super.setBounds(x, y, width, height); 590 this.maxAllowed = width; 591 this.maxAllowedSet = true; 592 } 593 594 595 600 601 public int getAlignment() 602 { 603 return (this.alignment); 604 } 605 606 607 612 613 public int getMarginWidth() 614 { 615 return (this.marginWidth); 616 } 617 618 619 624 625 public int getMarginHeight() 626 { 627 return (this.marginHeight); 628 } 629 630 631 635 636 public Dimension getPreferredSize() 637 { 638 measure(); 639 return (new Dimension (maxAllowed, (numLines * (lineHeight + lineAscent + lineDescent)) 640 + (2 * marginHeight))); 641 } 642 643 644 649 650 public Dimension getMinimumSize() 651 { 652 measure(); 653 return (new Dimension (maxAllowed, (numLines * (lineHeight + lineAscent + lineDescent)) 654 + (2 * marginHeight))); 655 } 656 657 658 662 663 public void addNotify() 664 { 665 super.addNotify(); } 667 } 668 669 | Popular Tags |