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

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

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

import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
import javafx.util.StringConverter;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.util.JavaFXUtils;
import xworker.javafx.util.converter.ThingValueConverter;

import java.util.Iterator;
import java.util.List;

public class ComboBoxActions {
	static{
		PropertyFactory.regist(ComboBox.class, "itemsProperty", o -> {
			ComboBox obj = (ComboBox) o;
			return obj.itemsProperty();
		});
		PropertyFactory.regist(ComboBox.class, "placeholderProperty", o -> {
			ComboBox obj = (ComboBox) o;
			return obj.placeholderProperty();
		});
		PropertyFactory.regist(ComboBox.class, "cellFactoryProperty", o -> {
			ComboBox obj = (ComboBox) o;
			return obj.cellFactoryProperty();
		});
		PropertyFactory.regist(ComboBox.class, "visibleRowCountProperty", o -> {
			ComboBox obj = (ComboBox) o;
			return obj.visibleRowCountProperty();
		});
		PropertyFactory.regist(ComboBox.class, "buttonCellProperty", o -> {
			ComboBox obj = (ComboBox) o;
			return obj.buttonCellProperty();
		});
		PropertyFactory.regist(ComboBox.class, "converterProperty", o -> {
			ComboBox obj = (ComboBox) o;
			return obj.converterProperty();
		});
		PropertyFactory.regist(ComboBox.class, "selectionModelProperty", o -> {
			ComboBox obj = (ComboBox) o;
			return obj.selectionModelProperty();
		});
	}

	public static void init(ComboBox node, Thing thing, ActionContext actionContext) {
		ComboBoxBaseActions.init(node, thing, actionContext);

		if(thing.valueExists("value")){
			Object value = JavaFXUtils.getObject(thing, "value", actionContext);
			if(value != null){
				node.setValue(value);
			}
		}
		if(thing.valueExists("converter")){
			StringConverter converter = JavaFXUtils.getObject(thing, "converter", actionContext);
			if(converter != null){
				node.setConverter(converter);
			}
		}
		if(thing.valueExists("visibleRowCount")) {
			node.setVisibleRowCount(thing.getInt("visibleRowCount"));
		}
		if(thing.valueExists("buttonCell")){
			ListCell buttonCell = JavaFXUtils.getObject(thing, "buttonCell", actionContext);
			if(buttonCell != null) {
				node.setButtonCell(buttonCell);
			}
		}
		if(thing.valueExists("cellFactory")){
			Callback,ListCell> cellFactory = JavaFXUtils.getObject(thing, "cellFactory", actionContext);
			if(cellFactory != null) {
				node.setCellFactory(cellFactory);
			}
		}
		if(thing.valueExists("items")){
			Object obj = JavaFXUtils.getObject(thing, "items", actionContext);
			if (obj instanceof ObservableList) {
				node.setItems((ObservableList) obj);
			} else if(obj instanceof Iterable){
				Iterable items = (Iterable) obj;
				Iterator iter = items.iterator();
				while (iter.hasNext()) {
					node.getItems().add(iter.next());
				}
			}
		}
		if(thing.valueExists("placeholder")){
			Node placeholder = JavaFXUtils.getObject(thing, "placeholder", actionContext);
			if(placeholder != null) {
				node.setPlaceholder(placeholder);
			}
		}
	}
	
	public static ComboBox create(ActionContext actionContext){
		Thing self = actionContext.getObject("self");
		
		ComboBox box = new ComboBox();
		init(box, self, actionContext);
		actionContext.g().put(self.getMetadata().getName(), box);
		
		actionContext.peek().put("parent", box);
		for(Thing child : self.getChilds()) {
			Object obj  = child.doAction("create", actionContext);
			if(obj instanceof StringConverter){
				box.setConverter((StringConverter) obj);
			}
		}

		return box;
	}
	
	public static void createThingItems(ActionContext actionContext) {
		Thing self = actionContext.getObject("self");
		ComboBox parent = actionContext.getObject("parent");
		
		List items = self.getChilds("Item");
		parent.setConverter(new ThingValueConverter(items));
		
		parent.getItems().addAll(items);
	}

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

		for(Thing child : self.getChilds()){
			Object obj = child.doAction("create", actionContext);
			if(obj instanceof ListCell){
				parent.setButtonCell((ListCell) obj);
				return;
			}
		}
	}

	public static void createCellFactory(ActionContext actionContext){
		Thing self = actionContext.getObject("self");
		ComboBox parent = actionContext.getObject("parent");
		parent.setCellFactory(new ListCellFactory(self, actionContext));
	}

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

		return null;
	}

	public static class ListCellFactory implements Callback, ListCell> {
		Thing thing;
		ActionContext actionContext;

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

		@Override
		public ListCell call(ListView param) {
			return thing.doAction("createCell", actionContext, "param", param);
		}
	}


}