package be.iminds.ilabt.jfed.experimenter_gui.ui.ribbon;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;

/**
 * User: twalcari
 * Date: 6/2/2015
 * Time: 15:53
 */
public class SplitRibbonButton extends RibbonButton {

    private final ObservableList<MenuItem> menuItems = FXCollections.observableArrayList();

    protected final StackPane arrow;
    protected final ContextMenu popup;


    {
        getStyleClass().add("split-ribbon-button");

        arrow = new StackPane();
        arrow.getStyleClass().setAll("arrow");
        arrow.setMaxWidth(Region.USE_PREF_SIZE);
        arrow.setMaxHeight(Region.USE_PREF_SIZE);


        secondLineContainer.setSpacing(3);
        secondLineContainer.getChildren().add(arrow);

        popup = new ContextMenu();
        popup.getScene().getStylesheets().add(RibbonBar.class.getResource(RibbonBar.STYLESHEET_URL).toExternalForm());

        popup.getItems().clear();


        ListChangeListener<MenuItem> itemsChangedListener = c -> {
            while (c.next()) {
                popup.getItems().removeAll(c.getRemoved());
                popup.getItems().addAll(c.getFrom(), c.getAddedSubList());
            }
        };
        menuItems.addListener(itemsChangedListener);

        setOnMousePressed(event -> popup.show(this, Side.BOTTOM,0,0));
    }

    public SplitRibbonButton() {
    }

    public SplitRibbonButton(String s) {
        super(s);
    }

    public SplitRibbonButton(String s, Node node) {
        super(s, node);
    }

    public ObservableList<MenuItem> getMenuItems() {
        return menuItems;
    }
}
