| 1 7 8 package javax.swing; 9 10 import java.awt.Component ; 11 import java.awt.Font ; 12 import java.awt.Image ; 13 import java.awt.*; 14 import java.text.*; 15 import java.awt.geom.*; 16 17 import java.io.ObjectOutputStream ; 18 import java.io.ObjectInputStream ; 19 import java.io.IOException ; 20 21 import javax.swing.plaf.LabelUI ; 22 import javax.accessibility.*; 23 import javax.swing.text.*; 24 import javax.swing.text.html.*; 25 import javax.swing.plaf.basic.*; 26 import java.util.*; 27 28 29 84 public class JLabel extends JComponent implements SwingConstants , Accessible 85 { 86 90 private static final String uiClassID = "LabelUI"; 91 92 private int mnemonic = '\0'; 93 private int mnemonicIndex = -1; 94 95 private String text = ""; private Icon defaultIcon = null; 97 private Icon disabledIcon = null; 98 private boolean disabledIconSet = false; 99 100 private int verticalAlignment = CENTER; 101 private int horizontalAlignment = LEADING; 102 private int verticalTextPosition = CENTER; 103 private int horizontalTextPosition = TRAILING; 104 private int iconTextGap = 4; 105 106 protected Component labelFor = null; 107 108 118 static final String LABELED_BY_PROPERTY = "labeledBy"; 119 120 136 public JLabel(String text, Icon icon, int horizontalAlignment) { 137 setText(text); 138 setIcon(icon); 139 setHorizontalAlignment(horizontalAlignment); 140 updateUI(); 141 setAlignmentX(LEFT_ALIGNMENT); 142 } 143 144 158 public JLabel(String text, int horizontalAlignment) { 159 this(text, null, horizontalAlignment); 160 } 161 162 169 public JLabel(String text) { 170 this(text, null, LEADING); 171 } 172 173 187 public JLabel(Icon image, int horizontalAlignment) { 188 this(null, image, horizontalAlignment); 189 } 190 191 198 public JLabel(Icon image) { 199 this(null, image, CENTER); 200 } 201 202 210 public JLabel() { 211 this("", null, LEADING); 212 } 213 214 215 220 public LabelUI getUI() { 221 return (LabelUI )ui; 222 } 223 224 225 236 public void setUI(LabelUI ui) { 237 super.setUI(ui); 238 if (!disabledIconSet && disabledIcon != null) { 240 setDisabledIcon(null); 241 } 242 } 243 244 245 250 public void updateUI() { 251 setUI((LabelUI )UIManager.getUI(this)); 252 } 253 254 255 264 public String getUIClassID() { 265 return uiClassID; 266 } 267 268 269 275 public String getText() { 276 return text; 277 } 278 279 280 297 public void setText(String text) { 298 299 String oldAccessibleName = null; 300 if (accessibleContext != null) { 301 oldAccessibleName = accessibleContext.getAccessibleName(); 302 } 303 304 String oldValue = this.text; 305 this.text = text; 306 firePropertyChange("text", oldValue, text); 307 308 setDisplayedMnemonicIndex( 309 SwingUtilities.findDisplayedMnemonicIndex( 310 text, getDisplayedMnemonic())); 311 312 if ((accessibleContext != null) 313 && (accessibleContext.getAccessibleName() != oldAccessibleName)) { 314 accessibleContext.firePropertyChange( 315 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 316 oldAccessibleName, 317 accessibleContext.getAccessibleName()); 318 } 319 if (text == null || oldValue == null || !text.equals(oldValue)) { 320 revalidate(); 321 repaint(); 322 } 323 } 324 325 326 332 public Icon getIcon() { 333 return defaultIcon; 334 } 335 336 353 public void setIcon(Icon icon) { 354 Icon oldValue = defaultIcon; 355 defaultIcon = icon; 356 357 362 if ((defaultIcon != oldValue) && !disabledIconSet) { 363 disabledIcon = null; 364 } 365 366 firePropertyChange("icon", oldValue, defaultIcon); 367 368 if ((accessibleContext != null) && (oldValue != defaultIcon)) { 369 accessibleContext.firePropertyChange( 370 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 371 oldValue, defaultIcon); 372 } 373 374 378 if (defaultIcon != oldValue) { 379 if ((defaultIcon == null) || 380 (oldValue == null) || 381 (defaultIcon.getIconWidth() != oldValue.getIconWidth()) || 382 (defaultIcon.getIconHeight() != oldValue.getIconHeight())) { 383 revalidate(); 384 } 385 repaint(); 386 } 387 } 388 389 390 403 public Icon getDisabledIcon() { 404 if (!disabledIconSet && disabledIcon == null && defaultIcon != null) { 405 disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, defaultIcon); 406 if (disabledIcon != null) { 407 firePropertyChange("disabledIcon", null, disabledIcon); 408 } 409 } 410 return disabledIcon; 411 } 412 413 414 428 public void setDisabledIcon(Icon disabledIcon) { 429 Icon oldValue = this.disabledIcon; 430 this.disabledIcon = disabledIcon; 431 disabledIconSet = (disabledIcon != null); 432 firePropertyChange("disabledIcon", oldValue, disabledIcon); 433 if (disabledIcon != oldValue) { 434 if (disabledIcon == null || oldValue == null || 435 disabledIcon.getIconWidth() != oldValue.getIconWidth() || 436 disabledIcon.getIconHeight() != oldValue.getIconHeight()) { 437 revalidate(); 438 } 439 if (!isEnabled()) { 440 repaint(); 441 } 442 } 443 } 444 445 446 460 public void setDisplayedMnemonic(int key) { 461 int oldKey = mnemonic; 462 mnemonic = key; 463 firePropertyChange("displayedMnemonic", oldKey, mnemonic); 464 465 setDisplayedMnemonicIndex( 466 SwingUtilities.findDisplayedMnemonicIndex(getText(), mnemonic)); 467 468 if (key != oldKey) { 469 revalidate(); 470 repaint(); 471 } 472 } 473 474 475 481 public void setDisplayedMnemonic(char aChar) { 482 int vk = (int) aChar; 483 if(vk >= 'a' && vk <='z') 484 vk -= ('a' - 'A'); 485 setDisplayedMnemonic(vk); 486 } 487 488 489 501 public int getDisplayedMnemonic() { 502 return mnemonic; 503 } 504 505 532 public void setDisplayedMnemonicIndex(int index) 533 throws IllegalArgumentException { 534 int oldValue = mnemonicIndex; 535 if (index == -1) { 536 mnemonicIndex = -1; 537 } else { 538 String text = getText(); 539 int textLength = (text == null) ? 0 : text.length(); 540 if (index < -1 || index >= textLength) { throw new IllegalArgumentException ("index == " + index); 542 } 543 } 544 mnemonicIndex = index; 545 firePropertyChange("displayedMnemonicIndex", oldValue, index); 546 if (index != oldValue) { 547 revalidate(); 548 repaint(); 549 } 550 } 551 552 560 public int getDisplayedMnemonicIndex() { 561 return mnemonicIndex; 562 } 563 564 574 protected int checkHorizontalKey(int key, String message) { 575 if ((key == LEFT) || 576 (key == CENTER) || 577 (key == RIGHT) || 578 (key == LEADING) || 579 (key == TRAILING)) { 580 return key; 581 } 582 else { 583 throw new IllegalArgumentException (message); 584 } 585 } 586 587 588 598 protected int checkVerticalKey(int key, String message) { 599 if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) { 600 return key; 601 } 602 else { 603 throw new IllegalArgumentException (message); 604 } 605 } 606 607 608 616 public int getIconTextGap() { 617 return iconTextGap; 618 } 619 620 621 636 public void setIconTextGap(int iconTextGap) { 637 int oldValue = this.iconTextGap; 638 this.iconTextGap = iconTextGap; 639 firePropertyChange("iconTextGap", oldValue, iconTextGap); 640 if (iconTextGap != oldValue) { 641 revalidate(); 642 repaint(); 643 } 644 } 645 646 647 648 660 public int getVerticalAlignment() { 661 return verticalAlignment; 662 } 663 664 665 686 public void setVerticalAlignment(int alignment) { 687 if (alignment == verticalAlignment) return; 688 int oldValue = verticalAlignment; 689 verticalAlignment = checkVerticalKey(alignment, "verticalAlignment"); 690 firePropertyChange("verticalAlignment", oldValue, verticalAlignment); 691 repaint(); 692 } 693 694 695 709 public int getHorizontalAlignment() { 710 return horizontalAlignment; 711 } 712 713 738 public void setHorizontalAlignment(int alignment) { 739 if (alignment == horizontalAlignment) return; 740 int oldValue = horizontalAlignment; 741 horizontalAlignment = checkHorizontalKey(alignment, 742 "horizontalAlignment"); 743 firePropertyChange("horizontalAlignment", 744 oldValue, horizontalAlignment); 745 repaint(); 746 } 747 748 749 762 public int getVerticalTextPosition() { 763 return verticalTextPosition; 764 } 765 766 767 792 public void setVerticalTextPosition(int textPosition) { 793 if (textPosition == verticalTextPosition) return; 794 int old = verticalTextPosition; 795 verticalTextPosition = checkVerticalKey(textPosition, 796 "verticalTextPosition"); 797 firePropertyChange("verticalTextPosition", old, verticalTextPosition); 798 repaint(); 799 } 800 801 802 816 public int getHorizontalTextPosition() { 817 return horizontalTextPosition; 818 } 819 820 821 847 public void setHorizontalTextPosition(int textPosition) { 848 int old = horizontalTextPosition; 849 this.horizontalTextPosition = checkHorizontalKey(textPosition, 850 "horizontalTextPosition"); 851 firePropertyChange("horizontalTextPosition", 852 old, horizontalTextPosition); 853 repaint(); 854 } 855 856 857 864 public boolean imageUpdate(Image img, int infoflags, 865 int x, int y, int w, int h) { 866 if (!isShowing() || 869 !SwingUtilities.doesIconReferenceImage(getIcon(), img) && 870 !SwingUtilities.doesIconReferenceImage(disabledIcon, img)) { 871 872 return false; 873 } 874 return super.imageUpdate(img, infoflags, x, y, w, h); 875 } 876 877 878 882 private void writeObject(ObjectOutputStream s) throws IOException { 883 s.defaultWriteObject(); 884 if (getUIClassID().equals(uiClassID)) { 885 byte count = JComponent.getWriteObjCounter(this); 886 JComponent.setWriteObjCounter(this, --count); 887 if (count == 0 && ui != null) { 888 ui.installUI(this); 889 } 890 } 891 } 892 893 894 903 protected String paramString() { 904 String textString = (text != null ? 905 text : ""); 906 String defaultIconString = ((defaultIcon != null) 907 && (defaultIcon != this) ? 908 defaultIcon.toString() : ""); 909 String disabledIconString = ((disabledIcon != null) 910 && (disabledIcon != this) ? 911 disabledIcon.toString() : ""); 912 String labelForString = (labelFor != null ? 913 labelFor.toString() : ""); 914 String verticalAlignmentString; 915 if (verticalAlignment == TOP) { 916 verticalAlignmentString = "TOP"; 917 } else if (verticalAlignment == CENTER) { 918 verticalAlignmentString = "CENTER"; 919 } else if (verticalAlignment == BOTTOM) { 920 verticalAlignmentString = "BOTTOM"; 921 } else verticalAlignmentString = ""; 922 String horizontalAlignmentString; 923 if (horizontalAlignment == LEFT) { 924 horizontalAlignmentString = "LEFT"; 925 } else if (horizontalAlignment == CENTER) { 926 horizontalAlignmentString = "CENTER"; 927 } else if (horizontalAlignment == RIGHT) { 928 horizontalAlignmentString = "RIGHT"; 929 } else if (horizontalAlignment == LEADING) { 930 horizontalAlignmentString = "LEADING"; 931 } else if (horizontalAlignment == TRAILING) { 932 horizontalAlignmentString = "TRAILING"; 933 } else horizontalAlignmentString = ""; 934 String verticalTextPositionString; 935 if (verticalTextPosition == TOP) { 936 verticalTextPositionString = "TOP"; 937 } else if (verticalTextPosition == CENTER) { 938 verticalTextPositionString = "CENTER"; 939 } else if (verticalTextPosition == BOTTOM) { 940 verticalTextPositionString = "BOTTOM"; 941 } else verticalTextPositionString = ""; 942 String horizontalTextPositionString; 943 if (horizontalTextPosition == LEFT) { 944 horizontalTextPositionString = "LEFT"; 945 } else if (horizontalTextPosition == CENTER) { 946 horizontalTextPositionString = "CENTER"; 947 } else if (horizontalTextPosition == RIGHT) { 948 horizontalTextPositionString = "RIGHT"; 949 } else if (horizontalTextPosition == LEADING) { 950 horizontalTextPositionString = "LEADING"; 951 } else if (horizontalTextPosition == TRAILING) { 952 horizontalTextPositionString = "TRAILING"; 953 } else horizontalTextPositionString = ""; 954 955 return super.paramString() + 956 ",defaultIcon=" + defaultIconString + 957 ",disabledIcon=" + disabledIconString + 958 ",horizontalAlignment=" + horizontalAlignmentString + 959 ",horizontalTextPosition=" + horizontalTextPositionString + 960 ",iconTextGap=" + iconTextGap + 961 ",labelFor=" + labelForString + 962 ",text=" + textString + 963 ",verticalAlignment=" + verticalAlignmentString + 964 ",verticalTextPosition=" + verticalTextPositionString; 965 } 966 967 970 971 983 public Component getLabelFor() { 984 return labelFor; 985 } 986 987 |