@SuppressWarnings("unchecked") static Object normalize(Property<?> prop, Class<?> clz) { if (clz.isAssignableFrom(IntegerProperty.class)) { return toInteger((Property<Integer>) prop); } else if (clz.isAssignableFrom(LongProperty.class)) { return toLong((Property<Long>) prop); } else if (clz.isAssignableFrom(FloatProperty.class)) { return toFloat((Property<Float>) prop); } else if (clz.isAssignableFrom(DoubleProperty.class)) { return toDouble((Property<Double>) prop); } else if (clz.isAssignableFrom(StringProperty.class)) { return toString((Property<String>) prop); } else if (clz.isAssignableFrom(ObjectProperty.class)) { return toObject((Property<Object>) prop); } return prop; }
/** * Reads a value saved in a preferences object and stores it in a JavaFX property. If no value is present in the * preferences object corresponding to the property's name, or if it is of an incompatible type, the property is * not modified. * * @param property the property that the read value should be placed in * @param preferences the preferences object to read from * @param parser the function to use to convert from the serialized String to an object of the proper type * @param <T> the type of the property */ public static <T> void read(Property<? super T> property, Preferences preferences, Function<String, ? extends T> parser) { String name = property.getName(); String saved = preferences.get(name, null); if (saved != null) { try { property.setValue(parser.apply(saved)); } catch (Exception ignore) { // NOPMD empty catch block // The saved value probably wasn't the expected type // No need to bubble the exception up } } }
@Test public void testBindBidirectionalWithConverter() { // given Property<String> str = new SimpleStringProperty("-42"); Property<Number> num = new SimpleDoubleProperty(1.23); // when PropertyUtils.bindBidirectionalWithConverter(str, num, Double::parseDouble, Object::toString); // then (initial conditions) assertAll( () -> assertEquals("1.23", str.getValue(), "String was not set correctly"), () -> assertEquals(1.23, num.getValue().doubleValue(), "Binding target should not have changed") ); // when changing one value str.setValue("89"); // then assertEquals(89, num.getValue().doubleValue(), "Number was not set correctly"); // when changing the other value num.setValue(10.01); // then assertEquals("10.01", str.getValue(), "String was not set correctly"); }
private void updateSeries(XYChart.Series<Number, Number> series, long now, double newData) { long elapsed = now - Time.getStartTime(); XYChart.Data<Number, Number> point = new XYChart.Data<>(elapsed, newData); ObservableList<XYChart.Data<Number, Number>> dataList = series.getData(); if (!dataList.isEmpty()) { // Make the graph a square wave // This prevents the graph from appearing to be continuous when the data is discreet // Note this only affects the chart; the actual data is not changed double prev = dataList.get(dataList.size() - 1).getYValue().doubleValue(); if (prev != newData) { dataList.add(new XYChart.Data<>(elapsed - 1, prev)); } } dataList.add(point); realData.computeIfAbsent(series, __ -> FXCollections.observableArrayList()).add(point); if (!chart.getData().contains(series) && Optional.ofNullable(visibleSeries.get(series)).map(Property::getValue).orElse(true)) { chart.getData().add(series); } updateBounds(elapsed); }
/** * Binds a {@link Property} by traversing the bean's field tree * * @param fieldPath * the <b><code>.</code></b> separated field paths relative to * the {@link #getBean()} that will be traversed * @param property * the {@link Property} to bind to the field class type of the * property * @param propertyType * the class type of the {@link Property} value */ @SuppressWarnings("unchecked") public <T> void bindBidirectional(final String fieldPath, final Property<T> property, final Class<T> propertyType) { Class<T> clazz = propertyType != null ? propertyType : propertyValueClass(property); if (clazz == null && property.getValue() != null) { clazz = (Class<T>) property.getValue().getClass(); } if (clazz == null || clazz == Object.class) { throw new UnsupportedOperationException(String.format( "Unable to determine property value class for %1$s " + "and declared type %2$s", property, propertyType)); } getRoot().performOperation(fieldPath, property, clazz, FieldBeanOperation.BIND); }
public static <F, T> ObservableValue<T> nestValue(ObservableValue<F> pf, Function<F, ObservableValue<T>> func) { ObservableValue<T> current = func.apply(pf.getValue()); Property<T> nestProp = new SimpleObjectProperty<>(); nestProp.bind(current); pf.addListener((ob, o, n) -> { ObservableValue<T> pt = func.apply(n); nestProp.unbind(); nestProp.bind(pt); }); return nestProp; }
/** * Provides the underlying value class for a given {@link Property} * * @param property * the {@link Property} to check * @return the value class of the {@link Property} */ @SuppressWarnings("unchecked") protected static <T> Class<T> propertyValueClass(final Property<T> property) { Class<T> clazz = null; if (property != null) { if (StringProperty.class.isAssignableFrom(property.getClass())) { clazz = (Class<T>) String.class; } else if (IntegerProperty.class.isAssignableFrom(property .getClass())) { clazz = (Class<T>) Integer.class; } else if (BooleanProperty.class.isAssignableFrom(property .getClass())) { clazz = (Class<T>) Boolean.class; } else if (DoubleProperty.class.isAssignableFrom(property .getClass())) { clazz = (Class<T>) Double.class; } else if (FloatProperty.class .isAssignableFrom(property.getClass())) { clazz = (Class<T>) Float.class; } else if (LongProperty.class.isAssignableFrom(property.getClass())) { clazz = (Class<T>) Long.class; } else if (ListProperty.class.isAssignableFrom(property.getClass())) { clazz = (Class<T>) List.class; } else if (MapProperty.class.isAssignableFrom(property.getClass())) { clazz = (Class<T>) Map.class; } else { clazz = (Class<T>) Object.class; } } return clazz; }
@Override public StyleableProperty<T> getStyleableProperty(S styleable) { Property<T> property = propertyExtractor.apply(styleable); if (property instanceof StyleableProperty) { // no need to wrap an already styleable property return (StyleableProperty<T>) property; } else { return new SimpleStyleableObjectPropertyWrapper<>(this, property); } }
/** * Observes changes to the given property and marks the project dirty * (edited) when the property is changed. * * @param property * The property to observe changes. * @return The observed property. */ private <T extends Property<?>> T observePropertyChanges(T property) { property.addListener((observable, oldValue, newValue) -> { JavaBeanProperty<?> changedProperty = (JavaBeanProperty<?>) observable; getCayennePropject().setDirty(true); LOGGER.debug("Property Changed: [" + changedProperty.getBean().getClass().getSimpleName() + " " + changedProperty.getName() + "] " + oldValue + " -> " + newValue); }); return property; }
/** * A more general version of {@link Bindings#when(ObservableBooleanValue)} * that can accept general boolean properties as conditions. * * @param condition the condition to bind to * * @see Bindings#when(ObservableBooleanValue) */ public static When when(Property<Boolean> condition) { if (condition instanceof ObservableBooleanValue) { return Bindings.when((ObservableBooleanValue) condition); } SimpleBooleanProperty realCondition = new SimpleBooleanProperty(); realCondition.bind(condition); return Bindings.when(realCondition); }
/** * Binds {@code firstProperty} to {@code secondProperty}, using a conversion function to map * values of type {@code U} to {@code T} so the first property can be bound. * * @param firstProperty the property to bind * @param secondProperty the property to bind to * @param u2tConverter the conversion function */ public static <T, U> void bindWithConverter( Property<T> firstProperty, Property<U> secondProperty, Function<U, T> u2tConverter) { Binding<T> binding = EasyBind.monadic(secondProperty).map(u2tConverter); bindings.put(firstProperty, binding); firstProperty.bind(binding); }
/** * Binds a property to a specific key in a map. If there is no entry for that key, the property's * value will be set to null. * * @param property the property to bind * @param map the map to bind to * @param key the key for the entry to bind to * @param v2t a conversion function for converting objects of type <i>V</i> to type <i>T</i> * @param <K> the types of the keys in the map * @param <V> the types of the values in the map * @param <T> the type of data in the property */ public static <K, V, T extends V> void bindToMapBidirectionally(Property<T> property, ObservableMap<K, V> map, K key, Function<V, T> v2t) { property.addListener((__, oldValue, newValue) -> map.put(key, newValue)); map.addListener((MapChangeListener<K, V>) change -> { if (change.getKey().equals(key)) { if (change.wasRemoved() && !map.containsKey(key)) { property.setValue(null); } else if (change.wasAdded()) { property.setValue(v2t.apply(change.getValueAdded())); } } }); }
/** * Saves a property to a preferences object. * * @param property the property to save * @param preferences the preferences object to save to * @param serializer a function to use to convert the property's value to a String that can be stored in * @param <T> the type of the property * * @throws IllegalArgumentException if the value of the property is null */ public static <T> void save(Property<? extends T> property, Preferences preferences, Function<? super T, String> serializer) { T value = property.getValue(); if (value == null) { throw new IllegalArgumentException("The property must have a value"); } preferences.put(property.getName(), serializer.apply(value)); }
/** * Creates a new property sheet containing items for each of the given properties. */ public ExtendedPropertySheet(Collection<? extends Property<?>> properties) { this(); getItems().setAll(properties.stream() .map(PropertyItem::new) .collect(Collectors.toList())); }
@Test public void testBindBidirectionalWithConverterNullFirstInitialValue() { // given Property<String> str = new SimpleStringProperty(null); Property<Number> num = new SimpleDoubleProperty(0.0); // when PropertyUtils.bindBidirectionalWithConverter(str, num, Double::parseDouble, Object::toString); // then assertAll( () -> assertEquals("0.0", str.getValue(), "String was not set correctly"), () -> assertEquals(0.0, num.getValue().doubleValue(), "Number should not have changed") ); }
@Test public void testBindBidirectionalWithConverterNullSecondInitialValue() { // given Property<String> str = new SimpleStringProperty("41"); Property<Number> num = new SimpleObjectProperty<>(null); // when PropertyUtils.bindBidirectionalWithConverter(str, num, Double::parseDouble, Object::toString); // then assertAll( () -> assertEquals(null, str.getValue(), "String should not have changed"), () -> assertEquals(null, num.getValue(), "Binding target should not have changed") ); }
/** * Gets a read-only list of all the preference properties. */ public ImmutableList<Property<?>> getProperties() { return ImmutableList.of( theme, defaultTileSize, autoLoadLastSaveFile, confirmExit ); }
/** * Sets the progress property to scrub. */ public void setProgressProperty(Property<Number> progressProperty) { Objects.requireNonNull(progressProperty, "progressProperty"); if (this.progressProperty != null) { this.progressProperty.removeListener(progressListener); } this.progressProperty = progressProperty; progressProperty.addListener(progressListener); }
/** * @see BeanPathAdapter.FieldBean#performOperation(String, String, * Class, String, Observable, Class, SelectionModel, FieldProperty, * FieldBeanOperation) */ public <T> FieldProperty<?, ?, ?> performOperation( final String fieldPath, final Property<T> property, final Class<T> propertyValueClass, final FieldBeanOperation operation) { return performOperation(fieldPath, fieldPath, propertyValueClass, null, (Observable) property, null, null, null, operation); }
@Override public List<Property<?>> getProperties() { return ImmutableList.<Property<?>>builder() .add(visibleTime) .addAll(visibleSeries.values() .stream() .sorted(Comparator.comparing(Property::getName, AlphanumComparator.INSTANCE)) .collect(Collectors.toList())) .build(); }
@Override public List<Property<?>> getProperties() { return ImmutableList.of( // use FlushableProperty so changes made are only effected when committed by the user new FlushableProperty<>(serverId) ); }
@Override public void invalidated(Observable observable) { if (updating) { return; } final Property<L> leftProperty = getLeftProperty(); final Property<R> rightProperty = getRightProperty(); if (wasGarbageCollected()) { if (leftProperty != null) { leftProperty.removeListener(this); } if (rightProperty != null) { rightProperty.removeListener(this); } } else { try { updating = true; if (observable == leftProperty) { rightProperty.setValue(converter.toRight(leftProperty.getValue())); } else { leftProperty.setValue(converter.toLeft(rightProperty.getValue())); } } finally { updating = false; } } }
/** * set bindings * * @param canProperties */ public void bind(Map<String, Property<?>> canProperties) { this.canProperties = canProperties; // bind values by name CANValuePane[] canValuePanes = { chargePane, odoPane }; for (CANValuePane canValuePane : canValuePanes) { if (canValuePane != null) { for (Entry<String, Gauge> gaugeEntry : canValuePane.getGaugeMap() .entrySet()) { Gauge gauge = gaugeEntry.getValue(); bind(gauge.valueProperty(), this.canProperties.get(gaugeEntry.getKey()), true); } } } if (dashBoardPane != null) { bind(dashBoardPane.getRpmGauge().valueProperty(), this.canProperties.get("RPM")); bind(dashBoardPane.rpmMax.valueProperty(), this.canProperties.get("RPM-max"), true); bind(dashBoardPane.rpmAvg.valueProperty(), this.canProperties.get("RPM-avg"), true); bind(dashBoardPane.getRpmSpeedGauge().valueProperty(), this.canProperties.get("RPMSpeed")); bind(dashBoardPane.rpmSpeedMax.valueProperty(), this.canProperties.get("RPMSpeed-max"), true); bind(dashBoardPane.rpmSpeedAvg.valueProperty(), this.canProperties.get("RPMSpeed-avg"), true); } if (clockPane != null) { ObservableValue<?> vehicleState = this.canProperties.get("vehicleState"); SimpleLongProperty msecsProperty = (SimpleLongProperty) this.canProperties .get("msecs"); if (vehicleState != null && msecsProperty != null) { msecsProperty.addListener((obs, oldValue, newValue) -> super.clockPane .updateMsecs(newValue, (State) vehicleState.getValue())); } } }
/** * bind the value to the valueTo * * @param value * @param valueTo * @param biDirectional */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected void bind(Property value, Property valueTo, boolean biDirectional) { if (valueTo != null) { if (debug && value.isBound()) LOGGER.log(Level.WARNING, "value is already bound"); if (biDirectional) value.bindBidirectional(valueTo); else value.bind(valueTo); } }
public StatusBarManager(StatusBar bar, ObservableValue<CodeArea> codeArea, Property<Boolean> overrideProperty) { this.bar = bar; this.area = codeArea; this.overrideProperty = overrideProperty; initBar(); initEvent(); }
public void setData(final Map<String, Property> data) { this.data = data; }
public static BooleanProperty toBoolean(Property<Boolean> p) { return andFinal(() -> new SimpleBooleanProperty(), np -> BidirectionalBinding.bind(np, p)); }
public static IntegerProperty toInteger(Property<Integer> p) { return andFinal(() -> new SimpleIntegerProperty(), np -> BidirectionalBinding.bindNumber(np, p)); }
public static DoubleProperty toDouble(Property<Double> p) { return andFinal(() -> new SimpleDoubleProperty(), np -> BidirectionalBinding.bindNumber(np, p)); }
public static LongProperty toLong(Property<Long> p) { return andFinal(() -> new SimpleLongProperty(), np -> BidirectionalBinding.bindNumber(np, p)); }
public static FloatProperty toFloat(Property<Float> p) { return andFinal(() -> new SimpleFloatProperty(), np -> BidirectionalBinding.bindNumber(np, p)); }
public static <T> ObjectProperty<T> toObject(Property<T> p) { return andFinal(() -> new SimpleObjectProperty<>(), np -> np.bindBidirectional(p)); }
private void unbind(NodeModel node) { Arrays.stream(AnimatableField.values()).forEach(f -> ModelFunctions.getDoubleProperty(node.getNode(), f).ifPresent(Property::unbind)); Arrays.stream(AnimatableField.values()).forEach(f -> ModelFunctions.getPaintProperty(node.getNode(), f).ifPresent(Property::unbind)); }
@Override public final ObservableList<Property<?>> getProperties() { return properties; }
public Binding(Property<T> uiProperty, Property<T> modelProperty) { this.uiProperty = uiProperty; this.modelProperty = modelProperty; }
public final Property<DataSource> sourceProperty() { return source; }
public final Property<DataSource<T>> typedSourceProperty() { return (Property) sourceProperty(); }
public final Property<N> numberProperty() { return number; }
public EditableLabel(Property<String> text) { this(); textProperty().bindBidirectional(text); }
@Test public void testSaveThrowsWithNullValue() { Property<?> property = new SimpleObjectProperty<>(null); assertThrows(IllegalArgumentException.class, () -> PreferencesUtils.save(property, preferences, null)); }