1 /* 2 * Copyright (c) 2010, 2014, 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 Label.this.impl_showMnemonicsProperty().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 */ 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 NodeHelper.getNodeAccessor().setLabeledBy(oldValue, null); 120 oldValue.impl_showMnemonicsProperty().removeListener(mnemonicStateListener); 121 } 122 final Node node = get(); 123 if (node != null) { 124 NodeHelper.getNodeAccessor().setLabeledBy(node, Label.this); 125 node.impl_showMnemonicsProperty().addListener(mnemonicStateListener); 126 impl_setShowMnemonics(node.impl_isShowMnemonics()); 127 } else { 128 impl_setShowMnemonics(false); 129 } 130 oldValue = node; 131 } 132 133 @Override public Object getBean() { 134 return Label.this; 135 } 136 137 @Override public String getName() { 138 return "labelFor"; 139 } 140 }; 141 142 } 143 return labelFor; 144 } 145 private ObjectProperty<Node> labelFor; 146 147 public final void setLabelFor(Node value) { labelForProperty().setValue(value); } 148 public final Node getLabelFor() { return labelFor == null ? null : labelFor.getValue(); } 149 150 /*************************************************************************** 151 * * 152 * Methods * 153 * * 154 **************************************************************************/ 155 156 /** {@inheritDoc} */ 157 @Override protected Skin<?> createDefaultSkin() { 158 return new LabelSkin(this); 159 } 160 161 /*************************************************************************** 162 * * 163 * CSS Support * 164 * * 165 **************************************************************************/ 166 167 /** 168 * Most Controls return true for focusTraversable, so Control overrides 169 * this method to return true, but Label returns false for 170 * focusTraversable's initial value; hence the override of the override. 171 * This method is called from CSS code to get the correct initial value. 172 * @treatAsPrivate implementation detail 173 */ 174 @Deprecated @Override 175 protected /*do not make final*/ Boolean impl_cssGetFocusTraversableInitialValue() { 176 return Boolean.FALSE; 177 } 178 }