Skip to content

Commit

Permalink
Added Copy & Paste to editors
Browse files Browse the repository at this point in the history
  • Loading branch information
rexc159 committed Mar 10, 2018
1 parent c2a06e3 commit 5e7f520
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/StellarisDK/FileClasses/GenericData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
42 changes: 41 additions & 1 deletion src/StellarisDK/FileClasses/Helper/DataCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class DataCell<T> extends TreeCell<T> {

private TextField textField;
private static TreeItem cellContent;

public DataCell() {
super();
Expand Down Expand Up @@ -143,6 +144,14 @@ protected void updateItem(T item, boolean empty) {
}
}

private TreeItem<T> clone(TreeItem<T> items){
TreeItem<T> copy = new TreeItem<>(items.getValue());
for(Object item : items.getChildren()){
copy.getChildren().add(clone((TreeItem<T>)item));
}
return copy;
}

private void setCM() {
ContextMenu contextMenu = new ContextMenu();
MenuItem createNew = new MenuItem("New..");
Expand All @@ -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);
}
});
}
}
42 changes: 39 additions & 3 deletions src/StellarisDK/GUI/guiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}
}
});
Expand Down

0 comments on commit 5e7f520

Please sign in to comment.