From e8b5137368be203990961a5db2487624d4611c3d Mon Sep 17 00:00:00 2001 From: Rahman Usta Date: Sun, 26 Aug 2018 16:50:21 +0200 Subject: [PATCH] Safely close tabs for un-open file --- .../java/com/kodedu/component/EditorPane.java | 32 +++++++++++++------ src/main/java/com/kodedu/component/MyTab.java | 3 +- src/main/java/com/kodedu/other/IOHelper.java | 3 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/kodedu/component/EditorPane.java b/src/main/java/com/kodedu/component/EditorPane.java index c7f34f77e..0e2ee0b1a 100644 --- a/src/main/java/com/kodedu/component/EditorPane.java +++ b/src/main/java/com/kodedu/component/EditorPane.java @@ -95,6 +95,7 @@ public class EditorPane extends AnchorPane { private ContextMenu contextMenu; private Number pageX; private Number pageY; + private MyTab myTab; @Autowired public EditorPane(ApplicationController controller, EditorConfigBean editorConfigBean, ThreadService threadService, ShortcutProvider shortcutProvider, ApplicationContext applicationContext, TabService tabService, AsciiTreeGenerator asciiTreeGenerator, ParserService parserService, SpellcheckConfigBean spellcheckConfigBean, DirectoryService directoryService) { @@ -131,15 +132,20 @@ private void afterEditorLoaded() { if (Objects.nonNull(path)) { threadService.runTaskLater(() -> { - final String content = IOHelper.readFile(path); - setLastModifiedTime(IOHelper.getLastModifiedTime(path)); - threadService.runActionLater(() -> { - changeEditorMode(); - setInitialized(); - setEditorValue(content); - resetUndoManager(); - ready.setValue(true); - }); + try { + final String content = IOHelper.readFile(path); + setLastModifiedTime(IOHelper.getLastModifiedTime(path)); + threadService.runActionLater(() -> { + changeEditorMode(); + setInitialized(); + setEditorValue(content); + resetUndoManager(); + ready.setValue(true); + }); + } catch (Exception e) { + myTab.closeIt(); + } + }); } else { setInitialized(); @@ -815,4 +821,12 @@ public BooleanProperty changedPropertyProperty() { public void setChangedProperty(boolean changedProperty) { this.changedProperty.set(changedProperty); } + + public void closeTab(Runnable runnable) { + runnable.run(); + } + + public void setTab(MyTab myTab) { + this.myTab = myTab; + } } diff --git a/src/main/java/com/kodedu/component/MyTab.java b/src/main/java/com/kodedu/component/MyTab.java index e4285dc49..358f39025 100644 --- a/src/main/java/com/kodedu/component/MyTab.java +++ b/src/main/java/com/kodedu/component/MyTab.java @@ -53,6 +53,7 @@ public class MyTab extends Tab { @Autowired public MyTab(EditorPane editorPane, StoredConfigBean storedConfigBean, DirectoryService directoryService, TabService tabService, ApplicationController controller, ThreadService threadService) { this.editorPane = editorPane; + this.editorPane.setTab(this); this.storedConfigBean = storedConfigBean; this.directoryService = directoryService; this.tabService = tabService; @@ -267,7 +268,7 @@ public void select() { this.getTabPane().getSelectionModel().select(this); } - private void closeIt() { + public void closeIt() { threadService.runActionLater(() -> { tabService.getClosedPaths().add(Optional.ofNullable(getPath())); this.getTabPane().getTabs().remove(this); // keep it here diff --git a/src/main/java/com/kodedu/other/IOHelper.java b/src/main/java/com/kodedu/other/IOHelper.java index 1c3308aa6..196a1ad0b 100644 --- a/src/main/java/com/kodedu/other/IOHelper.java +++ b/src/main/java/com/kodedu/other/IOHelper.java @@ -83,6 +83,7 @@ public static String readFile(Path path) { } catch (Exception e) { logger.error("Problem occured while reading file {}", path, e); + throw new RuntimeException(e); } return content; } @@ -110,7 +111,7 @@ private static Charset detectCharset(File f, String[] charsets, byte[] bytes) { } } - throw new RuntimeException("Charset not found, can't open this file "); + throw new RuntimeException("Charset not detected, can't open this file "); } private static Charset detectCharset(File f, Charset charset, byte[] bytes) {