public StringBindingSample() { final SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyyy"); final TextField dateField = new TextField(); dateField.setPromptText("Enter a birth date"); dateField.setMaxHeight(TextField.USE_PREF_SIZE); dateField.setMaxWidth(TextField.USE_PREF_SIZE); Label label = new Label(); label.textProperty().bind(new StringBinding() { { bind(dateField.textProperty()); } @Override protected String computeValue() { try { Date date = format.parse(dateField.getText()); Calendar c = Calendar.getInstance(); c.setTime(date); Date today = new Date(); Calendar c2 = Calendar.getInstance(); c2.setTime(today); if (c.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) - 1 && c.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) { return "You were born yesterday"; } else { return "You were born " + format.format(date); } } catch (Exception e) { return "Please enter a valid birth date (mm/dd/yyyy)"; } } }); VBox vBox = new VBox(7); vBox.setPadding(new Insets(12)); vBox.getChildren().addAll(label, dateField); getChildren().add(vBox); }
protected void invalidated() { Node node = get(); if (node != null) { node.accessibleTextProperty().unbind(); node.accessibleTextProperty().bind(new StringBinding() { { bind(currentXProperty(), currentYProperty()); } @Override protected String computeValue() { String seriesName = series != null ? series.getName() : ""; return seriesName + " X Axis is " + getCurrentX() + " Y Axis is " + getCurrentY(); } }); } }
private static <T> StringBinding mapString(ObservableValue<T> v, Function<T, String> func) { return new StringBinding() { { bind(v); } @Override protected String computeValue() { if (v.getValue() == null) { return ""; } else { return func.apply(v.getValue()); } } }; }
private void updateInfoPane(Theme selectedTheme) { view.themeNameLabel.textProperty().bind(selectedTheme.getLocalizedName()); view.themeDescriptionLabel.textProperty().bind(selectedTheme.getDescription()); view.themeAuthorsLabel.textProperty().bind(new StringBinding() { ObservableValue<ObservableStringValue[]> authorsBinding = selectedTheme.getAuthors(); { bind(authorsBinding); } @Override protected String computeValue() { return Stream .of(authorsBinding.getValue()) .map(ObservableStringValue::get) .collect(joining(", ")); } }); }
@Override default ObservableStringValue getLocalizedName() { return new StringBinding() { StringProperty aliasProperty = aliasProperty(); { bind(aliasProperty); } @Override protected String computeValue() { String alias = aliasProperty.get(); if (alias == null) { return getVersionNumber(); } return alias; } }; }
private void initializeBindings() { StringBinding readableKey = Bindings.createStringBinding(() -> { KeyCodeCombination kcc = keyCombinationProperty.get(); if (kcc == null) { disableModifiersTextFieldValidation(); return ""; } else { enableModifiersTextFieldValidation(); return kcc.getCode().getName(); } }, keyCombinationProperty); StringBinding readableModifiers = Bindings.createStringBinding(() -> { return modifiersTextFromKeyCombination(keyCombinationProperty.get()); }, keyCombinationProperty); modifiersTextField.textProperty().bind(readableModifiers); keyTextField.textProperty().bind(readableKey); // Disable Modifiers field if Key is not set. modifiersTextField.disableProperty().bind(keyTextField.textProperty().isEmpty()); }
private void initializeBindings() { StringBinding readableKey = Bindings.createStringBinding(() -> { KeyCodeCombination kcc = keyCombinationProperty.get(); if (kcc == null || kcc.getCode() == KeyCode.CLEAR) { return ""; } else { return kcc.getCode().getName(); } }, keyCombinationProperty); StringBinding readableModifiers = Bindings.createStringBinding(() -> { return modifiersTextFromKeyCombination(keyCombinationProperty.get()); }, keyCombinationProperty); modifiersTextField.textProperty().bind(readableModifiers); keyTextField.textProperty().bind(readableKey); // Disable Modifiers field if Key is not set. modifiersTextField.disableProperty().bind(keyTextField.textProperty().isEmpty()); }
@Test public void testJoinList() { ObservableList<Object> items = FXCollections.observableArrayList(); StringProperty delimiter = new SimpleStringProperty(", "); StringBinding joined = CollectionBindings.join(items, delimiter); assertThat(joined.get()).isEqualTo(""); items.add("A"); assertThat(joined.get()).isEqualTo("A"); items.add(1); assertThat(joined.get()).isEqualTo("A, 1"); items.add(Runnable.class); assertThat(joined.get()).isEqualTo("A, 1, interface java.lang.Runnable"); delimiter.set(":"); assertThat(joined.get()).isEqualTo("A:1:interface java.lang.Runnable"); }
@Test public void testTrim() { StringProperty text = new SimpleStringProperty(); StringBinding trimmed = StringBindings.trim(text); assertThat(trimmed).hasValue(""); text.setValue("test"); assertThat(trimmed).hasValue("test"); text.setValue("test "); assertThat(trimmed).hasValue("test"); text.setValue(" test "); assertThat(trimmed).hasValue("test"); }
private Slider addSliderControl(final String title, final DoubleProperty prop) { final Slider slider = new Slider(); slider.setValue(prop.get()); prop.bind(slider.valueProperty()); final VBox box = new VBox(); final Text titleText = new Text(title); titleText.textProperty().bind(new StringBinding() { { super.bind(slider.valueProperty()); } @Override protected String computeValue() { return title + " : " + twoDForm.format(slider.getValue()); } }); box.getChildren().addAll(titleText, slider); getChildren().add(box); return slider; }
public void addColorControl(final String title, final ObjectProperty<Paint> paintProperty) { final ColorPicker colorPicker = ColorPickerBuilder.create() .value((Color) paintProperty.get()).build(); paintProperty.bind(colorPicker.valueProperty()); final VBox box = new VBox(); final Text titleText = new Text(title); titleText.textProperty().bind(new StringBinding() { { super.bind(colorPicker.valueProperty()); } @Override protected String computeValue() { return title + " : " + colorPicker.getValue().toString(); } }); box.getChildren().addAll(titleText, colorPicker); getChildren().add(box); }
public void addBooleanControl(final String title, final BooleanProperty boolProp) { final CheckBox check = new CheckBox(); check.setSelected(boolProp.get()); boolProp.bind(check.selectedProperty()); final VBox box = new VBox(); final Text titleText = new Text(title); titleText.textProperty().bind(new StringBinding() { { super.bind(check.selectedProperty()); } @Override protected String computeValue() { return title + " : " + String.valueOf(check.selectedProperty().get()); } }); box.getChildren().addAll(titleText, check); getChildren().add(box); }
public StringBinding createDisplayNameBinding(final Contact contact) { final ContactType type = contact.getType(); final StringBinding binding; switch(type) { case PERSON: final Person person = (Person) contact; binding = createStringBinding(() -> String.format("%s, %s", person.getLastName(), person.getFirstName()), person.firstNameProperty(), person.lastNameProperty()); break; case COMPANY: final Company company = (Company) contact; binding = createStringBinding(() -> company.getName(), company.nameProperty()); break; case FAMILY: final Family family = (Family) contact; binding = createStringBinding(() -> family.getFamilyName(), family.familyNameProperty()); break; default: throw new IllegalArgumentException(String.format("Unknown contact type: %s", type.name())); } return binding; }
public static StringBinding toStringBinding(ObservableValue<String> ov) { return new StringBinding() { { bind(ov); } @Override protected String computeValue() { return ov.getValue(); } }; }
private StringBinding substring(final StringProperty property, final IntegerProperty lenght) { return Bindings.createStringBinding(() -> { if (property.get() != null && property.get().length() > lenght.get()) { return property.getValue().substring(0, lenght.get()) + "..."; } return property.get(); }, property); }
private void createSpineEditor() { SplineEditor = new SplineEditor(); GridPane.setConstraints(SplineEditor, 0, 0, 1, 10, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS); gridPane.add(SplineEditor, 0, 0); codeTextField.textProperty().bind(new StringBinding() { { bind(SplineEditor.controlPoint1xProperty(), SplineEditor.controlPoint1yProperty(), SplineEditor.controlPoint2xProperty(), SplineEditor.controlPoint2yProperty()); } @Override protected String computeValue() { return String.format("Interpolator.SPLINE(%.4f, %.4f, %.4f, %.4f);", SplineEditor.getControlPoint1x(), SplineEditor.getControlPoint1y(), SplineEditor.getControlPoint2x(), SplineEditor.getControlPoint2y()); } }); // create animation updater ChangeListener<Number> animUpdater = (ObservableValue<? extends Number> ov, Number t, Number t1) -> updateAnimation(); SplineEditor.controlPoint1xProperty().addListener(animUpdater); SplineEditor.controlPoint1yProperty().addListener(animUpdater); SplineEditor.controlPoint2xProperty().addListener(animUpdater); SplineEditor.controlPoint2yProperty().addListener(animUpdater); }
public ColorPickerTool(Color startColor) { setMinWidth(USE_PREF_SIZE); setMaxWidth(Double.MAX_VALUE); setAlignment(Pos.BASELINE_LEFT); getStyleClass().add("color-picker"); setTextFill(Color.WHITE); color.set(startColor); textProperty().bind(new StringBinding() { { bind(color); } @Override protected String computeValue() { return getWebColor(getColor()); } }); setOnAction((ActionEvent arg0) -> { if (popover == null) { popover = new ColorPickerPopover(); popover.colorProperty().addListener((ObservableValue<? extends Color> arg1, Color arg2, Color newValue) -> { setColor(newValue); }); } if (popover.isShowing()) { popover.hide(); } else { popover.setColor(getColor()); popover.show(ColorPickerTool.this); } }); Rectangle colorRect = new Rectangle(); colorRect.setWidth(16); colorRect.setHeight(16); colorRect.fillProperty().bind(new ObjectBinding<Paint>() { { bind(color); } @Override protected Paint computeValue() { return getColor(); } }); colorRect.setEffect(new DropShadow(3, 0, 1, Color.rgb(0, 0, 0, 0.8))); setGraphic(colorRect); }
private void init() { // name name.bind(Bindings.when(file.isNull()) .then(Bindings.createStringBinding(() -> "new" + order.get(), order)) .otherwise(toStringBinding(map(file, f -> f.getName())))); // graphics tab.setGraphic(icon); icon.setStyleClass("tab-icon"); icon.setIcon(FontAwesomeIcon.SAVE); // Hold the object in cache to avoid gc StringBinding state = CacheUtil.cache(this, "state", () -> Bindings.when(Bindings.equal(toObjectBinding(currentTabEntity()), this)) .then(Bindings.when(manager.modifiedProperty()) .then("selected-modified") .otherwise("selected")) .otherwise(Bindings.when(manager.modifiedProperty()) .then("modified") .otherwise(""))); state.addListener((ob, o, n) -> { uncatch(() -> icon.pseudoClassStateChanged(PseudoClass.getPseudoClass(o), false)); uncatch(() -> icon.pseudoClassStateChanged(PseudoClass.getPseudoClass(n), true)); }); // recent file.addListener((ob, o, n) -> todoAll( () -> ifThat(n != null).todo(() -> recentSupport.setLastFile(n)), () -> ifThat(o == null && n != null).todo(() -> releaseName()) )); }
private void configureTitleBinding() { UIUtils.whenWindowReady(mainPane, window -> { Stage stage = (Stage) window; StringBinding titleBinding = createTitleBinding(tailingFile, tailedFileName); stage.titleProperty().bind(titleBinding); }); }
private static StringBinding createTitleBinding(BooleanProperty appendFileName, StringProperty filename) { return Bindings.createStringBinding(() -> { String newTitle = FXLog.APP_NAME; if (appendFileName.get()) { newTitle += " - " + filename.get(); } return newTitle; }, filename, appendFileName); }
@Override public Binding<String> createMeasurement(final PathObject pathObject) { return new StringBinding() { @Override protected String computeValue() { return getMeasurementValue(pathObject); } }; }
public StringBinding getElapsedStringBinding(LocalDateTime start) { return Bindings.createStringBinding(() -> units.stream() .filter(u -> start.plus(1, u).isBefore(time.get())) // ignore units where less than 1 unit has elapsed .sorted(Comparator.reverseOrder()) // sort with biggest first .findFirst() // get the first (biggest) unit .map(u -> String.format("%d %s ago", u.between(start, time.get()), u)) // format as string .orElse("Just now") // default if elapsed time is less than smallest unit , time); }
private void bindProperties(final TextArea textAreaNotePreview, final SessionModel sessionModel) { SimpleObjectProperty<WordModel> currentWordProperty = sessionModel.currentWordProperty(); StringBinding noteBinding = selectString(currentWordProperty, "note"); textAreaNotePreview.textProperty().bind(noteBinding); textAreaNotePreview.visibleProperty().bind(noteBinding.isNotEmpty()); }
protected void bindValues() { ObservableNumberValue count = countValue(); StringBinding countText = Bindings.createStringBinding(() -> formatTotalWords(count), count); StringBinding filenameText = Bindings.createStringBinding(() -> filename(file.get()), file); error.bind(Bindings.equal(count, 0)); countDescription.bind(Bindings.when(error).then(ERROR).otherwise(countText)); filename.bind(filenameText); }
@Override public void bindToCuteElement(CuteElement editableCopyOfCE, CuteElement origCE) { hkAction = (HotkeyAction) editableCopyOfCE; origHkAction = (HotkeyAction) origCE; String keys = hkAction.getHotkey(); keyCombinationProperty.set(parseKeyCodeCombinationFromString(keys)); StringBinding applyChangesToCuteElement = Bindings.createStringBinding( () -> keyCombinationProperty.get().getName(), keyCombinationProperty); hkAction.keysProperty().bind(applyChangesToCuteElement); }
public BatchRunnerResult() { reportRow().add(new ReadOnlyStringWrapper("Batch run")); StringBinding successOrNot = Bindings.when(completelySuccessful()) .then(ApplicationState.getMessage(COMPLETED_SUCCESSFUL)) .otherwise(ApplicationState.getMessage(COMPLETED_UNSUCCESSFUL)); reportRow().add(Bindings.when(completed()) .then(successOrNot).otherwise(ApplicationState.getMessage(INCOMPLETE))); reportRow().add(Bindings.createStringBinding(()-> String.format("%.0f%%",100.0*percentComplete().get()),percentComplete())); reportRow().add(getDuration()); }
private void buildRow(Map<String, String> variables, Set<String> reportColumns) { StringBinding successOrNot = Bindings.when(completelySuccessful()) .then(ApplicationState.getMessage(COMPLETED_SUCCESSFUL)) .otherwise(ApplicationState.getMessage(COMPLETED_UNSUCCESSFUL)); reportRow().add(new ReadOnlyStringWrapper(task)); reportRow().add(Bindings.when(Bindings.greaterThanOrEqual(percentComplete(), 1)) .then(successOrNot) .otherwise(ApplicationState.getMessage(INCOMPLETE))); reportRow().add(Bindings.createStringBinding(()-> String.format("%.0f%%",100.0*percentComplete().get()),percentComplete())); reportRow().add(getDuration()); reportColumns.forEach((colName) -> reportRow().add(new SimpleStringProperty(variables.get(colName)))); }
protected StringBinding defaultConnectionUrlProperty(DbmsHandlerComponent dbmsHandlerComponent) { return Bindings.createStringBinding(() -> DatabaseUtil.findDbmsType(dbmsHandlerComponent, this).getConnectionUrlGenerator().from(this), typeNameProperty(), ipAddressProperty(), portProperty(), usernameProperty() ); }
protected StringBinding defaultConnectionUrlProperty(T document, DbmsHandlerComponent dbmsHandlerComponent) { return Bindings.createStringBinding(() -> DatabaseUtil.findDbmsType(dbmsHandlerComponent, document).getConnectionUrlGenerator().from(document), document.typeNameProperty(), document.ipAddressProperty(), document.portProperty(), document.usernameProperty() ); }
@Test public void testStringBinding(){ IntegerProperty property = new SimpleIntegerProperty(12); final StringBinding actual = property.asString(); assertThat(actual).hasValue("12"); assertThat(actual).hasSameValue(actual); }
@Test public void testTransforming() { StringProperty text = new SimpleStringProperty(); StringBinding transformed = StringBindings.transforming(text, s -> "*" + s + "*"); assertThat(transformed).hasValue(""); text.setValue("test"); assertThat(transformed).hasValue("*test*"); }
public StringBinding selectedRunTest_formattedTimeStamp() { return Bindings.createStringBinding(new Callable<String>() { @Override public String call() throws Exception { if (selectedRun_timeStamp().get() != null) { return DATE_FORMAT.format(selectedRun_timeStamp().get()); } else { return null; } } }, selectedRunProperty()); }
public StringBinding selectedRun_formattedTrace() { return Bindings.createStringBinding(new Callable<String>() { @Override public String call() throws Exception { if (selectedRun_trace().get() != null) { return Joiner.on('\n').join(selectedRun_trace().get()); } else { return null; } } }, selectedRunProperty()); }
public StringBinding createStringBinding(Callable<String> func) { return Bindings.createStringBinding(func, locale); }
private static <T> StringBinding map(ObservableValue<T> v, Function<T, Integer> func) { return mapString(v, t -> Integer.toString(func.apply(t))); }