From 5e7f520b09345689443f8ee6bfa0654e7e24b381 Mon Sep 17 00:00:00 2001 From: Rex Chan Date: Fri, 9 Mar 2018 20:21:32 -0800 Subject: [PATCH] Added Copy & Paste to editors --- src/StellarisDK/FileClasses/GenericData.java | 7 ++++ .../FileClasses/Helper/DataCell.java | 42 ++++++++++++++++++- src/StellarisDK/GUI/guiController.java | 42 +++++++++++++++++-- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/StellarisDK/FileClasses/GenericData.java b/src/StellarisDK/FileClasses/GenericData.java index b4c78ff..8e511fc 100644 --- a/src/StellarisDK/FileClasses/GenericData.java +++ b/src/StellarisDK/FileClasses/GenericData.java @@ -109,6 +109,13 @@ public int getSize(){ return data.getFullSize(); } + public Object clone() { + GenericData copy = createNew(); + copy.setType(type); + copy.setData(data); + return copy; + } + public abstract TreeItem getRequiredTreeSet(); public static int getTab() { diff --git a/src/StellarisDK/FileClasses/Helper/DataCell.java b/src/StellarisDK/FileClasses/Helper/DataCell.java index 2f1e09f..0197a2c 100644 --- a/src/StellarisDK/FileClasses/Helper/DataCell.java +++ b/src/StellarisDK/FileClasses/Helper/DataCell.java @@ -11,6 +11,7 @@ public class DataCell extends TreeCell { private TextField textField; + private static TreeItem cellContent; public DataCell() { super(); @@ -143,6 +144,14 @@ protected void updateItem(T item, boolean empty) { } } + private TreeItem clone(TreeItem items){ + TreeItem copy = new TreeItem<>(items.getValue()); + for(Object item : items.getChildren()){ + copy.getChildren().add(clone((TreeItem)item)); + } + return copy; + } + private void setCM() { ContextMenu contextMenu = new ContextMenu(); MenuItem createNew = new MenuItem("New.."); @@ -158,7 +167,38 @@ private void setCM() { getTreeItem().getParent().getChildren().remove(getTreeItem()); }); - contextMenu.getItems().addAll(createNew, edit, delete); + + MenuItem cut = new MenuItem("Cut"); + cut.setOnAction(event -> { + cellContent = getTreeItem(); + getTreeItem().getParent().getChildren().remove(getTreeItem()); + }); + + MenuItem copy = new MenuItem("Copy"); + copy.setOnAction(event -> { + cellContent = getTreeItem(); + }); + + MenuItem paste = new MenuItem("Paste"); + paste.setOnAction(event -> { + if(getTreeItem().getParent() != null) + getTreeItem().getParent().getChildren().add(clone(cellContent)); + else + getTreeItem().getChildren().add(clone(cellContent)); + }); + + contextMenu.getItems().addAll(createNew, edit, cut, copy, paste, delete); setContextMenu(contextMenu); + + setOnContextMenuRequested(event ->{ + if(this.getTreeItem().getParent() == null){ + contextMenu.getItems().removeAll(cut, copy, delete); + } + if(cellContent == null){ + paste.setDisable(true); + }else{ + paste.setDisable(false); + } + }); } } \ No newline at end of file diff --git a/src/StellarisDK/GUI/guiController.java b/src/StellarisDK/GUI/guiController.java index b99732c..6317582 100644 --- a/src/StellarisDK/GUI/guiController.java +++ b/src/StellarisDK/GUI/guiController.java @@ -47,6 +47,9 @@ public class guiController extends AnchorPane { private TreeItem modRoot; private TreeItem vanillaRoot = new TreeItem<>("Stellaris"); + private GenericData cContent; + private String cParent; + @FXML private TreeView itemView; @@ -185,7 +188,7 @@ protected void updateItem(Object item, boolean empty) { } int index = target.getParent().getChildren().indexOf(target); source.getParent().getChildren().remove(source); - if (target.toString().endsWith(".txt")) { + if (target.getValue().toString().endsWith(".txt")) { target.getChildren().add(source); } else { if (index == target.getParent().getChildren().size()) { @@ -238,15 +241,48 @@ private void setCM(TreeCell cell) { } }); + MenuItem cut = new MenuItem("Cut"); + cut.setOnAction(event -> { + if (cell.getTreeItem().getParent().getValue().toString().endsWith(".txt")) { + cContent = (GenericData) cell.getItem(); + cParent = cell.getTreeItem().getParent().getParent().getValue().toString(); + cell.getTreeItem().getParent().getChildren().remove(cell.getTreeItem()); + } + }); + + MenuItem copy = new MenuItem("Copy"); + copy.setOnAction(event -> { + if (cell.getTreeItem().getParent().getValue().toString().endsWith(".txt")) { + cContent = (GenericData) cell.getItem(); + cParent = cell.getTreeItem().getParent().getParent().getValue().toString(); + } + }); + + MenuItem paste = new MenuItem("Paste"); + paste.setOnAction(event -> { + if (cContent.getClass().equals(cell.getItem().getClass())) { + cell.getTreeItem().getParent().getChildren().add(new TreeItem<>(cContent.clone())); + } else if (cell.getTreeItem().getValue().toString().endsWith(".txt")) { + if (cell.getTreeItem().getParent().getValue().equals(cParent)) + cell.getTreeItem().getChildren().add(new TreeItem<>(cContent.clone())); + } + }); + + cell.setOnContextMenuRequested(event -> { cell.getContextMenu().getItems().clear(); + if(cContent == null){ + paste.setDisable(true); + }else{ + paste.setDisable(false); + } if (cell.getItem() != null && cell.getTreeItem().getParent() != null) { if (cell.getTreeItem().getParent().getValue().equals("common") || cell.getItem().equals("events")) { cell.getContextMenu().getItems().add(createNew); } else if (cell.getItem().toString().endsWith(".txt")) { - cell.getContextMenu().getItems().addAll(createNew, edit, delete); + cell.getContextMenu().getItems().addAll(createNew, edit, paste, delete); } else if (cell.getTreeItem().getParent().getValue().toString().endsWith(".txt")) { - cell.getContextMenu().getItems().addAll(createNew, delete); + cell.getContextMenu().getItems().addAll(createNew, cut, copy, paste, delete); } } });