Most of sheet implementation has been done, still a little left to do though.

This commit is contained in:
Brendan Robert 2016-07-05 02:53:22 -05:00
parent e9012275d5
commit 2f75c3e14a
5 changed files with 169 additions and 28 deletions

View File

@ -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());
});
}

View File

@ -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<UserType, String> addColumn;
@FXML
protected TableView<Row> table;
@FXML
abstract public void addColumnAction(ActionEvent event);
@FXML
abstract public void addRowAction(ActionEvent event);
@FXML
protected void initialize() {

View File

@ -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");

View File

@ -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<Row> 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<Row, String> 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<Map.Entry<QName, String>> 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;
}
}

View File

@ -1,13 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="sheetEditor" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="org.badvision.outlaweditor.ui.impl.SheetEditorControllerImpl">
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="sheetEditor" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.badvision.outlaweditor.ui.impl.SheetEditorControllerImpl">
<stylesheets>
<URL value="@/styles/styles.css" />
</stylesheets>
@ -35,14 +38,11 @@
</MenuBar>
<ToolBar prefHeight="40.0" prefWidth="200.0">
<items>
<Button mnemonicParsing="false" text="Button" />
<Button mnemonicParsing="false" onAction="#addColumnAction" text="Add Column" />
<Button mnemonicParsing="false" onAction="#addRowAction" text="Add Row" />
</items>
</ToolBar>
<TableView editable="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<columns>
<TableColumn fx:id="addColumn" onEditCommit="#addColumnAction" prefWidth="75.0" sortable="false" text="+" />
</columns>
</TableView>
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
</children>
</VBox>
</children>