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

xworker.javafx.beans.property.SimpleListPropertyActions Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package xworker.javafx.beans.property;

import javafx.beans.property.ListProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ObservableListValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.xmeta.ActionContext;
import org.xmeta.Thing;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class SimpleListPropertyActions {
    public static SimpleListProperty create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        Object bean = self.doAction("getBean", actionContext);
        String name = self.getMetadata().getName();
        Object value = self.doAction("getInitialValue", actionContext);;
        ObservableList initialValue = null;
        if(value instanceof ObservableList){
            initialValue = (ObservableList) value;
        }else if(initialValue instanceof List){
            initialValue = FXCollections.observableList((List) value);
        }else {
            initialValue = FXCollections.observableList(new ArrayList<>());
        }

        SimpleListProperty property = new SimpleListProperty(bean, name, initialValue);
        actionContext.g().put(name, property);

        actionContext.peek().put("parent", property);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        return property;
    }

    public static void createBind(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ListProperty parent = actionContext.getObject("parent");

        ObservableListValue value = self.doAction("getValue", actionContext);
        if(value != null) {
            parent.bind(value);
        }
    }

    public static void createDindBidirectional(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ListProperty parent = actionContext.getObject("parent");

        Property> other = self.doAction("getProperty", actionContext);
        if(other != null) {
            parent.bindBidirectional(other);
        }
    }
}