Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.control;
027
028import javafx.css.CssMetaData;
029import com.sun.javafx.scene.control.skin.LabelSkin;
030import javafx.beans.property.ObjectProperty;
031import javafx.beans.property.ObjectPropertyBase;
032import javafx.beans.value.ChangeListener;
033import javafx.beans.value.ObservableValue;
034import javafx.css.StyleableProperty;
035import javafx.scene.Node;
036
037/**
038 * Label is a non-editable text control. A Label is useful for displaying
039 * text that is required to fit within a specific space, and thus may need
040 * to use an ellipsis or truncation to size the string to fit. Labels also are
041 * useful in that they can have mnemonics which, if used, will send focus to
042 * the Control listed as the target of the <code>labelFor</code> property.
043 * <p>
044 * Label sets focusTraversable to false.
045 * </p>
046 *
047 * <p>Example:
048 * <pre><code>Label label = new Label("a label");</code></pre>
049 */
050public class Label extends Labeled {
051
052    /***************************************************************************
053     *                                                                         *
054     * Constructors                                                            *
055     *                                                                         *
056     **************************************************************************/
057
058    /**
059     * Creates an empty label
060     */
061    public Label() {
062        initialize();
063    }
064
065    /**
066     * Creates Label with supplied text.
067     * @param text null text is treated as the empty string
068     */
069    public Label(String text) {
070        super(text);
071        initialize();
072    }
073
074    /**
075     * Creates a Label with the supplied text and graphic.
076     * @param text null text is treated as the empty string
077     * @param graphic a null graphic is acceptable
078     */
079    public Label(String text, Node graphic) {
080        super(text, graphic);
081        initialize();
082    }
083
084    private void initialize() {
085        getStyleClass().setAll("label");
086        // Labels are not focus traversable, unlike most other UI Controls.
087        // focusTraversable is styleable through css. Calling setFocusTraversable
088        // makes it look to css like the user set the value and css will not 
089        // override. Initializing focusTraversable by calling set on the 
090        // CssMetaData ensures that css will be able to override the value.        
091        ((StyleableProperty)focusTraversableProperty()).applyStyle(null, Boolean.FALSE);
092    }
093    
094    /***************************************************************************
095     *                                                                         *
096     * Properties                                                              *
097     *                                                                         *
098     **************************************************************************/
099
100    private ChangeListener<Boolean> mnemonicStateListener = new ChangeListener<Boolean>() {
101        @Override
102        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
103            Label.this.impl_showMnemonicsProperty().setValue(newValue);
104        }
105
106    };
107
108    /**
109     * A Label can act as a label for a different Control or
110     * Node. This is used for Mnemonics and Accelerator parsing.
111     * This allows setting of the target Node.
112     */
113    public ObjectProperty<Node> labelForProperty() {
114        if (labelFor == null) {
115            labelFor = new ObjectPropertyBase<Node>() {
116                Node oldValue = null;
117                @Override protected void invalidated() {
118                    if (oldValue != null) {
119                        oldValue.impl_showMnemonicsProperty().removeListener(mnemonicStateListener);
120                    }
121                    final Node node = get();
122                    if (node != null) {
123                        node.impl_showMnemonicsProperty().addListener(mnemonicStateListener);
124                        impl_setShowMnemonics(node.impl_isShowMnemonics());
125                    } else {
126                        impl_setShowMnemonics(false);
127                    }
128                    oldValue = node;
129                }
130
131                @Override public Object getBean() {
132                    return Label.this;
133                }
134
135                @Override public String getName() {
136                    return "labelFor";
137                }
138            };
139
140        }
141        return labelFor;
142    }
143    private ObjectProperty<Node> labelFor;
144
145    public final void setLabelFor(Node value) { labelForProperty().setValue(value); }
146    public final Node getLabelFor() { return labelFor == null ? null : labelFor.getValue(); }
147
148    /***************************************************************************
149     *                                                                         *
150     * Methods                                                                 *
151     *                                                                         *
152     **************************************************************************/
153
154    /** {@inheritDoc} */
155    @Override protected Skin<?> createDefaultSkin() {
156        return new LabelSkin(this);
157    }
158
159    /***************************************************************************
160     *                                                                         *
161     * CSS Support                                                             *
162     *                                                                         *
163     **************************************************************************/
164
165    /**
166      * Most Controls return true for focusTraversable, so Control overrides
167      * this method to return true, but Label returns false for
168      * focusTraversable's initial value; hence the override of the override. 
169      * This method is called from CSS code to get the correct initial value.
170      * @treatAsPrivate implementation detail
171      */
172    @Deprecated @Override
173    protected /*do not make final*/ Boolean impl_cssGetFocusTraversableInitialValue() {
174        return Boolean.FALSE;
175    }    
176}