*
*/
public class JalviewFileChooser extends JFileChooser
- implements PropertyChangeListener, DialogRunnerI
++ implements PropertyChangeListener, DialogRunnerI<JalviewFileChooser>
{
+ DialogRunner<JalviewFileChooser> runner = new DialogRunner<>(this);
+
/**
* Factory method to return a file chooser that offers readable alignment file
* formats
return selfile;
}
- Component saveparent;
- RunResponse overwriteCheck = new RunResponse(
- JalviewFileChooser.APPROVE_OPTION)
+ @Override
+ public int showSaveDialog(Component parent) throws HeadlessException
{
- @Override
- public void run()
+ this.setAccessory(null);
++ // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
+ this.setSelectedFile(null);
++
+ return super.showSaveDialog(parent);
+ }
+
+ /**
+ * 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;
+ }
+
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
}
if (ourselectedFile == null)
{
- returned = new Response(JalviewFileChooser.CANCEL_OPTION);
return;
}
- // JBP Note - this code was executed regardless of 'SAVE' being pressed
- // need to see if there were side effects
- if (getFileFilter() instanceof JalviewFileFilter)
- {
- JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
- if (!jvf.accept(getSelectedFile()))
- {
- String withExtension = getSelectedFile() + "."
- + jvf.getAcceptableExtension();
- setSelectedFile(new File(withExtension));
- }
- }
- // All good, so we continue to save
- returned = new Response(JalviewFileChooser.APPROVE_OPTION);
+ if (getFileFilter() instanceof JalviewFileFilter)
+ {
+ JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
- // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
- // USER PROMPTED FOR A NEW FILENAME
- /**
- * @j2sNative
- */
+ if (!jvf.accept(ourselectedFile))
{
- if (getSelectedFile().exists())
- {
- // JAL-3048 - may not need to raise this for browser saves
-
- // yes/no cancel
- int confirm = JvOptionPane.showConfirmDialog(saveparent,
- MessageManager.getString("label.overwrite_existing_file"),
- MessageManager.getString("label.file_already_exists"),
- JvOptionPane.YES_NO_OPTION);
-
- if (confirm != JvOptionPane.YES_OPTION)
- {
- returned = new Response(JalviewFileChooser.CANCEL_OPTION);
- }
- }
+ String withExtension = getSelectedFile().getName() + "."
+ + jvf.getAcceptableExtension();
+ ourselectedFile = (new File(getCurrentDirectory(), withExtension));
+ setSelectedFile(ourselectedFile);
}
- };
- };
-
- /**
- * Overridden for JalviewJS compatibility: only one thread in Javascript,
- * so we can't wait for user choice in another thread and then perform the
- * desired action
- */
- @Override
- public int showSaveDialog(Component parent) throws HeadlessException
- {
- this.setAccessory(null);
-
- /*
- * Save dialog is opened until user picks a file format
- */
- if (!runner.isRegistered(overwriteCheck))
- {
- // first call for this instance
- runner.firstResponse(overwriteCheck);
}
- else
- {
- // reset response flags
- runner.resetResponses();
- }
-
- setDialogType(SAVE_DIALOG);
-
- // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
- this.setSelectedFile(null);
-
- saveparent = parent;
- int value = showDialog(parent, MessageManager.getString("action.save"));
- /**
- * @j2sNative
- */
+ if (ourselectedFile.exists())
- {
- int confirm = JvOptionPane.showConfirmDialog(this,
- MessageManager.getString("label.overwrite_existing_file"),
- MessageManager.getString("label.file_already_exists"),
- JvOptionPane.YES_NO_OPTION);
+ {
- runner.firstRun(value);
++ 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;
- }
++ if (confirm != JvOptionPane.YES_OPTION)
++ {
++ return;
+ }
+ }
- return value;
+
+ super.approveSelection();
}
void recentListSelectionChanged(Object selection)