package org.hildan.fxlog.view;

import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.*;
import javafx.scene.text.Text;

import org.hildan.fxlog.coloring.Colorizer;
import org.hildan.fxlog.config.Config;
import org.hildan.fxlog.core.LogEntry;

/**
 * A table cell that can be styled.
 */
public class StyledTableCell extends TableCell<LogEntry, String> {

    private final Text text = new Text();

    private final Property<Colorizer> colorizer = new SimpleObjectProperty<>();

    public StyledTableCell(TableColumn<LogEntry, String> column) {
        setWrap(column, Config.getInstance().getWrapLogsText());
        Config.getInstance().wrapLogsTextProperty().addListener((obs, oldVal, newVal) -> setWrap(column, newVal));
        text.fontProperty().bind(Config.getInstance().logsFontProperty());
        setGraphic(text);
        setText(null);
    }

    private void setWrap(TableColumn<LogEntry, String> column, boolean wrap) {
        if (wrap) {
            text.wrappingWidthProperty().bind(column.widthProperty());
        } else {
            text.wrappingWidthProperty().unbind();
            text.setWrappingWidth(0); // disables wrapping
        }
    }

    @Override
    public void updateItem(String item, boolean empty) {
        //noinspection StringEquality
        if (item == getItem()) {
            return;
        }
        super.updateItem(item, empty);
        if (item == null) {
            setGraphic(null);
            text.setText(null);
            return;
        }
        setGraphic(text);
        text.setText(item);
        if (colorizer.getValue() != null) {
            TableRow row = getTableRow();
            if (row != null && row.getItem() != null) {
                LogEntry log = (LogEntry) row.getItem();
                colorizer.getValue().applyTo(text, log);
            }
        }
    }

    public Colorizer getColorizer() {
        return colorizer.getValue();
    }

    public Property<Colorizer> colorizerProperty() {
        return colorizer;
    }

    public void setColorizer(Colorizer colorizer) {
        this.colorizer.setValue(colorizer);
    }
}

@@ -1304,109 +1304,8 @@
 ) %7B%0A
-        //noinspection StringEquality%0A        if (item == getItem()) %7B%0A            return;%0A        %7D%0A
     
@@ -1351,16 +1351,25 @@
     if (
+empty %7C%7C 
 item == 
@@ -1410,40 +1410,8 @@
 l);%0A
-            text.setText(null);%0A
     

Fix color shifting