Skip to content

Commit

Permalink
Fix action on exit "Ask to save and close all anonymous tab" to preve…
Browse files Browse the repository at this point in the history
…nt asking twice time
  • Loading branch information
dzmipt committed Sep 6, 2024
1 parent 412cb17 commit 29f4f6b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/studio/kdb/config/ActionOnExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public enum ActionOnExit {
NOTHING("Don't ask to save and keep workspace as is"),
SAVE("Ask to save all modified tabs"),
CLOSE_ANONYMOUS_NOT_SAVED("Ask to save and close all anonymous not-saved tabs");
CLOSE_ANONYMOUS_NOT_SAVED("Ask to save and close all anonymous tabs");

private final String description;

Expand Down
4 changes: 4 additions & 0 deletions src/studio/ui/EditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ public EditorsPanel getEditorsPanel() {
return (EditorsPanel) getPane().getParent().getParent();
}

public void selectEditor() {
getEditorsPanel().selectTab(this);
}

public boolean isAddedToPanel() {
return getPane().getParent() != null;
}
Expand Down
21 changes: 18 additions & 3 deletions src/studio/ui/EditorsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public static boolean saveEditor(EditorTab editor) {
public static boolean checkAndSaveTab(EditorTab editor) {
if (! editor.isModified()) return true;

editor.selectEditor();
int choice = StudioOptionPane.showYesNoCancelDialog(editor.getPane(),
editor.getTitle() + " is changed. Save changes?","Save changes?");

Expand All @@ -308,13 +309,16 @@ public static boolean checkAndSaveTab(EditorTab editor) {
return true;
}

public boolean closeTab(EditorTab editorTab) {
if (!checkAndSaveTab(editorTab)) return false;

public void closeTabForced(EditorTab editorTab) {
editorTab.closing();

tabbedEditors.remove(editorTab.getPane());
closeIfEmpty();
}

public boolean closeTab(EditorTab editorTab) {
if (!checkAndSaveTab(editorTab)) return false;
closeTabForced(editorTab);
return true;
}

Expand All @@ -341,6 +345,17 @@ public void selectNextTab(boolean forward) {
tabbedEditors.setSelectedIndex(index);
}

public void selectTab(EditorTab editorTab) {
int count = tabbedEditors.getTabCount();
for (int index = 0; index<count; index++) {
if ( getEditorTab(index) == editorTab) {
tabbedEditors.setSelectedIndex(index);
editorTab.getStudioWindow().refreshFrameTitle();
return;
}
}
}

private void removeFocusChangeKeysForWindows(JComponent component) {
if (Util.MAC_OS_X) return;

Expand Down
15 changes: 12 additions & 3 deletions src/studio/ui/StudioWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,12 @@ public static void about() {
help.setVisible(true);
}

public static void quit() {
private static volatile boolean quitting = false;

public static boolean quit() {
if (quitting) return true;
quitting = true;

WorkspaceSaver.setEnabled(false);
try {
ActionOnExit action = CONFIG.getEnum(Config.ACTION_ON_EXIT);
Expand All @@ -803,13 +808,16 @@ public static void quit() {

if (editorTab.isModified()) {
if (action == ActionOnExit.CLOSE_ANONYMOUS_NOT_SAVED && editorTab.getFilename()==null) {
editorTab.getEditorsPanel().closeTab(editorTab);
editorTab.getEditorsPanel().closeTabForced(editorTab);
}
}
}
return true;
});
if (!complete) return;
if (!complete) {
quitting = false;
return false;
}
}
}
} finally {
Expand All @@ -820,6 +828,7 @@ public static void quit() {
}
WorkspaceSaver.save(getWorkspace());
CONFIG.exit();
return true;
}

public void close() {
Expand Down
2 changes: 2 additions & 0 deletions src/studio/utils/FilesBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public OutputStream newFileOutputStream(String filename) throws IOException {
}

Path tmpFile = Paths.get(filename).resolveSibling(FilenameUtils.getName(filename) + ".tmp");
log.info("Saving to tmp file {}", tmpFile.toString());
Files.deleteIfExists(tmpFile);

FileOutputStream outputStream = new FileOutputStream(tmpFile.toFile());
Expand Down Expand Up @@ -95,6 +96,7 @@ public void flush() throws IOException {
public void close() throws IOException {
outputStream.close();
Files.move(tmpFile, Paths.get(filename), REPLACE_EXISTING);
log.info("moved {} -> {}", tmpFile.toString(), filename);
}
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/studio/utils/PropertiesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public void saveToDisk() {
ByteArrayOutputStream buffer = getStreamToSave();
if (buffer == null) return;

log.info("Saving config to file {} with number of properties {}", filename, size());

try (OutputStream out = FilesBackup.getInstance().newFileOutputStream(filename)) {
byte[] lineSeparator = System.getProperty("line.separator").getBytes(CHARSET);

Expand All @@ -119,6 +121,8 @@ public void saveToDisk() {
out.write(line.getBytes(CHARSET));
out.write(lineSeparator);
}

propertiesToSave = null;
} catch (IOException e) {
log.error("Error in saving config to the file {}", filename, e);
}
Expand Down

0 comments on commit 29f4f6b

Please sign in to comment.