/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dua3.meja.ui.javafx;

import com.dua3.meja.model.Cell;
import java.util.Optional;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringPropertyBase;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.StringPropertyBase;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;
import org.controlsfx.control.spreadsheet.SpreadsheetCell;
import org.controlsfx.control.spreadsheet.SpreadsheetCellType;

/**
 *
 * @author a5xysq1
 */
public class SpreadSheetCellImpl implements SpreadsheetCell {

    private final Cell cell;
    private final ObjectProperty<Node> graphic = new SimpleObjectProperty<>();

    private final StringProperty STYLE_PROPERTY = new StringPropertyBase() {
        @Override
        public Object getBean() {
            return cell;
        }

        @Override
        public String getName() {
            return "style";
        }
    };

    private final ReadOnlyStringProperty TEXT_PROPERTY = new ReadOnlyStringPropertyBase() {
        @Override
        public String get() {
            return cell.getAsText();
        }

        @Override
        public Object getBean() {
            return cell;
        }

        @Override
        public String getName() {
            return "text";
        }
    };

    public SpreadSheetCellImpl(Cell cell) {
        this.cell = cell;
    }

    @Override
    public boolean match(SpreadsheetCell cell) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setItem(Object value) {
        cell.set(value);
    }

    @Override
    public Object getItem() {
        // FIXME
        return cell.getAsText();
    }

    @Override
    public ObjectProperty<Object> itemProperty() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean isEditable() {
        // FIXME
        return true;
    }

    @Override
    public void setEditable(boolean editable) {
        // FIXME
    }

    @Override
    public boolean isWrapText() {
        return cell.getCellStyle().isWrap();
    }

    @Override
    public void setWrapText(boolean wrapText) {
        cell.getCellStyle().setWrap(wrapText);
    }

    @Override
    public void setStyle(String style) {
        cell.setStyle(style);
    }

    @Override
    public String getStyle() {
        return cell.getCellStyle().getName();
    }

    @Override
    public StringProperty styleProperty() {
        return STYLE_PROPERTY;
    }

    @Override
    public void activateCorner(CornerPosition position) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void deactivateCorner(CornerPosition position) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean isCornerActivated(CornerPosition position) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public StringProperty formatProperty() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String getFormat() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setFormat(String format) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ReadOnlyStringProperty textProperty() {
        return TEXT_PROPERTY;
    }

    @Override
    public String getText() {
        return cell.getAsText();
    }

    @Override
    public SpreadsheetCellType getCellType() {
        switch (cell.getCellType()) {
            case NUMERIC:
                return SpreadsheetCellType.DOUBLE;
            default:
                return SpreadsheetCellType.OBJECT;
        }
    }

    @Override
    public int getRow() {
        return cell.getRowNumber();
    }

    @Override
    public int getColumn() {
        return cell.getColumnNumber();
    }

    @Override
    public int getRowSpan() {
        return cell.getHorizontalSpan();
    }

    @Override
    public void setRowSpan(int rowSpan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int getColumnSpan() {
        return cell.getVerticalSpan();
    }

    @Override
    public void setColumnSpan(int columnSpan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ObservableSet<String> getStyleClass() {
        // FIXME
        return FXCollections.emptyObservableSet();
    }

    @Override
    public ObjectProperty<Node> graphicProperty() {
        return graphic;
    }

    @Override
    public void setGraphic(Node graphic) {
        this.graphic.setValue(graphic);
    }

    @Override
    public Node getGraphic() {
        return graphic.getValue();
    }

    @Override
    public Optional<String> getTooltip() {
        return Optional.empty(); // FIXME
    }

    @Override
    public void addEventHandler(EventType<Event> eventType, EventHandler<Event> eventHandler) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void removeEventHandler(EventType<Event> eventType, EventHandler<Event> eventHandler) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

@@ -249,24 +249,74 @@
 odel.Cell;%0D%0A
+import com.sun.javafx.event.EventHandlerManager;%0D%0A
 import java.
@@ -797,24 +797,65 @@
 ent.Event;%0D%0A
+import javafx.event.EventDispatchChain;%0D%0A
 import javaf
@@ -1187,24 +1187,118 @@
 Cell cell;%0D%0A
+    private final EventHandlerManager eventHandlerManager = new EventHandlerManager(this);%0D%0A%0D%0A
     private 
@@ -3986,135 +3986,39 @@
     
-throw new UnsupportedOperationException(%22Not supported yet.%22); //To change body of generated methods, choose Tools %7C Templates.
+// FIXME%0D%0A        return false;
 %0D%0A  
@@ -6576,135 +6576,70 @@
     
-throw new UnsupportedOperationException(%22Not supported yet.%22); //To change body of generated methods, choose Tools %7C Templates.
+ eventHandlerManager.addEventHandler(eventType, eventHandler);
 %0D%0A  
@@ -6772,135 +6772,73 @@
     
-throw new UnsupportedOperationException(%22Not supported yet.%22); //To change body of generated methods, choose Tools %7C Templates.
+ eventHandlerManager.removeEventHandler(eventType, eventHandler);
 %0D%0A  

implement SheetView for JavaFX (wip)