48 * {@link #CheckBoxTreeTableCell(Callback, StringConverter)} constructor. 49 * 50 * <p>To construct an instance of this class, it is necessary to provide a 51 * {@link Callback} that, given an object of type T, will return an 52 * {@code ObservableProperty<Boolean>} that represents whether the given item is 53 * selected or not. This ObservableValue will be bound bidirectionally (meaning 54 * that the CheckBox in the cell will set/unset this property based on user 55 * interactions, and the CheckBox will reflect the state of the ObservableValue, 56 * if it changes externally). 57 * 58 * <p>Note that the CheckBoxTreeTableCell renders the CheckBox 'live', meaning that 59 * the CheckBox is always interactive and can be directly toggled by the user. 60 * This means that it is not necessary that the cell enter its 61 * {@link #editingProperty() editing state} (usually by the user double-clicking 62 * on the cell). A side-effect of this is that the usual editing callbacks 63 * (such as {@link javafx.scene.control.TreeTableColumn#onEditCommitProperty() on edit commit}) 64 * will <strong>not</strong> be called. If you want to be notified of changes, 65 * it is recommended to directly observe the boolean properties that are 66 * manipulated by the CheckBox.</p> 67 * 68 * @param <T> The type of the elements contained within the TreeTableColumn. 69 * @since JavaFX 8.0 70 */ 71 public class CheckBoxTreeTableCell<S,T> extends TreeTableCell<S,T> { 72 73 /*************************************************************************** 74 * * 75 * Static cell factories * 76 * * 77 **************************************************************************/ 78 79 /** 80 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 81 * This method requires that the TreeTableColumn be of type {@link Boolean}. 82 * 83 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 84 * CheckBox centered in the column. 85 * 86 * <p>The {@code ObservableValue<Boolean>} contained within each cell in the 87 * column will be bound bidirectionally. This means that the CheckBox in 88 * the cell will set/unset this property based on user interactions, and the 89 * CheckBox will reflect the state of the {@code ObservableValue<Boolean>}, 90 * if it changes externally). 91 * 92 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 93 * able to work on the type of element contained within the TreeTableColumn. 94 */ 95 public static <S> Callback<TreeTableColumn<S,Boolean>, TreeTableCell<S,Boolean>> forTreeTableColumn( 96 final TreeTableColumn<S, Boolean> column) { 97 return forTreeTableColumn(null, null); 98 } 99 100 /** 101 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 102 * This method requires that the TreeTableColumn be of type 103 * {@code ObservableValue<Boolean>}. 104 * 105 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 106 * CheckBox centered in the column. 107 * 108 * @param <T> The type of the elements contained within the {@link TreeTableColumn} 109 * instance. 110 * @param getSelectedProperty A Callback that, given an object of 111 * type {@code TreeTableColumn<S,T>}, will return an 112 * {@code ObservableValue<Boolean>} 113 * that represents whether the given item is selected or not. This 114 * {@code ObservableValue<Boolean>} will be bound bidirectionally 115 * (meaning that the CheckBox in the cell will set/unset this property 116 * based on user interactions, and the CheckBox will reflect the state of 117 * the {@code ObservableValue<Boolean>}, if it changes externally). 118 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 119 * able to work on the type of element contained within the TreeTableColumn. 120 */ 121 public static <S,T> Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> forTreeTableColumn( 122 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty) { 123 return forTreeTableColumn(getSelectedProperty, null); 124 } 125 126 /** 127 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 128 * This method requires that the TreeTableColumn be of type 129 * {@code ObservableValue<Boolean>}. 130 * 131 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 132 * CheckBox centered in the column. 133 * 134 * @param <T> The type of the elements contained within the {@link TreeTableColumn} 135 * instance. 136 * @param getSelectedProperty A Callback that, given an object of 137 * type {@code TreeTableColumn<S,T>}, will return an 138 * {@code ObservableValue<Boolean>} 139 * that represents whether the given item is selected or not. This 140 * {@code ObservableValue<Boolean>} will be bound bidirectionally 141 * (meaning that the CheckBox in the cell will set/unset this property 142 * based on user interactions, and the CheckBox will reflect the state of 143 * the {@code ObservableValue<Boolean>}, if it changes externally). 144 * @param showLabel In some cases, it may be desirable to show a label in 145 * the TreeTableCell beside the {@link CheckBox}. By default a label is not 146 * shown, but by setting this to true the item in the cell will also 147 * have toString() called on it. If this is not the desired behavior, 148 * consider using 149 * {@link #forTreeTableColumn(javafx.util.Callback, javafx.util.StringConverter) }, 150 * which allows for you to provide a callback that specifies the label for a 151 * given row item. 152 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 153 * able to work on the type of element contained within the TreeTableColumn. 154 */ 155 public static <S,T> Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> forTreeTableColumn( 156 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty, 157 final boolean showLabel) { 158 StringConverter<T> converter = ! showLabel ? 159 null : CellUtils.<T>defaultStringConverter(); 160 return forTreeTableColumn(getSelectedProperty, converter); 161 } 162 163 /** 164 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 165 * This method requires that the TreeTableColumn be of type 166 * {@code ObservableValue<Boolean>}. 167 * 168 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 169 * CheckBox centered in the column. 170 * 171 * @param <T> The type of the elements contained within the {@link TreeTableColumn} 172 * instance. 173 * @param getSelectedProperty A Callback that, given an object of type 174 * {@code TreeTableColumn<S,T>}, will return an 175 * {@code ObservableValue<Boolean>} that represents whether the given 176 * item is selected or not. This {@code ObservableValue<Boolean>} will 177 * be bound bidirectionally (meaning that the CheckBox in the cell will 178 * set/unset this property based on user interactions, and the CheckBox 179 * will reflect the state of the {@code ObservableValue<Boolean>}, if 180 * it changes externally). 181 * @param converter A StringConverter that, give an object of type T, will return a 182 * String that can be used to represent the object visually. The default 183 * implementation in {@link #forTreeTableColumn(Callback, boolean)} (when 184 * showLabel is true) is to simply call .toString() on all non-null 185 * items (and to just return an empty string in cases where the given 186 * item is null). 187 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 188 * able to work on the type of element contained within the TreeTableColumn. 189 */ 190 public static <S,T> Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> forTreeTableColumn( 264 // prop.set(this, Pos.CENTER); 265 } 266 267 268 /*************************************************************************** 269 * * 270 * Properties * 271 * * 272 **************************************************************************/ 273 274 // --- converter 275 private ObjectProperty<StringConverter<T>> converter = 276 new SimpleObjectProperty<StringConverter<T>>(this, "converter") { 277 protected void invalidated() { 278 updateShowLabel(); 279 } 280 }; 281 282 /** 283 * The {@link StringConverter} property. 284 */ 285 public final ObjectProperty<StringConverter<T>> converterProperty() { 286 return converter; 287 } 288 289 /** 290 * Sets the {@link StringConverter} to be used in this cell. 291 */ 292 public final void setConverter(StringConverter<T> value) { 293 converterProperty().set(value); 294 } 295 296 /** 297 * Returns the {@link StringConverter} used in this cell. 298 */ 299 public final StringConverter<T> getConverter() { 300 return converterProperty().get(); 301 } 302 303 304 305 // --- selected state callback property 306 private ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> 307 selectedStateCallback = 308 new SimpleObjectProperty<Callback<Integer, ObservableValue<Boolean>>>( 309 this, "selectedStateCallback"); 310 311 /** 312 * Property representing the {@link Callback} that is bound to by the 313 * CheckBox shown on screen. 314 */ 315 public final ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> selectedStateCallbackProperty() { 316 return selectedStateCallback; 317 } 318 319 /** 320 * Sets the {@link Callback} that is bound to by the CheckBox shown on screen. 321 */ 322 public final void setSelectedStateCallback(Callback<Integer, ObservableValue<Boolean>> value) { 323 selectedStateCallbackProperty().set(value); 324 } 325 326 /** 327 * Returns the {@link Callback} that is bound to by the CheckBox shown on screen. 328 */ 329 public final Callback<Integer, ObservableValue<Boolean>> getSelectedStateCallback() { 330 return selectedStateCallbackProperty().get(); 331 } 332 333 334 335 /*************************************************************************** 336 * * 337 * Public API * 338 * * 339 **************************************************************************/ 340 341 /** {@inheritDoc} */ 342 @SuppressWarnings("unchecked") 343 @Override public void updateItem(T item, boolean empty) { 344 super.updateItem(item, empty); 345 346 if (empty) { 347 setText(null); | 48 * {@link #CheckBoxTreeTableCell(Callback, StringConverter)} constructor. 49 * 50 * <p>To construct an instance of this class, it is necessary to provide a 51 * {@link Callback} that, given an object of type T, will return an 52 * {@code ObservableProperty<Boolean>} that represents whether the given item is 53 * selected or not. This ObservableValue will be bound bidirectionally (meaning 54 * that the CheckBox in the cell will set/unset this property based on user 55 * interactions, and the CheckBox will reflect the state of the ObservableValue, 56 * if it changes externally). 57 * 58 * <p>Note that the CheckBoxTreeTableCell renders the CheckBox 'live', meaning that 59 * the CheckBox is always interactive and can be directly toggled by the user. 60 * This means that it is not necessary that the cell enter its 61 * {@link #editingProperty() editing state} (usually by the user double-clicking 62 * on the cell). A side-effect of this is that the usual editing callbacks 63 * (such as {@link javafx.scene.control.TreeTableColumn#onEditCommitProperty() on edit commit}) 64 * will <strong>not</strong> be called. If you want to be notified of changes, 65 * it is recommended to directly observe the boolean properties that are 66 * manipulated by the CheckBox.</p> 67 * 68 * @param <S> The type of the TableView generic type 69 * @param <T> The type of the elements contained within the TreeTableColumn. 70 * @since JavaFX 8.0 71 */ 72 public class CheckBoxTreeTableCell<S,T> extends TreeTableCell<S,T> { 73 74 /*************************************************************************** 75 * * 76 * Static cell factories * 77 * * 78 **************************************************************************/ 79 80 /** 81 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 82 * This method requires that the TreeTableColumn be of type {@link Boolean}. 83 * 84 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 85 * CheckBox centered in the column. 86 * 87 * <p>The {@code ObservableValue<Boolean>} contained within each cell in the 88 * column will be bound bidirectionally. This means that the CheckBox in 89 * the cell will set/unset this property based on user interactions, and the 90 * CheckBox will reflect the state of the {@code ObservableValue<Boolean>}, 91 * if it changes externally). 92 * 93 * @param <S> The type of the TableView generic type 94 * @param column the TreeTableColumn of type {@link Boolean} 95 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 96 * able to work on the type of element contained within the TreeTableColumn. 97 */ 98 public static <S> Callback<TreeTableColumn<S,Boolean>, TreeTableCell<S,Boolean>> forTreeTableColumn( 99 final TreeTableColumn<S, Boolean> column) { 100 return forTreeTableColumn(null, null); 101 } 102 103 /** 104 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 105 * This method requires that the TreeTableColumn be of type 106 * {@code ObservableValue<Boolean>}. 107 * 108 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 109 * CheckBox centered in the column. 110 * 111 * @param <S> The type of the TableView generic type 112 * @param <T> The type of the elements contained within the {@link TreeTableColumn} 113 * instance. 114 * @param getSelectedProperty A Callback that, given an object of 115 * type {@code TreeTableColumn<S,T>}, will return an 116 * {@code ObservableValue<Boolean>} 117 * that represents whether the given item is selected or not. This 118 * {@code ObservableValue<Boolean>} will be bound bidirectionally 119 * (meaning that the CheckBox in the cell will set/unset this property 120 * based on user interactions, and the CheckBox will reflect the state of 121 * the {@code ObservableValue<Boolean>}, if it changes externally). 122 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 123 * able to work on the type of element contained within the TreeTableColumn. 124 */ 125 public static <S,T> Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> forTreeTableColumn( 126 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty) { 127 return forTreeTableColumn(getSelectedProperty, null); 128 } 129 130 /** 131 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 132 * This method requires that the TreeTableColumn be of type 133 * {@code ObservableValue<Boolean>}. 134 * 135 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 136 * CheckBox centered in the column. 137 * 138 * @param <S> The type of the TableView generic type 139 * @param <T> The type of the elements contained within the {@link TreeTableColumn} 140 * instance. 141 * @param getSelectedProperty A Callback that, given an object of 142 * type {@code TreeTableColumn<S,T>}, will return an 143 * {@code ObservableValue<Boolean>} 144 * that represents whether the given item is selected or not. This 145 * {@code ObservableValue<Boolean>} will be bound bidirectionally 146 * (meaning that the CheckBox in the cell will set/unset this property 147 * based on user interactions, and the CheckBox will reflect the state of 148 * the {@code ObservableValue<Boolean>}, if it changes externally). 149 * @param showLabel In some cases, it may be desirable to show a label in 150 * the TreeTableCell beside the {@link CheckBox}. By default a label is not 151 * shown, but by setting this to true the item in the cell will also 152 * have toString() called on it. If this is not the desired behavior, 153 * consider using 154 * {@link #forTreeTableColumn(javafx.util.Callback, javafx.util.StringConverter) }, 155 * which allows for you to provide a callback that specifies the label for a 156 * given row item. 157 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 158 * able to work on the type of element contained within the TreeTableColumn. 159 */ 160 public static <S,T> Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> forTreeTableColumn( 161 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty, 162 final boolean showLabel) { 163 StringConverter<T> converter = ! showLabel ? 164 null : CellUtils.<T>defaultStringConverter(); 165 return forTreeTableColumn(getSelectedProperty, converter); 166 } 167 168 /** 169 * Creates a cell factory for use in a {@link TreeTableColumn} cell factory. 170 * This method requires that the TreeTableColumn be of type 171 * {@code ObservableValue<Boolean>}. 172 * 173 * <p>When used in a TreeTableColumn, the CheckBoxCell is rendered with a 174 * CheckBox centered in the column. 175 * 176 * @param <S> The type of the TableView generic type 177 * @param <T> The type of the elements contained within the {@link TreeTableColumn} 178 * instance. 179 * @param getSelectedProperty A Callback that, given an object of type 180 * {@code TreeTableColumn<S,T>}, will return an 181 * {@code ObservableValue<Boolean>} that represents whether the given 182 * item is selected or not. This {@code ObservableValue<Boolean>} will 183 * be bound bidirectionally (meaning that the CheckBox in the cell will 184 * set/unset this property based on user interactions, and the CheckBox 185 * will reflect the state of the {@code ObservableValue<Boolean>}, if 186 * it changes externally). 187 * @param converter A StringConverter that, give an object of type T, will return a 188 * String that can be used to represent the object visually. The default 189 * implementation in {@link #forTreeTableColumn(Callback, boolean)} (when 190 * showLabel is true) is to simply call .toString() on all non-null 191 * items (and to just return an empty string in cases where the given 192 * item is null). 193 * @return A {@link Callback} that will return a {@link TreeTableCell} that is 194 * able to work on the type of element contained within the TreeTableColumn. 195 */ 196 public static <S,T> Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> forTreeTableColumn( 270 // prop.set(this, Pos.CENTER); 271 } 272 273 274 /*************************************************************************** 275 * * 276 * Properties * 277 * * 278 **************************************************************************/ 279 280 // --- converter 281 private ObjectProperty<StringConverter<T>> converter = 282 new SimpleObjectProperty<StringConverter<T>>(this, "converter") { 283 protected void invalidated() { 284 updateShowLabel(); 285 } 286 }; 287 288 /** 289 * The {@link StringConverter} property. 290 * @return the {@link StringConverter} property 291 */ 292 public final ObjectProperty<StringConverter<T>> converterProperty() { 293 return converter; 294 } 295 296 /** 297 * Sets the {@link StringConverter} to be used in this cell. 298 * @param value the {@link StringConverter} to be used in this cell 299 */ 300 public final void setConverter(StringConverter<T> value) { 301 converterProperty().set(value); 302 } 303 304 /** 305 * Returns the {@link StringConverter} used in this cell. 306 * @return the {@link StringConverter} used in this cell 307 */ 308 public final StringConverter<T> getConverter() { 309 return converterProperty().get(); 310 } 311 312 313 314 // --- selected state callback property 315 private ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> 316 selectedStateCallback = 317 new SimpleObjectProperty<Callback<Integer, ObservableValue<Boolean>>>( 318 this, "selectedStateCallback"); 319 320 /** 321 * Property representing the {@link Callback} that is bound to by the 322 * CheckBox shown on screen. 323 * @return the property representing the {@link Callback} that is bound to 324 * by the CheckBox shown on screen 325 */ 326 public final ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> selectedStateCallbackProperty() { 327 return selectedStateCallback; 328 } 329 330 /** 331 * Sets the {@link Callback} that is bound to by the CheckBox shown on screen. 332 * @param value the {@link Callback} that is bound to by the CheckBox shown 333 * on screen 334 */ 335 public final void setSelectedStateCallback(Callback<Integer, ObservableValue<Boolean>> value) { 336 selectedStateCallbackProperty().set(value); 337 } 338 339 /** 340 * Returns the {@link Callback} that is bound to by the CheckBox shown on screen. 341 * @return the {@link Callback} that is bound to by the CheckBox shown on 342 * screen 343 */ 344 public final Callback<Integer, ObservableValue<Boolean>> getSelectedStateCallback() { 345 return selectedStateCallbackProperty().get(); 346 } 347 348 349 350 /*************************************************************************** 351 * * 352 * Public API * 353 * * 354 **************************************************************************/ 355 356 /** {@inheritDoc} */ 357 @SuppressWarnings("unchecked") 358 @Override public void updateItem(T item, boolean empty) { 359 super.updateItem(item, empty); 360 361 if (empty) { 362 setText(null); |