0N/A/*
2362N/A * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Font;
0N/Aimport java.awt.Image;
0N/Aimport java.awt.*;
0N/Aimport java.text.*;
0N/Aimport java.awt.geom.*;
243N/Aimport java.beans.Transient;
0N/A
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport javax.swing.plaf.LabelUI;
0N/Aimport javax.accessibility.*;
0N/Aimport javax.swing.text.*;
0N/Aimport javax.swing.text.html.*;
0N/Aimport javax.swing.plaf.basic.*;
0N/Aimport java.util.*;
0N/A
0N/A
0N/A/**
0N/A * A display area for a short text string or an image,
0N/A * or both.
0N/A * A label does not react to input events.
0N/A * As a result, it cannot get the keyboard focus.
0N/A * A label can, however, display a keyboard alternative
0N/A * as a convenience for a nearby component
0N/A * that has a keyboard alternative but can't display it.
0N/A * <p>
0N/A * A <code>JLabel</code> object can display
0N/A * either text, an image, or both.
0N/A * You can specify where in the label's display area
0N/A * the label's contents are aligned
0N/A * by setting the vertical and horizontal alignment.
0N/A * By default, labels are vertically centered
0N/A * in their display area.
0N/A * Text-only labels are leading edge aligned, by default;
0N/A * image-only labels are horizontally centered, by default.
0N/A * <p>
0N/A * You can also specify the position of the text
0N/A * relative to the image.
0N/A * By default, text is on the trailing edge of the image,
0N/A * with the text and image vertically aligned.
0N/A * <p>
0N/A * A label's leading and trailing edge are determined from the value of its
0N/A * {@link java.awt.ComponentOrientation} property. At present, the default
0N/A * ComponentOrientation setting maps the leading edge to left and the trailing
0N/A * edge to right.
0N/A *
0N/A * <p>
0N/A * Finally, you can use the <code>setIconTextGap</code> method
0N/A * to specify how many pixels
0N/A * should appear between the text and the image.
0N/A * The default is 4 pixels.
0N/A * <p>
0N/A * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/label.html">How to Use Labels</a>
0N/A * in <em>The Java Tutorial</em>
0N/A * for further documentation.
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A component that displays a short string and an icon.
0N/A *
0N/A * @author Hans Muller
0N/A */
0N/Apublic class JLabel extends JComponent implements SwingConstants, Accessible
0N/A{
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "LabelUI";
0N/A
0N/A private int mnemonic = '\0';
0N/A private int mnemonicIndex = -1;
0N/A
0N/A private String text = ""; // "" rather than null, for BeanBox
0N/A private Icon defaultIcon = null;
0N/A private Icon disabledIcon = null;
0N/A private boolean disabledIconSet = false;
0N/A
0N/A private int verticalAlignment = CENTER;
0N/A private int horizontalAlignment = LEADING;
0N/A private int verticalTextPosition = CENTER;
0N/A private int horizontalTextPosition = TRAILING;
0N/A private int iconTextGap = 4;
0N/A
0N/A protected Component labelFor = null;
0N/A
0N/A /**
0N/A * Client property key used to determine what label is labeling the
0N/A * component. This is generally not used by labels, but is instead
0N/A * used by components such as text areas that are being labeled by
0N/A * labels. When the labelFor property of a label is set, it will
0N/A * automatically set the LABELED_BY_PROPERTY of the component being
0N/A * labelled.
0N/A *
0N/A * @see #setLabelFor
0N/A */
0N/A static final String LABELED_BY_PROPERTY = "labeledBy";
0N/A
0N/A /**
0N/A * Creates a <code>JLabel</code> instance with the specified
0N/A * text, image, and horizontal alignment.
0N/A * The label is centered vertically in its display area.
0N/A * The text is on the trailing edge of the image.
0N/A *
0N/A * @param text The text to be displayed by the label.
0N/A * @param icon The image to be displayed by the label.
0N/A * @param horizontalAlignment One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>LEFT</code>,
0N/A * <code>CENTER</code>,
0N/A * <code>RIGHT</code>,
0N/A * <code>LEADING</code> or
0N/A * <code>TRAILING</code>.
0N/A */
0N/A public JLabel(String text, Icon icon, int horizontalAlignment) {
0N/A setText(text);
0N/A setIcon(icon);
0N/A setHorizontalAlignment(horizontalAlignment);
0N/A updateUI();
0N/A setAlignmentX(LEFT_ALIGNMENT);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JLabel</code> instance with the specified
0N/A * text and horizontal alignment.
0N/A * The label is centered vertically in its display area.
0N/A *
0N/A * @param text The text to be displayed by the label.
0N/A * @param horizontalAlignment One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>LEFT</code>,
0N/A * <code>CENTER</code>,
0N/A * <code>RIGHT</code>,
0N/A * <code>LEADING</code> or
0N/A * <code>TRAILING</code>.
0N/A */
0N/A public JLabel(String text, int horizontalAlignment) {
0N/A this(text, null, horizontalAlignment);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JLabel</code> instance with the specified text.
0N/A * The label is aligned against the leading edge of its display area,
0N/A * and centered vertically.
0N/A *
0N/A * @param text The text to be displayed by the label.
0N/A */
0N/A public JLabel(String text) {
0N/A this(text, null, LEADING);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JLabel</code> instance with the specified
0N/A * image and horizontal alignment.
0N/A * The label is centered vertically in its display area.
0N/A *
0N/A * @param image The image to be displayed by the label.
0N/A * @param horizontalAlignment One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>LEFT</code>,
0N/A * <code>CENTER</code>,
0N/A * <code>RIGHT</code>,
0N/A * <code>LEADING</code> or
0N/A * <code>TRAILING</code>.
0N/A */
0N/A public JLabel(Icon image, int horizontalAlignment) {
0N/A this(null, image, horizontalAlignment);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JLabel</code> instance with the specified image.
0N/A * The label is centered vertically and horizontally
0N/A * in its display area.
0N/A *
0N/A * @param image The image to be displayed by the label.
0N/A */
0N/A public JLabel(Icon image) {
0N/A this(null, image, CENTER);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JLabel</code> instance with
0N/A * no image and with an empty string for the title.
0N/A * The label is centered vertically
0N/A * in its display area.
0N/A * The label's contents, once set, will be displayed on the leading edge
0N/A * of the label's display area.
0N/A */
0N/A public JLabel() {
0N/A this("", null, LEADING);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the L&F object that renders this component.
0N/A *
0N/A * @return LabelUI object
0N/A */
0N/A public LabelUI getUI() {
0N/A return (LabelUI)ui;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the L&F object that renders this component.
0N/A *
0N/A * @param ui the LabelUI L&F object
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * attribute: visualUpdate true
0N/A * description: The UI object that implements the Component's LookAndFeel.
0N/A */
0N/A public void setUI(LabelUI ui) {
0N/A super.setUI(ui);
0N/A // disabled icon is generated by LF so it should be unset here
0N/A if (!disabledIconSet && disabledIcon != null) {
0N/A setDisabledIcon(null);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Resets the UI property to a value from the current look and feel.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((LabelUI)UIManager.getUI(this));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string that specifies the name of the l&f class
0N/A * that renders this component.
0N/A *
0N/A * @return String "LabelUI"
0N/A *
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the text string that the label displays.
0N/A *
0N/A * @return a String
0N/A * @see #setText
0N/A */
0N/A public String getText() {
0N/A return text;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Defines the single line of text this component will display. If
0N/A * the value of text is null or empty string, nothing is displayed.
0N/A * <p>
0N/A * The default value of this property is null.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @see #setVerticalTextPosition
0N/A * @see #setHorizontalTextPosition
0N/A * @see #setIcon
0N/A * @beaninfo
0N/A * preferred: true
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: Defines the single line of text this component will display.
0N/A */
0N/A public void setText(String text) {
0N/A
0N/A String oldAccessibleName = null;
0N/A if (accessibleContext != null) {
0N/A oldAccessibleName = accessibleContext.getAccessibleName();
0N/A }
0N/A
0N/A String oldValue = this.text;
0N/A this.text = text;
0N/A firePropertyChange("text", oldValue, text);
0N/A
0N/A setDisplayedMnemonicIndex(
0N/A SwingUtilities.findDisplayedMnemonicIndex(
0N/A text, getDisplayedMnemonic()));
0N/A
0N/A if ((accessibleContext != null)
0N/A && (accessibleContext.getAccessibleName() != oldAccessibleName)) {
0N/A accessibleContext.firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A oldAccessibleName,
0N/A accessibleContext.getAccessibleName());
0N/A }
0N/A if (text == null || oldValue == null || !text.equals(oldValue)) {
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the graphic image (glyph, icon) that the label displays.
0N/A *
0N/A * @return an Icon
0N/A * @see #setIcon
0N/A */
0N/A public Icon getIcon() {
0N/A return defaultIcon;
0N/A }
0N/A
0N/A /**
0N/A * Defines the icon this component will display. If
0N/A * the value of icon is null, nothing is displayed.
0N/A * <p>
0N/A * The default value of this property is null.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @see #setVerticalTextPosition
0N/A * @see #setHorizontalTextPosition
0N/A * @see #getIcon
0N/A * @beaninfo
0N/A * preferred: true
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The icon this component will display.
0N/A */
0N/A public void setIcon(Icon icon) {
0N/A Icon oldValue = defaultIcon;
0N/A defaultIcon = icon;
0N/A
0N/A /* If the default icon has really changed and we had
0N/A * generated the disabled icon for this component
0N/A * (in other words, setDisabledIcon() was never called), then
0N/A * clear the disabledIcon field.
0N/A */
0N/A if ((defaultIcon != oldValue) && !disabledIconSet) {
0N/A disabledIcon = null;
0N/A }
0N/A
0N/A firePropertyChange("icon", oldValue, defaultIcon);
0N/A
0N/A if ((accessibleContext != null) && (oldValue != defaultIcon)) {
0N/A accessibleContext.firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A oldValue, defaultIcon);
0N/A }
0N/A
0N/A /* If the default icon has changed and the new one is
0N/A * a different size, then revalidate. Repaint if the
0N/A * default icon has changed.
0N/A */
0N/A if (defaultIcon != oldValue) {
0N/A if ((defaultIcon == null) ||
0N/A (oldValue == null) ||
0N/A (defaultIcon.getIconWidth() != oldValue.getIconWidth()) ||
0N/A (defaultIcon.getIconHeight() != oldValue.getIconHeight())) {
0N/A revalidate();
0N/A }
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the icon used by the label when it's disabled.
0N/A * If no disabled icon has been set this will forward the call to
0N/A * the look and feel to construct an appropriate disabled Icon.
0N/A * <p>
0N/A * Some look and feels might not render the disabled Icon, in which
0N/A * case they will ignore this.
0N/A *
0N/A * @return the <code>disabledIcon</code> property
0N/A * @see #setDisabledIcon
0N/A * @see javax.swing.LookAndFeel#getDisabledIcon
0N/A * @see ImageIcon
0N/A */
243N/A @Transient
0N/A public Icon getDisabledIcon() {
0N/A if (!disabledIconSet && disabledIcon == null && defaultIcon != null) {
0N/A disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, defaultIcon);
0N/A if (disabledIcon != null) {
0N/A firePropertyChange("disabledIcon", null, disabledIcon);
0N/A }
0N/A }
0N/A return disabledIcon;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Set the icon to be displayed if this JLabel is "disabled"
0N/A * (JLabel.setEnabled(false)).
0N/A * <p>
0N/A * The default value of this property is null.
0N/A *
0N/A * @param disabledIcon the Icon to display when the component is disabled
0N/A * @see #getDisabledIcon
0N/A * @see #setEnabled
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The icon to display if the label is disabled.
0N/A */
0N/A public void setDisabledIcon(Icon disabledIcon) {
0N/A Icon oldValue = this.disabledIcon;
0N/A this.disabledIcon = disabledIcon;
0N/A disabledIconSet = (disabledIcon != null);
0N/A firePropertyChange("disabledIcon", oldValue, disabledIcon);
0N/A if (disabledIcon != oldValue) {
0N/A if (disabledIcon == null || oldValue == null ||
0N/A disabledIcon.getIconWidth() != oldValue.getIconWidth() ||
0N/A disabledIcon.getIconHeight() != oldValue.getIconHeight()) {
0N/A revalidate();
0N/A }
0N/A if (!isEnabled()) {
0N/A repaint();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Specify a keycode that indicates a mnemonic key.
0N/A * This property is used when the label is part of a larger component.
0N/A * If the labelFor property of the label is not null, the label will
0N/A * call the requestFocus method of the component specified by the
0N/A * labelFor property when the mnemonic is activated.
0N/A *
0N/A * @see #getLabelFor
0N/A * @see #setLabelFor
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The mnemonic keycode.
0N/A */
0N/A public void setDisplayedMnemonic(int key) {
0N/A int oldKey = mnemonic;
0N/A mnemonic = key;
0N/A firePropertyChange("displayedMnemonic", oldKey, mnemonic);
0N/A
0N/A setDisplayedMnemonicIndex(
0N/A SwingUtilities.findDisplayedMnemonicIndex(getText(), mnemonic));
0N/A
0N/A if (key != oldKey) {
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Specifies the displayedMnemonic as a char value.
0N/A *
0N/A * @param aChar a char specifying the mnemonic to display
0N/A * @see #setDisplayedMnemonic(int)
0N/A */
0N/A public void setDisplayedMnemonic(char aChar) {
1067N/A int vk = java.awt.event.KeyEvent.getExtendedKeyCodeForChar(aChar);
1067N/A if (vk != java.awt.event.KeyEvent.VK_UNDEFINED) {
1067N/A setDisplayedMnemonic(vk);
1067N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return the keycode that indicates a mnemonic key.
0N/A * This property is used when the label is part of a larger component.
0N/A * If the labelFor property of the label is not null, the label will
0N/A * call the requestFocus method of the component specified by the
0N/A * labelFor property when the mnemonic is activated.
0N/A *
0N/A * @return int value for the mnemonic key
0N/A *
0N/A * @see #getLabelFor
0N/A * @see #setLabelFor
0N/A */
0N/A public int getDisplayedMnemonic() {
0N/A return mnemonic;
0N/A }
0N/A
0N/A /**
0N/A * Provides a hint to the look and feel as to which character in the
0N/A * text should be decorated to represent the mnemonic. Not all look and
0N/A * feels may support this. A value of -1 indicates either there is no
0N/A * mnemonic, the mnemonic character is not contained in the string, or
0N/A * the developer does not wish the mnemonic to be displayed.
0N/A * <p>
0N/A * The value of this is updated as the properties relating to the
0N/A * mnemonic change (such as the mnemonic itself, the text...).
0N/A * You should only ever have to call this if
0N/A * you do not wish the default character to be underlined. For example, if
0N/A * the text was 'Save As', with a mnemonic of 'a', and you wanted the 'A'
0N/A * to be decorated, as 'Save <u>A</u>s', you would have to invoke
0N/A * <code>setDisplayedMnemonicIndex(5)</code> after invoking
0N/A * <code>setDisplayedMnemonic(KeyEvent.VK_A)</code>.
0N/A *
0N/A * @since 1.4
0N/A * @param index Index into the String to underline
0N/A * @exception IllegalArgumentException will be thrown if <code>index</code
0N/A * is >= length of the text, or < -1
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: the index into the String to draw the keyboard character
0N/A * mnemonic at
0N/A */
0N/A public void setDisplayedMnemonicIndex(int index)
0N/A throws IllegalArgumentException {
0N/A int oldValue = mnemonicIndex;
0N/A if (index == -1) {
0N/A mnemonicIndex = -1;
0N/A } else {
0N/A String text = getText();
0N/A int textLength = (text == null) ? 0 : text.length();
0N/A if (index < -1 || index >= textLength) { // index out of range
0N/A throw new IllegalArgumentException("index == " + index);
0N/A }
0N/A }
0N/A mnemonicIndex = index;
0N/A firePropertyChange("displayedMnemonicIndex", oldValue, index);
0N/A if (index != oldValue) {
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the character, as an index, that the look and feel should
0N/A * provide decoration for as representing the mnemonic character.
0N/A *
0N/A * @since 1.4
0N/A * @return index representing mnemonic character
0N/A * @see #setDisplayedMnemonicIndex
0N/A */
0N/A public int getDisplayedMnemonicIndex() {
0N/A return mnemonicIndex;
0N/A }
0N/A
0N/A /**
0N/A * Verify that key is a legal value for the horizontalAlignment properties.
0N/A *
0N/A * @param key the property value to check
0N/A * @param message the IllegalArgumentException detail message
0N/A * @exception IllegalArgumentException if key isn't LEFT, CENTER, RIGHT,
0N/A * LEADING or TRAILING.
0N/A * @see #setHorizontalTextPosition
0N/A * @see #setHorizontalAlignment
0N/A */
0N/A protected int checkHorizontalKey(int key, String message) {
0N/A if ((key == LEFT) ||
0N/A (key == CENTER) ||
0N/A (key == RIGHT) ||
0N/A (key == LEADING) ||
0N/A (key == TRAILING)) {
0N/A return key;
0N/A }
0N/A else {
0N/A throw new IllegalArgumentException(message);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Verify that key is a legal value for the
0N/A * verticalAlignment or verticalTextPosition properties.
0N/A *
0N/A * @param key the property value to check
0N/A * @param message the IllegalArgumentException detail message
0N/A * @exception IllegalArgumentException if key isn't TOP, CENTER, or BOTTOM.
0N/A * @see #setVerticalAlignment
0N/A * @see #setVerticalTextPosition
0N/A */
0N/A protected int checkVerticalKey(int key, String message) {
0N/A if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
0N/A return key;
0N/A }
0N/A else {
0N/A throw new IllegalArgumentException(message);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the amount of space between the text and the icon
0N/A * displayed in this label.
0N/A *
0N/A * @return an int equal to the number of pixels between the text
0N/A * and the icon.
0N/A * @see #setIconTextGap
0N/A */
0N/A public int getIconTextGap() {
0N/A return iconTextGap;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * If both the icon and text properties are set, this property
0N/A * defines the space between them.
0N/A * <p>
0N/A * The default value of this property is 4 pixels.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @see #getIconTextGap
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: If both the icon and text properties are set, this
0N/A * property defines the space between them.
0N/A */
0N/A public void setIconTextGap(int iconTextGap) {
0N/A int oldValue = this.iconTextGap;
0N/A this.iconTextGap = iconTextGap;
0N/A firePropertyChange("iconTextGap", oldValue, iconTextGap);
0N/A if (iconTextGap != oldValue) {
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Returns the alignment of the label's contents along the Y axis.
0N/A *
0N/A * @return The value of the verticalAlignment property, one of the
0N/A * following constants defined in <code>SwingConstants</code>:
0N/A * <code>TOP</code>,
0N/A * <code>CENTER</code>, or
0N/A * <code>BOTTOM</code>.
0N/A *
0N/A * @see SwingConstants
0N/A * @see #setVerticalAlignment
0N/A */
0N/A public int getVerticalAlignment() {
0N/A return verticalAlignment;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the alignment of the label's contents along the Y axis.
0N/A * <p>
0N/A * The default value of this property is CENTER.
0N/A *
0N/A * @param alignment One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>TOP</code>,
0N/A * <code>CENTER</code> (the default), or
0N/A * <code>BOTTOM</code>.
0N/A *
0N/A * @see SwingConstants
0N/A * @see #getVerticalAlignment
0N/A * @beaninfo
0N/A * bound: true
0N/A * enum: TOP SwingConstants.TOP
0N/A * CENTER SwingConstants.CENTER
0N/A * BOTTOM SwingConstants.BOTTOM
0N/A * attribute: visualUpdate true
0N/A * description: The alignment of the label's contents along the Y axis.
0N/A */
0N/A public void setVerticalAlignment(int alignment) {
0N/A if (alignment == verticalAlignment) return;
0N/A int oldValue = verticalAlignment;
0N/A verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
0N/A firePropertyChange("verticalAlignment", oldValue, verticalAlignment);
0N/A repaint();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the alignment of the label's contents along the X axis.
0N/A *
0N/A * @return The value of the horizontalAlignment property, one of the
0N/A * following constants defined in <code>SwingConstants</code>:
0N/A * <code>LEFT</code>,
0N/A * <code>CENTER</code>,
0N/A * <code>RIGHT</code>,
0N/A * <code>LEADING</code> or
0N/A * <code>TRAILING</code>.
0N/A *
0N/A * @see #setHorizontalAlignment
0N/A * @see SwingConstants
0N/A */
0N/A public int getHorizontalAlignment() {
0N/A return horizontalAlignment;
0N/A }
0N/A
0N/A /**
0N/A * Sets the alignment of the label's contents along the X axis.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param alignment One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>LEFT</code>,
0N/A * <code>CENTER</code> (the default for image-only labels),
0N/A * <code>RIGHT</code>,
0N/A * <code>LEADING</code> (the default for text-only labels) or
0N/A * <code>TRAILING</code>.
0N/A *
0N/A * @see SwingConstants
0N/A * @see #getHorizontalAlignment
0N/A * @beaninfo
0N/A * bound: true
0N/A * enum: LEFT SwingConstants.LEFT
0N/A * CENTER SwingConstants.CENTER
0N/A * RIGHT SwingConstants.RIGHT
0N/A * LEADING SwingConstants.LEADING
0N/A * TRAILING SwingConstants.TRAILING
0N/A * attribute: visualUpdate true
0N/A * description: The alignment of the label's content along the X axis.
0N/A */
0N/A public void setHorizontalAlignment(int alignment) {
0N/A if (alignment == horizontalAlignment) return;
0N/A int oldValue = horizontalAlignment;
0N/A horizontalAlignment = checkHorizontalKey(alignment,
0N/A "horizontalAlignment");
0N/A firePropertyChange("horizontalAlignment",
0N/A oldValue, horizontalAlignment);
0N/A repaint();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the vertical position of the label's text,
0N/A * relative to its image.
0N/A *
0N/A * @return One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>TOP</code>,
0N/A * <code>CENTER</code>, or
0N/A * <code>BOTTOM</code>.
0N/A *
0N/A * @see #setVerticalTextPosition
0N/A * @see SwingConstants
0N/A */
0N/A public int getVerticalTextPosition() {
0N/A return verticalTextPosition;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the vertical position of the label's text,
0N/A * relative to its image.
0N/A * <p>
0N/A * The default value of this property is CENTER.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param textPosition One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>TOP</code>,
0N/A * <code>CENTER</code> (the default), or
0N/A * <code>BOTTOM</code>.
0N/A *
0N/A * @see SwingConstants
0N/A * @see #getVerticalTextPosition
0N/A * @beaninfo
0N/A * bound: true
0N/A * enum: TOP SwingConstants.TOP
0N/A * CENTER SwingConstants.CENTER
0N/A * BOTTOM SwingConstants.BOTTOM
0N/A * expert: true
0N/A * attribute: visualUpdate true
0N/A * description: The vertical position of the text relative to it's image.
0N/A */
0N/A public void setVerticalTextPosition(int textPosition) {
0N/A if (textPosition == verticalTextPosition) return;
0N/A int old = verticalTextPosition;
0N/A verticalTextPosition = checkVerticalKey(textPosition,
0N/A "verticalTextPosition");
0N/A firePropertyChange("verticalTextPosition", old, verticalTextPosition);
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the horizontal position of the label's text,
0N/A * relative to its image.
0N/A *
0N/A * @return One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>LEFT</code>,
0N/A * <code>CENTER</code>,
0N/A * <code>RIGHT</code>,
0N/A * <code>LEADING</code> or
0N/A * <code>TRAILING</code>.
0N/A *
0N/A * @see SwingConstants
0N/A */
0N/A public int getHorizontalTextPosition() {
0N/A return horizontalTextPosition;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the horizontal position of the label's text,
0N/A * relative to its image.
0N/A *
0N/A * @param textPosition One of the following constants
0N/A * defined in <code>SwingConstants</code>:
0N/A * <code>LEFT</code>,
0N/A * <code>CENTER</code>,
0N/A * <code>RIGHT</code>,
0N/A * <code>LEADING</code>, or
0N/A * <code>TRAILING</code> (the default).
0N/A * @exception IllegalArgumentException
0N/A *
0N/A * @see SwingConstants
0N/A * @beaninfo
0N/A * expert: true
0N/A * bound: true
0N/A * enum: LEFT SwingConstants.LEFT
0N/A * CENTER SwingConstants.CENTER
0N/A * RIGHT SwingConstants.RIGHT
0N/A * LEADING SwingConstants.LEADING
0N/A * TRAILING SwingConstants.TRAILING
0N/A * attribute: visualUpdate true
0N/A * description: The horizontal position of the label's text,
0N/A * relative to its image.
0N/A */
0N/A public void setHorizontalTextPosition(int textPosition) {
0N/A int old = horizontalTextPosition;
0N/A this.horizontalTextPosition = checkHorizontalKey(textPosition,
0N/A "horizontalTextPosition");
0N/A firePropertyChange("horizontalTextPosition",
0N/A old, horizontalTextPosition);
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * This is overridden to return false if the current Icon's Image is
0N/A * not equal to the passed in Image <code>img</code>.
0N/A *
0N/A * @see java.awt.image.ImageObserver
0N/A * @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
0N/A */
0N/A public boolean imageUpdate(Image img, int infoflags,
0N/A int x, int y, int w, int h) {
0N/A // Don't use getDisabledIcon, will trigger creation of icon if icon
0N/A // not set.
0N/A if (!isShowing() ||
0N/A !SwingUtilities.doesIconReferenceImage(getIcon(), img) &&
0N/A !SwingUtilities.doesIconReferenceImage(disabledIcon, img)) {
0N/A
0N/A return false;
0N/A }
0N/A return super.imageUpdate(img, infoflags, x, y, w, h);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * See readObject() and writeObject() in JComponent for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this JLabel. This method
0N/A * is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this JLabel.
0N/A */
0N/A protected String paramString() {
0N/A String textString = (text != null ?
0N/A text : "");
0N/A String defaultIconString = ((defaultIcon != null)
0N/A && (defaultIcon != this) ?
0N/A defaultIcon.toString() : "");
0N/A String disabledIconString = ((disabledIcon != null)
0N/A && (disabledIcon != this) ?
0N/A disabledIcon.toString() : "");
0N/A String labelForString = (labelFor != null ?
0N/A labelFor.toString() : "");
0N/A String verticalAlignmentString;
0N/A if (verticalAlignment == TOP) {
0N/A verticalAlignmentString = "TOP";
0N/A } else if (verticalAlignment == CENTER) {
0N/A verticalAlignmentString = "CENTER";
0N/A } else if (verticalAlignment == BOTTOM) {
0N/A verticalAlignmentString = "BOTTOM";
0N/A } else verticalAlignmentString = "";
0N/A String horizontalAlignmentString;
0N/A if (horizontalAlignment == LEFT) {
0N/A horizontalAlignmentString = "LEFT";
0N/A } else if (horizontalAlignment == CENTER) {
0N/A horizontalAlignmentString = "CENTER";
0N/A } else if (horizontalAlignment == RIGHT) {
0N/A horizontalAlignmentString = "RIGHT";
0N/A } else if (horizontalAlignment == LEADING) {
0N/A horizontalAlignmentString = "LEADING";
0N/A } else if (horizontalAlignment == TRAILING) {
0N/A horizontalAlignmentString = "TRAILING";
0N/A } else horizontalAlignmentString = "";
0N/A String verticalTextPositionString;
0N/A if (verticalTextPosition == TOP) {
0N/A verticalTextPositionString = "TOP";
0N/A } else if (verticalTextPosition == CENTER) {
0N/A verticalTextPositionString = "CENTER";
0N/A } else if (verticalTextPosition == BOTTOM) {
0N/A verticalTextPositionString = "BOTTOM";
0N/A } else verticalTextPositionString = "";
0N/A String horizontalTextPositionString;
0N/A if (horizontalTextPosition == LEFT) {
0N/A horizontalTextPositionString = "LEFT";
0N/A } else if (horizontalTextPosition == CENTER) {
0N/A horizontalTextPositionString = "CENTER";
0N/A } else if (horizontalTextPosition == RIGHT) {
0N/A horizontalTextPositionString = "RIGHT";
0N/A } else if (horizontalTextPosition == LEADING) {
0N/A horizontalTextPositionString = "LEADING";
0N/A } else if (horizontalTextPosition == TRAILING) {
0N/A horizontalTextPositionString = "TRAILING";
0N/A } else horizontalTextPositionString = "";
0N/A
0N/A return super.paramString() +
0N/A ",defaultIcon=" + defaultIconString +
0N/A ",disabledIcon=" + disabledIconString +
0N/A ",horizontalAlignment=" + horizontalAlignmentString +
0N/A ",horizontalTextPosition=" + horizontalTextPositionString +
0N/A ",iconTextGap=" + iconTextGap +
0N/A ",labelFor=" + labelForString +
0N/A ",text=" + textString +
0N/A ",verticalAlignment=" + verticalAlignmentString +
0N/A ",verticalTextPosition=" + verticalTextPositionString;
0N/A }
0N/A
0N/A /**
0N/A * --- Accessibility Support ---
0N/A */
0N/A
0N/A /**
0N/A * Get the component this is labelling.
0N/A *
0N/A * @return the Component this is labelling. Can be null if this
0N/A * does not label a Component. If the displayedMnemonic
0N/A * property is set and the labelFor property is also set, the label
0N/A * will call the requestFocus method of the component specified by the
0N/A * labelFor property when the mnemonic is activated.
0N/A *
0N/A * @see #getDisplayedMnemonic
0N/A * @see #setDisplayedMnemonic
0N/A */
0N/A public Component getLabelFor() {
0N/A return labelFor;
0N/A }
0N/A
0N/A /**
0N/A * Set the component this is labelling. Can be null if this does not
0N/A * label a Component. If the displayedMnemonic property is set
0N/A * and the labelFor property is also set, the label will
0N/A * call the requestFocus method of the component specified by the
0N/A * labelFor property when the mnemonic is activated.
0N/A *
0N/A * @param c the Component this label is for, or null if the label is
0N/A * not the label for a component
0N/A *
0N/A * @see #getDisplayedMnemonic
0N/A * @see #setDisplayedMnemonic
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The component this is labelling.
0N/A */
0N/A public void setLabelFor(Component c) {
0N/A Component oldC = labelFor;
0N/A labelFor = c;
0N/A firePropertyChange("labelFor", oldC, c);
0N/A
0N/A if (oldC instanceof JComponent) {
0N/A ((JComponent)oldC).putClientProperty(LABELED_BY_PROPERTY, null);
0N/A }
0N/A if (c instanceof JComponent) {
0N/A ((JComponent)c).putClientProperty(LABELED_BY_PROPERTY, this);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleContext of this object
0N/A *
0N/A * @return the AccessibleContext of this object
0N/A * @beaninfo
0N/A * expert: true
0N/A * description: The AccessibleContext associated with this Label.
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJLabel();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * The class used to obtain the accessible role for this object.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJLabel extends AccessibleJComponent
0N/A implements AccessibleText, AccessibleExtendedComponent {
0N/A
0N/A /**
0N/A * Get the accessible name of this object.
0N/A *
0N/A * @return the localized name of the object -- can be null if this
0N/A * object does not have a name
0N/A * @see AccessibleContext#setAccessibleName
0N/A */
0N/A public String getAccessibleName() {
0N/A String name = accessibleName;
0N/A
0N/A if (name == null) {
0N/A name = (String)getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY);
0N/A }
0N/A if (name == null) {
0N/A name = JLabel.this.getText();
0N/A }
0N/A if (name == null) {
0N/A name = super.getAccessibleName();
0N/A }
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.LABEL;
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleIcons associated with this object if one
0N/A * or more exist. Otherwise return null.
0N/A * @since 1.3
0N/A */
0N/A public AccessibleIcon [] getAccessibleIcon() {
0N/A Icon icon = getIcon();
0N/A if (icon instanceof Accessible) {
0N/A AccessibleContext ac =
0N/A ((Accessible)icon).getAccessibleContext();
0N/A if (ac != null && ac instanceof AccessibleIcon) {
0N/A return new AccessibleIcon[] { (AccessibleIcon)ac };
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleRelationSet associated with this object if one
0N/A * exists. Otherwise return null.
0N/A * @see AccessibleRelation
0N/A * @since 1.3
0N/A */
0N/A public AccessibleRelationSet getAccessibleRelationSet() {
0N/A // Check where the AccessibleContext's relation
0N/A // set already contains a LABEL_FOR relation.
0N/A AccessibleRelationSet relationSet
0N/A = super.getAccessibleRelationSet();
0N/A
0N/A if (!relationSet.contains(AccessibleRelation.LABEL_FOR)) {
0N/A Component c = JLabel.this.getLabelFor();
0N/A if (c != null) {
0N/A AccessibleRelation relation
0N/A = new AccessibleRelation(AccessibleRelation.LABEL_FOR);
0N/A relation.setTarget(c);
0N/A relationSet.add(relation);
0N/A }
0N/A }
0N/A return relationSet;
0N/A }
0N/A
0N/A
0N/A /* AccessibleText ---------- */
0N/A
0N/A public AccessibleText getAccessibleText() {
0N/A View view = (View)JLabel.this.getClientProperty("html");
0N/A if (view != null) {
0N/A return this;
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Given a point in local coordinates, return the zero-based index
0N/A * of the character under that Point. If the point is invalid,
0N/A * this method returns -1.
0N/A *
0N/A * @param p the Point in local coordinates
0N/A * @return the zero-based index of the character under Point p; if
0N/A * Point is invalid returns -1.
0N/A * @since 1.3
0N/A */
0N/A public int getIndexAtPoint(Point p) {
0N/A View view = (View) JLabel.this.getClientProperty("html");
0N/A if (view != null) {
0N/A Rectangle r = getTextRectangle();
0N/A if (r == null) {
0N/A return -1;
0N/A }
0N/A Rectangle2D.Float shape =
0N/A new Rectangle2D.Float(r.x, r.y, r.width, r.height);
0N/A Position.Bias bias[] = new Position.Bias[1];
0N/A return view.viewToModel(p.x, p.y, shape, bias);
0N/A } else {
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Determine the bounding box of the character at the given
0N/A * index into the string. The bounds are returned in local
0N/A * coordinates. If the index is invalid an empty rectangle is
0N/A * returned.
0N/A *
0N/A * @param i the index into the String
0N/A * @return the screen coordinates of the character's the bounding box,
0N/A * if index is invalid returns an empty rectangle.
0N/A * @since 1.3
0N/A */
0N/A public Rectangle getCharacterBounds(int i) {
0N/A View view = (View) JLabel.this.getClientProperty("html");
0N/A if (view != null) {
0N/A Rectangle r = getTextRectangle();
0N/A if (r == null) {
0N/A return null;
0N/A }
0N/A Rectangle2D.Float shape =
0N/A new Rectangle2D.Float(r.x, r.y, r.width, r.height);
0N/A try {
0N/A Shape charShape =
0N/A view.modelToView(i, shape, Position.Bias.Forward);
0N/A return charShape.getBounds();
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the number of characters (valid indicies)
0N/A *
0N/A * @return the number of characters
0N/A * @since 1.3
0N/A */
0N/A public int getCharCount() {
0N/A View view = (View) JLabel.this.getClientProperty("html");
0N/A if (view != null) {
0N/A Document d = view.getDocument();
0N/A if (d instanceof StyledDocument) {
0N/A StyledDocument doc = (StyledDocument)d;
0N/A return doc.getLength();
0N/A }
0N/A }
0N/A return accessibleContext.getAccessibleName().length();
0N/A }
0N/A
0N/A /**
0N/A * Return the zero-based offset of the caret.
0N/A *
0N/A * Note: That to the right of the caret will have the same index
0N/A * value as the offset (the caret is between two characters).
0N/A * @return the zero-based offset of the caret.
0N/A * @since 1.3
0N/A */
0N/A public int getCaretPosition() {
0N/A // There is no caret.
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the String at a given index.
0N/A *
0N/A * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
0N/A * or AccessibleText.SENTENCE to retrieve
0N/A * @param index an index within the text >= 0
0N/A * @return the letter, word, or sentence,
0N/A * null for an invalid index or part
0N/A * @since 1.3
0N/A */
0N/A public String getAtIndex(int part, int index) {
0N/A if (index < 0 || index >= getCharCount()) {
0N/A return null;
0N/A }
0N/A switch (part) {
0N/A case AccessibleText.CHARACTER:
0N/A try {
0N/A return getText(index, 1);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A case AccessibleText.WORD:
0N/A try {
0N/A String s = getText(0, getCharCount());
0N/A BreakIterator words = BreakIterator.getWordInstance(getLocale());
0N/A words.setText(s);
0N/A int end = words.following(index);
0N/A return s.substring(words.previous(), end);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A case AccessibleText.SENTENCE:
0N/A try {
0N/A String s = getText(0, getCharCount());
0N/A BreakIterator sentence =
0N/A BreakIterator.getSentenceInstance(getLocale());
0N/A sentence.setText(s);
0N/A int end = sentence.following(index);
0N/A return s.substring(sentence.previous(), end);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the String after a given index.
0N/A *
0N/A * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
0N/A * or AccessibleText.SENTENCE to retrieve
0N/A * @param index an index within the text >= 0
0N/A * @return the letter, word, or sentence, null for an invalid
0N/A * index or part
0N/A * @since 1.3
0N/A */
0N/A public String getAfterIndex(int part, int index) {
0N/A if (index < 0 || index >= getCharCount()) {
0N/A return null;
0N/A }
0N/A switch (part) {
0N/A case AccessibleText.CHARACTER:
0N/A if (index+1 >= getCharCount()) {
0N/A return null;
0N/A }
0N/A try {
0N/A return getText(index+1, 1);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A case AccessibleText.WORD:
0N/A try {
0N/A String s = getText(0, getCharCount());
0N/A BreakIterator words = BreakIterator.getWordInstance(getLocale());
0N/A words.setText(s);
0N/A int start = words.following(index);
0N/A if (start == BreakIterator.DONE || start >= s.length()) {
0N/A return null;
0N/A }
0N/A int end = words.following(start);
0N/A if (end == BreakIterator.DONE || end >= s.length()) {
0N/A return null;
0N/A }
0N/A return s.substring(start, end);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A case AccessibleText.SENTENCE:
0N/A try {
0N/A String s = getText(0, getCharCount());
0N/A BreakIterator sentence =
0N/A BreakIterator.getSentenceInstance(getLocale());
0N/A sentence.setText(s);
0N/A int start = sentence.following(index);
0N/A if (start == BreakIterator.DONE || start > s.length()) {
0N/A return null;
0N/A }
0N/A int end = sentence.following(start);
0N/A if (end == BreakIterator.DONE || end > s.length()) {
0N/A return null;
0N/A }
0N/A return s.substring(start, end);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the String before a given index.
0N/A *
0N/A * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
0N/A * or AccessibleText.SENTENCE to retrieve
0N/A * @param index an index within the text >= 0
0N/A * @return the letter, word, or sentence, null for an invalid index
0N/A * or part
0N/A * @since 1.3
0N/A */
0N/A public String getBeforeIndex(int part, int index) {
0N/A if (index < 0 || index > getCharCount()-1) {
0N/A return null;
0N/A }
0N/A switch (part) {
0N/A case AccessibleText.CHARACTER:
0N/A if (index == 0) {
0N/A return null;
0N/A }
0N/A try {
0N/A return getText(index-1, 1);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A case AccessibleText.WORD:
0N/A try {
0N/A String s = getText(0, getCharCount());
0N/A BreakIterator words = BreakIterator.getWordInstance(getLocale());
0N/A words.setText(s);
0N/A int end = words.following(index);
0N/A end = words.previous();
0N/A int start = words.previous();
0N/A if (start == BreakIterator.DONE) {
0N/A return null;
0N/A }
0N/A return s.substring(start, end);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A case AccessibleText.SENTENCE:
0N/A try {
0N/A String s = getText(0, getCharCount());
0N/A BreakIterator sentence =
0N/A BreakIterator.getSentenceInstance(getLocale());
0N/A sentence.setText(s);
0N/A int end = sentence.following(index);
0N/A end = sentence.previous();
0N/A int start = sentence.previous();
0N/A if (start == BreakIterator.DONE) {
0N/A return null;
0N/A }
0N/A return s.substring(start, end);
0N/A } catch (BadLocationException e) {
0N/A return null;
0N/A }
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the AttributeSet for a given character at a given index
0N/A *
0N/A * @param i the zero-based index into the text
0N/A * @return the AttributeSet of the character
0N/A * @since 1.3
0N/A */
0N/A public AttributeSet getCharacterAttribute(int i) {
0N/A View view = (View) JLabel.this.getClientProperty("html");
0N/A if (view != null) {
0N/A Document d = view.getDocument();
0N/A if (d instanceof StyledDocument) {
0N/A StyledDocument doc = (StyledDocument)d;
0N/A Element elem = doc.getCharacterElement(i);
0N/A if (elem != null) {
0N/A return elem.getAttributes();
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the start offset within the selected text.
0N/A * If there is no selection, but there is
0N/A * a caret, the start and end offsets will be the same.
0N/A *
0N/A * @return the index into the text of the start of the selection
0N/A * @since 1.3
0N/A */
0N/A public int getSelectionStart() {
0N/A // Text cannot be selected.
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the end offset within the selected text.
0N/A * If there is no selection, but there is
0N/A * a caret, the start and end offsets will be the same.
0N/A *
0N/A * @return the index into teh text of the end of the selection
0N/A * @since 1.3
0N/A */
0N/A public int getSelectionEnd() {
0N/A // Text cannot be selected.
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the portion of the text that is selected.
0N/A *
0N/A * @return the String portion of the text that is selected
0N/A * @since 1.3
0N/A */
0N/A public String getSelectedText() {
0N/A // Text cannot be selected.
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Returns the text substring starting at the specified
0N/A * offset with the specified length.
0N/A */
0N/A private String getText(int offset, int length)
0N/A throws BadLocationException {
0N/A
0N/A View view = (View) JLabel.this.getClientProperty("html");
0N/A if (view != null) {
0N/A Document d = view.getDocument();
0N/A if (d instanceof StyledDocument) {
0N/A StyledDocument doc = (StyledDocument)d;
0N/A return doc.getText(offset, length);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Returns the bounding rectangle for the component text.
0N/A */
0N/A private Rectangle getTextRectangle() {
0N/A
0N/A String text = JLabel.this.getText();
0N/A Icon icon = (JLabel.this.isEnabled()) ? JLabel.this.getIcon() : JLabel.this.getDisabledIcon();
0N/A
0N/A if ((icon == null) && (text == null)) {
0N/A return null;
0N/A }
0N/A
0N/A Rectangle paintIconR = new Rectangle();
0N/A Rectangle paintTextR = new Rectangle();
0N/A Rectangle paintViewR = new Rectangle();
0N/A Insets paintViewInsets = new Insets(0, 0, 0, 0);
0N/A
0N/A paintViewInsets = JLabel.this.getInsets(paintViewInsets);
0N/A paintViewR.x = paintViewInsets.left;
0N/A paintViewR.y = paintViewInsets.top;
0N/A paintViewR.width = JLabel.this.getWidth() - (paintViewInsets.left + paintViewInsets.right);
0N/A paintViewR.height = JLabel.this.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
0N/A
0N/A String clippedText = SwingUtilities.layoutCompoundLabel(
0N/A (JComponent)JLabel.this,
0N/A getFontMetrics(getFont()),
0N/A text,
0N/A icon,
0N/A JLabel.this.getVerticalAlignment(),
0N/A JLabel.this.getHorizontalAlignment(),
0N/A JLabel.this.getVerticalTextPosition(),
0N/A JLabel.this.getHorizontalTextPosition(),
0N/A paintViewR,
0N/A paintIconR,
0N/A paintTextR,
0N/A JLabel.this.getIconTextGap());
0N/A
0N/A return paintTextR;
0N/A }
0N/A
0N/A // ----- AccessibleExtendedComponent
0N/A
0N/A /**
0N/A * Returns the AccessibleExtendedComponent
0N/A *
0N/A * @return the AccessibleExtendedComponent
0N/A */
0N/A AccessibleExtendedComponent getAccessibleExtendedComponent() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns the tool tip text
0N/A *
0N/A * @return the tool tip text, if supported, of the object;
0N/A * otherwise, null
0N/A * @since 1.4
0N/A */
0N/A public String getToolTipText() {
0N/A return JLabel.this.getToolTipText();
0N/A }
0N/A
0N/A /**
0N/A * Returns the titled border text
0N/A *
0N/A * @return the titled border text, if supported, of the object;
0N/A * otherwise, null
0N/A * @since 1.4
0N/A */
0N/A public String getTitledBorderText() {
0N/A return super.getTitledBorderText();
0N/A }
0N/A
0N/A /**
0N/A * Returns key bindings associated with this object
0N/A *
0N/A * @return the key bindings, if supported, of the object;
0N/A * otherwise, null
0N/A * @see AccessibleKeyBinding
0N/A * @since 1.4
0N/A */
0N/A public AccessibleKeyBinding getAccessibleKeyBinding() {
0N/A int mnemonic = JLabel.this.getDisplayedMnemonic();
0N/A if (mnemonic == 0) {
0N/A return null;
0N/A }
0N/A return new LabelKeyBinding(mnemonic);
0N/A }
0N/A
0N/A class LabelKeyBinding implements AccessibleKeyBinding {
0N/A int mnemonic;
0N/A
0N/A LabelKeyBinding(int mnemonic) {
0N/A this.mnemonic = mnemonic;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of key bindings for this object
0N/A *
0N/A * @return the zero-based number of key bindings for this object
0N/A */
0N/A public int getAccessibleKeyBindingCount() {
0N/A return 1;
0N/A }
0N/A
0N/A /**
0N/A * Returns a key binding for this object. The value returned is an
0N/A * java.lang.Object which must be cast to appropriate type depending
0N/A * on the underlying implementation of the key. For example, if the
0N/A * Object returned is a javax.swing.KeyStroke, the user of this
0N/A * method should do the following:
0N/A * <nf><code>
0N/A * Component c = <get the component that has the key bindings>
0N/A * AccessibleContext ac = c.getAccessibleContext();
0N/A * AccessibleKeyBinding akb = ac.getAccessibleKeyBinding();
0N/A * for (int i = 0; i < akb.getAccessibleKeyBindingCount(); i++) {
0N/A * Object o = akb.getAccessibleKeyBinding(i);
0N/A * if (o instanceof javax.swing.KeyStroke) {
0N/A * javax.swing.KeyStroke keyStroke = (javax.swing.KeyStroke)o;
0N/A * <do something with the key binding>
0N/A * }
0N/A * }
0N/A * </code></nf>
0N/A *
0N/A * @param i zero-based index of the key bindings
0N/A * @return a javax.lang.Object which specifies the key binding
0N/A * @exception IllegalArgumentException if the index is
0N/A * out of bounds
0N/A * @see #getAccessibleKeyBindingCount
0N/A */
0N/A public java.lang.Object getAccessibleKeyBinding(int i) {
0N/A if (i != 0) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A return KeyStroke.getKeyStroke(mnemonic, 0);
0N/A }
0N/A }
0N/A
0N/A } // AccessibleJComponent
0N/A}