X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FJalviewFileChooser.java;h=7fbe8014a41986112756931f2b24c6ecde864ad2;hb=5a631296dd1dcc1df7b50487a647c27333696c74;hp=7c3cf9289a00b72f6accf37b3f115499f1377d51;hpb=db80eb8e1acf352e72a33e1e3825d40f7c6e4046;p=jalview.git diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 7c3cf92..7fbe801 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -48,6 +48,9 @@ import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SpringLayout; +import javax.swing.SwingUtilities; +import javax.swing.border.TitledBorder; +import javax.swing.filechooser.FileFilter; import javax.swing.plaf.basic.BasicFileChooserUI; /** @@ -80,6 +83,12 @@ public class JalviewFileChooser extends JFileChooser public static JalviewFileChooser forRead(String directory, String selected) { + return JalviewFileChooser.forRead(directory, selected, false); + } + + public static JalviewFileChooser forRead(String directory, + String selected, boolean allowBackupFiles) + { List extensions = new ArrayList<>(); List descs = new ArrayList<>(); for (FileFormatI format : FileFormats.getInstance().getFormats()) @@ -90,9 +99,11 @@ public class JalviewFileChooser extends JFileChooser descs.add(format.getName()); } } + return new JalviewFileChooser(directory, extensions.toArray(new String[extensions.size()]), - descs.toArray(new String[descs.size()]), selected, true); + descs.toArray(new String[descs.size()]), selected, true, + allowBackupFiles); } /** @@ -257,17 +268,24 @@ public class JalviewFileChooser extends JFileChooser includeBackupFiles = backupfilesCheckBox.isSelected(); Cache.setProperty(BackupFiles.NS + "_FC_INCLUDE", String.valueOf(includeBackupFiles)); + + FileFilter f = jfc.getFileFilter(); + // deselect the selected file if it's no longer choosable + File selectedFile = jfc.getSelectedFile(); + if (selectedFile != null && !f.accept(selectedFile)) + { + jfc.setSelectedFile(null); + } + // fake the OK button changing (to force it to upate) + String s = jfc.getApproveButtonText(); + jfc.firePropertyChange( + APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, null, s); + // fake the file filter changing (its behaviour actually has) + jfc.firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, null, f); + jfc.rescanCurrentDirectory(); jfc.revalidate(); jfc.repaint(); - // This is a kludge. Cannot find out how to get the file list to - // refresh! - /* - Object o = jfc.getClientProperty( - CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY); - jfc.firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, - o, o); - */ } }); } @@ -373,14 +391,29 @@ public class JalviewFileChooser extends JFileChooser 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 @@ -397,11 +430,15 @@ 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) { - return JalviewFileChooser.CANCEL_OPTION; + return; } + if (getFileFilter() instanceof JalviewFileFilter) { JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); @@ -415,26 +452,20 @@ public class JalviewFileChooser extends JFileChooser } } - // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE - // USER PROMPTED FOR A NEW FILENAME. - // DO NOT need to confirm file overwrite if using backup files (the files - // aren't being overwritten!) - if ((ret == JalviewFileChooser.APPROVE_OPTION) - && ourselectedFile.exists() && (!BackupFiles.getEnabled())) + if (ourselectedFile.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) @@ -457,28 +488,35 @@ public class JalviewFileChooser extends JFileChooser } } + /** + * A panel to set as the 'accessory' component to the file chooser dialog, + * holding a list of recently opened files (if any). These are held as a + * tab-separated list of file paths under key RECENT_FILE in + * .jalview_properties. A click in the list calls a method in + * JalviewFileChooser to set the chosen file as the selection. + */ class RecentlyOpened extends JPanel { - JList list; + private static final long serialVersionUID = 1L; - public RecentlyOpened() - { + private JList list; - String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE"); + RecentlyOpened() + { + String historyItems = Cache.getProperty("RECENT_FILE"); StringTokenizer st; - Vector recent = new Vector(); + Vector recent = new Vector<>(); if (historyItems != null) { st = new StringTokenizer(historyItems, "\t"); - 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); @@ -493,7 +531,7 @@ public class JalviewFileChooser extends JFileChooser } }); - this.setBorder(new javax.swing.border.TitledBorder( + this.setBorder(new TitledBorder( MessageManager.getString("label.recently_opened"))); final JScrollPane scroller = new JScrollPane(list); @@ -504,7 +542,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)); } @@ -515,7 +553,7 @@ public class JalviewFileChooser extends JFileChooser this.add(scroller); - javax.swing.SwingUtilities.invokeLater(new Runnable() + SwingUtilities.invokeLater(new Runnable() { @Override public void run() @@ -524,8 +562,6 @@ public class JalviewFileChooser extends JFileChooser .setValue(scroller.getHorizontalScrollBar().getMaximum()); } }); - } - } }