Skip to content

Commit

Permalink
Modified Context Menu in Main Editor View
Browse files Browse the repository at this point in the history
Menu Options are given according to use
  • Loading branch information
rexc159 committed Mar 9, 2018
1 parent fcd457f commit d0beeb6
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 53 deletions.
15 changes: 7 additions & 8 deletions src/StellarisDK/GUI/FXML/guiFX.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@
<items>
<MenuItem mnemonicParsing="false" onAction="#createNewMD" text="New Mod" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" onAction="#openFile" text="Open File" />
<MenuItem mnemonicParsing="false" onAction="#openLocale" text="Open Locale" />
<MenuItem mnemonicParsing="false" onAction="#openFile" text="Open File (Debug Option)" />
<MenuItem mnemonicParsing="false" onAction="#openLocale" text="Open Locale (Debug Option)" />
<MenuItem mnemonicParsing="false" onAction="#openMod" text="Open Mod" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" onAction="#saveAll" text="Save All" />
<MenuItem mnemonicParsing="false" onAction="#export" text="Export..." />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" onAction="#saveAll" text="Export Mod" />
<MenuItem mnemonicParsing="false" onAction="#closeMod" text="Close Mod" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Settings" />
<MenuItem mnemonicParsing="false" text="Settings (Not Implemented)" />
<MenuItem mnemonicParsing="false" onAction="#exit" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
<MenuItem mnemonicParsing="false" text="Not Implemented" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
<MenuItem mnemonicParsing="false" text="Not Implemented" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Debug Options">
Expand Down
146 changes: 101 additions & 45 deletions src/StellarisDK/GUI/guiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javafx.scene.control.*;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyCode;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
Expand Down Expand Up @@ -65,31 +66,68 @@ public guiController() {
@Override
public TreeCell call(TreeView param) {
TreeCell<Object> cell = new TreeCell<Object>() {
private TextField txt;

@Override
public void startEdit() {
if (this.getTreeItem().getValue() instanceof GenericData) {
if (getItem() instanceof GenericData) {
System.out.println("Starting Editor");
super.startEdit();
((GenericData) this.getItem()).ui.setRoot(mainWindow);
open((GenericData) this.getItem());
super.cancelEdit();
}else if (getItem().toString().endsWith(".txt")){
super.startEdit();
if(txt == null){
txt = new TextField(getItem().toString());
txt.setOnKeyReleased(event ->{
if(event.getCode() == KeyCode.ENTER){
if(!txt.getText().trim().endsWith(".txt")){
txt.setText(txt.getText().trim()+".txt");
}
commitEdit(txt.getText());
}else if (event.getCode() == KeyCode.ESCAPE){
cancelEdit();
}
});
}
setText(null);
setGraphic(txt);
txt.selectAll();
}
}

@Override
public void cancelEdit() {
super.cancelEdit();
setText(getItem().toString());
setGraphic(null);
}

@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(item.toString());
if (isEditing()) {
if (txt != null) {
txt.setText(getItem().toString());
}
setText(null);
setGraphic(txt);
} else {
setText(getItem().toString());
setGraphic(null);
}
}
}
};
ContextMenu contextMenu = createCM(cell);

cell.setOnDragDetected(event -> {
TreeItem item = cell.getTreeItem();
if (item.isLeaf() && item.getParent().getValue().toString().contains(".txt")) {
if (item.isLeaf() && item.getParent().getValue().toString().endsWith(".txt")) {
Dragboard db = cell.startDragAndDrop(TransferMode.MOVE);
ClipboardContent clipboard = new ClipboardContent();
clipboard.putString(Integer.toString(item.getParent().getChildren().indexOf(item)));
Expand All @@ -102,7 +140,7 @@ protected void updateItem(Object item, boolean empty) {
if (event.getGestureSource() != cell) {
TreeItem source;
TreeItem item = cell.getTreeItem();
if (item.getValue().toString().contains(".txt") || item.getParent().getValue().toString().contains(".txt")) {
if (item.getValue().toString().endsWith(".txt") || item.getParent().getValue().toString().endsWith(".txt")) {
TreeItem targetParent = item.getParent();
if (event.getGestureSource() instanceof LabeledText) {
source = ((TreeCell) ((Node) event.getGestureSource()).getParent()).getTreeItem();
Expand Down Expand Up @@ -142,7 +180,7 @@ protected void updateItem(Object item, boolean empty) {
}
int index = target.getParent().getChildren().indexOf(target);
source.getParent().getChildren().remove(source);
if (target.toString().contains(".txt")) {
if (target.toString().endsWith(".txt")) {
target.getChildren().add(source);
} else {
if(index == target.getParent().getChildren().size()){
Expand All @@ -162,39 +200,57 @@ protected void updateItem(Object item, boolean empty) {
event.consume();
});

cell.setContextMenu(contextMenu);
setCM(cell);

return cell;
}
});
}

private ContextMenu createCM(TreeCell cell) {
private void setCM(TreeCell cell) {
ContextMenu contextMenu = new ContextMenu();
MenuItem createNew = new MenuItem("New..");
createNew.setOnAction(event -> {
if (cell.getTreeItem().getValue() instanceof GenericData) {
TreeItem newCell = new TreeItem<>(((GenericData) cell.getTreeItem().getValue()).createNew());
cell.getTreeItem().getParent().getChildren().add(newCell);
} else if (cell.getTreeItem().getValue().toString().contains(".txt")) {
} else if (cell.getTreeItem().getValue().toString().endsWith(".txt")) {
cell.getTreeItem().getChildren().add(createNew(cell.getTreeItem().getParent().getValue().toString()));
} else if (cell.getTreeItem().getParent() != null) {
if (cell.getTreeItem().getParent().getValue().equals("common")) {
cell.getTreeItem().getChildren().add(new TreeItem<>("Test.txt"));
} else if (cell.getTreeItem().getValue().equals("events")) {
cell.getTreeItem().getChildren().add(new TreeItem<>("Test.txt"));
} else if (cell.getTreeItem().getValue().equals("localisation")) {
cell.getTreeItem().getChildren().add(new TreeItem<>("Test.yml"));
if(cell.getItem().equals("events") || cell.getTreeItem().getParent().getValue().toString().equals("common")){
cell.getTreeItem().getChildren().add(new TreeItem<>("New File.txt"));
}else if (cell.getTreeItem().getParent().getValue().toString().equals("localisation")){
cell.getTreeItem().getChildren().add(new TreeItem<>("New Locale.yml"));
}
}
});
MenuItem edit = new MenuItem("Rename");
edit.setOnAction(event -> {
cell.startEdit();
});
MenuItem delete = new MenuItem("Delete");
delete.setOnAction(event -> {
if(cell.getItem().toString().contains(".txt") || cell.getTreeItem().getParent().getValue().toString().contains(".txt")){
if (cell.getItem().toString().contains(".txt") || cell.getTreeItem().getParent().getValue().toString().contains(".txt")) {
cell.getTreeItem().getParent().getChildren().remove(cell.getTreeItem());
}
});
contextMenu.getItems().addAll(createNew, delete);
return contextMenu;

cell.setOnContextMenuRequested(event -> {
cell.getContextMenu().getItems().clear();
if(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);
} else if (cell.getTreeItem().getParent().getValue().toString().endsWith(".txt")) {
cell.getContextMenu().getItems().addAll(createNew, delete);
}
}
});

contextMenu.getItems().addAll(createNew, edit, delete);

cell.setContextMenu(contextMenu);
}

private TreeItem createNew(String type) {
Expand Down Expand Up @@ -512,44 +568,44 @@ private void saveFiles(File saveLoc, TreeItem current) {

@FXML
protected void saveAll() {
DirectoryChooser fc = new DirectoryChooser();
File main = fc.showDialog(stage);
if (main != null) {
String folder = ((ModDescriptor) itemView.getRoot().getValue()).getDir();
main = new File(main.getPath(),folder);
if(main.exists()){
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("_yyyy-MM-dd_HH-mm-ss");
main.renameTo(new File(main.getParent(),folder + dTF.format(LocalDateTime.now())));
main = new File(main.getParent(),folder);
if (mainMd != null && mainMd.getValue("path") != null) {
DirectoryChooser fc = new DirectoryChooser();
File main = fc.showDialog(stage);
if (main != null) {
String folder = ((ModDescriptor) itemView.getRoot().getValue()).getDir();
main = new File(main.getPath(), folder);
if (main.exists()) {
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("_yyyy-MM-dd_HH-mm-ss");
main.renameTo(new File(main.getParent(), folder + dTF.format(LocalDateTime.now())));
main = new File(main.getParent(), folder);
}
main.mkdir();
saveFiles(main, itemView.getRoot());

// Temp Code
Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(this.stage);
HBox box = new HBox();
box.getChildren().add(new Text("Saving Complete"));
dialog.setScene(new Scene(box));
dialog.show();
}
main.mkdir();
saveFiles(main, itemView.getRoot());

// Temp Code
}else{
Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(this.stage);
HBox box = new HBox();
box.getChildren().add(new Text("Saving Complete"));
box.getChildren().add(new Text("No Mod Loaded/No Mod Path Defined!"));
dialog.setScene(new Scene(box));
dialog.show();
}
}

@FXML
protected void export() {
FileChooser fc = new FileChooser();
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("Mod Descriptors (*.mod)", "*.mod"));
File mod = fc.showSaveDialog(stage);
if (mod != null) {
try {
FileWriter fw = new FileWriter(mod);
fw.write(mainMd.export());
fw.close();
} catch (IOException e) {
System.out.println("Export Failed.");
}
}
protected void closeMod(){
mainMd = null;
itemView.setRoot(null);
}

@FXML
Expand Down

0 comments on commit d0beeb6

Please sign in to comment.