72 73 /*************************************************************************** 74 * * 75 * Static cell factories * 76 * * 77 **************************************************************************/ 78 79 /** 80 * Creates a cell factory for use in a {@link TableColumn} cell factory. 81 * This method requires that the TableColumn be of type {@link Boolean}. 82 * 83 * <p>When used in a TableColumn, 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 TableCell} that is 93 * able to work on the type of element contained within the TableColumn. 94 */ 95 public static <S> Callback<TableColumn<S,Boolean>, TableCell<S,Boolean>> forTableColumn( 96 final TableColumn<S, Boolean> column) { 97 return forTableColumn(null, null); 98 } 99 100 /** 101 * Creates a cell factory for use in a {@link TableColumn} cell factory. 102 * This method requires that the TableColumn be of type 103 * {@code ObservableValue<Boolean>}. 104 * 105 * <p>When used in a TableColumn, 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 TableColumn} 109 * instance. 110 * @param getSelectedProperty A Callback that, given an object of 111 * type {@code TableColumn<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 TableCell} that is 119 * able to work on the type of element contained within the TableColumn. 120 */ 121 public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn( 122 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty) { 123 return forTableColumn(getSelectedProperty, null); 124 } 125 126 /** 127 * Creates a cell factory for use in a {@link TableColumn} cell factory. 128 * This method requires that the TableColumn be of type 129 * {@code ObservableValue<Boolean>}. 130 * 131 * <p>When used in a TableColumn, 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 TableColumn} 135 * instance. 136 * @param getSelectedProperty A Callback that, given an object of 137 * type {@code TableColumn<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 TableCell 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 #forTableColumn(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 TableCell} that is 153 * able to work on the type of element contained within the TableColumn. 154 */ 155 public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn( 156 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty, 157 final boolean showLabel) { 158 StringConverter<T> converter = ! showLabel ? 159 null : CellUtils.<T>defaultStringConverter(); 160 return forTableColumn(getSelectedProperty, converter); 161 } 162 163 /** 164 * Creates a cell factory for use in a {@link TableColumn} cell factory. 165 * This method requires that the TableColumn be of type 166 * {@code ObservableValue<Boolean>}. 167 * 168 * <p>When used in a TableColumn, 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 TableColumn} 172 * instance. 173 * @param getSelectedProperty A Callback that, given an object of type 174 * {@code TableColumn<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 #forTableColumn(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 TableCell} that is 188 * able to work on the type of element contained within the TableColumn. 189 */ 190 public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn( 266 267 } 268 269 270 /*************************************************************************** 271 * * 272 * Properties * 273 * * 274 **************************************************************************/ 275 276 // --- converter 277 private ObjectProperty<StringConverter<T>> converter = 278 new SimpleObjectProperty<StringConverter<T>>(this, "converter") { 279 protected void invalidated() { 280 updateShowLabel(); 281 } 282 }; 283 284 /** 285 * The {@link StringConverter} property. 286 */ 287 public final ObjectProperty<StringConverter<T>> converterProperty() { 288 return converter; 289 } 290 291 /** 292 * Sets the {@link StringConverter} to be used in this cell. 293 */ 294 public final void setConverter(StringConverter<T> value) { 295 converterProperty().set(value); 296 } 297 298 /** 299 * Returns the {@link StringConverter} used in this cell. 300 */ 301 public final StringConverter<T> getConverter() { 302 return converterProperty().get(); 303 } 304 305 306 307 // --- selected state callback property 308 private ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> 309 selectedStateCallback = 310 new SimpleObjectProperty<Callback<Integer, ObservableValue<Boolean>>>( 311 this, "selectedStateCallback"); 312 313 /** 314 * Property representing the {@link Callback} that is bound to by the 315 * CheckBox shown on screen. 316 */ 317 public final ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> selectedStateCallbackProperty() { 318 return selectedStateCallback; 319 } 320 321 /** 322 * Sets the {@link Callback} that is bound to by the CheckBox shown on screen. 323 */ 324 public final void setSelectedStateCallback(Callback<Integer, ObservableValue<Boolean>> value) { 325 selectedStateCallbackProperty().set(value); 326 } 327 328 /** 329 * Returns the {@link Callback} that is bound to by the CheckBox shown on screen. 330 */ 331 public final Callback<Integer, ObservableValue<Boolean>> getSelectedStateCallback() { 332 return selectedStateCallbackProperty().get(); 333 } 334 335 336 337 /*************************************************************************** 338 * * 339 * Public API * 340 * * 341 **************************************************************************/ 342 343 /** {@inheritDoc} */ 344 @SuppressWarnings("unchecked") 345 @Override public void updateItem(T item, boolean empty) { 346 super.updateItem(item, empty); 347 348 if (empty) { 349 setText(null); | 72 73 /*************************************************************************** 74 * * 75 * Static cell factories * 76 * * 77 **************************************************************************/ 78 79 /** 80 * Creates a cell factory for use in a {@link TableColumn} cell factory. 81 * This method requires that the TableColumn be of type {@link Boolean}. 82 * 83 * <p>When used in a TableColumn, 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 * @param <S> The type of the TableView generic type 93 * @param column The TableColumn of type Boolean 94 * @return A {@link Callback} that will return a {@link TableCell} that is 95 * able to work on the type of element contained within the TableColumn. 96 */ 97 public static <S> Callback<TableColumn<S,Boolean>, TableCell<S,Boolean>> forTableColumn( 98 final TableColumn<S, Boolean> column) { 99 return forTableColumn(null, null); 100 } 101 102 /** 103 * Creates a cell factory for use in a {@link TableColumn} cell factory. 104 * This method requires that the TableColumn be of type 105 * {@code ObservableValue<Boolean>}. 106 * 107 * <p>When used in a TableColumn, the CheckBoxCell is rendered with a 108 * CheckBox centered in the column. 109 * 110 * @param <S> The type of the TableView generic type 111 * @param <T> The type of the elements contained within the {@link TableColumn} 112 * instance. 113 * @param getSelectedProperty A Callback that, given an object of 114 * type {@code TableColumn<S,T>}, will return an 115 * {@code ObservableValue<Boolean>} 116 * that represents whether the given item is selected or not. This 117 * {@code ObservableValue<Boolean>} will be bound bidirectionally 118 * (meaning that the CheckBox in the cell will set/unset this property 119 * based on user interactions, and the CheckBox will reflect the state of 120 * the {@code ObservableValue<Boolean>}, if it changes externally). 121 * @return A {@link Callback} that will return a {@link TableCell} that is 122 * able to work on the type of element contained within the TableColumn. 123 */ 124 public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn( 125 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty) { 126 return forTableColumn(getSelectedProperty, null); 127 } 128 129 /** 130 * Creates a cell factory for use in a {@link TableColumn} cell factory. 131 * This method requires that the TableColumn be of type 132 * {@code ObservableValue<Boolean>}. 133 * 134 * <p>When used in a TableColumn, the CheckBoxCell is rendered with a 135 * CheckBox centered in the column. 136 * 137 * @param <S> The type of the TableView generic type 138 * @param <T> The type of the elements contained within the {@link TableColumn} 139 * instance. 140 * @param getSelectedProperty A Callback that, given an object of 141 * type {@code TableColumn<S,T>}, will return an 142 * {@code ObservableValue<Boolean>} 143 * that represents whether the given item is selected or not. This 144 * {@code ObservableValue<Boolean>} will be bound bidirectionally 145 * (meaning that the CheckBox in the cell will set/unset this property 146 * based on user interactions, and the CheckBox will reflect the state of 147 * the {@code ObservableValue<Boolean>}, if it changes externally). 148 * @param showLabel In some cases, it may be desirable to show a label in 149 * the TableCell beside the {@link CheckBox}. By default a label is not 150 * shown, but by setting this to true the item in the cell will also 151 * have toString() called on it. If this is not the desired behavior, 152 * consider using 153 * {@link #forTableColumn(javafx.util.Callback, javafx.util.StringConverter) }, 154 * which allows for you to provide a callback that specifies the label for a 155 * given row item. 156 * @return A {@link Callback} that will return a {@link TableCell} that is 157 * able to work on the type of element contained within the TableColumn. 158 */ 159 public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn( 160 final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty, 161 final boolean showLabel) { 162 StringConverter<T> converter = ! showLabel ? 163 null : CellUtils.<T>defaultStringConverter(); 164 return forTableColumn(getSelectedProperty, converter); 165 } 166 167 /** 168 * Creates a cell factory for use in a {@link TableColumn} cell factory. 169 * This method requires that the TableColumn be of type 170 * {@code ObservableValue<Boolean>}. 171 * 172 * <p>When used in a TableColumn, the CheckBoxCell is rendered with a 173 * CheckBox centered in the column. 174 * 175 * @param <S> The type of the TableView generic type 176 * @param <T> The type of the elements contained within the {@link TableColumn} 177 * instance. 178 * @param getSelectedProperty A Callback that, given an object of type 179 * {@code TableColumn<S,T>}, will return an 180 * {@code ObservableValue<Boolean>} that represents whether the given 181 * item is selected or not. This {@code ObservableValue<Boolean>} will 182 * be bound bidirectionally (meaning that the CheckBox in the cell will 183 * set/unset this property based on user interactions, and the CheckBox 184 * will reflect the state of the {@code ObservableValue<Boolean>}, if 185 * it changes externally). 186 * @param converter A StringConverter that, give an object of type T, will return a 187 * String that can be used to represent the object visually. The default 188 * implementation in {@link #forTableColumn(Callback, boolean)} (when 189 * showLabel is true) is to simply call .toString() on all non-null 190 * items (and to just return an empty string in cases where the given 191 * item is null). 192 * @return A {@link Callback} that will return a {@link TableCell} that is 193 * able to work on the type of element contained within the TableColumn. 194 */ 195 public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn( 271 272 } 273 274 275 /*************************************************************************** 276 * * 277 * Properties * 278 * * 279 **************************************************************************/ 280 281 // --- converter 282 private ObjectProperty<StringConverter<T>> converter = 283 new SimpleObjectProperty<StringConverter<T>>(this, "converter") { 284 protected void invalidated() { 285 updateShowLabel(); 286 } 287 }; 288 289 /** 290 * The {@link StringConverter} property. 291 * @return the {@link StringConverter} property 292 */ 293 public final ObjectProperty<StringConverter<T>> converterProperty() { 294 return converter; 295 } 296 297 /** 298 * Sets the {@link StringConverter} to be used in this cell. 299 * @param value the {@link StringConverter} to be used in this cell 300 */ 301 public final void setConverter(StringConverter<T> value) { 302 converterProperty().set(value); 303 } 304 305 /** 306 * Returns the {@link StringConverter} used in this cell. 307 * @return the {@link StringConverter} used in this cell 308 */ 309 public final StringConverter<T> getConverter() { 310 return converterProperty().get(); 311 } 312 313 314 315 // --- selected state callback property 316 private ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> 317 selectedStateCallback = 318 new SimpleObjectProperty<Callback<Integer, ObservableValue<Boolean>>>( 319 this, "selectedStateCallback"); 320 321 /** 322 * Property representing the {@link Callback} that is bound to by the 323 * CheckBox shown on screen. 324 * @return the property representing the {@link Callback} that is bound to 325 * by the CheckBox shown on screen 326 */ 327 public final ObjectProperty<Callback<Integer, ObservableValue<Boolean>>> selectedStateCallbackProperty() { 328 return selectedStateCallback; 329 } 330 331 /** 332 * Sets the {@link Callback} that is bound to by the CheckBox shown on screen. 333 * @param value the {@link Callback} that is bound to by the CheckBox shown 334 * on screen 335 */ 336 public final void setSelectedStateCallback(Callback<Integer, ObservableValue<Boolean>> value) { 337 selectedStateCallbackProperty().set(value); 338 } 339 340 /** 341 * Returns the {@link Callback} that is bound to by the CheckBox shown on screen. 342 * @return the {@link Callback} that is bound to by the CheckBox shown on 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); |