55 * @param <T> The type of the TreeItems contained within the TreeView.
56 * @since JavaFX 2.2
57 */
58 public class ComboBoxTreeCell<T> extends DefaultTreeCell<T> {
59
60 /***************************************************************************
61 * *
62 * Static cell factories *
63 * *
64 **************************************************************************/
65
66 /**
67 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
68 * default, the ComboBoxCell is rendered as a {@link Label} when not being
69 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
70 * default, stretch to fill the entire tree cell.
71 *
72 * @param <T> The type of the elements contained within the TreeView.
73 * @param items Zero or more items that will be shown to the user when the
74 * {@link ComboBox} menu is showing. These items must be of the same
75 * type as the TreeView<T>, such that upon selection, they replace the
76 * existing value in the TreeItem {@link TreeItem#valueProperty() value}
77 * property.
78 * @return A {@link Callback} that will return a TreeCell that is able to
79 * work on the type of element contained within the TreeView.
80 */
81 @SafeVarargs
82 public static <T> Callback<TreeView<T>, TreeCell<T>> forTreeView(T... items) {
83 return forTreeView(FXCollections.observableArrayList(items));
84 }
85
86 /**
87 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
88 * default, the ComboBoxCell is rendered as a {@link Label} when not being
89 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
90 * default, stretch to fill the entire tree cell, excluding the space
91 * allocated to the tree cell indentation and disclosure node(i.e. the arrow).
92 *
93 * @param <T> The type of the {@link TreeItem} elements contained within the
94 * TreeView.
95 * @param items An {@link ObservableList} containing zero or more items that
100 * {@link TreeView#editingItemProperty()}.
101 * @return A {@link Callback} that will return a TreeCell that is able to
102 * work on the type of element contained within the TreeView.
103 */
104 public static <T> Callback<TreeView<T>, TreeCell<T>> forTreeView(
105 final ObservableList<T> items) {
106 return forTreeView(null, items);
107 }
108
109 /**
110 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
111 * default, the ComboBoxCell is rendered as a {@link Label} when not being
112 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
113 * default, stretch to fill the entire tree cell.
114 *
115 * @param <T> The type of the elements contained within the TreeView.
116 * @param converter A {@link StringConverter} to convert the given item (of
117 * type T) to a String for displaying to the user.
118 * @param items Zero or more items that will be shown to the user when the
119 * {@link ComboBox} menu is showing. These items must be of the same
120 * type as the TreeView<T>, such that upon selection, they replace the
121 * existing value in the TreeItem {@link TreeItem#valueProperty() value}
122 * property.
123 * @return A {@link Callback} that will return a TreeCell that is able to
124 * work on the type of element contained within the TreeView.
125 */
126 @SafeVarargs
127 public static <T> Callback<TreeView<T>, TreeCell<T>> forTreeView(
128 final StringConverter<T> converter,
129 final T... items) {
130 return forTreeView(converter, FXCollections.observableArrayList(items));
131 }
132
133 /**
134 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
135 * default, the ComboBoxCell is rendered as a {@link Label} when not being
136 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
137 * default, stretch to fill the entire tree cell.
138 *
139 * @param <T> The type of the elements contained within the TreeView.
140 * @param converter A {@link StringConverter} to convert the given item (of
236 public ComboBoxTreeCell(StringConverter<T> converter, ObservableList<T> items) {
237 this.getStyleClass().add("combo-box-tree-cell");
238 this.items = items;
239 setConverter(converter != null ? converter : CellUtils.<T>defaultStringConverter());
240 }
241
242
243
244 /***************************************************************************
245 * *
246 * Properties *
247 * *
248 **************************************************************************/
249
250 // --- converter
251 private ObjectProperty<StringConverter<T>> converter =
252 new SimpleObjectProperty<StringConverter<T>>(this, "converter");
253
254 /**
255 * The {@link StringConverter} property.
256 */
257 public final ObjectProperty<StringConverter<T>> converterProperty() {
258 return converter;
259 }
260
261 /**
262 * Sets the {@link StringConverter} to be used in this cell.
263 */
264 public final void setConverter(StringConverter<T> value) {
265 converterProperty().set(value);
266 }
267
268 /**
269 * Returns the {@link StringConverter} used in this cell.
270 */
271 public final StringConverter<T> getConverter() {
272 return converterProperty().get();
273 }
274
275
276 // --- comboBox editable
277 private BooleanProperty comboBoxEditable =
278 new SimpleBooleanProperty(this, "comboBoxEditable");
279
280 /**
281 * A property representing whether the ComboBox, when shown to the user,
282 * is editable or not.
283 */
284 public final BooleanProperty comboBoxEditableProperty() {
285 return comboBoxEditable;
286 }
287
288 /**
289 * Configures the ComboBox to be editable (to allow user input outside of the
290 * options provide in the dropdown list).
291 */
292 public final void setComboBoxEditable(boolean value) {
293 comboBoxEditableProperty().set(value);
294 }
295
296 /**
297 * Returns true if the ComboBox is editable.
298 */
299 public final boolean isComboBoxEditable() {
300 return comboBoxEditableProperty().get();
301 }
302
303
304
305 /***************************************************************************
306 * *
307 * Public API *
308 * *
309 **************************************************************************/
310
311 /**
312 * Returns the items to be displayed in the ChoiceBox when it is showing.
313 */
314 public ObservableList<T> getItems() {
315 return items;
316 }
317
318 /** {@inheritDoc} */
319 @Override public void startEdit() {
320 if (! isEditable() || ! getTreeView().isEditable()) {
321 return;
322 }
323
324 TreeItem<T> treeItem = getTreeItem();
325 if (treeItem == null) {
326 return;
327 }
328
329 if (comboBox == null) {
330 comboBox = createComboBox(this, items, converterProperty());
331 comboBox.editableProperty().bind(comboBoxEditableProperty());
332 }
|
55 * @param <T> The type of the TreeItems contained within the TreeView.
56 * @since JavaFX 2.2
57 */
58 public class ComboBoxTreeCell<T> extends DefaultTreeCell<T> {
59
60 /***************************************************************************
61 * *
62 * Static cell factories *
63 * *
64 **************************************************************************/
65
66 /**
67 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
68 * default, the ComboBoxCell is rendered as a {@link Label} when not being
69 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
70 * default, stretch to fill the entire tree cell.
71 *
72 * @param <T> The type of the elements contained within the TreeView.
73 * @param items Zero or more items that will be shown to the user when the
74 * {@link ComboBox} menu is showing. These items must be of the same
75 * type as the {@literal TreeView<T>}, such that upon selection, they replace the
76 * existing value in the TreeItem {@link TreeItem#valueProperty() value}
77 * property.
78 * @return A {@link Callback} that will return a TreeCell that is able to
79 * work on the type of element contained within the TreeView.
80 */
81 @SafeVarargs
82 public static <T> Callback<TreeView<T>, TreeCell<T>> forTreeView(T... items) {
83 return forTreeView(FXCollections.observableArrayList(items));
84 }
85
86 /**
87 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
88 * default, the ComboBoxCell is rendered as a {@link Label} when not being
89 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
90 * default, stretch to fill the entire tree cell, excluding the space
91 * allocated to the tree cell indentation and disclosure node(i.e. the arrow).
92 *
93 * @param <T> The type of the {@link TreeItem} elements contained within the
94 * TreeView.
95 * @param items An {@link ObservableList} containing zero or more items that
100 * {@link TreeView#editingItemProperty()}.
101 * @return A {@link Callback} that will return a TreeCell that is able to
102 * work on the type of element contained within the TreeView.
103 */
104 public static <T> Callback<TreeView<T>, TreeCell<T>> forTreeView(
105 final ObservableList<T> items) {
106 return forTreeView(null, items);
107 }
108
109 /**
110 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
111 * default, the ComboBoxCell is rendered as a {@link Label} when not being
112 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
113 * default, stretch to fill the entire tree cell.
114 *
115 * @param <T> The type of the elements contained within the TreeView.
116 * @param converter A {@link StringConverter} to convert the given item (of
117 * type T) to a String for displaying to the user.
118 * @param items Zero or more items that will be shown to the user when the
119 * {@link ComboBox} menu is showing. These items must be of the same
120 * type as the {@literal TreeView<T>}, such that upon selection, they replace the
121 * existing value in the TreeItem {@link TreeItem#valueProperty() value}
122 * property.
123 * @return A {@link Callback} that will return a TreeCell that is able to
124 * work on the type of element contained within the TreeView.
125 */
126 @SafeVarargs
127 public static <T> Callback<TreeView<T>, TreeCell<T>> forTreeView(
128 final StringConverter<T> converter,
129 final T... items) {
130 return forTreeView(converter, FXCollections.observableArrayList(items));
131 }
132
133 /**
134 * Creates a ComboBox cell factory for use in {@link TreeView} controls. By
135 * default, the ComboBoxCell is rendered as a {@link Label} when not being
136 * edited, and as a ComboBox when in editing mode. The ComboBox will, by
137 * default, stretch to fill the entire tree cell.
138 *
139 * @param <T> The type of the elements contained within the TreeView.
140 * @param converter A {@link StringConverter} to convert the given item (of
236 public ComboBoxTreeCell(StringConverter<T> converter, ObservableList<T> items) {
237 this.getStyleClass().add("combo-box-tree-cell");
238 this.items = items;
239 setConverter(converter != null ? converter : CellUtils.<T>defaultStringConverter());
240 }
241
242
243
244 /***************************************************************************
245 * *
246 * Properties *
247 * *
248 **************************************************************************/
249
250 // --- converter
251 private ObjectProperty<StringConverter<T>> converter =
252 new SimpleObjectProperty<StringConverter<T>>(this, "converter");
253
254 /**
255 * The {@link StringConverter} property.
256 * @return the {@link StringConverter} property
257 */
258 public final ObjectProperty<StringConverter<T>> converterProperty() {
259 return converter;
260 }
261
262 /**
263 * Sets the {@link StringConverter} to be used in this cell.
264 * @param value the {@link StringConverter} to be used in this cell
265 */
266 public final void setConverter(StringConverter<T> value) {
267 converterProperty().set(value);
268 }
269
270 /**
271 * Returns the {@link StringConverter} used in this cell.
272 * @return the {@link StringConverter} used in this cell
273 */
274 public final StringConverter<T> getConverter() {
275 return converterProperty().get();
276 }
277
278
279 // --- comboBox editable
280 private BooleanProperty comboBoxEditable =
281 new SimpleBooleanProperty(this, "comboBoxEditable");
282
283 /**
284 * A property representing whether the ComboBox, when shown to the user,
285 * is editable or not.
286 * @return the property representing whether the ComboBox, when shown to the
287 * user, is editable or not
288 */
289 public final BooleanProperty comboBoxEditableProperty() {
290 return comboBoxEditable;
291 }
292
293 /**
294 * Configures the ComboBox to be editable (to allow user input outside of the
295 * options provide in the dropdown list).
296 * @param value the editable value to be set for this ComboBox
297 */
298 public final void setComboBoxEditable(boolean value) {
299 comboBoxEditableProperty().set(value);
300 }
301
302 /**
303 * Returns true if the ComboBox is editable.
304 * @return true if the ComboBox is editable
305 */
306 public final boolean isComboBoxEditable() {
307 return comboBoxEditableProperty().get();
308 }
309
310
311
312 /***************************************************************************
313 * *
314 * Public API *
315 * *
316 **************************************************************************/
317
318 /**
319 * Returns the items to be displayed in the ComboBox when it is showing.
320 * @return the items to be displayed in this ComboBox when it is showing
321 */
322 public ObservableList<T> getItems() {
323 return items;
324 }
325
326 /** {@inheritDoc} */
327 @Override public void startEdit() {
328 if (! isEditable() || ! getTreeView().isEditable()) {
329 return;
330 }
331
332 TreeItem<T> treeItem = getTreeItem();
333 if (treeItem == null) {
334 return;
335 }
336
337 if (comboBox == null) {
338 comboBox = createComboBox(this, items, converterProperty());
339 comboBox.editableProperty().bind(comboBoxEditableProperty());
340 }
|