1 /* 2 * Copyright (c) 2012, 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.cell; 27 28 import javafx.beans.property.ObjectProperty; 29 import javafx.beans.property.SimpleObjectProperty; 30 import javafx.scene.Node; 31 import javafx.scene.control.Label; 32 import javafx.scene.control.TextField; 33 import javafx.scene.control.TreeCell; 34 import javafx.scene.control.TreeItem; 35 import javafx.scene.control.TreeView; 36 import javafx.scene.layout.HBox; 37 import javafx.util.Callback; 38 import javafx.util.StringConverter; 39 import javafx.util.converter.DefaultStringConverter; 40 41 /** 42 * A class containing a {@link TreeCell} implementation that draws a 43 * {@link TextField} node inside the cell. 44 * 45 * <p>By default, the TextFieldTreeCell is rendered as a {@link Label} when not 46 * being edited, and as a TextField when in editing mode. The TextField will, by 47 * default, stretch to fill the entire tree cell. 48 * 49 * @param <T> The type of the elements contained within the TreeView. 50 * @since JavaFX 2.2 51 */ 52 public class TextFieldTreeCell<T> extends DefaultTreeCell<T> { 53 54 /*************************************************************************** 55 * * 56 * Static cell factories * 57 * * 58 **************************************************************************/ 59 60 /** 61 * Provides a {@link TextField} that allows editing of the cell content when 62 * the cell is double-clicked, or when 63 * {@link TreeView#edit(javafx.scene.control.TreeItem)} is called. 64 * This method will only work on {@link TreeView} instances which are of 65 * type String. 66 * 67 * @return A {@link Callback} that can be inserted into the 68 * {@link TreeView#cellFactoryProperty() cell factory property} of a 69 * TreeView, that enables textual editing of the content. 70 */ 71 public static Callback<TreeView<String>, TreeCell<String>> forTreeView() { 72 return forTreeView(new DefaultStringConverter()); 73 } 74 75 /** 76 * Provides a {@link TextField} that allows editing of the cell content when 77 * the cell is double-clicked, or when 78 * {@link TreeView#edit(javafx.scene.control.TreeItem)} is called. This 79 * method will work on any {@link TreeView} instance, 80 * regardless of its generic type. However, to enable this, a 81 * {@link StringConverter} must be provided that will convert the given String 82 * (from what the user typed in) into an instance of type T. This item will 83 * then be passed along to the {@link TreeView#onEditCommitProperty()} 84 * callback. 85 * 86 * @param <T> The type of the elements contained within the TreeView 87 * @param converter A {@link StringConverter} that can convert the given String 88 * (from what the user typed in) into an instance of type T. 89 * @return A {@link Callback} that can be inserted into the 90 * {@link TreeView#cellFactoryProperty() cell factory property} of a 91 * TreeView, that enables textual editing of the content. 92 */ 93 public static <T> Callback<TreeView<T>, TreeCell<T>> forTreeView( 94 final StringConverter<T> converter) { 95 return list -> new TextFieldTreeCell<T>(converter); 96 } 97 98 99 100 /*************************************************************************** 101 * * 102 * Fields * 103 * * 104 **************************************************************************/ 105 106 private TextField textField; 107 private HBox hbox; 108 109 110 111 /*************************************************************************** 112 * * 113 * Constructors * 114 * * 115 **************************************************************************/ 116 117 /** 118 * Creates a default TextFieldTreeCell with a null converter. Without a 119 * {@link StringConverter} specified, this cell will not be able to accept 120 * input from the TextField (as it will not know how to convert this back 121 * to the domain object). It is therefore strongly encouraged to not use 122 * this constructor unless you intend to set the converter separately. 123 */ 124 public TextFieldTreeCell() { 125 this(null); 126 } 127 128 /** 129 * Creates a TextFieldTreeCell that provides a {@link TextField} when put 130 * into editing mode that allows editing of the cell content. This method 131 * will work on any TreeView instance, regardless of its generic type. 132 * However, to enable this, a {@link StringConverter} must be provided that 133 * will convert the given String (from what the user typed in) into an 134 * instance of type T. This item will then be passed along to the 135 * {@link TreeView#onEditCommitProperty()} callback. 136 * 137 * @param converter A {@link StringConverter converter} that can convert 138 * the given String (from what the user typed in) into an instance of 139 * type T. 140 */ 141 public TextFieldTreeCell(StringConverter<T> converter) { 142 this.getStyleClass().add("text-field-tree-cell"); 143 setConverter(converter); 144 } 145 146 147 148 /*************************************************************************** 149 * * 150 * Properties * 151 * * 152 **************************************************************************/ 153 154 // --- converter 155 private ObjectProperty<StringConverter<T>> converter = 156 new SimpleObjectProperty<StringConverter<T>>(this, "converter"); 157 158 /** 159 * The {@link StringConverter} property. 160 * @return the {@link StringConverter} property 161 */ 162 public final ObjectProperty<StringConverter<T>> converterProperty() { 163 return converter; 164 } 165 166 /** 167 * Sets the {@link StringConverter} to be used in this cell. 168 * @param value the {@link StringConverter} to be used in this cell 169 */ 170 public final void setConverter(StringConverter<T> value) { 171 converterProperty().set(value); 172 } 173 174 /** 175 * Returns the {@link StringConverter} used in this cell. 176 * @return the {@link StringConverter} used in this cell 177 */ 178 public final StringConverter<T> getConverter() { 179 return converterProperty().get(); 180 } 181 182 183 184 /*************************************************************************** 185 * * 186 * Public API * 187 * * 188 **************************************************************************/ 189 190 /** {@inheritDoc} */ 191 @Override public void startEdit() { 192 if (! isEditable() || ! getTreeView().isEditable()) { 193 return; 194 } 195 super.startEdit(); 196 197 if (isEditing()) { 198 StringConverter<T> converter = getConverter(); 199 if (textField == null) { 200 textField = CellUtils.createTextField(this, converter); 201 } 202 if (hbox == null) { 203 hbox = new HBox(CellUtils.TREE_VIEW_HBOX_GRAPHIC_PADDING); 204 } 205 206 CellUtils.startEdit(this, converter, hbox, getTreeItemGraphic(), textField); 207 } 208 } 209 210 /** {@inheritDoc} */ 211 @Override public void cancelEdit() { 212 super.cancelEdit(); 213 CellUtils.cancelEdit(this, getConverter(), getTreeItemGraphic()); 214 } 215 216 /** {@inheritDoc} */ 217 @Override public void updateItem(T item, boolean empty) { 218 super.updateItem(item, empty); 219 CellUtils.updateItem(this, getConverter(), hbox, getTreeItemGraphic(), textField); 220 } 221 222 223 224 /*************************************************************************** 225 * * 226 * Private Implementation * 227 * * 228 **************************************************************************/ 229 230 private Node getTreeItemGraphic() { 231 TreeItem<T> treeItem = getTreeItem(); 232 return treeItem == null ? null : treeItem.getGraphic(); 233 } 234 }