2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 //////////////////////////////////////////////////////////////////
24 import jalview.bin.Cache;
25 import jalview.gui.JvOptionPane;
26 import jalview.util.MessageManager;
27 import jalview.util.Platform;
29 import java.awt.Component;
30 import java.awt.Dimension;
31 import java.awt.EventQueue;
32 import java.awt.HeadlessException;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseAdapter;
36 import java.awt.event.MouseEvent;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.StringTokenizer;
41 import java.util.Vector;
43 import javax.swing.BoxLayout;
44 import javax.swing.DefaultListCellRenderer;
45 import javax.swing.JCheckBox;
46 import javax.swing.JFileChooser;
47 import javax.swing.JList;
48 import javax.swing.JPanel;
49 import javax.swing.JScrollPane;
50 import javax.swing.SpringLayout;
51 import javax.swing.SwingUtilities;
52 import javax.swing.border.TitledBorder;
53 import javax.swing.filechooser.FileFilter;
54 import javax.swing.plaf.basic.BasicFileChooserUI;
57 * Enhanced file chooser dialog box.
59 * NOTE: bug on Windows systems when filechooser opened on directory to view
60 * files with colons in title.
65 public class JalviewFileChooser extends JFileChooser
68 * backupfilesCheckBox = "Include backup files" checkbox includeBackupfiles =
69 * flag set by checkbox
71 private JCheckBox backupfilesCheckBox = null;
73 protected boolean includeBackupFiles = false;
76 * Factory method to return a file chooser that offers readable alignment file
83 public static JalviewFileChooser forRead(String directory,
86 return JalviewFileChooser.forRead(directory, selected, false);
89 public static JalviewFileChooser forRead(String directory,
90 String selected, boolean allowBackupFiles)
92 List<String> extensions = new ArrayList<>();
93 List<String> descs = new ArrayList<>();
94 for (FileFormatI format : FileFormats.getInstance().getFormats())
96 if (format.isReadable())
98 extensions.add(format.getExtensions());
99 descs.add(format.getName());
103 return new JalviewFileChooser(directory,
104 extensions.toArray(new String[extensions.size()]),
105 descs.toArray(new String[descs.size()]), selected, true,
110 * Factory method to return a file chooser that offers writable alignment file
117 public static JalviewFileChooser forWrite(String directory,
120 // TODO in Java 8, forRead and forWrite can be a single method
121 // with a lambda expression parameter for isReadable/isWritable
122 List<String> extensions = new ArrayList<>();
123 List<String> descs = new ArrayList<>();
124 for (FileFormatI format : FileFormats.getInstance().getFormats())
126 if (format.isWritable())
128 extensions.add(format.getExtensions());
129 descs.add(format.getName());
132 return new JalviewFileChooser(directory,
133 extensions.toArray(new String[extensions.size()]),
134 descs.toArray(new String[descs.size()]), selected, false);
137 public JalviewFileChooser(String dir)
139 super(safePath(dir));
140 setAccessory(new RecentlyOpened());
143 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
146 this(dir, suffix, desc, selected, true);
150 * Constructor for a single choice of file extension and description
155 public JalviewFileChooser(String extension, String desc)
157 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
159 { desc }, desc, true);
162 JalviewFileChooser(String dir, String[] extensions, String[] descs,
163 String selected, boolean allFiles)
165 this(dir, extensions, descs, selected, allFiles, false);
168 public JalviewFileChooser(String dir, String[] extensions, String[] descs,
169 String selected, boolean allFiles, boolean allowBackupFiles)
171 super(safePath(dir));
172 if (extensions.length == descs.length)
174 List<String[]> formats = new ArrayList<>();
175 for (int i = 0; i < extensions.length; i++)
177 formats.add(new String[] { extensions[i], descs[i] });
179 init(formats, selected, allFiles, allowBackupFiles);
183 System.err.println("JalviewFileChooser arguments mismatch: "
184 + extensions + ", " + descs);
188 private static File safePath(String dir)
195 File f = new File(dir);
196 if (f.getName().indexOf(':') > -1)
206 * a list of {extensions, description} for each file format
209 * if true, 'any format' option is included
211 void init(List<String[]> formats, String selected, boolean allFiles)
213 init(formats, selected, allFiles, false);
216 void init(List<String[]> formats, String selected, boolean allFiles,
217 boolean allowBackupFiles)
220 JalviewFileFilter chosen = null;
222 // SelectAllFilter needs to be set first before adding further
223 // file filters to fix bug on Mac OSX
224 setAcceptAllFileFilterUsed(allFiles);
226 for (String[] format : formats)
228 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
229 if (allowBackupFiles)
231 jvf.setParentJFC(this);
233 addChoosableFileFilter(jvf);
234 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
242 setFileFilter(chosen);
245 if (allowBackupFiles)
247 JPanel multi = new JPanel();
248 multi.setLayout(new BoxLayout(multi, BoxLayout.PAGE_AXIS));
249 if (backupfilesCheckBox == null)
252 includeBackupFiles = Boolean.parseBoolean(
253 Cache.getProperty(BackupFiles.NS + "_FC_INCLUDE"));
254 } catch (Exception e)
256 includeBackupFiles = false;
258 backupfilesCheckBox = new JCheckBox(
259 MessageManager.getString("label.include_backup_files"),
261 backupfilesCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT);
262 JalviewFileChooser jfc = this;
263 backupfilesCheckBox.addActionListener(new ActionListener()
266 public void actionPerformed(ActionEvent e)
268 includeBackupFiles = backupfilesCheckBox.isSelected();
269 Cache.setProperty(BackupFiles.NS + "_FC_INCLUDE",
270 String.valueOf(includeBackupFiles));
272 FileFilter f = jfc.getFileFilter();
273 // deselect the selected file if it's no longer choosable
274 File selectedFile = jfc.getSelectedFile();
275 if (selectedFile != null && !f.accept(selectedFile))
277 jfc.setSelectedFile(null);
279 // fake the OK button changing (to force it to upate)
280 String s = jfc.getApproveButtonText();
281 jfc.firePropertyChange(
282 APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, null, s);
283 // fake the file filter changing (its behaviour actually has)
284 jfc.firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, null, f);
286 jfc.rescanCurrentDirectory();
292 multi.add(new RecentlyOpened());
293 multi.add(backupfilesCheckBox);
298 // set includeBackupFiles=false to avoid other file choosers from picking
299 // up backup files (Just In Case)
300 includeBackupFiles = false;
301 setAccessory(new RecentlyOpened());
306 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
308 super.setFileFilter(filter);
312 if (getUI() instanceof BasicFileChooserUI)
314 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
315 final String name = fcui.getFileName().trim();
317 if ((name == null) || (name.length() == 0))
322 EventQueue.invokeLater(new Thread()
327 String currentName = fcui.getFileName();
328 if ((currentName == null) || (currentName.length() == 0))
330 fcui.setFileName(name);
335 } catch (Exception ex)
337 ex.printStackTrace();
338 // Some platforms do not have BasicFileChooserUI
343 * Returns the selected file format, or null if none selected
347 public FileFormatI getSelectedFormat()
349 if (getFileFilter() == null)
355 * logic here depends on option description being formatted as
356 * formatName (extension, extension...)
357 * or the 'no option selected' value
359 * @see JalviewFileFilter.getDescription
361 String format = getFileFilter().getDescription();
362 int parenPos = format.indexOf("(");
365 format = format.substring(0, parenPos).trim();
368 return FileFormats.getInstance().forName(format);
369 } catch (IllegalArgumentException e)
371 System.err.println("Unexpected format: " + format);
377 File ourselectedFile = null;
380 public File getSelectedFile()
382 File selfile = super.getSelectedFile();
383 if (selfile == null && ourselectedFile != null)
385 return ourselectedFile;
391 public int showSaveDialog(Component parent) throws HeadlessException
393 this.setAccessory(null);
394 this.setSelectedFile(null);
395 return super.showSaveDialog(parent);
399 * If doing a Save, and an existing file is chosen or entered, prompt for
400 * confirmation of overwrite. Proceed if Yes, else leave the file chooser
403 * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
406 public void approveSelection()
408 if (getDialogType() != SAVE_DIALOG)
410 super.approveSelection();
414 ourselectedFile = getSelectedFile();
416 if (ourselectedFile == null)
418 // Workaround for Java 9,10 on OSX - no selected file, but there is a
422 String filename = ((BasicFileChooserUI) getUI()).getFileName();
423 if (filename != null && filename.length() > 0)
425 ourselectedFile = new File(getCurrentDirectory(), filename);
427 } catch (Throwable x)
430 "Unexpected exception when trying to get filename.");
433 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
435 // USER PROMPTED FOR A NEW FILENAME
437 if (ourselectedFile == null)
442 if (getFileFilter() instanceof JalviewFileFilter)
444 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
446 if (!jvf.accept(ourselectedFile))
448 String withExtension = getSelectedFile().getName() + "."
449 + jvf.getAcceptableExtension();
450 ourselectedFile = (new File(getCurrentDirectory(), withExtension));
451 setSelectedFile(ourselectedFile);
455 if (ourselectedFile.exists())
457 int confirm = JvOptionPane.showConfirmDialog(this,
458 MessageManager.getString("label.overwrite_existing_file"),
459 MessageManager.getString("label.file_already_exists"),
460 JvOptionPane.YES_NO_OPTION);
462 if (confirm != JvOptionPane.YES_OPTION)
468 super.approveSelection();
471 void recentListSelectionChanged(Object selection)
473 setSelectedFile(null);
474 if (selection != null)
476 File file = new File((String) selection);
477 if (getFileFilter() instanceof JalviewFileFilter)
479 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
481 if (!jvf.accept(file))
483 setFileFilter(getChoosableFileFilters()[0]);
487 setSelectedFile(file);
492 * A panel to set as the 'accessory' component to the file chooser dialog,
493 * holding a list of recently opened files (if any). These are held as a
494 * tab-separated list of file paths under key <code>RECENT_FILE</code> in
495 * <code>.jalview_properties</code>. A click in the list calls a method in
496 * JalviewFileChooser to set the chosen file as the selection.
498 class RecentlyOpened extends JPanel
500 private static final long serialVersionUID = 1L;
502 private JList<String> list;
506 String historyItems = Cache.getProperty("RECENT_FILE");
508 Vector<String> recent = new Vector<>();
510 if (historyItems != null)
512 st = new StringTokenizer(historyItems, "\t");
513 while (st.hasMoreTokens())
515 recent.addElement(st.nextToken());
519 list = new JList<>(recent);
521 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
522 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
523 list.setCellRenderer(dlcr);
525 list.addMouseListener(new MouseAdapter()
528 public void mousePressed(MouseEvent evt)
530 recentListSelectionChanged(list.getSelectedValue());
534 this.setBorder(new TitledBorder(
535 MessageManager.getString("label.recently_opened")));
537 final JScrollPane scroller = new JScrollPane(list);
539 SpringLayout layout = new SpringLayout();
540 layout.putConstraint(SpringLayout.WEST, scroller, 5,
541 SpringLayout.WEST, this);
542 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
543 SpringLayout.NORTH, this);
545 if (Platform.isAMac())
547 scroller.setPreferredSize(new Dimension(500, 100));
551 scroller.setPreferredSize(new Dimension(130, 200));
556 SwingUtilities.invokeLater(new Runnable()
561 scroller.getHorizontalScrollBar()
562 .setValue(scroller.getHorizontalScrollBar().getMaximum());