2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 import java
.awt
.Component
;
43 import java
.awt
.Image
;
44 import java
.awt
.Point
;
45 import java
.awt
.Rectangle
;
46 import java
.awt
.event
.KeyEvent
;
48 import javax
.accessibility
.Accessible
;
49 import javax
.accessibility
.AccessibleContext
;
50 import javax
.accessibility
.AccessibleExtendedComponent
;
51 import javax
.accessibility
.AccessibleText
;
52 import javax
.swing
.plaf
.LabelUI
;
53 import javax
.swing
.text
.AttributeSet
;
54 import javax
.swing
.text
.SimpleAttributeSet
;
57 * A swing widget that displays a text message and/or an icon.
59 public class JLabel
extends JComponent
implements Accessible
, SwingConstants
63 * Accessibility support for JLabel.
65 protected class AccessibleJLabel
66 extends JComponent
.AccessibleJComponent
67 implements AccessibleText
, AccessibleExtendedComponent
70 * Returns the selected text. This is an empty string since JLabels
73 * @return the selected text
75 public String
getSelectedText()
77 // We return "" here since JLabel's text is not selectable.
82 * Returns the start index of the selected text.
84 * @return the start index of the selected text
86 public int getSelectionStart()
88 // TODO: Figure out what should be returned here, because JLabels don't
89 // allow selection. I guess -1 for now.
94 * Returns the end index of the selected text.
96 * @return the end index of the selected text
98 public int getSelectionEnd()
100 // TODO: Figure out what should be returned here, because JLabels don't
101 // allow selection. I guess -1 for now.
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).
111 * @param index the index of the character
113 * @return an {@link AttributeSet} that reflects the text attributes of
114 * the specified character
116 public AttributeSet
getCharacterAttribute(int index
)
118 return new SimpleAttributeSet();
122 * Returns the character, word or sentence at the specified index. The
123 * <code>part</code> parameter determines what is returned, the character,
124 * word or sentence after the index.
126 * @param part one of {@link AccessibleText#CHARACTER},
127 * {@link AccessibleText#WORD} or
128 * {@link AccessibleText#SENTENCE}, specifying what is returned
129 * @param index the index
131 * @return the character, word or sentence after <code>index</code>
133 public String
getAtIndex(int part
, int index
)
140 case AccessibleText
.CHARACTER
:
141 result
= String
.valueOf(text
.charAt(index
));
143 case AccessibleText
.WORD
:
144 startIndex
= text
.lastIndexOf(' ', index
);
145 endIndex
= text
.indexOf(' ', startIndex
+ 1);
147 endIndex
= startIndex
+ 1;
148 result
= text
.substring(startIndex
+ 1, endIndex
);
150 case AccessibleText
.SENTENCE
:
152 startIndex
= text
.lastIndexOf('.', index
);
153 endIndex
= text
.indexOf('.', startIndex
+ 1);
155 endIndex
= startIndex
+ 1;
156 result
= text
.substring(startIndex
+ 1, endIndex
);
163 * Returns the character, word or sentence after the specified index. The
164 * <code>part</code> parameter determines what is returned, the character,
165 * word or sentence after the index.
167 * @param part one of {@link AccessibleText#CHARACTER},
168 * {@link AccessibleText#WORD} or
169 * {@link AccessibleText#SENTENCE}, specifying what is returned
170 * @param index the index
172 * @return the character, word or sentence after <code>index</code>
174 public String
getAfterIndex(int part
, int index
)
181 case AccessibleText
.CHARACTER
:
182 result
= String
.valueOf(text
.charAt(index
+ 1));
184 case AccessibleText
.WORD
:
185 startIndex
= text
.indexOf(' ', index
);
186 endIndex
= text
.indexOf(' ', startIndex
+ 1);
188 endIndex
= startIndex
+ 1;
189 result
= text
.substring(startIndex
+ 1, endIndex
);
191 case AccessibleText
.SENTENCE
:
193 startIndex
= text
.indexOf('.', index
);
194 endIndex
= text
.indexOf('.', startIndex
+ 1);
196 endIndex
= startIndex
+ 1;
197 result
= text
.substring(startIndex
+ 1, endIndex
);
204 * Returns the character, word or sentence before the specified index. The
205 * <code>part</code> parameter determines what is returned, the character,
206 * word or sentence before the index.
208 * @param part one of {@link AccessibleText#CHARACTER},
209 * {@link AccessibleText#WORD} or
210 * {@link AccessibleText#SENTENCE}, specifying what is returned
211 * @param index the index
213 * @return the character, word or sentence before <code>index</code>
215 public String
getBeforeIndex(int part
, int index
)
222 case AccessibleText
.CHARACTER
:
223 result
= String
.valueOf(text
.charAt(index
- 1));
225 case AccessibleText
.WORD
:
226 endIndex
= text
.lastIndexOf(' ', index
);
229 startIndex
= text
.lastIndexOf(' ', endIndex
- 1);
230 result
= text
.substring(startIndex
+ 1, endIndex
);
232 case AccessibleText
.SENTENCE
:
234 endIndex
= text
.lastIndexOf('.', index
);
237 startIndex
= text
.lastIndexOf('.', endIndex
- 1);
238 result
= text
.substring(startIndex
+ 1, endIndex
);
245 * Returns the caret position. This method returns -1 because JLabel don't
248 * @return the caret position
250 public int getCaretPosition()
256 * Returns the number of characters that are displayed by the JLabel.
258 * @return the number of characters that are displayed by the JLabel
260 public int getCharCount()
262 return text
.length();
266 * Returns the bounding box of the character at the specified index.
268 * @param index the index of the character that we return the
271 * @return the bounding box of the character at the specified index
273 public Rectangle
getCharacterBounds(int index
)
275 // FIXME: Implement this correctly.
276 return new Rectangle();
280 * Returns the index of the character that is located at the specified
283 * @param point the location that we lookup the character for
285 * @return the index of the character that is located at the specified
288 public int getIndexAtPoint(Point point
)
290 // FIXME: Implement this correctly.
296 private static final long serialVersionUID
= 5496508283662221534L;
299 * The Component the label will give focus to when its mnemonic is
302 protected Component labelFor
;
304 /** The label's text. */
305 transient String text
;
307 /** Where the label will be positioned horizontally. */
308 private transient int horizontalAlignment
= LEADING
;
310 /** Where the label text will be placed horizontally relative to the icon. */
311 private transient int horizontalTextPosition
= TRAILING
;
313 /** Where the label will be positioned vertically. */
314 private transient int verticalAlignment
= CENTER
;
316 /** Where the label text will be place vertically relative to the icon. */
317 private transient int verticalTextPosition
= CENTER
;
319 /** The icon painted when the label is enabled. */
320 private transient Icon icon
;
322 /** The icon painted when the label is disabled. */
323 private transient Icon disabledIcon
;
325 /** The label's mnemnonic key. */
326 private transient int displayedMnemonic
= KeyEvent
.VK_UNDEFINED
;
328 /** The index of the menemonic character in the text. */
329 private transient int displayedMnemonicIndex
= -1;
331 /** The gap between the icon and the text. */
332 private transient int iconTextGap
= 4;
335 * Creates a new vertically centered, horizontally on the leading edge
336 * JLabel object with text and no icon.
340 this(null, null, LEADING
);
344 * Creates a new vertically and horizontally centered
345 * JLabel object with no text and the given icon.
347 * @param image The icon to use with the label.
349 public JLabel(Icon image
)
351 this(null, image
, CENTER
);
355 * Creates a new vertically centered JLabel object with no text and the
356 * given icon and horizontal alignment. By default, the text is TRAILING
359 * @param image The icon to use with the label.
360 * @param horizontalAlignment The horizontal alignment of the label.
362 public JLabel(Icon image
, int horizontalAlignment
)
364 this(null, image
, horizontalAlignment
);
368 * Creates a new horizontally leading and vertically centered JLabel
369 * object with no icon and the given text.
371 * @param text The text to use with the label.
373 public JLabel(String text
)
375 this(text
, null, LEADING
);
379 * Creates a new vertically centered JLabel object with no icon and the
380 * given text and horizontal alignment.
382 * @param text The text to use with the label.
383 * @param horizontalAlignment The horizontal alignment of the label.
385 public JLabel(String text
, int horizontalAlignment
)
387 this(text
, null, horizontalAlignment
);
391 * Creates a new vertically centered JLabel object with the given text,
392 * icon, and horizontal alignment.
394 * @param text The text to use with the label.
395 * @param icon The icon to use with the label.
396 * @param horizontalAlignment The horizontal alignment of the label.
398 public JLabel(String text
, Icon icon
, int horizontalAlignment
)
402 this.horizontalAlignment
= horizontalAlignment
;
408 * This method returns the label's UI delegate.
410 * @return The label's UI delegate.
412 public LabelUI
getUI()
418 * This method sets the label's UI delegate.
420 * @param ui The label's UI delegate.
422 public void setUI(LabelUI ui
)
428 * This method resets the label's UI delegate to the default UI for the
429 * current look and feel.
431 public void updateUI()
433 setUI((LabelUI
) UIManager
.getUI(this));
437 * This method returns a name to identify which look and feel class will be
438 * the UI delegate for this label.
440 * @return The UIClass identifier. "LabelUI"
442 public String
getUIClassID()
448 * This method is used primarily for debugging purposes and returns a string
449 * that can be used to represent this label.
451 * @return A string to represent this label.
453 protected String
paramString()
459 * This method returns the label text.
461 * @return The label text.
463 public String
getText()
469 * This method changes the "text" property. The given text will be painted
472 * @param newText The label's text.
474 public void setText(String newText
)
478 String oldText
= text
;
480 firePropertyChange("text", oldText
, newText
);
482 if (text
!= null && text
.length() <= displayedMnemonicIndex
)
483 setDisplayedMnemonicIndex(text
.length() - 1);
490 * This method returns the active icon. The active icon is painted when the
493 * @return The active icon.
495 public Icon
getIcon()
501 * This method changes the "icon" property. This icon (the active icon) will
502 * be the one displayed when the label is enabled.
504 * @param newIcon The active icon.
506 public void setIcon(Icon newIcon
)
512 firePropertyChange("icon", oldIcon
, newIcon
);
517 * This method returns the disabled icon. The disabled icon is painted when
518 * the label is disabled. If the disabled icon is null and the active icon
519 * is an ImageIcon, this method returns a grayed version of the icon. The
520 * grayed version of the icon becomes the disabledIcon.
522 * @return The disabled icon.
524 public Icon
getDisabledIcon()
526 if (disabledIcon
== null && icon
instanceof ImageIcon
)
527 disabledIcon
= new ImageIcon(GrayFilter
.createDisabledImage(((ImageIcon
) icon
)
534 * This method changes the "disabledIcon" property. This icon (the disabled
535 * icon) will be the one displayed when the label is disabled.
537 * @param newIcon The disabled icon.
539 public void setDisabledIcon(Icon newIcon
)
541 if (disabledIcon
!= newIcon
)
543 Icon oldIcon
= disabledIcon
;
544 disabledIcon
= newIcon
;
545 firePropertyChange("disabledIcon", oldIcon
, newIcon
);
550 * This method sets the keycode that will be the label's mnemonic. If the
551 * label is used as a label for another component, the label will give
552 * focus to that component when the mnemonic is activated.
554 * @param mnemonic The keycode to use for the mnemonic.
556 public void setDisplayedMnemonic(int mnemonic
)
558 if (displayedMnemonic
!= mnemonic
)
560 firePropertyChange("displayedMnemonic",
561 displayedMnemonic
, mnemonic
);
562 displayedMnemonic
= mnemonic
;
565 setDisplayedMnemonicIndex(text
.toUpperCase().indexOf(mnemonic
));
570 * This method sets the character that will be the mnemonic used. If the
571 * label is used as a label for another component, the label will give
572 * focus to that component when the mnemonic is activated.
574 * @param mnemonic The character to use for the mnemonic.
576 public void setDisplayedMnemonic(char mnemonic
)
578 setDisplayedMnemonic((int) Character
.toUpperCase(mnemonic
));
582 * This method returns the keycode that is used for the label's mnemonic.
584 * @return The keycode that is used for the label's mnemonic.
586 public int getDisplayedMnemonic()
588 return (int) displayedMnemonic
;
592 * This method sets which character in the text will be the underlined
593 * character. If the given index is -1, then this indicates that there is
594 * no mnemonic. If the index is less than -1 or if the index is equal to
595 * the length, this method will throw an IllegalArgumentException.
597 * @param newIndex The index of the character to underline.
599 * @throws IllegalArgumentException If index less than -1 or index equals
602 public void setDisplayedMnemonicIndex(int newIndex
)
603 throws IllegalArgumentException
605 if (newIndex
< -1 || (text
!= null && newIndex
>= text
.length()))
606 throw new IllegalArgumentException();
610 || text
.charAt(newIndex
) != displayedMnemonic
)
613 if (newIndex
!= displayedMnemonicIndex
)
615 int oldIndex
= displayedMnemonicIndex
;
616 displayedMnemonicIndex
= newIndex
;
617 firePropertyChange("displayedMnemonicIndex",
623 * This method returns which character in the text will be the underlined
626 * @return The index of the character that will be underlined.
628 public int getDisplayedMnemonicIndex()
630 return displayedMnemonicIndex
;
634 * This method ensures that the key is valid as a horizontal alignment.
635 * Valid keys are: LEFT, CENTER, RIGHT, LEADING, TRAILING
637 * @param key The key to check.
638 * @param message The message of the exception to be thrown if the key is
641 * @return The key if it's valid.
643 * @throws IllegalArgumentException If the key is invalid.
645 protected int checkHorizontalKey(int key
, String message
)
647 if (key
!= LEFT
&& key
!= CENTER
&& key
!= RIGHT
&& key
!= LEADING
649 throw new IllegalArgumentException(message
);
655 * This method ensures that the key is valid as a vertical alignment. Valid
656 * keys are: TOP, CENTER, and BOTTOM.
658 * @param key The key to check.
659 * @param message The message of the exception to be thrown if the key is
662 * @return The key if it's valid.
664 * @throws IllegalArgumentException If the key is invalid.
666 protected int checkVerticalKey(int key
, String message
)
668 if (key
!= TOP
&& key
!= BOTTOM
&& key
!= CENTER
)
669 throw new IllegalArgumentException(message
);
675 * This method returns the gap between the icon and the text.
677 * @return The gap between the icon and the text.
679 public int getIconTextGap()
685 * This method changes the "iconTextGap" property. The iconTextGap
686 * determines how much space there is between the icon and the text.
688 * @param newGap The gap between the icon and the text.
690 public void setIconTextGap(int newGap
)
692 if (iconTextGap
!= newGap
)
694 firePropertyChange("iconTextGap", iconTextGap
, newGap
);
695 iconTextGap
= newGap
;
700 * This method returns the vertical alignment of the label.
702 * @return The vertical alignment of the label.
704 public int getVerticalAlignment()
706 return verticalAlignment
;
710 * This method changes the "verticalAlignment" property of the label. The
711 * vertical alignment determines how where the label will be placed
712 * vertically. If the alignment is not valid, it will default to the
715 * @param alignment The vertical alignment of the label.
717 public void setVerticalAlignment(int alignment
)
719 if (alignment
== verticalAlignment
)
722 int oldAlignment
= verticalAlignment
;
723 verticalAlignment
= checkVerticalKey(alignment
, "verticalAlignment");
724 firePropertyChange("verticalAlignment", oldAlignment
, verticalAlignment
);
728 * This method returns the horziontal alignment of the label.
730 * @return The horizontal alignment of the label.
732 public int getHorizontalAlignment()
734 return horizontalAlignment
;
738 * This method changes the "horizontalAlignment" property. The horizontal
739 * alignment determines where the label will be placed horizontally.
741 * @param alignment The horizontal alignment of the label.
743 public void setHorizontalAlignment(int alignment
)
745 if (horizontalAlignment
== alignment
)
748 int oldAlignment
= horizontalAlignment
;
749 horizontalAlignment
= checkHorizontalKey(alignment
, "horizontalAlignment");
750 firePropertyChange("horizontalAlignment", oldAlignment
,
751 horizontalAlignment
);
755 * This method returns the vertical text position of the label.
757 * @return The vertical text position of the label.
759 public int getVerticalTextPosition()
761 return verticalTextPosition
;
765 * This method changes the "verticalTextPosition" property of the label. The
766 * vertical text position determines where the text will be placed
767 * vertically relative to the icon.
769 * @param textPosition The vertical text position.
771 public void setVerticalTextPosition(int textPosition
)
773 if (textPosition
!= verticalTextPosition
)
775 int oldPos
= verticalTextPosition
;
776 verticalTextPosition
= checkVerticalKey(textPosition
,
777 "verticalTextPosition");
778 firePropertyChange("verticalTextPosition", oldPos
,
779 verticalTextPosition
);
784 * This method returns the horizontal text position of the label.
786 * @return The horizontal text position.
788 public int getHorizontalTextPosition()
790 return horizontalTextPosition
;
794 * This method changes the "horizontalTextPosition" property of the label.
795 * The horizontal text position determines where the text will be placed
796 * horizontally relative to the icon.
798 * @param textPosition The horizontal text position.
800 public void setHorizontalTextPosition(int textPosition
)
802 if (textPosition
!= horizontalTextPosition
)
804 int oldPos
= horizontalTextPosition
;
805 horizontalTextPosition
= checkHorizontalKey(textPosition
,
806 "horizontalTextPosition");
807 firePropertyChange("horizontalTextPosition", oldPos
,
808 horizontalTextPosition
);
813 * This method simply returns false if the current icon image (current icon
814 * will depend on whether the label is enabled) is not equal to the passed
817 * @param img The image to check.
818 * @param infoflags The bitwise inclusive OR of ABORT, ALLBITS, ERROR,
819 * FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, and WIDTH
820 * @param x The x position
821 * @param y The y position
823 * @param h The height
825 * @return Whether the current icon image is equal to the image given.
827 public boolean imageUpdate(Image img
, int infoflags
, int x
, int y
, int w
,
830 Icon currIcon
= isEnabled() ? icon
: disabledIcon
;
832 // XXX: Is this the correct way to check for image equality?
833 if (currIcon
!= null && currIcon
instanceof ImageIcon
)
834 return (((ImageIcon
) currIcon
).getImage() == img
);
840 * This method returns the component that the label gives focus to when the
841 * mnemonic is activated.
843 * @return The component that gets focus when the label's mnemonic is
846 public Component
getLabelFor()
852 * This method changes the "labelFor" property. The component that the label
853 * is acting as a label for will request focus when the label's mnemonic
856 * @param c The component that gets focus when the label's mnemonic is
859 public void setLabelFor(Component c
)
863 Component oldLabelFor
= labelFor
;
865 firePropertyChange("labelFor", oldLabelFor
, labelFor
);
870 * This method overrides setFont so that we can call for a repaint after the
873 * @param f The font for this label.
875 public void setFont(Font f
)
884 * @return The accessible context.
886 public AccessibleContext
getAccessibleContext()
888 if (accessibleContext
== null)
889 accessibleContext
= new AccessibleJLabel();
890 return accessibleContext
;