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

xworker.javafx.control.TreeViewActions Maven / Gradle / Ivy

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

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.*;
import javafx.util.Callback;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.util.JavaFXUtils;
import xworker.javafx.util.ThingCallback;

public class TreeViewActions {
    static{
        PropertyFactory.regist(TreeView.class, "onEditCommitProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.onEditCommitProperty();
        });
        PropertyFactory.regist(TreeView.class, "onEditCancelProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.onEditCancelProperty();
        });
        PropertyFactory.regist(TreeView.class, "fixedCellSizeProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.fixedCellSizeProperty();
        });
        PropertyFactory.regist(TreeView.class, "onEditStartProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.onEditStartProperty();
        });
        PropertyFactory.regist(TreeView.class, "rootProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.rootProperty();
        });
        PropertyFactory.regist(TreeView.class, "cellFactoryProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.cellFactoryProperty();
        });
        PropertyFactory.regist(TreeView.class, "selectionModelProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.selectionModelProperty();
        });
        PropertyFactory.regist(TreeView.class, "editableProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.editableProperty();
        });
        PropertyFactory.regist(TreeView.class, "onScrollToProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.onScrollToProperty();
        });
        PropertyFactory.regist(TreeView.class, "focusModelProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.focusModelProperty();
        });
        PropertyFactory.regist(TreeView.class, "showRootProperty", o -> {
            TreeView obj = (TreeView) o;
            return obj.showRootProperty();
        });
    }

    public static void init(TreeView node, Thing thing, ActionContext actionContext){
        ControlActions.init(node, thing, actionContext);

        if(thing.valueExists("cellFactory")){
            Callback, TreeCell> cellFactory = JavaFXUtils.getObject(thing, "cellFactory", actionContext);
            if(cellFactory != null) {
                node.setCellFactory(cellFactory);
            }
        }
        if(thing.valueExists("editable")){
            node.setEditable(thing.getBoolean("editable"));
        }
        if(thing.valueExists("fixedCellSize")){
            node.setFixedCellSize(thing.getDouble("fixedCellSize"));
        }
        if(thing.valueExists("focusModel")){
            FocusModel> focusModel = JavaFXUtils.getObject(thing, "focusModel", actionContext);
            if(focusModel != null) {
                node.setFocusModel(focusModel);
            }
        }
        if(thing.valueExists("root")){
            TreeItem root = JavaFXUtils.getObject(thing, "root", actionContext);
            if(root != null) {
                node.setRoot(root);
            }
        }
        if(thing.valueExists("selectionModel")){
            if("MULTIPLE".equals(thing.getString("selectionModel"))){
                node.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
            }else{
                node.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            }
        }
        if(thing.valueExists("showRoot")){
            node.setShowRoot(thing.getBoolean("showRoot"));
        }
    }

    public static TreeView create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        TreeView node = new TreeView();
        init(node, self, actionContext);
        actionContext.g().put(self.getMetadata().getName(), node);

        actionContext.peek().put("parent", node);
        for(Thing child : self.getChilds()) {
            Object obj = child.doAction("create", actionContext);
            if (obj instanceof TreeItem) {
                node.setRoot((TreeItem) obj);
            }
        }

        node.getSelectionModel().selectedItemProperty().addListener(new ChangeListener>() {
            @Override
            public void changed(ObservableValue> observable, TreeItem oldValue, TreeItem newValue) {

            }
        });

        return node;
    }

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

        parent.setCellFactory(new ThingCallback<>(self, actionContext));
    }

    public static Object defaultCreateCell(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        for(Thing  child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof TreeCell){
                return obj;
            }
        }

        return null;
    }

    public static void createSelectionChangeListener(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Object parent = actionContext.getObject("parent");
        if(parent instanceof TreeView){
            ((TreeView) parent).getSelectionModel().selectedItemProperty().addListener(new ThingChangeListener(self, actionContext));
        }else if(parent instanceof TableView){
            ((TableView) parent).getSelectionModel().selectedItemProperty().addListener(new ThingChangeListener(self, actionContext));
        }else if(parent instanceof TreeTableView){
            ((TreeTableView) parent).getSelectionModel().selectedItemProperty().addListener(new ThingChangeListener(self, actionContext));
        }else if(parent instanceof ListView){
            ((ListView) parent).getSelectionModel().selectedItemProperty().addListener(new ThingChangeListener(self, actionContext));
        }else if(parent instanceof ComboBox){
            ((ComboBox) parent).getSelectionModel().selectedItemProperty().addListener(new ThingChangeListener(self, actionContext));
        }else if(parent instanceof ChoiceBox){
            ((ChoiceBox) parent).getSelectionModel().selectedItemProperty().addListener(new ThingChangeListener(self, actionContext));
        }
    }

    public static class ThingChangeListener implements  ChangeListener{
        Thing thing;
        ActionContext actionContext;

        public ThingChangeListener(Thing thing, ActionContext actionContext){
            this.thing = thing;
            this.actionContext = actionContext;
        }

        @Override
        public void changed(ObservableValue observable, T oldValue, T newValue) {
            thing.doAction("changed", actionContext, "observable", observable,
                    "oldValue", oldValue, "newValue", newValue);
        }
    }
}