public int showSaveDialog(Component parent) throws HeadlessException
{
this.setAccessory(null);
+ this.setSelectedFile(null);
+ return super.showSaveDialog(parent);
+ }
- setDialogType(SAVE_DIALOG);
+ /**
+ * If doing a Save, and an existing file is chosen or entered, prompt for
+ * confirmation of overwrite. Proceed if Yes, else leave the file chooser
+ * open.
+ *
+ * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
+ */
+ @Override
+ public void approveSelection()
+ {
+ if (getDialogType() != SAVE_DIALOG)
+ {
+ super.approveSelection();
+ return;
+ }
- this.setSelectedFile(null);
- int ret = showDialog(parent, MessageManager.getString("action.save"));
ourselectedFile = getSelectedFile();
- if (getSelectedFile() == null)
+ if (ourselectedFile == null)
{
// Workaround for Java 9,10 on OSX - no selected file, but there is a
// filename typed in
"Unexpected exception when trying to get filename.");
x.printStackTrace();
}
+ // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
+ // THE
+ // USER PROMPTED FOR A NEW FILENAME
}
if (ourselectedFile == null)
{
- return JalviewFileChooser.CANCEL_OPTION;
+ return;
}
+
if (getFileFilter() instanceof JalviewFileFilter)
{
JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
setSelectedFile(ourselectedFile);
}
}
- // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
- // USER PROMPTED FOR A NEW FILENAME
- if ((ret == JalviewFileChooser.APPROVE_OPTION)
- && ourselectedFile.exists())
- {
- int confirm = JvOptionPane.showConfirmDialog(parent,
- MessageManager.getString("label.overwrite_existing_file"),
- MessageManager.getString("label.file_already_exists"),
- JvOptionPane.YES_NO_OPTION);
- if (confirm != JvOptionPane.YES_OPTION)
+ if (ourselectedFile.exists())
{
- ret = JalviewFileChooser.CANCEL_OPTION;
+ int confirm = JvOptionPane.showConfirmDialog(this,
+ MessageManager.getString("label.overwrite_existing_file"),
+ MessageManager.getString("label.file_already_exists"),
+ JvOptionPane.YES_NO_OPTION);
+
+ if (confirm != JvOptionPane.YES_OPTION)
+ {
+ return;
+ }
}
- }
- return ret;
+ super.approveSelection();
}
void recentListSelectionChanged(Object selection)