From 2f75c3e14a2c058a8e3e5e1b36d0f588c11885c6 Mon Sep 17 00:00:00 2001 From: Brendan Robert Date: Tue, 5 Jul 2016 02:53:22 -0500 Subject: [PATCH] Most of sheet implementation has been done, still a little left to do though. --- .../ui/MythosScriptEditorController.java | 11 +- .../ui/SheetEditorController.java | 10 +- .../badvision/outlaweditor/ui/UIAction.java | 16 ++- .../ui/impl/SheetEditorControllerImpl.java | 134 +++++++++++++++++- .../src/main/resources/SheetEditor.fxml | 26 ++-- 5 files changed, 169 insertions(+), 28 deletions(-) diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/MythosScriptEditorController.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/MythosScriptEditorController.java index aa691e7d..52e2eb34 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/MythosScriptEditorController.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/MythosScriptEditorController.java @@ -116,16 +116,7 @@ public class MythosScriptEditorController }); editorView.getEngine().setPromptHandler((PromptData prompt) -> { - TextInputDialog dialog = new TextInputDialog(prompt.getDefaultValue()); - dialog.setTitle("MythosScript Editor"); - dialog.setHeaderText("Respond and press OK, or Cancel to abort"); - ImageView graphic = new ImageView(new Image("images/revolver_icon.png")); - graphic.setFitHeight(50.0); - graphic.setFitWidth(50.0); - graphic.setSmooth(true); - dialog.setGraphic(graphic); - dialog.setContentText(prompt.getMessage()); - return dialog.showAndWait().orElse(""); + return UIAction.getText(prompt.getMessage(), prompt.getDefaultValue()); }); } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/SheetEditorController.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/SheetEditorController.java index b959ff99..11fa667a 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/SheetEditorController.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/SheetEditorController.java @@ -14,12 +14,14 @@ package org.badvision.outlaweditor.ui; import java.net.URL; +import java.util.Map; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.TableColumn; -import org.badvision.outlaweditor.data.xml.Sheet; +import javafx.scene.control.TableView; +import org.badvision.outlaweditor.data.xml.Rows.Row; import org.badvision.outlaweditor.data.xml.UserType; /** @@ -34,9 +36,15 @@ public abstract class SheetEditorController implements Initializable { @FXML protected TableColumn addColumn; + + @FXML + protected TableView table; @FXML abstract public void addColumnAction(ActionEvent event); + + @FXML + abstract public void addRowAction(ActionEvent event); @FXML protected void initialize() { diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java index 28a1eef7..262794ff 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java @@ -36,6 +36,7 @@ import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; +import javafx.scene.control.TextInputDialog; import javafx.scene.control.cell.ComboBoxTableCell; import javafx.scene.control.cell.TextFieldTableCell; import javafx.scene.effect.BlurType; @@ -88,7 +89,7 @@ import org.badvision.outlaweditor.ui.impl.ImageConversionWizardController; public class UIAction { private static File currentSaveFile; - + public static enum MAIN_ACTIONS { _General, @@ -246,6 +247,19 @@ public class UIAction { dialogStage.show(); } + static public String getText(String message, String defaultValue) { + TextInputDialog dialog = new TextInputDialog(defaultValue); + dialog.setTitle("MythosScript Editor"); + dialog.setHeaderText("Respond and press OK, or Cancel to abort"); + ImageView graphic = new ImageView(new Image("images/revolver_icon.png")); + graphic.setFitHeight(50.0); + graphic.setFitWidth(50.0); + graphic.setSmooth(true); + dialog.setGraphic(graphic); + dialog.setContentText(message); + return dialog.showAndWait().orElse(""); + } + public static Script createAndEditScript(Scope scope) { Script script = new Script(); script.setName("New Script"); diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/SheetEditorControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/SheetEditorControllerImpl.java index f0dca7a7..84d42d73 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/SheetEditorControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/SheetEditorControllerImpl.java @@ -16,13 +16,30 @@ package org.badvision.outlaweditor.ui.impl; import java.net.URL; +import java.util.Map; +import java.util.Optional; import java.util.ResourceBundle; +import java.util.function.Consumer; +import javafx.beans.property.SimpleObjectProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.event.ActionEvent; +import javafx.scene.control.ContextMenu; +import javafx.scene.control.MenuItem; +import javafx.scene.control.TableColumn; +import javafx.scene.control.cell.TextFieldTableCell; +import javax.xml.namespace.QName; import org.badvision.outlaweditor.SheetEditor; +import org.badvision.outlaweditor.data.xml.Columns; +import org.badvision.outlaweditor.data.xml.Rows.Row; +import org.badvision.outlaweditor.data.xml.UserType; import org.badvision.outlaweditor.ui.SheetEditorController; +import org.badvision.outlaweditor.ui.UIAction; + public class SheetEditorControllerImpl extends SheetEditorController { private SheetEditor editor; + private ObservableList tableData; /** * Initializes the controller class. @@ -30,16 +47,127 @@ public class SheetEditorControllerImpl extends SheetEditorController { @Override public void initialize(URL url, ResourceBundle rb) { super.initialize(); - } + tableData = FXCollections.observableArrayList(); + table.setItems(tableData); + table.setEditable(true); + } public void setEditor(SheetEditor editor) { + tableData.clear(); this.editor = editor; -// editor.getSheet(). + if (editor.getSheet().getColumns() != null) { + editor.getSheet().getColumns().getColumn().stream().forEach(this::insertViewColumn); + } + if (editor.getSheet().getRows() != null) { + editor.getSheet().getRows().getRow().forEach(this::insertViewRow); + } } @Override public void addColumnAction(ActionEvent event) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + String newColName = UIAction.getText("Enter new column name", "new"); + if (newColName != null && !newColName.isEmpty()) { + UserType col = new UserType(); + col.setName(newColName); + if (editor.getSheet().getColumns() == null) { + editor.getSheet().setColumns(new Columns()); + } + editor.getSheet().getColumns().getColumn().add(col); + insertViewColumn(col); + } + } + + @Override + public void addRowAction(ActionEvent event) { + insertViewRow(new Row()); + } + + private void insertViewColumn(UserType col) { + insertViewColumn(col, -1); } + private void insertViewColumn(UserType col, int pos) { + if (pos < 0) { + pos = table.getColumns().size(); + } + TableColumn tableCol = new TableColumn<>(col.getName()); + tableCol.setCellValueFactory((features) -> { + String val = getValue(features.getValue(), col.getName()); + if (val == null) { + val = ""; + } + return new SimpleObjectProperty(val); + }); + tableCol.setCellFactory(TextFieldTableCell.forTableColumn()); + tableCol.setOnEditCommit((event) + -> setValue(event.getRowValue(), col.getName(), event.getNewValue())); + tableCol.setEditable(true); + tableCol.setContextMenu(new ContextMenu( + createMenuItem("Rename Column", () -> renameColumn(col)), + createMenuItem("Delete Column", () -> deleteColumnWithConfirmation(col)) + )); + table.getColumns().add(pos, tableCol); + } + + private void insertViewRow(Row row) { + tableData.add(row); + } + + private String getValue(Row row, String name) { + return row.getOtherAttributes().entrySet().stream() + .filter((e) -> e.getKey().getLocalPart().equals(name)) + .map(e -> e.getValue()) + .findFirst().orElse(null); + + } + + private void setValue(Row row, String name, String newValue) { + Optional> attr = row.getOtherAttributes().entrySet().stream() + .filter((e) -> e.getKey().getLocalPart().equals(name)).findFirst(); + if (attr.isPresent()) { + attr.get().setValue(newValue); + } else { + row.getOtherAttributes().put(new QName(name), newValue); + } + } + + private MenuItem createMenuItem(String text, Runnable action) { + MenuItem menuItem = new MenuItem(text); + menuItem.setOnAction((evt) -> action.run()); + return menuItem; + } + + private void deleteColumnWithConfirmation(UserType col) { + UIAction.confirm("Are you sure you want to delete column " + col.getName() + "?", + () -> deleteColumn(col), + null); + } + + private void renameColumn(UserType col) { + String newColName = UIAction.getText("Enter new column name", col.getName()); + if (newColName != null && !newColName.isEmpty() && !col.getName().equals(newColName)) { + UserType newCol = new UserType(); + newCol.setName(newColName); + editor.getSheet().getColumns().getColumn().add(newCol); + tableData.forEach(row -> setValue(row, newColName, getValue(row, col.getName()))); + int oldPos = deleteColumn(col); + insertViewColumn(newCol, oldPos); + } + } + + private int deleteColumn(UserType col) { + editor.getSheet().getColumns().getColumn().remove(col); + tableData.stream() + .map(Row::getOtherAttributes) + .forEach( + m -> m.keySet().removeIf(n -> n.getLocalPart().equals(col.getName())) + ); + for (int i=0; i < table.getColumns().size(); i++) { + if (table.getColumns().get(i).getText().equals(col.getName())) { + table.getColumns().remove(i); + return i; + } + } + return -1; + } } diff --git a/OutlawEditor/src/main/resources/SheetEditor.fxml b/OutlawEditor/src/main/resources/SheetEditor.fxml index 5c2de716..5854c192 100644 --- a/OutlawEditor/src/main/resources/SheetEditor.fxml +++ b/OutlawEditor/src/main/resources/SheetEditor.fxml @@ -1,13 +1,16 @@ - - - - - - + + + + + + + + + - + @@ -35,14 +38,11 @@ -