Skip to content

Commit

Permalink
Safely close tabs for un-open file
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmanusta committed Aug 26, 2018
1 parent a5b3f96 commit e8b5137
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
32 changes: 23 additions & 9 deletions src/main/java/com/kodedu/component/EditorPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/kodedu/component/MyTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/kodedu/other/IOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e8b5137

Please sign in to comment.