1 /* 2 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.control; 27 28 import javafx.scene.control.skin.LabelSkin; 29 import com.sun.javafx.scene.NodeHelper; 30 31 import javafx.beans.property.ObjectProperty; 32 import javafx.beans.property.ObjectPropertyBase; 33 import javafx.beans.value.ChangeListener; 34 import javafx.beans.value.WritableValue; 35 import javafx.css.StyleableProperty; 36 import javafx.scene.AccessibleRole; 37 import javafx.scene.Node; 38 39 /** 40 * Label is a non-editable text control. A Label is useful for displaying 41 * text that is required to fit within a specific space, and thus may need 42 * to use an ellipsis or truncation to size the string to fit. Labels also are 43 * useful in that they can have mnemonics which, if used, will send focus to 44 * the Control listed as the target of the <code>labelFor</code> property. 45 * <p> 46 * Label sets focusTraversable to false. 47 * </p> 48 * 49 * <p>Example: 50 * <pre><code>Label label = new Label("a label");</code></pre> 51 * @since JavaFX 2.0 52 */ 53 public class Label extends Labeled { 54 55 /*************************************************************************** 56 * * 57 * Constructors * 58 * * 59 **************************************************************************/ 60 61 /** 62 * Creates an empty label 63 */ 64 public Label() { 65 initialize(); 66 } 67 68 /** 69 * Creates Label with supplied text. 70 * @param text null text is treated as the empty string 71 */ 72 public Label(String text) { 73 super(text); 74 initialize(); 75 } 76 77 /** 78 * Creates a Label with the supplied text and graphic. 79 * @param text null text is treated as the empty string 80 * @param graphic a null graphic is acceptable 81 */ 82 public Label(String text, Node graphic) { 83 super(text, graphic); 84 initialize(); 85 } 86 87 private void initialize() { 88 getStyleClass().setAll("label"); 89 setAccessibleRole(AccessibleRole.TEXT); 90 // Labels are not focus traversable, unlike most other UI Controls. 91 // focusTraversable is styleable through css. Calling setFocusTraversable 92 // makes it look to css like the user set the value and css will not 93 // override. Initializing focusTraversable by calling set on the 94 // CssMetaData ensures that css will be able to override the value. 95 ((StyleableProperty<Boolean>)(WritableValue<Boolean>)focusTraversableProperty()).applyStyle(null, Boolean.FALSE); 96 } 97 98 /*************************************************************************** 99 * * 100 * Properties * 101 * * 102 **************************************************************************/ 103 104 private ChangeListener<Boolean> mnemonicStateListener = (observable, oldValue, newValue) -> { 105 NodeHelper.showMnemonicsProperty(Label.this).setValue(newValue); 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 * @return the label for this node 113 */ 114 public ObjectProperty<Node> labelForProperty() { 115 if (labelFor == null) { 116 labelFor = new ObjectPropertyBase<Node>() { 117 Node oldValue = null; 118 @Override protected void invalidated() { 119 if (oldValue != null) { 120 NodeHelper.getNodeAccessor().setLabeledBy(oldValue, null); 121 NodeHelper.showMnemonicsProperty(oldValue).removeListener(mnemonicStateListener); 122 } 123 final Node node = get(); 124 if (node != null) { 125 NodeHelper.getNodeAccessor().setLabeledBy(node, Label.this); 126 NodeHelper.showMnemonicsProperty(node).addListener(mnemonicStateListener); 127 NodeHelper.setShowMnemonics(Label.this, NodeHelper.isShowMnemonics(node)); 128 } else { 129 NodeHelper.setShowMnemonics(Label.this, false); 130 } 131 oldValue = node; 132 } 133 134 @Override public Object getBean() { 135 return Label.this; 136 } 137 138 @Override public String getName() { 139 return "labelFor"; 140 } 141 }; 142 143 } 144 return labelFor; 145 } 146 private ObjectProperty<Node> labelFor; 147 148 public final void setLabelFor(Node value) { labelForProperty().setValue(value); } 149 public final Node getLabelFor() { return labelFor == null ? null : labelFor.getValue(); } 150 151 /*************************************************************************** 152 * * 153 * Methods * 154 * * 155 **************************************************************************/ 156 157 /** {@inheritDoc} */ 158 @Override protected Skin<?> createDefaultSkin() { 159 return new LabelSkin(this); 160 } 161 162 /*************************************************************************** 163 * * 164 * CSS Support * 165 * * 166 **************************************************************************/ 167 168 /** 169 * Returns the initial focus traversable state of this control, for use 170 * by the JavaFX CSS engine to correctly set its initial value. This method 171 * is overridden as by default UI controls have focus traversable set to true, 172 * but that is not appropriate for this control. 173 * 174 * @since 9 175 */ 176 @Override protected Boolean getInitialFocusTraversable() { 177 return Boolean.FALSE; 178 } 179 180 }