package by.post.control.ui;

import by.post.data.Cell;
import by.post.data.Row;
import by.post.data.Table;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Dmitriy V.Yefremov
 */
public class TableDataResolver {

    private Table table;
    private ObservableList tableColumns;
    private ObservableList<ObservableList> items;

    public TableDataResolver() {
    }

    public TableDataResolver(Table table) {
        this.table = table;
        resolve();
    }

    public void setTable(Table table) {
        this.table = table;
    }

    public ObservableList getTableColumns() {
        return tableColumns;
    }

    public ObservableList getItems() {
        return items;
    }

    private void resolve() {
        List<Row> rows = table.getRows();
        List<Cell> values;
        tableColumns = FXCollections.observableArrayList();
        items = FXCollections.observableArrayList();

        if (rows != null && !rows.isEmpty()) {
            values = rows.get(0).getCells();
            doRow(values);
        }
    }

    /**
     * @param values
     */
    private void doRow(List<Cell> values) {
        // *** Row!!!! ***
        ObservableList<String> row = FXCollections.observableArrayList();

        for (Cell cell: values) {
            final int index = values.indexOf(cell);
            TableColumn column = new TableColumn(cell.getName());

            column.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
                public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(index).toString());
                }
            });

            tableColumns.addAll(column);
            // null поля будем выводить как пустые ("")
            row.add(cell.getValue() != null ? cell.getValue().toString() : "");
        }
        items.add(row);
    }

}

@@ -104,59 +104,8 @@
 le;%0A
-import javafx.beans.property.SimpleObjectProperty;%0A
 impo
@@ -336,131 +336,23 @@
 afx.
-scene.control.cell.PropertyValueFactory;%0Aimport javafx.util.Callback;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;
+util.Callback;%0A
 %0Aimp
@@ -920,24 +920,71 @@
 ems;%0A    %7D%0A%0A
+    /**%0A     * Resolve data from table%0A     */%0A
     private 
@@ -1046,35 +1046,8 @@
 ();%0A
-        List%3CCell%3E values;%0A
     
@@ -1219,201 +1219,175 @@
     
-values = rows.get(0).getCells();%0A            doRow(values);%0A        %7D%0A    %7D%0A%0A    /**%0A     * @param values
+// Add columns by first row in table%0A            doColumns(rows.get(0).getCells());%0A            // Add data
 %0A     
-*/%0A
     
-private void doRow(List%3CCell%3E values) %7B%0A        // *** Row!!!! ***%0A
+   rows.stream().forEach(row -%3E %7B%0A          
       
-  
 Obse
@@ -1405,17 +1405,20 @@
 String%3E 
-r
+newR
 ow = FXC
@@ -1455,40 +1455,399 @@
 ();%0A
-%0A        for (Cell cell: values)
+                row.getCells().forEach(cell-%3E %7B%0A                    newRow.add(cell.getValue() != null ? cell.getValue().toString() : %22%22);%0A                %7D);%0A                items.add(newRow);%0A            %7D);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Add columns by first row in table%0A     *%0A     * @param values%0A     */%0A    private void doColumns(List%3CCell%3E values) %7B%0A%0A        values.forEach(cell -%3E
  %7B%0A 
@@ -2201,21 +2201,20 @@
 String%3E 
-param
+cell
 ) %7B%0A    
@@ -2261,21 +2261,20 @@
 roperty(
-param
+cell
 .getValu
@@ -2389,167 +2389,9 @@
     
-    // null %D0%BF%D0%BE%D0%BB%D1%8F %D0%B1%D1%83%D0%B4%D0%B5%D0%BC %D0%B2%D1%8B%D0%B2%D0%BE%D0%B4%D0%B8%D1%82%D1%8C %D0%BA%D0%B0%D0%BA %D0%BF%D1%83%D1%81%D1%82%D1%8B%D0%B5 (%22%22)%0A            row.add(cell.getValue() != null ? cell.getValue().toString() : %22%22);%0A        %7D%0A        items.add(row
+%7D
 );%0A 

change the filling of the table data