From 001da5c53451cee16fc23b53ee7fd60b2d8b3ddf Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sun, 12 Apr 2020 18:56:58 +0200 Subject: [PATCH] select existing tab on same file --- .../sparrowwallet/sparrow/AppController.java | 19 ++++++++-- .../com/sparrowwallet/sparrow/TabData.java | 37 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/TabData.java diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index b62fdfa6..4d541016 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -81,8 +81,8 @@ public class AppController implements Initializable { tabs.getSelectionModel().selectedItemProperty().addListener((observable, old_val, selectedTab) -> { if(selectedTab != null) { - String tabType = (String)selectedTab.getUserData(); - if(tabType.equals(TRANSACTION_TAB_TYPE)) { + TabData tabData = (TabData)selectedTab.getUserData(); + if(tabData.getType() == TabData.TabType.TRANSACTION) { EventManager.get().post(new TransactionTabSelectedEvent(selectedTab)); } } @@ -109,6 +109,14 @@ public class AppController implements Initializable { } private void openFile(File file) { + for(Tab tab : tabs.getTabs()) { + TabData tabData = (TabData)tab.getUserData(); + if(file.equals(tabData.getFile())) { + tabs.getSelectionModel().select(tab); + return; + } + } + if(file.exists()) { try { byte[] bytes = new byte[(int)file.length()]; @@ -119,6 +127,8 @@ public class AppController implements Initializable { try { Tab tab = addTransactionTab(name, bytes); + TabData tabData = (TabData)tab.getUserData(); + tabData.setFile(file); tabs.getSelectionModel().select(tab); } catch(ParseException e) { ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); @@ -153,6 +163,8 @@ public class AppController implements Initializable { if(text.isPresent() && !text.get().isEmpty()) { try { Tab tab = addTransactionTab(null, text.get().trim()); + TabData tabData = (TabData)tab.getUserData(); + tabData.setText(text.get()); tabs.getSelectionModel().select(tab); } catch(PSBTParseException e) { showErrorDialog("Invalid PSBT", e.getMessage()); @@ -240,7 +252,8 @@ public class AppController implements Initializable { } Tab tab = new Tab(tabName); - tab.setUserData(TRANSACTION_TAB_TYPE); + TabData tabData = new TabData(TabData.TabType.TRANSACTION); + tab.setUserData(tabData); tab.setClosable(true); FXMLLoader transactionLoader = new FXMLLoader(getClass().getResource("transaction/transaction.fxml")); tab.setContent(transactionLoader.load()); diff --git a/src/main/java/com/sparrowwallet/sparrow/TabData.java b/src/main/java/com/sparrowwallet/sparrow/TabData.java new file mode 100644 index 00000000..857701c2 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/TabData.java @@ -0,0 +1,37 @@ +package com.sparrowwallet.sparrow; + +import java.io.File; + +public class TabData { + private TabType type; + private File file; + private String text; + + public TabData(TabType type) { + this.type = type; + } + + public TabType getType() { + return type; + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public enum TabType { + TRANSACTION + } +}