From 31fd48eefd63999bf0d3f1fd7e8ceb22c6280c29 Mon Sep 17 00:00:00 2001 From: Brendan Robert Date: Mon, 11 Jul 2016 21:38:54 -0500 Subject: [PATCH] Added context menu for sheet editor to insert, clone, and delete rows. --- .../ui/impl/MapEditorTabControllerImpl.java | 1 - .../ui/impl/SheetEditorControllerImpl.java | 37 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java index b087ca65..ac9b64ef 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java @@ -25,7 +25,6 @@ import javafx.scene.image.WritableImage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javax.xml.bind.JAXBException; -import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.MapEditor; import org.badvision.outlaweditor.TransferHelper; import org.badvision.outlaweditor.api.ApplicationState; 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 b94298ad..c28c11af 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 @@ -19,7 +19,10 @@ import java.io.File; import java.net.URL; import java.util.List; import java.util.ResourceBundle; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; +import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleObjectProperty; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; @@ -28,17 +31,23 @@ import javafx.event.ActionEvent; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; import javafx.scene.control.TableColumn; +import javafx.scene.control.TableRow; +import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.cell.TextFieldTableCell; import javafx.stage.FileChooser; +import javafx.util.Callback; import javafx.util.converter.DefaultStringConverter; +import javax.xml.bind.JAXBException; import org.badvision.outlaweditor.SheetEditor; +import org.badvision.outlaweditor.TransferHelper; import org.badvision.outlaweditor.data.DataUtilities; import static org.badvision.outlaweditor.data.DataUtilities.getValue; import static org.badvision.outlaweditor.data.DataUtilities.setValue; import org.badvision.outlaweditor.data.xml.Columns; import org.badvision.outlaweditor.data.xml.Rows; import org.badvision.outlaweditor.data.xml.Rows.Row; +import org.badvision.outlaweditor.data.xml.Script; import org.badvision.outlaweditor.data.xml.UserType; import org.badvision.outlaweditor.ui.ApplicationUIController; import org.badvision.outlaweditor.ui.SheetEditorController; @@ -59,6 +68,21 @@ public class SheetEditorControllerImpl extends SheetEditorController { tableData = FXCollections.observableArrayList(); table.setItems(tableData); table.setEditable(true); + table.setRowFactory(tableView -> { + final TableRow row = new TableRow<>(); + + final ContextMenu contextMenu = new ContextMenu( + createMenuItem("Insert Row", () -> tableData.add(row.getIndex(), new Row())), + createMenuItem("Clone Row", () -> cloneRow(row.getItem())), + createMenuItem("Delete Row", () -> deleteRowWithConfirmation(row.getItem())) + ); + row.contextMenuProperty().bind( + Bindings.when(row.emptyProperty()) + .then((ContextMenu) null) + .otherwise(contextMenu) + ); + return row; + }); } @Override @@ -231,6 +255,19 @@ public class SheetEditorControllerImpl extends SheetEditorController { return colNumber; } + private void deleteRowWithConfirmation(Row row) { + UIAction.confirm("Delete row, are you sure?", ()->tableData.remove(row), ()->{}); + } + + private void cloneRow(Row row) { + try { + Row newRow = TransferHelper.cloneObject(row, Row.class, "row"); + tableData.add(tableData.lastIndexOf(row), newRow); + } catch (JAXBException ex) { + Logger.getLogger(SheetEditorControllerImpl.class.getName()).log(Level.SEVERE, null, ex); + } + } + private void syncData() { if (editor.getSheet().getRows() == null) { editor.getSheet().setRows(new Rows());