120
121
122
123 /***************************************************************************
124 * *
125 * Constructors *
126 * *
127 **************************************************************************/
128
129 /**
130 * Create a new ChoiceBox which has an empty list of items.
131 */
132 public ChoiceBox() {
133 this(FXCollections.<T>observableArrayList());
134 }
135
136 /**
137 * Create a new ChoiceBox with the given set of items. Since it is observable,
138 * the content of this list may change over time and the ChoiceBox will
139 * be updated accordingly.
140 * @param items
141 */
142 public ChoiceBox(ObservableList<T> items) {
143 getStyleClass().setAll("choice-box");
144 setAccessibleRole(AccessibleRole.COMBO_BOX);
145 setItems(items);
146 setSelectionModel(new ChoiceBoxSelectionModel<T>(this));
147
148 // listen to the value property, if the value is
149 // set to something that exists in the items list, update the
150 // selection model to indicate that this is the selected item
151 valueProperty().addListener((ov, t, t1) -> {
152 if (getItems() == null) return;
153 int index = getItems().indexOf(t1);
154 if (index > -1) {
155 getSelectionModel().select(index);
156 }
157 });
158 }
159
160 /***************************************************************************
291
292 // Look for the selected item as having been removed. If it has been,
293 // then we need to clear the selection in the selection model.
294 final T selectedItem = sm.getSelectedItem();
295 while (c.next()) {
296 if (selectedItem != null && c.getRemoved().contains(selectedItem)) {
297 sm.clearSelection();
298 break;
299 }
300 }
301 }
302 };
303
304 /**
305 * Allows a way to specify how to represent objects in the items list. When
306 * a StringConverter is set, the object toString method is not called and
307 * instead its toString(object T) is called, passing the objects in the items list.
308 * This is useful when using domain objects in a ChoiceBox as this property
309 * allows for customization of the representation. Also, any of the pre-built
310 * Converters available in the {@link javafx.util.converter} package can be set.
311 * @since JavaFX 2.1
312 */
313 public ObjectProperty<StringConverter<T>> converterProperty() { return converter; }
314 private ObjectProperty<StringConverter<T>> converter =
315 new SimpleObjectProperty<StringConverter<T>>(this, "converter", null);
316 public final void setConverter(StringConverter<T> value) { converterProperty().set(value); }
317 public final StringConverter<T> getConverter() {return converterProperty().get(); }
318
319 /**
320 * The value of this ChoiceBox is defined as the selected item in the ChoiceBox
321 * selection model. The valueProperty is synchronized with the selectedItem.
322 * This property allows for bi-directional binding of external properties to the
323 * ChoiceBox and updates the selection model accordingly.
324 * @since JavaFX 2.1
325 */
326 public ObjectProperty<T> valueProperty() { return value; }
327 private ObjectProperty<T> value = new SimpleObjectProperty<T>(this, "value") {
328 @Override protected void invalidated() {
329 super.invalidated();
330 fireEvent(new ActionEvent());
331 // Update selection
332 final SingleSelectionModel<T> sm = getSelectionModel();
333 if (sm != null) {
334 sm.select(super.getValue());
335 }
336 notifyAccessibleAttributeChanged(AccessibleAttribute.TEXT);
337 }
338 };
339 public final void setValue(T value) { valueProperty().set(value); }
340 public final T getValue() { return valueProperty().get(); }
341
342
343 // --- On Action
344 /**
345 * The ChoiceBox action, which is invoked whenever the ChoiceBox
346 * {@link #valueProperty() value} property is changed. This
347 * may be due to the value property being programmatically changed or when the
348 * user selects an item in a popup menu.
349 *
350 * @since JavaFX 8u60
351 */
352 public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() { return onAction; }
353 public final void setOnAction(EventHandler<ActionEvent> value) { onActionProperty().set(value); }
354 public final EventHandler<ActionEvent> getOnAction() { return onActionProperty().get(); }
355 private ObjectProperty<EventHandler<ActionEvent>> onAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
356 @Override protected void invalidated() {
357 setEventHandler(ActionEvent.ACTION, get());
358 }
359
360 @Override
361 public Object getBean() {
362 return ChoiceBox.this;
363 }
364
365 @Override
366 public String getName() {
367 return "onAction";
368 }
369 };
370
371
372 // --- On Showing
373 /**
374 * Called just prior to the {@code ChoiceBox} popup being shown.
375 * @since JavaFX 8u60
376 */
377 public final ObjectProperty<EventHandler<Event>> onShowingProperty() { return onShowing; }
378 public final void setOnShowing(EventHandler<Event> value) { onShowingProperty().set(value); }
379 public final EventHandler<Event> getOnShowing() { return onShowingProperty().get(); }
380 private ObjectProperty<EventHandler<Event>> onShowing = new ObjectPropertyBase<EventHandler<Event>>() {
381 @Override protected void invalidated() {
382 setEventHandler(ON_SHOWING, get());
383 }
384
385 @Override public Object getBean() {
386 return ChoiceBox.this;
387 }
388
389 @Override public String getName() {
390 return "onShowing";
391 }
392 };
393
394
395 // -- On Shown
396 /**
397 * Called just after the {@link ChoiceBox} popup is shown.
398 * @since JavaFX 8u60
399 */
400 public final ObjectProperty<EventHandler<Event>> onShownProperty() { return onShown; }
401 public final void setOnShown(EventHandler<Event> value) { onShownProperty().set(value); }
402 public final EventHandler<Event> getOnShown() { return onShownProperty().get(); }
403 private ObjectProperty<EventHandler<Event>> onShown = new ObjectPropertyBase<EventHandler<Event>>() {
404 @Override protected void invalidated() {
405 setEventHandler(ON_SHOWN, get());
406 }
407
408 @Override public Object getBean() {
409 return ChoiceBox.this;
410 }
411
412 @Override public String getName() {
413 return "onShown";
414 }
415 };
416
417
418 // --- On Hiding
419 /**
420 * Called just prior to the {@link ChoiceBox} popup being hidden.
421 * @since JavaFX 8u60
422 */
423 public final ObjectProperty<EventHandler<Event>> onHidingProperty() { return onHiding; }
424 public final void setOnHiding(EventHandler<Event> value) { onHidingProperty().set(value); }
425 public final EventHandler<Event> getOnHiding() { return onHidingProperty().get(); }
426 private ObjectProperty<EventHandler<Event>> onHiding = new ObjectPropertyBase<EventHandler<Event>>() {
427 @Override protected void invalidated() {
428 setEventHandler(ON_HIDING, get());
429 }
430
431 @Override public Object getBean() {
432 return ChoiceBox.this;
433 }
434
435 @Override public String getName() {
436 return "onHiding";
437 }
438 };
439
440
441 // --- On Hidden
442 /**
443 * Called just after the {@link ChoiceBox} popup has been hidden.
444 * @since JavaFX 8u60
445 */
446 public final ObjectProperty<EventHandler<Event>> onHiddenProperty() { return onHidden; }
447 public final void setOnHidden(EventHandler<Event> value) { onHiddenProperty().set(value); }
448 public final EventHandler<Event> getOnHidden() { return onHiddenProperty().get(); }
449 private ObjectProperty<EventHandler<Event>> onHidden = new ObjectPropertyBase<EventHandler<Event>>() {
450 @Override protected void invalidated() {
451 setEventHandler(ON_HIDDEN, get());
452 }
453
454 @Override public Object getBean() {
455 return ChoiceBox.this;
456 }
457
458 @Override public String getName() {
459 return "onHidden";
460 }
461 };
462
463 /***************************************************************************
|
120
121
122
123 /***************************************************************************
124 * *
125 * Constructors *
126 * *
127 **************************************************************************/
128
129 /**
130 * Create a new ChoiceBox which has an empty list of items.
131 */
132 public ChoiceBox() {
133 this(FXCollections.<T>observableArrayList());
134 }
135
136 /**
137 * Create a new ChoiceBox with the given set of items. Since it is observable,
138 * the content of this list may change over time and the ChoiceBox will
139 * be updated accordingly.
140 * @param items the set of items
141 */
142 public ChoiceBox(ObservableList<T> items) {
143 getStyleClass().setAll("choice-box");
144 setAccessibleRole(AccessibleRole.COMBO_BOX);
145 setItems(items);
146 setSelectionModel(new ChoiceBoxSelectionModel<T>(this));
147
148 // listen to the value property, if the value is
149 // set to something that exists in the items list, update the
150 // selection model to indicate that this is the selected item
151 valueProperty().addListener((ov, t, t1) -> {
152 if (getItems() == null) return;
153 int index = getItems().indexOf(t1);
154 if (index > -1) {
155 getSelectionModel().select(index);
156 }
157 });
158 }
159
160 /***************************************************************************
291
292 // Look for the selected item as having been removed. If it has been,
293 // then we need to clear the selection in the selection model.
294 final T selectedItem = sm.getSelectedItem();
295 while (c.next()) {
296 if (selectedItem != null && c.getRemoved().contains(selectedItem)) {
297 sm.clearSelection();
298 break;
299 }
300 }
301 }
302 };
303
304 /**
305 * Allows a way to specify how to represent objects in the items list. When
306 * a StringConverter is set, the object toString method is not called and
307 * instead its toString(object T) is called, passing the objects in the items list.
308 * This is useful when using domain objects in a ChoiceBox as this property
309 * allows for customization of the representation. Also, any of the pre-built
310 * Converters available in the {@link javafx.util.converter} package can be set.
311 * @return the string converter property
312 * @since JavaFX 2.1
313 */
314 public ObjectProperty<StringConverter<T>> converterProperty() { return converter; }
315 private ObjectProperty<StringConverter<T>> converter =
316 new SimpleObjectProperty<StringConverter<T>>(this, "converter", null);
317 public final void setConverter(StringConverter<T> value) { converterProperty().set(value); }
318 public final StringConverter<T> getConverter() {return converterProperty().get(); }
319
320 /**
321 * The value of this ChoiceBox is defined as the selected item in the ChoiceBox
322 * selection model. The valueProperty is synchronized with the selectedItem.
323 * This property allows for bi-directional binding of external properties to the
324 * ChoiceBox and updates the selection model accordingly.
325 * @return the value property
326 * @since JavaFX 2.1
327 */
328 public ObjectProperty<T> valueProperty() { return value; }
329 private ObjectProperty<T> value = new SimpleObjectProperty<T>(this, "value") {
330 @Override protected void invalidated() {
331 super.invalidated();
332 fireEvent(new ActionEvent());
333 // Update selection
334 final SingleSelectionModel<T> sm = getSelectionModel();
335 if (sm != null) {
336 sm.select(super.getValue());
337 }
338 notifyAccessibleAttributeChanged(AccessibleAttribute.TEXT);
339 }
340 };
341 public final void setValue(T value) { valueProperty().set(value); }
342 public final T getValue() { return valueProperty().get(); }
343
344
345 // --- On Action
346 /**
347 * The ChoiceBox action, which is invoked whenever the ChoiceBox
348 * {@link #valueProperty() value} property is changed. This
349 * may be due to the value property being programmatically changed or when the
350 * user selects an item in a popup menu.
351 *
352 * @return the on action property
353 * @since JavaFX 8u60
354 */
355 public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() { return onAction; }
356 public final void setOnAction(EventHandler<ActionEvent> value) { onActionProperty().set(value); }
357 public final EventHandler<ActionEvent> getOnAction() { return onActionProperty().get(); }
358 private ObjectProperty<EventHandler<ActionEvent>> onAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
359 @Override protected void invalidated() {
360 setEventHandler(ActionEvent.ACTION, get());
361 }
362
363 @Override
364 public Object getBean() {
365 return ChoiceBox.this;
366 }
367
368 @Override
369 public String getName() {
370 return "onAction";
371 }
372 };
373
374
375 // --- On Showing
376 /**
377 * Called just prior to the {@code ChoiceBox} popup being shown.
378 * @return the on showing property
379 * @since JavaFX 8u60
380 */
381 public final ObjectProperty<EventHandler<Event>> onShowingProperty() { return onShowing; }
382 public final void setOnShowing(EventHandler<Event> value) { onShowingProperty().set(value); }
383 public final EventHandler<Event> getOnShowing() { return onShowingProperty().get(); }
384 private ObjectProperty<EventHandler<Event>> onShowing = new ObjectPropertyBase<EventHandler<Event>>() {
385 @Override protected void invalidated() {
386 setEventHandler(ON_SHOWING, get());
387 }
388
389 @Override public Object getBean() {
390 return ChoiceBox.this;
391 }
392
393 @Override public String getName() {
394 return "onShowing";
395 }
396 };
397
398
399 // -- On Shown
400 /**
401 * Called just after the {@link ChoiceBox} popup is shown.
402 * @return the on shown property
403 * @since JavaFX 8u60
404 */
405 public final ObjectProperty<EventHandler<Event>> onShownProperty() { return onShown; }
406 public final void setOnShown(EventHandler<Event> value) { onShownProperty().set(value); }
407 public final EventHandler<Event> getOnShown() { return onShownProperty().get(); }
408 private ObjectProperty<EventHandler<Event>> onShown = new ObjectPropertyBase<EventHandler<Event>>() {
409 @Override protected void invalidated() {
410 setEventHandler(ON_SHOWN, get());
411 }
412
413 @Override public Object getBean() {
414 return ChoiceBox.this;
415 }
416
417 @Override public String getName() {
418 return "onShown";
419 }
420 };
421
422
423 // --- On Hiding
424 /**
425 * Called just prior to the {@link ChoiceBox} popup being hidden.
426 * @return the on hiding property
427 * @since JavaFX 8u60
428 */
429 public final ObjectProperty<EventHandler<Event>> onHidingProperty() { return onHiding; }
430 public final void setOnHiding(EventHandler<Event> value) { onHidingProperty().set(value); }
431 public final EventHandler<Event> getOnHiding() { return onHidingProperty().get(); }
432 private ObjectProperty<EventHandler<Event>> onHiding = new ObjectPropertyBase<EventHandler<Event>>() {
433 @Override protected void invalidated() {
434 setEventHandler(ON_HIDING, get());
435 }
436
437 @Override public Object getBean() {
438 return ChoiceBox.this;
439 }
440
441 @Override public String getName() {
442 return "onHiding";
443 }
444 };
445
446
447 // --- On Hidden
448 /**
449 * Called just after the {@link ChoiceBox} popup has been hidden.
450 * @return the on hidden property
451 * @since JavaFX 8u60
452 */
453 public final ObjectProperty<EventHandler<Event>> onHiddenProperty() { return onHidden; }
454 public final void setOnHidden(EventHandler<Event> value) { onHiddenProperty().set(value); }
455 public final EventHandler<Event> getOnHidden() { return onHiddenProperty().get(); }
456 private ObjectProperty<EventHandler<Event>> onHidden = new ObjectPropertyBase<EventHandler<Event>>() {
457 @Override protected void invalidated() {
458 setEventHandler(ON_HIDDEN, get());
459 }
460
461 @Override public Object getBean() {
462 return ChoiceBox.this;
463 }
464
465 @Override public String getName() {
466 return "onHidden";
467 }
468 };
469
470 /***************************************************************************
|