Search in sources :

Example 1 with BeanProperty

use of org.controlsfx.property.BeanProperty in project KNOBS by ESSICS.

the class KnobController method initialize.

@Override
public void initialize(URL location, ResourceBundle resources) {
    propertySheet.setPropertyEditorFactory(new KnobPropertyEditorFactory());
    long beforeTime = System.currentTimeMillis();
    Knob knob = KnobBuilder.create().onAdjusted(e -> LOGGER.info(MessageFormat.format("Current value reached target: {0}", ((Knob) e.getSource()).getCurrentValue()))).onTargetSet(e -> {
        LOGGER.info(MessageFormat.format("Target changed: {0}", ((Knob) e.getSource()).getTargetValue()));
        updateCurrentValue = true;
    }).build();
    long afterTime = System.currentTimeMillis();
    LOGGER.log(Level.INFO, "Construction time: {0,number,#########0}ms", afterTime - beforeTime);
    knobContainer.getChildren().add(knob);
    Map<Class<?>, String> categories = new HashMap<>(5);
    categories.put(Knob.class, "\u200BKnob");
    categories.put(Region.class, "\u200B\u200BRegion");
    categories.put(Parent.class, "\u200B\u200B\u200BParent");
    categories.put(Node.class, "\u200B\u200B\u200B\u200BNode");
    categories.put(Object.class, "\u200B\u200B\u200B\u200B\u200BObject");
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(Knob.class, Object.class);
        for (PropertyDescriptor p : beanInfo.getPropertyDescriptors()) {
            try {
                if (p.getReadMethod() != null && p.getWriteMethod() != null) {
                    p.setValue(BeanProperty.CATEGORY_LABEL_KEY, categories.get(p.getReadMethod().getDeclaringClass()));
                    propertySheet.getItems().add(new BeanProperty(knob, p));
                }
            } catch (Exception iex) {
                LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to handle property \"{0}\" [{1}].", p.getName(), iex.getMessage()));
            }
        }
    } catch (IntrospectionException ex) {
        LOGGER.log(Level.SEVERE, "Unable to initialize the controller.", ex);
    }
    propertySheet.setMode(PropertySheet.Mode.CATEGORY);
    timer.scheduleAtFixedRate(() -> {
        if (updateCurrentValue) {
            double step = (knob.getMaxValue() - knob.getMinValue()) / 234;
            double cValue = knob.getCurrentValue();
            double tValue = knob.getTargetValue();
            if (cValue < tValue) {
                Platform.runLater(() -> {
                    if ((tValue - cValue) > step) {
                        knob.setCurrentValue(cValue + step);
                    } else {
                        knob.setCurrentValue(tValue);
                        updateCurrentValue = false;
                    }
                });
            } else if (cValue > tValue) {
                Platform.runLater(() -> {
                    if ((cValue - tValue) > step) {
                        knob.setCurrentValue(cValue - step);
                    } else {
                        knob.setCurrentValue(tValue);
                        updateCurrentValue = false;
                    }
                });
            }
        }
    }, 2000, 50, TimeUnit.MILLISECONDS);
}
Also used : Initializable(javafx.fxml.Initializable) URL(java.net.URL) PropertySheet(org.controlsfx.control.PropertySheet) Node(javafx.scene.Node) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) Executors(java.util.concurrent.Executors) IntrospectionException(java.beans.IntrospectionException) MessageFormat(java.text.MessageFormat) Level(java.util.logging.Level) TimeUnit(java.util.concurrent.TimeUnit) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) Introspector(java.beans.Introspector) Parent(javafx.scene.Parent) Region(javafx.scene.layout.Region) ResourceBundle(java.util.ResourceBundle) FlowPane(javafx.scene.layout.FlowPane) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BeanProperty(org.controlsfx.property.BeanProperty) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) IntrospectionException(java.beans.IntrospectionException) BeanProperty(org.controlsfx.property.BeanProperty)

Aggregations

BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 Introspector (java.beans.Introspector)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 URL (java.net.URL)1 MessageFormat (java.text.MessageFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ResourceBundle (java.util.ResourceBundle)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Platform (javafx.application.Platform)1 FXML (javafx.fxml.FXML)1 Initializable (javafx.fxml.Initializable)1 Node (javafx.scene.Node)1 Parent (javafx.scene.Parent)1 FlowPane (javafx.scene.layout.FlowPane)1