Skip to content

Commit

Permalink
fix #29 repare table input tools
Browse files Browse the repository at this point in the history
  • Loading branch information
firm1 committed Mar 9, 2016
1 parent 90bccb8 commit 8e19eb3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/com/zestedesavoir/zestwriter/view/MdConvertController.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void HandleQuoteButtonAction(ActionEvent event) {
@FXML
private void HandleTableButtonAction(ActionEvent event) throws IOException {
// Create the custom dialog.
Dialog<Pair<ObservableList, ObservableList<ObservableList>>> dialog = new Dialog<>();
Dialog<Pair<ObservableList, ObservableList<ZRow>>> dialog = new Dialog<>();
dialog.setTitle("Editeur de tableau");
dialog.setHeaderText("");

Expand All @@ -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<ZRow> tbView = (TableView) tableEditor.getCenter();

TableController controller = loader.getController();
controller.setEditor(this);
Expand All @@ -139,10 +139,10 @@ private void HandleTableButtonAction(ActionEvent event) throws IOException {
return null;
});

Optional<Pair<ObservableList, ObservableList<ObservableList>>> result = dialog.showAndWait();
Optional<Pair<ObservableList, ObservableList<ZRow>>> 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()) {
Expand All @@ -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);
Expand Down
81 changes: 61 additions & 20 deletions src/com/zestedesavoir/zestwriter/view/TableController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,13 +19,43 @@
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.Callback;

import java.util.List;
class ZRow {
private SimpleListProperty<String> row = new SimpleListProperty<>();

public ZRow() {
super();
}

public ZRow(int n) {
super();
List<String> lst = new ArrayList<>();
for(int i=0; i<n; i++) {
lst.add(" - ");
}
ObservableList<String> observableList = FXCollections.observableArrayList(lst);
this.row = new SimpleListProperty<String>(observableList);

}

public ZRow(ObservableList<String> row) {
super();
this.row.set(row);
}

public List<String> getRow() {
return row.get();
}

public void setRow(ObservableList<String> row) {
this.row.set(row);
}

}
public class TableController {
private MdConvertController editorController;
@FXML
private TableView tableView;
private ObservableList<ObservableList> datas = FXCollections.observableArrayList();
private TableView<ZRow> tableView;
private final ObservableList<ZRow> datas = FXCollections.observableArrayList(new ZRow(1));

public void setEditor(MdConvertController editorController) {
this.editorController = editorController;
Expand All @@ -30,11 +64,11 @@ public void setEditor(MdConvertController editorController) {
@FXML
private void initialize() {

tableView.setItems(datas);

addCol();

addRow();

tableView.setItems(datas);
}

@FXML
Expand All @@ -50,45 +84,52 @@ private void HandleAddColumnButtonAction(ActionEvent event) {
private void addCol() {
TableColumn tc = new TableColumn();
tc.setEditable(true);
tc.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(0).toString());
tc.setCellValueFactory(new Callback<CellDataFeatures<ZRow, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ZRow, String> param) {
return new SimpleStringProperty(param.getValue().getRow().get(0).toString());
}
});

tc.setCellFactory(TextFieldTableCell.forTableColumn());
tc.setOnEditCommit(new EventHandler<CellEditEvent<ObservableList, String>>() {
tc.setOnEditCommit(new EventHandler<CellEditEvent<ZRow, String>>() {
@Override
public void handle(CellEditEvent<ObservableList, String> t) {

List row = t.getTableView().getItems().get(t.getTablePosition().getRow());
public void handle(CellEditEvent<ZRow, String> t) {
List<String> 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<String> 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();
}
}

0 comments on commit 8e19eb3

Please sign in to comment.