X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FJalviewFileChooser.java;h=a7f8fa6f43ac905b2a174678bd84ecc777d2185f;hb=7047b076a62df177047ceb16c3611efe020bdd76;hp=ae6c9ebe6b091e2ae2141b2245ec8841ea12fb88;hpb=5969ead0fd401f4435a3435ede9e4e87cf9c1d0b;p=jalview.git diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index ae6c9eb..a7f8fa6 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -25,6 +25,10 @@ import jalview.bin.Cache; import jalview.gui.JvOptionPane; import jalview.util.MessageManager; import jalview.util.Platform; +import jalview.util.dialogrunner.DialogRunner; +import jalview.util.dialogrunner.DialogRunnerI; +import jalview.util.dialogrunner.Response; +import jalview.util.dialogrunner.RunResponse; import java.awt.Component; import java.awt.Dimension; @@ -58,8 +62,10 @@ import javax.swing.plaf.basic.BasicFileChooserUI; * */ public class JalviewFileChooser extends JFileChooser - implements PropertyChangeListener + implements PropertyChangeListener, DialogRunnerI { + DialogRunner runner = new DialogRunner<>(this); + /** * Factory method to return a file chooser that offers readable alignment file * formats @@ -159,25 +165,14 @@ public class JalviewFileChooser extends JFileChooser } } - private Runnable theCallback; - - public void setCallback(Runnable callback) - { - this.theCallback = callback; - } - - public Runnable getCallback() - { - return theCallback; - } - @Override public void propertyChange(PropertyChangeEvent evt) { + // TODO other properties need runners... switch (evt.getPropertyName()) { case "SelectedFile": - theCallback.run(); + runner.run(APPROVE_OPTION); break; } } @@ -198,6 +193,25 @@ public class JalviewFileChooser extends JFileChooser } /** + * 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 showOpenDialog(Component parent) + { + runner.resetResponses(); + int value = super.showOpenDialog(this); + /** + * @j2sNative + */ + { + runner.firstRun(value); + } + return value; + } + + /** * * @param formats * a list of {extensions, description} for each file format @@ -303,45 +317,132 @@ public class JalviewFileChooser extends JFileChooser } return null; } + + File ourselectedFile = null; @Override - public int showSaveDialog(Component parent) throws HeadlessException + public File getSelectedFile() { - this.setAccessory(null); - - setDialogType(SAVE_DIALOG); - - int ret = showDialog(parent, MessageManager.getString("action.save")); - + File selfile = super.getSelectedFile(); + if (selfile == null && ourselectedFile != null) + { + return ourselectedFile; + } + return selfile; + } - if (getFileFilter() instanceof JalviewFileFilter) + Component saveparent; + RunResponse overwriteCheck = new RunResponse( + JalviewFileChooser.APPROVE_OPTION) + { + @Override + public void run() { - JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); + ourselectedFile = getSelectedFile(); - if (!jvf.accept(getSelectedFile())) + if (getSelectedFile() == null) + { + // Workaround for Java 9,10 on OSX - no selected file, but there is a + // filename typed in + try + { + String filename = ((BasicFileChooserUI) getUI()).getFileName(); + if (filename != null && filename.length() > 0) + { + ourselectedFile = new File(getCurrentDirectory(), filename); + } + } catch (Throwable x) { - String withExtension = getSelectedFile() + "." - + jvf.getAcceptableExtension(); - setSelectedFile(new File(withExtension)); + System.err.println( + "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 ((ret == JalviewFileChooser.APPROVE_OPTION) - && getSelectedFile().exists()) + if (ourselectedFile == null) { - int confirm = JvOptionPane.showConfirmDialog(parent, - MessageManager.getString("label.overwrite_existing_file"), - MessageManager.getString("label.file_already_exists"), - JvOptionPane.YES_NO_OPTION); + 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 (confirm != JvOptionPane.YES_OPTION) + 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); + + // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE + // USER PROMPTED FOR A NEW FILENAME + /** + * @j2sNative + */ { - ret = JalviewFileChooser.CANCEL_OPTION; + 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); + } + } } + }; + }; + + /** + * 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(); } - return ret; + 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 + */ + { + runner.firstRun(value); + } + return value; } void recentListSelectionChanged(Object selection) @@ -366,14 +467,14 @@ public class JalviewFileChooser extends JFileChooser class RecentlyOpened extends JPanel { - JList list; + JList list; public RecentlyOpened() { String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE"); StringTokenizer st; - Vector recent = new Vector(); + Vector recent = new Vector<>(); if (historyItems != null) { @@ -381,11 +482,11 @@ public class JalviewFileChooser extends JFileChooser while (st.hasMoreTokens()) { - recent.addElement(st.nextElement()); + recent.addElement(st.nextToken()); } } - list = new JList(recent); + list = new JList<>(recent); DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT); @@ -411,7 +512,7 @@ public class JalviewFileChooser extends JFileChooser layout.putConstraint(SpringLayout.NORTH, scroller, 5, SpringLayout.NORTH, this); - if (new Platform().isAMac()) + if (Platform.isAMac()) { scroller.setPreferredSize(new Dimension(500, 100)); } @@ -436,4 +537,10 @@ public class JalviewFileChooser extends JFileChooser } + @Override + public JalviewFileChooser response(RunResponse action) + { + return runner.response(action); + } + }