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..e09615692 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; @@ -187,11 +188,16 @@ public synchronized void reload() { public synchronized void load() { FileTime latestModifiedTime = IOHelper.getLastModifiedTime(getPath()); setLastModifiedTime(latestModifiedTime); - String content = IOHelper.readFile(getPath()); - editorPane.setEditorValue(content); - this.select(); - setTabText(getPath().getFileName().toString()); - setChangedProperty(false); + try { + String content = IOHelper.readFile(getPath()); + editorPane.setEditorValue(content); + this.select(); + setTabText(getPath().getFileName().toString()); + setChangedProperty(false); + } catch (Exception e) { + closeIt(); + } + } private synchronized void save() { @@ -267,7 +273,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) {