All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.marid.jfx.props.Props Maven / Gradle / Ivy

There is a newer version: 0.9.8.10
Show newest version
/*-
 * #%L
 * marid-fx
 * %%
 * Copyright (C) 2012 - 2017 MARID software development group
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 * #L%
 */

package org.marid.jfx.props;

import javafx.beans.property.*;
import javafx.beans.value.WritableObjectValue;
import javafx.event.Event;
import javafx.event.EventHandler;

import org.jetbrains.annotations.NotNull;
import java.util.function.*;
import java.util.prefs.Preferences;

/**
 * @author Dmitry Ovchinnikov
 */
public interface Props {

  static StringProperty stringProp(Supplier supplier, Consumer consumer) {
    final StringProperty property = new SimpleStringProperty(supplier.get());
    property.addListener((observable, oldValue, newValue) -> consumer.accept(newValue));
    return property;
  }

  static BooleanProperty boolProp(BooleanSupplier supplier, Consumer consumer) {
    final BooleanProperty property = new SimpleBooleanProperty(supplier.getAsBoolean());
    property.addListener((observable, oldValue, newValue) -> consumer.accept(newValue));
    return property;
  }

  static IntegerProperty intProp(IntSupplier supplier, IntConsumer consumer) {
    final IntegerProperty property = new SimpleIntegerProperty(supplier.getAsInt());
    property.addListener((observable, oldValue, newValue) -> consumer.accept(newValue.intValue()));
    return property;
  }

  static  ObjectProperty prop(Supplier supplier, Consumer consumer) {
    final ObjectProperty property = new SimpleObjectProperty<>(supplier.get());
    property.addListener((observable, oldValue, newValue) -> consumer.accept(newValue));
    return property;
  }

  static  WritableObjectValue value(Supplier supplier, Consumer consumer) {
    return new WritableValueImpl<>(consumer, supplier);
  }

  static WritableObjectValue string(@NotNull Preferences node, @NotNull String key, String defaultValue) {
    return value(() -> node.get(key, defaultValue), v -> {
      if (v == null || v.isEmpty()) {
        node.remove(key);
      } else {
        node.put(key, v);
      }
    });
  }

  static  void addHandler(Property> property, EventHandler handler) {
    final EventHandler old = property.getValue();
    if (old == null) {
      property.setValue(handler);
    } else {
      property.setValue(event -> {
        old.handle(event);
        handler.handle(event);
      });
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy