X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fio%2FJalviewFileChooser.java;h=c275ef0d4d4e9c3f3c5547b4dd89cb4c6379b9e2;hb=6b62e4c0242d4f6d43fb0059f32268ff9e992e4f;hp=7a21c16cd52622d58e384572f2bd6a30fe61544c;hpb=75a43bd6a9f7a85dfbc04246540f27505622ca97;p=jalview.git diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 7a21c16..c275ef0 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -25,6 +25,7 @@ import jalview.bin.Cache; import jalview.gui.JvOptionPane; import jalview.util.MessageManager; import jalview.util.Platform; +import jalview.util.dialogrunner.DialogRunnerI; import java.awt.Component; import java.awt.Dimension; @@ -32,9 +33,13 @@ import java.awt.EventQueue; import java.awt.HeadlessException; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.StringTokenizer; import java.util.Vector; @@ -55,8 +60,15 @@ import javax.swing.plaf.basic.BasicFileChooserUI; * @author AMW * */ -public class JalviewFileChooser extends JFileChooser +public class JalviewFileChooser extends JFileChooser implements DialogRunnerI, + PropertyChangeListener { + private static final long serialVersionUID = 1L; + + private Map callbacks = new HashMap<>(); + + File selectedFile = null; + /** * Factory method to return a file chooser that offers readable alignment file * formats @@ -137,7 +149,7 @@ public class JalviewFileChooser extends JFileChooser } JalviewFileChooser(String dir, String[] extensions, String[] descs, - String selected, boolean allFiles) + String selected, boolean acceptAny) { super(safePath(dir)); if (extensions.length == descs.length) @@ -147,7 +159,7 @@ public class JalviewFileChooser extends JFileChooser { formats.add(new String[] { extensions[i], descs[i] }); } - init(formats, selected, allFiles); + init(formats, selected, acceptAny); } else { @@ -172,21 +184,44 @@ 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) + { + int value = super.showOpenDialog(this); + + /* + * code below here is not reached in JalviewJS, instead + * propertyChange() is called for dialog action + */ + /** + * @j2sNative + */ + { + handleResponse(value); + } + return value; + } + + /** * * @param formats * a list of {extensions, description} for each file format * @param selected - * @param allFiles + * @param acceptAny * if true, 'any format' option is included */ - void init(List formats, String selected, boolean allFiles) + void init(List formats, String selected, boolean acceptAny) { JalviewFileFilter chosen = null; // SelectAllFilter needs to be set first before adding further // file filters to fix bug on Mac OSX - setAcceptAllFileFilterUsed(allFiles); + setAcceptAllFileFilterUsed(acceptAny); for (String[] format : formats) { @@ -278,31 +313,42 @@ public class JalviewFileChooser extends JFileChooser return null; } - File ourselectedFile = null; - @Override public File getSelectedFile() { - File selfile = super.getSelectedFile(); - if (selfile == null && ourselectedFile != null) - { - return ourselectedFile; - } - return selfile; + File f = super.getSelectedFile(); + return f == null ? selectedFile : f; } @Override public int showSaveDialog(Component parent) throws HeadlessException { this.setAccessory(null); + // Java 9,10,11 on OSX - clear selected file so name isn't auto populated + this.setSelectedFile(null); - setDialogType(SAVE_DIALOG); + return super.showSaveDialog(parent); + } - this.setSelectedFile(null); - int ret = showDialog(parent, MessageManager.getString("action.save")); - ourselectedFile = getSelectedFile(); + /** + * 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; + } - if (getSelectedFile() == null) + selectedFile = getSelectedFile(); + + if (selectedFile == null) { // Workaround for Java 9,10 on OSX - no selected file, but there is a // filename typed in @@ -311,7 +357,7 @@ public class JalviewFileChooser extends JFileChooser String filename = ((BasicFileChooserUI) getUI()).getFileName(); if (filename != null && filename.length() > 0) { - ourselectedFile = new File(getCurrentDirectory(), filename); + selectedFile = new File(getCurrentDirectory(), filename); } } catch (Throwable x) { @@ -319,40 +365,42 @@ public class JalviewFileChooser extends JFileChooser "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) + + if (selectedFile == null) { - return JalviewFileChooser.CANCEL_OPTION; + return; } + if (getFileFilter() instanceof JalviewFileFilter) { JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); - if (!jvf.accept(ourselectedFile)) + if (!jvf.accept(selectedFile)) { String withExtension = getSelectedFile().getName() + "." + jvf.getAcceptableExtension(); - ourselectedFile = (new File(getCurrentDirectory(), withExtension)); - setSelectedFile(ourselectedFile); + selectedFile = (new File(getCurrentDirectory(), withExtension)); + setSelectedFile(selectedFile); } } - // 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()) + + if (selectedFile.exists()) { - int confirm = JvOptionPane.showConfirmDialog(parent, + 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) { - ret = JalviewFileChooser.CANCEL_OPTION; + return; } } - return ret; + super.approveSelection(); } void recentListSelectionChanged(Object selection) @@ -377,14 +425,15 @@ public class JalviewFileChooser extends JFileChooser class RecentlyOpened extends JPanel { - JList list; + private static final long serialVersionUID = 1L; + JList list; - public RecentlyOpened() + RecentlyOpened() { - - String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE"); + setPreferredSize(new Dimension(300,100)); + String historyItems = Cache.getProperty("RECENT_FILE"); StringTokenizer st; - Vector recent = new Vector(); + Vector recent = new Vector<>(); if (historyItems != null) { @@ -392,14 +441,14 @@ 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); +// dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT); list.setCellRenderer(dlcr); list.addMouseListener(new MouseAdapter() @@ -422,13 +471,13 @@ 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)); } else { - scroller.setPreferredSize(new Dimension(130, 200)); + scroller.setPreferredSize(new Dimension(530, 200)); } this.add(scroller); @@ -446,4 +495,50 @@ public class JalviewFileChooser extends JFileChooser } } + + @Override + public DialogRunnerI setResponseHandler(Object response, Runnable action) + { + callbacks.put(response, action); + return this; + } + + @Override + public void handleResponse(Object response) + { + /* + * this test is for NaN in Chrome + */ + if (response != null && !response.equals(response)) + { + return; + } + Runnable action = callbacks.get(response); + if (action != null) + { + action.run(); + } + } + + /** + * JalviewJS signals file selection by a property change event + * for property "SelectedFile". This methods responds to that by + * running the response action for 'OK' in the dialog. + * + * @param evt + */ + @Override + public void propertyChange(PropertyChangeEvent evt) + { + // TODO other properties need runners... + switch (evt.getPropertyName()) + { + /* + * property name here matches that used in JFileChooser.js + */ + case "SelectedFile": + handleResponse(APPROVE_OPTION); + break; + } + } }