Source for javax.swing.JLabel

   1: /* JLabel.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import gnu.classpath.NotImplementedException;
  42: 
  43: import java.awt.Component;
  44: import java.awt.Font;
  45: import java.awt.Image;
  46: import java.awt.Point;
  47: import java.awt.Rectangle;
  48: import java.awt.event.KeyEvent;
  49: 
  50: import javax.accessibility.Accessible;
  51: import javax.accessibility.AccessibleContext;
  52: import javax.accessibility.AccessibleExtendedComponent;
  53: import javax.accessibility.AccessibleText;
  54: import javax.swing.plaf.LabelUI;
  55: import javax.swing.text.AttributeSet;
  56: import javax.swing.text.SimpleAttributeSet;
  57: 
  58: /**
  59:  * A swing widget that displays a text message and/or an icon.
  60:  */
  61: public class JLabel extends JComponent implements Accessible, SwingConstants
  62: {
  63: 
  64:   /**
  65:    * Accessibility support for JLabel.
  66:    */
  67:   protected class AccessibleJLabel
  68:     extends JComponent.AccessibleJComponent
  69:     implements AccessibleText, AccessibleExtendedComponent
  70:   {
  71:     /**
  72:      * Returns the selected text. This is null since JLabels
  73:      * are not selectable.
  74:      *
  75:      * @return <code>null</code> because JLabels cannot have selected text
  76:      */
  77:     public String getSelectedText()
  78:     {
  79:       // We return null here since JLabel's text is not selectable.
  80:       return null;
  81:     }
  82: 
  83:     /**
  84:      * Returns the start index of the selected text.
  85:      *
  86:      * @return the start index of the selected text
  87:      */
  88:     public int getSelectionStart()
  89:     {
  90:       // JLabel don't have selected text, so we return -1 here.
  91:       return -1;
  92:     }
  93: 
  94:     /**
  95:      * Returns the end index of the selected text.
  96:      *
  97:      * @return the end index of the selected text
  98:      */
  99:     public int getSelectionEnd()
 100:     {
 101:       // JLabel don't have selected text, so we return -1 here.
 102:       return -1;
 103:     }
 104: 
 105:     /**
 106:      * Returns an {@link AttributeSet} that reflects the text attributes of
 107:      * the specified character. We return an empty
 108:      * <code>AttributeSet</code> here, because JLabels don't support text
 109:      * attributes (at least not yet).
 110:      *
 111:      * @param index the index of the character
 112:      *
 113:      * @return an {@link AttributeSet} that reflects the text attributes of
 114:      *         the specified character
 115:      */
 116:     public AttributeSet getCharacterAttribute(int index)
 117:     {
 118:       // FIXME: Return null here for simple labels, and query the HTML
 119:       // view for HTML labels.
 120:       return new SimpleAttributeSet();
 121:     }
 122: 
 123:     /**
 124:      * Returns the character, word or sentence at the specified index. The
 125:      * <code>part</code> parameter determines what is returned, the character,
 126:      * word or sentence after the index.
 127:      *
 128:      * @param part one of {@link AccessibleText#CHARACTER},
 129:      *             {@link AccessibleText#WORD} or
 130:      *             {@link AccessibleText#SENTENCE}, specifying what is returned
 131:      * @param index the index
 132:      *
 133:      * @return the character, word or sentence after <code>index</code>
 134:      */
 135:     public String getAtIndex(int part, int index)
 136:     {
 137:       String result = "";
 138:       int startIndex = -1;
 139:       int endIndex = -1;
 140:       switch(part)
 141:         {
 142:         case AccessibleText.CHARACTER:
 143:           result = String.valueOf(text.charAt(index));
 144:           break;
 145:         case AccessibleText.WORD:
 146:           startIndex = text.lastIndexOf(' ', index);
 147:           endIndex = text.indexOf(' ', startIndex + 1);
 148:           if (endIndex == -1)
 149:             endIndex = startIndex + 1;
 150:           result = text.substring(startIndex + 1, endIndex);
 151:           break;
 152:         case AccessibleText.SENTENCE:
 153:         default:
 154:           startIndex = text.lastIndexOf('.', index);
 155:           endIndex = text.indexOf('.', startIndex + 1);
 156:           if (endIndex == -1)
 157:             endIndex = startIndex + 1;
 158:           result = text.substring(startIndex + 1, endIndex);
 159:           break;
 160:         }
 161:       return result;
 162:     }
 163: 
 164:     /**
 165:      * Returns the character, word or sentence after the specified index. The
 166:      * <code>part</code> parameter determines what is returned, the character,
 167:      * word or sentence after the index.
 168:      *
 169:      * @param part one of {@link AccessibleText#CHARACTER},
 170:      *             {@link AccessibleText#WORD} or
 171:      *             {@link AccessibleText#SENTENCE}, specifying what is returned
 172:      * @param index the index
 173:      *
 174:      * @return the character, word or sentence after <code>index</code>
 175:      */
 176:     public String getAfterIndex(int part, int index)
 177:     {
 178:       String result = "";
 179:       int startIndex = -1;
 180:       int endIndex = -1;
 181:       switch(part)
 182:         {
 183:         case AccessibleText.CHARACTER:
 184:           result = String.valueOf(text.charAt(index + 1));
 185:           break;
 186:         case AccessibleText.WORD:
 187:           startIndex = text.indexOf(' ', index);
 188:           endIndex = text.indexOf(' ', startIndex + 1);
 189:           if (endIndex == -1)
 190:             endIndex = startIndex + 1;
 191:           result = text.substring(startIndex + 1, endIndex);
 192:           break;
 193:         case AccessibleText.SENTENCE:
 194:         default:
 195:           startIndex = text.indexOf('.', index);
 196:           endIndex = text.indexOf('.', startIndex + 1);
 197:           if (endIndex == -1)
 198:             endIndex = startIndex + 1;
 199:           result = text.substring(startIndex + 1, endIndex);
 200:           break;
 201:         }
 202:       return result;
 203:     }
 204: 
 205:     /**
 206:      * Returns the character, word or sentence before the specified index. The
 207:      * <code>part</code> parameter determines what is returned, the character,
 208:      * word or sentence before the index.
 209:      *
 210:      * @param part one of {@link AccessibleText#CHARACTER},
 211:      *             {@link AccessibleText#WORD} or
 212:      *             {@link AccessibleText#SENTENCE}, specifying what is returned
 213:      * @param index the index
 214:      *
 215:      * @return the character, word or sentence before <code>index</code>
 216:      */
 217:     public String getBeforeIndex(int part, int index)
 218:     {
 219:       String result = "";
 220:       int startIndex = -1;
 221:       int endIndex = -1;
 222:       switch(part)
 223:         {
 224:         case AccessibleText.CHARACTER:
 225:           result = String.valueOf(text.charAt(index - 1));
 226:           break;
 227:         case AccessibleText.WORD:
 228:           endIndex = text.lastIndexOf(' ', index);
 229:           if (endIndex == -1)
 230:             endIndex = 0;
 231:           startIndex = text.lastIndexOf(' ', endIndex - 1);
 232:           result = text.substring(startIndex + 1, endIndex);
 233:           break;
 234:         case AccessibleText.SENTENCE:
 235:         default:
 236:           endIndex = text.lastIndexOf('.', index);
 237:           if (endIndex == -1)
 238:             endIndex = 0;
 239:           startIndex = text.lastIndexOf('.', endIndex - 1);
 240:           result = text.substring(startIndex + 1, endIndex);
 241:           break;
 242:         }
 243:       return result;
 244:     }
 245: 
 246:     /**
 247:      * Returns the caret position. This method returns -1 because JLabel don't
 248:      * have a caret.
 249:      *
 250:      * @return the caret position
 251:      */
 252:     public int getCaretPosition()
 253:     {
 254:       return -1;
 255:     }
 256: 
 257:     /**
 258:      * Returns the number of characters that are displayed by the JLabel.
 259:      *
 260:      * @return the number of characters that are displayed by the JLabel
 261:      */
 262:     public int getCharCount()
 263:     {
 264:       // FIXME: Query HTML view for HTML labels.
 265:       return text.length();
 266:     }
 267: 
 268:     /**
 269:      * Returns the bounding box of the character at the specified index.
 270:      *
 271:      * @param index the index of the character that we return the
 272:      *        bounds for
 273:      *
 274:      * @return the bounding box of the character at the specified index
 275:      */
 276:     public Rectangle getCharacterBounds(int index)
 277:       throws NotImplementedException
 278:     {
 279:       // FIXME: Implement this correctly.
 280:       return new Rectangle();
 281:     }
 282: 
 283:     /**
 284:      * Returns the index of the character that is located at the specified
 285:      * point.
 286:      *
 287:      * @param point the location that we lookup the character for
 288:      *
 289:      * @return the index of the character that is located at the specified
 290:      *         point
 291:      */
 292:     public int getIndexAtPoint(Point point)
 293:       throws NotImplementedException
 294:     {
 295:       // FIXME: Implement this correctly.
 296:       return 0;
 297:     }
 298:   }
 299: 
 300:   /** DOCUMENT ME! */
 301:   private static final long serialVersionUID = 5496508283662221534L;
 302: 
 303:   static final String LABEL_PROPERTY = "labeledBy";
 304: 
 305:   /**
 306:    * The Component the label will give focus to when its mnemonic is
 307:    * activated.
 308:    */
 309:   protected Component labelFor;
 310: 
 311:   /** The label's text. */
 312:   transient String text;
 313: 
 314:   /** Where the label will be positioned horizontally. */
 315:   private transient int horizontalAlignment = LEADING;
 316: 
 317:   /** Where the label text will be placed horizontally relative to the icon. */
 318:   private transient int horizontalTextPosition = TRAILING;
 319: 
 320:   /** Where the label will be positioned vertically. */
 321:   private transient int verticalAlignment = CENTER;
 322: 
 323:   /** Where the label text will be place vertically relative to the icon. */
 324:   private transient int verticalTextPosition = CENTER;
 325: 
 326:   /** The icon painted when the label is enabled. */
 327:   private transient Icon icon;
 328: 
 329:   /** The icon painted when the label is disabled. */
 330:   private transient Icon disabledIcon;
 331: 
 332:   /** The label's mnemnonic key. */
 333:   private transient int displayedMnemonic = KeyEvent.VK_UNDEFINED;
 334: 
 335:   /** The index of the menemonic character in the text. */
 336:   private transient int displayedMnemonicIndex = -1;
 337: 
 338:   /** The gap between the icon and the text. */
 339:   private transient int iconTextGap = 4;
 340: 
 341:   /**
 342:    * Creates a new vertically centered, horizontally on the leading edge
 343:    * JLabel object with text and no icon.
 344:    */
 345:   public JLabel()
 346:   {
 347:     this("", null, LEADING);
 348:   }
 349: 
 350:   /**
 351:    * Creates a new vertically and horizontally centered
 352:    * JLabel object with no text and the given icon.
 353:    *
 354:    * @param image The icon to use with the label.
 355:    */
 356:   public JLabel(Icon image)
 357:   {
 358:     this("", image, CENTER);
 359:   }
 360: 
 361:   /**
 362:    * Creates a new vertically centered JLabel object with no text and the
 363:    * given icon and horizontal alignment. By default, the text is TRAILING
 364:    * the image.
 365:    *
 366:    * @param image The icon to use with the label.
 367:    * @param horizontalAlignment The horizontal alignment of the label.
 368:    */
 369:   public JLabel(Icon image, int horizontalAlignment)
 370:   {
 371:     this("", image, horizontalAlignment);
 372:   }
 373: 
 374:   /**
 375:    * Creates a new horizontally leading and vertically centered JLabel 
 376:    * object with no icon and the given text.
 377:    *
 378:    * @param text The text to use with the label.
 379:    */
 380:   public JLabel(String text)
 381:   {
 382:     this(text, null, LEADING);
 383:   }
 384: 
 385:   /**
 386:    * Creates a new vertically centered JLabel object with no icon and the
 387:    * given text and horizontal alignment.
 388:    *
 389:    * @param text The text to use with the label.
 390:    * @param horizontalAlignment The horizontal alignment of the label.
 391:    */
 392:   public JLabel(String text, int horizontalAlignment)
 393:   {
 394:     this(text, null, horizontalAlignment);
 395:   }
 396: 
 397:   /**
 398:    * Creates a new vertically centered JLabel object with the given text,
 399:    * icon, and horizontal alignment.
 400:    *
 401:    * @param text The text to use with the label.
 402:    * @param icon The icon to use with the label.
 403:    * @param horizontalAlignment The horizontal alignment of the label.
 404:    */
 405:   public JLabel(String text, Icon icon, int horizontalAlignment)
 406:   {
 407:     this.text = text;
 408:     this.icon = icon;
 409:     this.horizontalAlignment = horizontalAlignment;
 410:     setAlignmentX(0.0F);
 411:     updateUI();
 412:   }
 413: 
 414:   /**
 415:    * This method returns the label's UI delegate.
 416:    *
 417:    * @return The label's UI delegate.
 418:    */
 419:   public LabelUI getUI()
 420:   {
 421:     return (LabelUI) ui;
 422:   }
 423: 
 424:   /**
 425:    * This method sets the label's UI delegate.
 426:    *
 427:    * @param ui The label's UI delegate.
 428:    */
 429:   public void setUI(LabelUI ui)
 430:   {
 431:     super.setUI(ui);
 432:   }
 433: 
 434:   /**
 435:    * This method resets the label's UI delegate to the default UI for the
 436:    * current look and feel.
 437:    */
 438:   public void updateUI()
 439:   {
 440:     setUI((LabelUI) UIManager.getUI(this));
 441:   }
 442: 
 443:   /**
 444:    * This method returns a name to identify which look and feel class will be
 445:    * the UI delegate for this label.
 446:    *
 447:    * @return The UIClass identifier. "LabelUI"
 448:    */
 449:   public String getUIClassID()
 450:   {
 451:     return "LabelUI";
 452:   }
 453: 
 454:   /**
 455:    * This method is used primarily for debugging purposes and returns a string
 456:    * that can be used to represent this label.
 457:    *
 458:    * @return A string to represent this label.
 459:    */
 460:   protected String paramString()
 461:   {
 462:     return super.paramString();
 463:   }
 464: 
 465:   /**
 466:    * This method returns the label text.
 467:    *
 468:    * @return The label text.
 469:    */
 470:   public String getText()
 471:   {
 472:     return text;
 473:   }
 474: 
 475:   /**
 476:    * This method changes the "text" property. The given text will be painted
 477:    * in the label.
 478:    *
 479:    * @param newText The label's text.
 480:    */
 481:   public void setText(String newText)
 482:   {
 483:     if (text != newText)
 484:       {
 485:         String oldText = text;
 486:         text = newText;
 487:         firePropertyChange("text", oldText, newText);
 488: 
 489:         if (text != null && text.length() <= displayedMnemonicIndex)
 490:           setDisplayedMnemonicIndex(text.length() - 1);
 491:         revalidate();
 492:         repaint();
 493:       }
 494:   }
 495: 
 496:   /**
 497:    * This method returns the active icon. The active icon is painted when the
 498:    * label is enabled.
 499:    *
 500:    * @return The active icon.
 501:    */
 502:   public Icon getIcon()
 503:   {
 504:     return icon;
 505:   }
 506: 
 507:   /**
 508:    * This method changes the "icon" property. This icon (the active icon) will
 509:    * be the one displayed when the label is enabled.
 510:    *
 511:    * @param newIcon The active icon.
 512:    */
 513:   public void setIcon(Icon newIcon)
 514:   {
 515:     if (icon != newIcon)
 516:       {
 517:     Icon oldIcon = icon;
 518:     icon = newIcon;
 519:     firePropertyChange("icon", oldIcon, newIcon);
 520:     repaint();
 521:       }
 522:   }
 523: 
 524:   /**
 525:    * This method returns the disabled icon. The disabled icon is painted when
 526:    * the label is disabled. If the disabled icon is null and the active icon
 527:    * is an ImageIcon, this method returns a grayed version of the icon. The
 528:    * grayed  version of the icon becomes the disabledIcon.
 529:    *
 530:    * @return The disabled icon.
 531:    */
 532:   public Icon getDisabledIcon()
 533:   {
 534:     if (disabledIcon == null && icon instanceof ImageIcon)
 535:       disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon) icon)
 536:                                                                   .getImage()));
 537: 
 538:     return disabledIcon;
 539:   }
 540: 
 541:   /**
 542:    * This method changes the "disabledIcon" property. This icon (the disabled
 543:    * icon) will be the one displayed when the label is disabled.
 544:    *
 545:    * @param newIcon The disabled icon.
 546:    */
 547:   public void setDisabledIcon(Icon newIcon)
 548:   {
 549:     if (disabledIcon != newIcon)
 550:       {
 551:     Icon oldIcon = disabledIcon;
 552:     disabledIcon = newIcon;
 553:     firePropertyChange("disabledIcon", oldIcon, newIcon);
 554:       }
 555:   }
 556: 
 557:   /**
 558:    * This method sets the keycode that will be the label's mnemonic. If the
 559:    * label is used as a label for another component, the label will give
 560:    * focus to that component when the mnemonic is activated.
 561:    *
 562:    * @param mnemonic The keycode to use for the mnemonic.
 563:    */
 564:   public void setDisplayedMnemonic(int mnemonic)
 565:   {
 566:     if (displayedMnemonic != mnemonic)
 567:       {
 568:     firePropertyChange("displayedMnemonic",
 569:                        displayedMnemonic, mnemonic);
 570:     displayedMnemonic = mnemonic;
 571: 
 572:     if (text != null)
 573:       setDisplayedMnemonicIndex(text.toUpperCase().indexOf(mnemonic));
 574:       }
 575:   }
 576: 
 577:   /**
 578:    * This method sets the character that will be the mnemonic used. If the
 579:    * label is used as a label for another component, the label will give
 580:    * focus to that component when the mnemonic is activated.
 581:    *
 582:    * @param mnemonic The character to use for the mnemonic.
 583:    */
 584:   public void setDisplayedMnemonic(char mnemonic)
 585:   {
 586:     setDisplayedMnemonic((int) Character.toUpperCase(mnemonic));
 587:   }
 588: 
 589:   /**
 590:    * This method returns the keycode that is used for the label's mnemonic.
 591:    *
 592:    * @return The keycode that is used for the label's mnemonic.
 593:    */
 594:   public int getDisplayedMnemonic()
 595:   {
 596:     return (int) displayedMnemonic;
 597:   }
 598: 
 599:   /**
 600:    * This method sets which character in the text will be the underlined
 601:    * character. If the given index is -1, then this indicates  that there is
 602:    * no mnemonic. If the index is less than -1 or if the index is equal to
 603:    * the length, this method will throw an IllegalArgumentException.
 604:    *
 605:    * @param newIndex The index of the character to underline.
 606:    *
 607:    * @throws IllegalArgumentException If index less than -1 or index equals
 608:    *         length.
 609:    */
 610:   public void setDisplayedMnemonicIndex(int newIndex)
 611:     throws IllegalArgumentException
 612:   {
 613:     if (newIndex < -1 || (text != null && newIndex >= text.length()))
 614:       throw new IllegalArgumentException();
 615: 
 616:     if (newIndex == -1
 617:         || text == null
 618:     || text.charAt(newIndex) != displayedMnemonic)
 619:       newIndex = -1;
 620: 
 621:     if (newIndex != displayedMnemonicIndex)
 622:       {
 623:     int oldIndex = displayedMnemonicIndex;
 624:     displayedMnemonicIndex = newIndex;
 625:     firePropertyChange("displayedMnemonicIndex",
 626:                        oldIndex, newIndex);
 627:       }
 628:   }
 629: 
 630:   /**
 631:    * This method returns which character in the text will be  the underlined
 632:    * character.
 633:    *
 634:    * @return The index of the character that will be underlined.
 635:    */
 636:   public int getDisplayedMnemonicIndex()
 637:   {
 638:     return displayedMnemonicIndex;
 639:   }
 640: 
 641:   /**
 642:    * This method ensures that the key is valid as a horizontal alignment.
 643:    * Valid keys are: LEFT, CENTER, RIGHT, LEADING, TRAILING
 644:    *
 645:    * @param key The key to check.
 646:    * @param message The message of the exception to be thrown if the key is
 647:    *        invalid.
 648:    *
 649:    * @return The key if it's valid.
 650:    *
 651:    * @throws IllegalArgumentException If the key is invalid.
 652:    */
 653:   protected int checkHorizontalKey(int key, String message)
 654:   {
 655:     if (key != LEFT && key != CENTER && key != RIGHT && key != LEADING
 656:         && key != TRAILING)
 657:       throw new IllegalArgumentException(message);
 658:     else
 659:       return key;
 660:   }
 661: 
 662:   /**
 663:    * This method ensures that the key is valid as a  vertical alignment. Valid
 664:    * keys are: TOP, CENTER, and BOTTOM.
 665:    *
 666:    * @param key The key to check.
 667:    * @param message The message of the exception to be thrown if the key is
 668:    *        invalid.
 669:    *
 670:    * @return The key if it's valid.
 671:    *
 672:    * @throws IllegalArgumentException If the key is invalid.
 673:    */
 674:   protected int checkVerticalKey(int key, String message)
 675:   {
 676:     if (key != TOP && key != BOTTOM && key != CENTER)
 677:       throw new IllegalArgumentException(message);
 678:     else
 679:       return key;
 680:   }
 681: 
 682:   /**
 683:    * This method returns the gap between the icon and the text.
 684:    *
 685:    * @return The gap between the icon and the text.
 686:    */
 687:   public int getIconTextGap()
 688:   {
 689:     return iconTextGap;
 690:   }
 691: 
 692:   /**
 693:    * This method changes the "iconTextGap" property. The iconTextGap
 694:    * determines how much space there is between the icon and the text.
 695:    *
 696:    * @param newGap The gap between the icon and the text.
 697:    */
 698:   public void setIconTextGap(int newGap)
 699:   {
 700:     if (iconTextGap != newGap)
 701:       {
 702:     firePropertyChange("iconTextGap", iconTextGap, newGap);
 703:     iconTextGap = newGap;
 704:       }
 705:   }
 706: 
 707:   /**
 708:    * This method returns the vertical alignment of the label.
 709:    *
 710:    * @return The vertical alignment of the label.
 711:    */
 712:   public int getVerticalAlignment()
 713:   {
 714:     return verticalAlignment;
 715:   }
 716: 
 717:   /**
 718:    * This method changes the "verticalAlignment" property of the label. The
 719:    * vertical alignment determines how where the label will be placed
 720:    * vertically. If the alignment is not valid, it will default to the
 721:    * center.
 722:    *
 723:    * @param alignment The vertical alignment of the label.
 724:    */
 725:   public void setVerticalAlignment(int alignment)
 726:   {
 727:     if (alignment == verticalAlignment)
 728:       return;
 729: 
 730:     int oldAlignment = verticalAlignment;
 731:     verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
 732:     firePropertyChange("verticalAlignment", oldAlignment, verticalAlignment);
 733:   }
 734: 
 735:   /**
 736:    * This method returns the horziontal alignment of the label.
 737:    *
 738:    * @return The horizontal alignment of the label.
 739:    */
 740:   public int getHorizontalAlignment()
 741:   {
 742:     return horizontalAlignment;
 743:   }
 744: 
 745:   /**
 746:    * This method changes the "horizontalAlignment" property. The horizontal
 747:    * alignment determines where the label will be placed horizontally.
 748:    *
 749:    * @param alignment The horizontal alignment of the label.
 750:    */
 751:   public void setHorizontalAlignment(int alignment)
 752:   {
 753:     if (horizontalAlignment == alignment)
 754:       return;
 755:     
 756:     int oldAlignment = horizontalAlignment;
 757:     horizontalAlignment = checkHorizontalKey(alignment, "horizontalAlignment");
 758:     firePropertyChange("horizontalAlignment", oldAlignment,
 759:                        horizontalAlignment);
 760:   }
 761: 
 762:   /**
 763:    * This method returns the vertical text position of the label.
 764:    *
 765:    * @return The vertical text position of the label.
 766:    */
 767:   public int getVerticalTextPosition()
 768:   {
 769:     return verticalTextPosition;
 770:   }
 771: 
 772:   /**
 773:    * This method changes the "verticalTextPosition" property of the label. The
 774:    * vertical text position determines where the text will be placed
 775:    * vertically relative to the icon.
 776:    *
 777:    * @param textPosition The vertical text position.
 778:    */
 779:   public void setVerticalTextPosition(int textPosition)
 780:   {
 781:     if (textPosition != verticalTextPosition)
 782:       {
 783:     int oldPos = verticalTextPosition;
 784:     verticalTextPosition = checkVerticalKey(textPosition,
 785:                                             "verticalTextPosition");
 786:     firePropertyChange("verticalTextPosition", oldPos,
 787:                        verticalTextPosition);
 788:       }
 789:   }
 790: 
 791:   /**
 792:    * This method returns the horizontal text position of the label.
 793:    *
 794:    * @return The horizontal text position.
 795:    */
 796:   public int getHorizontalTextPosition()
 797:   {
 798:     return horizontalTextPosition;
 799:   }
 800: 
 801:   /**
 802:    * This method changes the "horizontalTextPosition" property of the label.
 803:    * The horizontal text position determines where the text will be placed
 804:    * horizontally relative to the icon.
 805:    *
 806:    * @param textPosition The horizontal text position.
 807:    */
 808:   public void setHorizontalTextPosition(int textPosition)
 809:   {
 810:     if (textPosition != horizontalTextPosition)
 811:       {
 812:     int oldPos = horizontalTextPosition;
 813:     horizontalTextPosition = checkHorizontalKey(textPosition,
 814:                                                 "horizontalTextPosition");
 815:     firePropertyChange("horizontalTextPosition", oldPos,
 816:                        horizontalTextPosition);
 817:       }
 818:   }
 819: 
 820:   /**
 821:    * This method simply returns false if the current icon image (current  icon
 822:    * will depend on whether the label is enabled) is not equal to the passed
 823:    * in image.
 824:    *
 825:    * @param img The image to check.
 826:    * @param infoflags The bitwise inclusive OR of ABORT, ALLBITS, ERROR,
 827:    *        FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, and WIDTH
 828:    * @param x The x position
 829:    * @param y The y position
 830:    * @param w The width
 831:    * @param h The height
 832:    *
 833:    * @return Whether the current icon image is equal to the image given.
 834:    */
 835:   public boolean imageUpdate(Image img, int infoflags, int x, int y, int w,
 836:                              int h)
 837:   {
 838:     Icon currIcon = isEnabled() ? icon : disabledIcon;
 839: 
 840:     // XXX: Is this the correct way to check for image equality?
 841:     if (currIcon != null && currIcon instanceof ImageIcon)
 842:       return (((ImageIcon) currIcon).getImage() == img);
 843: 
 844:     return false;
 845:   }
 846: 
 847:   /**
 848:    * This method returns the component that the label gives focus to  when the
 849:    * mnemonic is activated.
 850:    *
 851:    * @return The component that gets focus when the label's mnemonic is
 852:    *         activated.
 853:    */
 854:   public Component getLabelFor()
 855:   {
 856:     return labelFor;
 857:   }
 858: 
 859:   /**
 860:    * This method changes the "labelFor" property. The component that the label
 861:    * is acting as a label for will request focus when the label's  mnemonic
 862:    * is activated.
 863:    *
 864:    * @param c The component that gets focus when the label's mnemonic is
 865:    *        activated.
 866:    */
 867:   public void setLabelFor(Component c)
 868:   {
 869:     if (c != labelFor)
 870:       {
 871:         // We put the label into the client properties for the labeled
 872:         // component so that it can be read by the AccessibleJComponent.
 873:         // The other option would be to reserve a default visible field
 874:         // in JComponent, but since this is relativly seldomly used, it
 875:         // would be unnecessary waste of memory to do so.
 876:     Component oldLabelFor = labelFor;
 877:         if (oldLabelFor instanceof JComponent)
 878:           {
 879:             ((JComponent) oldLabelFor).putClientProperty(LABEL_PROPERTY, null);
 880:           }
 881: 
 882:     labelFor = c;
 883:     if (labelFor instanceof JComponent)
 884:           {
 885:             ((JComponent) labelFor).putClientProperty(LABEL_PROPERTY, this);
 886:           }
 887: 
 888:     firePropertyChange("labelFor", oldLabelFor, labelFor);
 889:       }
 890:   }
 891: 
 892:   /**
 893:    * This method overrides setFont so that we can call for a repaint after the
 894:    * font is changed.
 895:    *
 896:    * @param f The font for this label.
 897:    */
 898:   public void setFont(Font f)
 899:   {
 900:     super.setFont(f);
 901:     repaint();
 902:   }
 903: 
 904:   /**
 905:    * DOCUMENT ME!
 906:    *
 907:    * @return The accessible context.
 908:    */
 909:   public AccessibleContext getAccessibleContext()
 910:   {
 911:     if (accessibleContext == null)
 912:       accessibleContext = new AccessibleJLabel();
 913:     return accessibleContext;
 914:   }
 915: }