/** * Attempts to coerce a value into a {@link String} * * @param v * the value to coerce * @return the coerced value (null when value failed to be coerced) */ public static <VT> String coerceToString(final VT v) { String cv = null; if (v != null && SelectionModel.class.isAssignableFrom(v.getClass())) { cv = ((SelectionModel<?>) v).getSelectedItem() != null ? ((SelectionModel<?>) v) .getSelectedItem().toString() : null; } else if (v != null && (Calendar.class.isAssignableFrom(v.getClass()) || Date.class .isAssignableFrom(v.getClass()))) { final Date date = Date.class.isAssignableFrom(v.getClass()) ? (Date) v : ((Calendar) v).getTime(); cv = SDF.format(date); } else if (v != null) { cv = v.toString(); } return cv; }
@FXML private void initialize() { viewModel = new KeyBindingsDialogViewModel(keyBindingRepository, dialogService, preferences); keyBindingsTable.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); viewModel.selectedKeyBindingProperty().bind( EasyBind.monadic(keyBindingsTable.selectionModelProperty()) .flatMap(SelectionModel::selectedItemProperty) .selectProperty(TreeItem::valueProperty) ); keyBindingsTable.setOnKeyPressed(evt -> viewModel.setNewBindingForCurrent(evt)); keyBindingsTable.rootProperty().bind( EasyBind.map(viewModel.rootKeyBindingProperty(), keybinding -> new RecursiveTreeItem<>(keybinding, KeyBindingViewModel::getChildren)) ); actionColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().nameProperty()); shortcutColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().shownBindingProperty()); resetColumn.setCellFactory(new ViewModelTreeTableCellFactory<KeyBindingViewModel, String>() .withGraphic(keyBinding -> keyBinding.getIcon().map(IconTheme.JabRefIcon::getGraphicNode).orElse(null)) .withOnMouseClickedEvent(keyBinding -> evt -> keyBinding.resetToDefault()) ); }
@SuppressWarnings("rawtypes") public static void disableIfNotSelected(SelectionModel selectionModel, Node... nodes) { BooleanBinding selected = selectionModel.selectedItemProperty().isNull(); for (Node node : nodes) { node.disableProperty().bind(selected); } }
public static <T> void bind(SelectionModel<T> selectionModel, ObjectProperty<T> selectedItem) { selectionModel.selectedItemProperty().addListener((v, o, n) -> { selectedItem.set(n); }); selectedItem.addListener((v, o, n) -> { selectionModel.select(n); }); selectionModel.select(selectedItem.get()); }
/** * @see BeanPathAdapter.FieldBean#performOperation(String, String, * Class, String, Observable, Class, SelectionModel, FieldProperty, * FieldBeanOperation) */ public <T> FieldProperty<?, ?, ?> performOperation( final String fieldPath, final ObservableList<T> observableList, final Class<T> listValueClass, final String collectionItemPath, final Class<?> collectionItemPathType, final SelectionModel<T> selectionModel, final FieldProperty<?, ?, ?> itemMaster, final FieldBeanOperation operation) { return performOperation(fieldPath, fieldPath, listValueClass, collectionItemPath, (Observable) observableList, collectionItemPathType, selectionModel, itemMaster, operation); }
/** * @see BeanPathAdapter.FieldBean#performOperation(String, String, * Class, String, Observable, Class, SelectionModel, FieldProperty, * FieldBeanOperation) */ public <T> FieldProperty<?, ?, ?> performOperation( final String fieldPath, final ObservableSet<T> observableSet, final Class<T> setValueClass, final String collectionItemPath, final Class<?> collectionItemPathType, final SelectionModel<T> selectionModel, final FieldProperty<?, ?, ?> itemMaster, final FieldBeanOperation operation) { return performOperation(fieldPath, fieldPath, setValueClass, collectionItemPath, (Observable) observableSet, collectionItemPathType, selectionModel, itemMaster, operation); }
/** * @see BeanPathAdapter.FieldBean#performOperation(String, String, * Class, String, Observable, Class, SelectionModel, FieldProperty, * FieldBeanOperation) */ public <K, V> FieldProperty<?, ?, ?> performOperation( final String fieldPath, final ObservableMap<K, V> observableMap, final Class<V> mapValueClass, final String collectionItemPath, final Class<?> collectionItemPathType, final SelectionModel<V> selectionModel, final FieldProperty<?, ?, ?> itemMaster, final FieldBeanOperation operation) { return performOperation(fieldPath, fieldPath, mapValueClass, collectionItemPath, (Observable) observableMap, collectionItemPathType, selectionModel, itemMaster, operation); }
private void moveSelection(int startIndex, int delta) { final DockTabPane tabPane = getControl(); int tabIndex = findValidTab(startIndex, delta); if (tabIndex > -1) { final SelectionModel<DockTab> selectionModel = tabPane.getSelectionModel(); selectionModel.select(tabIndex); } tabPane.requestFocus(); }
public static <T> void reselectCurrent(ListView<T> list) { SelectionModel<T> model = list.getSelectionModel(); int i = model.getSelectedIndex(); model.clearSelection(); System.out.println("selecting i = " + i); model.select(i); }
public static <T> void selectNearby(ListView<T> list, T item) { SelectionModel<T> model = list.getSelectionModel(); int i = model.getSelectedIndex(); int n = list.getItems().size(); if (i + 1 < n) model.select(i + 1); else if (i - 1 >= 0) model.select(i - 1); else model.clearSelection(); }
/** * Binds a {@link ObservableList} by traversing the bean's field tree. An * additional item path can be specified when the path points to a * {@link Collection} that contains beans that also need traversed in order * to establish the final value. For example: If a field path points to * <code>phoneNumbers</code> (relative to the {@link #getBean()}) where * <code>phoneNumbers</code> is a {@link Collection} that contains * <code>PhoneNumber</code> instances which in turn have a field called * <code>areaCode</code> then an item path can be passed in addition to the * field path with <code>areaCode</code> as it's value. * * @param fieldPath * the <b><code>.</code></b> separated field paths relative to * the {@link #getBean()} that will be traversed * @param itemFieldPath * the <b><code>.</code></b> separated field paths relative to * each item in the bean's underlying {@link Collection} that * will be traversed (empty/null when each item value does not * need traversed) * @param itemFieldPathType * the {@link Class} of that the item path points to * @param list * the {@link ObservableList} to bind to the field class type of * the property * @param listValueType * the class type of the {@link ObservableList} value * @param selectionModel * the {@link SelectionModel} used to set the values within the * {@link ObservableList} <b>only applicable when the * {@link ObservableList} is used for selection(s) and therefore * cannot be updated directly because it is read-only</b> * @param selectionModelItemMasterPath * when binding to {@link SelectionModel} items, this will be the * optional path to the collection field that contains all the * items to select from */ public <E> void bindContentBidirectional(final String fieldPath, final String itemFieldPath, final Class<?> itemFieldPathType, final ObservableList<E> list, final Class<E> listValueType, final SelectionModel<E> selectionModel, final String selectionModelItemMasterPath) { FieldProperty<?, ?, ?> itemMaster = null; if (selectionModelItemMasterPath != null && !selectionModelItemMasterPath.isEmpty()) { itemMaster = getRoot().performOperation( selectionModelItemMasterPath, list, listValueType, itemFieldPath, itemFieldPathType, null, null, FieldBeanOperation.CREATE_OR_FIND); } getRoot().performOperation(fieldPath, list, listValueType, itemFieldPath, itemFieldPathType, selectionModel, itemMaster, FieldBeanOperation.BIND); }
/** * Binds a {@link ObservableSet} by traversing the bean's field tree. An * additional item path can be specified when the path points to a * {@link Collection} that contains beans that also need traversed in order * to establish the final value. For example: If a field path points to * <code>phoneNumbers</code> (relative to the {@link #getBean()}) where * <code>phoneNumbers</code> is a {@link Collection} that contains * <code>PhoneNumber</code> instances which in turn have a field called * <code>areaCode</code> then an item path can be passed in addition to the * field path with <code>areaCode</code> as it's value. * * @param fieldPath * the <b><code>.</code></b> separated field paths relative to * the {@link #getBean()} that will be traversed * @param itemFieldPath * the <b><code>.</code></b> separated field paths relative to * each item in the bean's underlying {@link Collection} that * will be traversed (empty/null when each item value does not * need traversed) * @param itemFieldPathType * the {@link Class} of that the item path points to * @param set * the {@link ObservableSet} to bind to the field class type of * the property * @param setValueType * the class type of the {@link ObservableSet} value * @param selectionModel * the {@link SelectionModel} used to set the values within the * {@link ObservableSet} <b>only applicable when the * {@link ObservableSet} is used for selection(s) and therefore * cannot be updated directly because it is read-only</b> * @param selectionModelItemMasterPath * when binding to {@link SelectionModel} items, this will be the * optional path to the collection field that contains all the * items to select from */ public <E> void bindContentBidirectional(final String fieldPath, final String itemFieldPath, final Class<?> itemFieldPathType, final ObservableSet<E> set, final Class<E> setValueType, final SelectionModel<E> selectionModel, final String selectionModelItemMasterPath) { FieldProperty<?, ?, ?> itemMaster = null; if (selectionModelItemMasterPath != null && !selectionModelItemMasterPath.isEmpty()) { itemMaster = getRoot().performOperation( selectionModelItemMasterPath, set, setValueType, itemFieldPath, itemFieldPathType, null, null, FieldBeanOperation.CREATE_OR_FIND); } getRoot().performOperation(fieldPath, set, setValueType, itemFieldPath, itemFieldPathType, selectionModel, itemMaster, FieldBeanOperation.BIND); }
/** * Binds a {@link ObservableMap} by traversing the bean's field tree. An * additional item path can be specified when the path points to a * {@link Collection} that contains beans that also need traversed in order * to establish the final value. For example: If a field path points to * <code>phoneNumbers</code> (relative to the {@link #getBean()}) where * <code>phoneNumbers</code> is a {@link Collection} that contains * <code>PhoneNumber</code> instances which in turn have a field called * <code>areaCode</code> then an item path can be passed in addition to the * field path with <code>areaCode</code> as it's value. * * @param fieldPath * the <b><code>.</code></b> separated field paths relative to * the {@link #getBean()} that will be traversed * @param itemFieldPath * the <b><code>.</code></b> separated field paths relative to * each item in the bean's underlying {@link Collection} that * will be traversed (empty/null when each item value does not * need traversed) * @param itemFieldPathType * the {@link Class} of that the item path points to * @param map * the {@link ObservableMap} to bind to the field class type of * the property * @param mapValueType * the class type of the {@link ObservableMap} value * @param selectionModel * the {@link SelectionModel} used to set the values within the * {@link ObservableMap} <b>only applicable when the * {@link ObservableMap} is used for selection(s) and therefore * cannot be updated directly because it is read-only</b> * @param selectionModelItemMasterPath * when binding to {@link SelectionModel} items, this will be the * optional path to the collection field that contains all the * items to select from */ public <K, V> void bindContentBidirectional(final String fieldPath, final String itemFieldPath, final Class<?> itemFieldPathType, final ObservableMap<K, V> map, final Class<V> mapValueType, final SelectionModel<V> selectionModel, final String selectionModelItemMasterPath) { FieldProperty<?, ?, ?> itemMaster = null; if (selectionModelItemMasterPath != null && !selectionModelItemMasterPath.isEmpty()) { itemMaster = getRoot().performOperation( selectionModelItemMasterPath, map, mapValueType, itemFieldPath, itemFieldPathType, null, null, FieldBeanOperation.CREATE_OR_FIND); } getRoot().performOperation(fieldPath, map, mapValueType, itemFieldPath, itemFieldPathType, selectionModel, itemMaster, FieldBeanOperation.BIND); }
private void setupTabPane() { SelectionModel<Tab> model = vTabPane.getSelectionModel(); model.selectedItemProperty().addListener(mTabChangeListener); model.clearSelection(); model.selectFirst(); }
public static <T> void forceSelect(ListView<T> list, T item) { SelectionModel<T> model = list.getSelectionModel(); model.clearSelection(); model.select(item); }
public static <T> void forceSelect(ChoiceBox<T> list, T item) { SelectionModel<T> model = list.getSelectionModel(); model.clearSelection(); model.select(item); }
public static <T> void forceSelectFirst(ListView<T> list) { SelectionModel<T> model = list.getSelectionModel(); model.clearSelection(); model.selectFirst(); }
public NavPane() { this.setCellFactory(new NavItemCell()); this.getItems().addAll(NavItem.navs); this.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); SelectionModel sm = this.getSelectionModel(); sm.selectedItemProperty().addListener( new ChangeListener<NavItem>() { @Override public void changed(ObservableValue<? extends NavItem> observable, NavItem oldValue, NavItem newValue) { System.out.println("change" + oldValue + newValue); Event.fireEvent(NavPane.this, new NavItemEvent(oldValue, newValue, NavItemEvent.NAV_CHANGED)); } }); }
/** * Constructor * * @param bean * the bean that the path belongs to * @param fullPath * the full <code>.</code> separated path to the * {@link FieldProperty} * @param fieldName * the name of the field within the bean * @param notifyProperty * the {@link FieldPathValueProperty} that will be set every * time the {@link FieldProperty#setValue(Object)} is * performed or an item within the value is changed * @param declaredFieldType * the declared {@link Class} of the field * @param collectionItemPath * the the <code>.</code> separated field names of the * {@link Observable} collection (only applicable when the * {@link Observable} is a {@link ObservableList}, * {@link ObservableSet}, or {@link ObservableMap}) * @param collectionObservable * the {@link Observable} {@link Collection} used to bind to * the {@link FieldProperty} <b>OR</b> when the * {@link SelectionModel} is specified this is the * {@link Observable} {@link Collection} of available items * to select from * @param collectionType * the {@link Collection} {@link Class} used to attempt to * transform the underlying field {@link Observable} * {@link Collection} to the {@link Collection} {@link Class} * (only applicable when the actual field is a * {@link Collection}) * @param collectionSelectionModel * the {@link SelectionModel} used to set the values within * the {@link Observable} <b>only applicable when the * {@link Observable} is used for selection(s) and therefore * cannot be updated directly because it is read-only</b> * @param itemMaster * the {@link FieldProperty} that contains the item(s) that * the {@link SelectionModel} can select from */ @SuppressWarnings("unchecked") protected FieldProperty(final BT bean, final String fullPath, final String fieldName, final FieldPathValueProperty notifyProperty, final Class<T> declaredFieldType, final String collectionItemPath, final Observable collectionObservable, final Class<?> collectionType, final SelectionModel<?> collectionSelectionModel, final FieldProperty<?, ?, ?> itemMaster) { super(); this.fullPath = fullPath; this.notifyProperty = notifyProperty; this.fieldHandle = new FieldHandle<BT, T>(bean, fieldName, declaredFieldType); this.itemMaster = itemMaster; this.collectionObservable = new WeakReference<Observable>( collectionObservable); this.collectionItemPath = collectionItemPath; this.collectionType = collectionType; this.collectionSelectionModel = (SelectionModel<Object>) collectionSelectionModel; if (this.collectionSelectionModel != null && this.itemMaster != null) { this.itemMaster.addListener(this); } setDerived(); }
/** * @return a {@link SelectionModel} for the {@link FieldProperty} when * the field references a collection/map for item selection or * {@code null} when not a selection {@link FieldProperty} */ protected SelectionModel<Object> getCollectionSelectionModel() { return collectionSelectionModel; }