35 import javafx.scene.control.ListCell;
36 import javafx.scene.control.ListView;
37 import javafx.util.Callback;
38 import javafx.util.StringConverter;
39
40 /**
41 * A class containing a {@link ListCell} implementation that draws a
42 * {@link CheckBox} node inside the cell, optionally with a label to indicate
43 * what the checkbox represents.
44 *
45 * <p>The CheckBoxListCell is rendered with a CheckBox on the left-hand side of
46 * the {@link ListView}, and the text related to the list item taking up all
47 * remaining horizontal space.
48 *
49 * <p>To construct an instance of this class, it is necessary to provide a
50 * {@link Callback} that, given an object of type T, will return a
51 * {@code ObservableValue<Boolean>} that represents whether the given item is
52 * selected or not. This ObservableValue will be bound bidirectionally (meaning
53 * that the CheckBox in the cell will set/unset this property based on user
54 * interactions, and the CheckBox will reflect the state of the
55 * ObservableValue<Boolean>, if it changes externally).
56 *
57 * <p>Note that the CheckBoxListCell renders the CheckBox 'live', meaning that
58 * the CheckBox is always interactive and can be directly toggled by the user.
59 * This means that it is not necessary that the cell enter its
60 * {@link #editingProperty() editing state} (usually by the user double-clicking
61 * on the cell). A side-effect of this is that the usual editing callbacks
62 * (such as {@link javafx.scene.control.ListView#onEditCommitProperty() on edit commit})
63 * will <strong>not</strong> be called. If you want to be notified of changes,
64 * it is recommended to directly observe the boolean properties that are
65 * manipulated by the CheckBox.</p>
66 *
67 * @see CheckBox
68 * @see ListCell
69 * @param <T> The type of the elements contained within the ListView.
70 * @since JavaFX 2.2
71 */
72 public class CheckBoxListCell<T> extends ListCell<T> {
73
74 /***************************************************************************
75 * *
184 setAlignment(Pos.CENTER_LEFT);
185 setContentDisplay(ContentDisplay.LEFT);
186
187 // by default the graphic is null until the cell stops being empty
188 setGraphic(null);
189 }
190
191
192 /***************************************************************************
193 * *
194 * Properties *
195 * *
196 **************************************************************************/
197
198 // --- converter
199 private ObjectProperty<StringConverter<T>> converter =
200 new SimpleObjectProperty<StringConverter<T>>(this, "converter");
201
202 /**
203 * The {@link StringConverter} property.
204 */
205 public final ObjectProperty<StringConverter<T>> converterProperty() {
206 return converter;
207 }
208
209 /**
210 * Sets the {@link StringConverter} to be used in this cell.
211 */
212 public final void setConverter(StringConverter<T> value) {
213 converterProperty().set(value);
214 }
215
216 /**
217 * Returns the {@link StringConverter} used in this cell.
218 */
219 public final StringConverter<T> getConverter() {
220 return converterProperty().get();
221 }
222
223
224 // --- selected state callback property
225 private ObjectProperty<Callback<T, ObservableValue<Boolean>>>
226 selectedStateCallback =
227 new SimpleObjectProperty<Callback<T, ObservableValue<Boolean>>>(
228 this, "selectedStateCallback");
229
230 /**
231 * Property representing the {@link Callback} that is bound to by the
232 * CheckBox shown on screen.
233 */
234 public final ObjectProperty<Callback<T, ObservableValue<Boolean>>> selectedStateCallbackProperty() {
235 return selectedStateCallback;
236 }
237
238 /**
239 * Sets the {@link Callback} that is bound to by the CheckBox shown on screen.
240 */
241 public final void setSelectedStateCallback(Callback<T, ObservableValue<Boolean>> value) {
242 selectedStateCallbackProperty().set(value);
243 }
244
245 /**
246 * Returns the {@link Callback} that is bound to by the CheckBox shown on screen.
247 */
248 public final Callback<T, ObservableValue<Boolean>> getSelectedStateCallback() {
249 return selectedStateCallbackProperty().get();
250 }
251
252
253
254 /***************************************************************************
255 * *
256 * Public API *
257 * *
258 **************************************************************************/
259
260 /** {@inheritDoc} */
261 @Override public void updateItem(T item, boolean empty) {
262 super.updateItem(item, empty);
263
264 if (! empty) {
265 StringConverter<T> c = getConverter();
266 Callback<T, ObservableValue<Boolean>> callback = getSelectedStateCallback();
|
35 import javafx.scene.control.ListCell;
36 import javafx.scene.control.ListView;
37 import javafx.util.Callback;
38 import javafx.util.StringConverter;
39
40 /**
41 * A class containing a {@link ListCell} implementation that draws a
42 * {@link CheckBox} node inside the cell, optionally with a label to indicate
43 * what the checkbox represents.
44 *
45 * <p>The CheckBoxListCell is rendered with a CheckBox on the left-hand side of
46 * the {@link ListView}, and the text related to the list item taking up all
47 * remaining horizontal space.
48 *
49 * <p>To construct an instance of this class, it is necessary to provide a
50 * {@link Callback} that, given an object of type T, will return a
51 * {@code ObservableValue<Boolean>} that represents whether the given item is
52 * selected or not. This ObservableValue will be bound bidirectionally (meaning
53 * that the CheckBox in the cell will set/unset this property based on user
54 * interactions, and the CheckBox will reflect the state of the
55 * {@code ObservableValue<Boolean>}, if it changes externally).
56 *
57 * <p>Note that the CheckBoxListCell renders the CheckBox 'live', meaning that
58 * the CheckBox is always interactive and can be directly toggled by the user.
59 * This means that it is not necessary that the cell enter its
60 * {@link #editingProperty() editing state} (usually by the user double-clicking
61 * on the cell). A side-effect of this is that the usual editing callbacks
62 * (such as {@link javafx.scene.control.ListView#onEditCommitProperty() on edit commit})
63 * will <strong>not</strong> be called. If you want to be notified of changes,
64 * it is recommended to directly observe the boolean properties that are
65 * manipulated by the CheckBox.</p>
66 *
67 * @see CheckBox
68 * @see ListCell
69 * @param <T> The type of the elements contained within the ListView.
70 * @since JavaFX 2.2
71 */
72 public class CheckBoxListCell<T> extends ListCell<T> {
73
74 /***************************************************************************
75 * *
184 setAlignment(Pos.CENTER_LEFT);
185 setContentDisplay(ContentDisplay.LEFT);
186
187 // by default the graphic is null until the cell stops being empty
188 setGraphic(null);
189 }
190
191
192 /***************************************************************************
193 * *
194 * Properties *
195 * *
196 **************************************************************************/
197
198 // --- converter
199 private ObjectProperty<StringConverter<T>> converter =
200 new SimpleObjectProperty<StringConverter<T>>(this, "converter");
201
202 /**
203 * The {@link StringConverter} property.
204 * @return the {@link StringConverter} property
205 */
206 public final ObjectProperty<StringConverter<T>> converterProperty() {
207 return converter;
208 }
209
210 /**
211 * Sets the {@link StringConverter} to be used in this cell.
212 * @param value the {@link StringConverter}
213 */
214 public final void setConverter(StringConverter<T> value) {
215 converterProperty().set(value);
216 }
217
218 /**
219 * Returns the {@link StringConverter} used in this cell.
220 * @return the {@link StringConverter} used in this cell
221 */
222 public final StringConverter<T> getConverter() {
223 return converterProperty().get();
224 }
225
226
227 // --- selected state callback property
228 private ObjectProperty<Callback<T, ObservableValue<Boolean>>>
229 selectedStateCallback =
230 new SimpleObjectProperty<Callback<T, ObservableValue<Boolean>>>(
231 this, "selectedStateCallback");
232
233 /**
234 * Property representing the {@link Callback} that is bound to by the
235 * CheckBox shown on screen.
236 * @return the {@link Callback} that is bound to by the CheckBox shown on
237 * screen
238 */
239 public final ObjectProperty<Callback<T, ObservableValue<Boolean>>> selectedStateCallbackProperty() {
240 return selectedStateCallback;
241 }
242
243 /**
244 * Sets the {@link Callback} that is bound to by the CheckBox shown on screen.
245 * @param value the {@link Callback}
246 */
247 public final void setSelectedStateCallback(Callback<T, ObservableValue<Boolean>> value) {
248 selectedStateCallbackProperty().set(value);
249 }
250
251 /**
252 * Returns the {@link Callback} that is bound to by the CheckBox shown on screen.
253 * @return the {@link Callback} that is bound to by the CheckBox shown on screen
254 */
255 public final Callback<T, ObservableValue<Boolean>> getSelectedStateCallback() {
256 return selectedStateCallbackProperty().get();
257 }
258
259
260
261 /***************************************************************************
262 * *
263 * Public API *
264 * *
265 **************************************************************************/
266
267 /** {@inheritDoc} */
268 @Override public void updateItem(T item, boolean empty) {
269 super.updateItem(item, empty);
270
271 if (! empty) {
272 StringConverter<T> c = getConverter();
273 Callback<T, ObservableValue<Boolean>> callback = getSelectedStateCallback();
|