From 8e19eb366ebdcb88df9f9455779fd71c961d2f51 Mon Sep 17 00:00:00 2001 From: firm1 Date: Wed, 9 Mar 2016 14:25:09 +0100 Subject: [PATCH] fix #29 repare table input tools --- .../zestwriter/view/MdConvertController.java | 12 +-- .../zestwriter/view/TableController.java | 81 ++++++++++++++----- 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/src/com/zestedesavoir/zestwriter/view/MdConvertController.java b/src/com/zestedesavoir/zestwriter/view/MdConvertController.java index 64ad85037..49ac181c5 100644 --- a/src/com/zestedesavoir/zestwriter/view/MdConvertController.java +++ b/src/com/zestedesavoir/zestwriter/view/MdConvertController.java @@ -112,7 +112,7 @@ private void HandleQuoteButtonAction(ActionEvent event) { @FXML private void HandleTableButtonAction(ActionEvent event) throws IOException { // Create the custom dialog. - Dialog>> dialog = new Dialog<>(); + Dialog>> dialog = new Dialog<>(); dialog.setTitle("Editeur de tableau"); dialog.setHeaderText(""); @@ -125,7 +125,7 @@ private void HandleTableButtonAction(ActionEvent event) throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("TableEditor.fxml")); BorderPane tableEditor = loader.load(); - TableView tbView = (TableView) tableEditor.getCenter(); + TableView tbView = (TableView) tableEditor.getCenter(); TableController controller = loader.getController(); controller.setEditor(this); @@ -139,10 +139,10 @@ private void HandleTableButtonAction(ActionEvent event) throws IOException { return null; }); - Optional>> result = dialog.showAndWait(); + Optional>> result = dialog.showAndWait(); result.ifPresent(datas -> { - String[][] data = new String[datas.getValue().size()][datas.getValue().get(0).size()]; + String[][] data = new String[datas.getValue().size()][datas.getValue().get(0).getRow().size()]; String[] headers = new String[datas.getKey().size()]; int cpt = 0; for (Object key : datas.getKey()) { @@ -151,8 +151,8 @@ private void HandleTableButtonAction(ActionEvent event) throws IOException { } for (int i = 0; i < datas.getValue().size(); i++) { - for (int j = 0; j < datas.getValue().get(i).size(); j++) { - data[i][j] = (String) datas.getValue().get(i).get(j); + for (int j = 0; j < datas.getValue().get(i).getRow().size(); j++) { + data[i][j] = (String) datas.getValue().get(i).getRow().get(j); } } String tablestring = FlipTable.of(headers, data); diff --git a/src/com/zestedesavoir/zestwriter/view/TableController.java b/src/com/zestedesavoir/zestwriter/view/TableController.java index 054e0cc03..126cc6da7 100644 --- a/src/com/zestedesavoir/zestwriter/view/TableController.java +++ b/src/com/zestedesavoir/zestwriter/view/TableController.java @@ -1,5 +1,9 @@ package com.zestedesavoir.zestwriter.view; +import java.util.ArrayList; +import java.util.List; + +import javafx.beans.property.SimpleListProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; @@ -15,13 +19,43 @@ import javafx.scene.control.cell.TextFieldTableCell; import javafx.util.Callback; -import java.util.List; +class ZRow { + private SimpleListProperty row = new SimpleListProperty<>(); + + public ZRow() { + super(); + } + + public ZRow(int n) { + super(); + List lst = new ArrayList<>(); + for(int i=0; i observableList = FXCollections.observableArrayList(lst); + this.row = new SimpleListProperty(observableList); + + } + + public ZRow(ObservableList row) { + super(); + this.row.set(row); + } + + public List getRow() { + return row.get(); + } + public void setRow(ObservableList row) { + this.row.set(row); + } + +} public class TableController { private MdConvertController editorController; @FXML - private TableView tableView; - private ObservableList datas = FXCollections.observableArrayList(); + private TableView tableView; + private final ObservableList datas = FXCollections.observableArrayList(new ZRow(1)); public void setEditor(MdConvertController editorController) { this.editorController = editorController; @@ -30,11 +64,11 @@ public void setEditor(MdConvertController editorController) { @FXML private void initialize() { + tableView.setItems(datas); + addCol(); addRow(); - - tableView.setItems(datas); } @FXML @@ -50,45 +84,52 @@ private void HandleAddColumnButtonAction(ActionEvent event) { private void addCol() { TableColumn tc = new TableColumn(); tc.setEditable(true); - tc.setCellValueFactory(new Callback, ObservableValue>() { - public ObservableValue call(CellDataFeatures param) { - return new SimpleStringProperty(param.getValue().get(0).toString()); + tc.setCellValueFactory(new Callback, ObservableValue>() { + public ObservableValue call(CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getRow().get(0).toString()); } }); + tc.setCellFactory(TextFieldTableCell.forTableColumn()); - tc.setOnEditCommit(new EventHandler>() { + tc.setOnEditCommit(new EventHandler>() { @Override - public void handle(CellEditEvent t) { - - List row = t.getTableView().getItems().get(t.getTablePosition().getRow()); + public void handle(CellEditEvent t) { + List row = t.getTableView().getItems().get(t.getTablePosition().getRow()).getRow(); row.set(t.getTablePosition().getColumn(), t.getNewValue()); } }); tc.setPrefWidth(100); TextField txf = new TextField(); txf.setPrefWidth(100); - txf.setPromptText("Colonne 1"); + txf.setPromptText("Colonne "+(tableView.getColumns().size()+1)); tc.setGraphic(txf); tableView.getColumns().addAll(tc); postAddColumn(); } private void addRow() { - ObservableList row = FXCollections.observableArrayList(); - for (Object header : tableView.getColumns()) { - row.add("-"); - } + ZRow row = new ZRow(tableView.getColumns().size()); datas.add(row); } private void postAddColumn() { - for (ObservableList data : datas) { + for (ZRow data : datas) { for (int i = 0; i < tableView.getColumns().size(); i++) { - if (data.size() <= i) { - data.add(i, "-"); + if(data.getRow() != null) { + if (data.getRow().size() <= i) { + data.getRow().add(i, " - "); + } } } } } + + public String toString() { + StringBuilder sb = new StringBuilder(); + for(ZRow data:datas) { + sb.append(data.getRow()).append("\n"); + } + return sb.toString(); + } }