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.MouseAdapter;
34 import java.awt.event.MouseEvent;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
42 import javax.swing.DefaultListCellRenderer;
43 import javax.swing.JFileChooser;
44 import javax.swing.JList;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.SpringLayout;
48 import javax.swing.plaf.basic.BasicFileChooserUI;
51 * Enhanced file chooser dialog box.
53 * NOTE: bug on Windows systems when filechooser opened on directory to view
54 * files with colons in title.
59 public class JalviewFileChooser extends JFileChooser
62 * Factory method to return a file chooser that offers readable alignment file
69 public static JalviewFileChooser forRead(String directory,
72 List<String> extensions = new ArrayList<>();
73 List<String> descs = new ArrayList<>();
74 for (FileFormatI format : FileFormats.getInstance().getFormats())
76 if (format.isReadable())
78 extensions.add(format.getExtensions());
79 descs.add(format.getName());
82 return new JalviewFileChooser(directory,
83 extensions.toArray(new String[extensions.size()]),
84 descs.toArray(new String[descs.size()]), selected, true);
88 * Factory method to return a file chooser that offers writable alignment file
95 public static JalviewFileChooser forWrite(String directory,
98 // TODO in Java 8, forRead and forWrite can be a single method
99 // with a lambda expression parameter for isReadable/isWritable
100 List<String> extensions = new ArrayList<>();
101 List<String> descs = new ArrayList<>();
102 for (FileFormatI format : FileFormats.getInstance().getFormats())
104 if (format.isWritable())
106 extensions.add(format.getExtensions());
107 descs.add(format.getName());
110 return new JalviewFileChooser(directory,
111 extensions.toArray(new String[extensions.size()]),
112 descs.toArray(new String[descs.size()]), selected, false);
115 public JalviewFileChooser(String dir)
117 super(safePath(dir));
118 setAccessory(new RecentlyOpened());
121 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
124 this(dir, suffix, desc, selected, true);
128 * Constructor for a single choice of file extension and description
133 public JalviewFileChooser(String extension, String desc)
135 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
137 { desc }, desc, true);
140 JalviewFileChooser(String dir, String[] extensions, String[] descs,
141 String selected, boolean allFiles)
143 super(safePath(dir));
144 if (extensions.length == descs.length)
146 List<String[]> formats = new ArrayList<>();
147 for (int i = 0; i < extensions.length; i++)
149 formats.add(new String[] { extensions[i], descs[i] });
151 init(formats, selected, allFiles);
155 System.err.println("JalviewFileChooser arguments mismatch: "
156 + extensions + ", " + descs);
160 private static File safePath(String dir)
167 File f = new File(dir);
168 if (f.getName().indexOf(':') > -1)
178 * a list of {extensions, description} for each file format
181 * if true, 'any format' option is included
183 void init(List<String[]> formats, String selected, boolean allFiles)
186 JalviewFileFilter chosen = null;
188 // SelectAllFilter needs to be set first before adding further
189 // file filters to fix bug on Mac OSX
190 setAcceptAllFileFilterUsed(allFiles);
192 for (String[] format : formats)
194 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
195 addChoosableFileFilter(jvf);
196 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
204 setFileFilter(chosen);
207 setAccessory(new RecentlyOpened());
211 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
213 super.setFileFilter(filter);
217 if (getUI() instanceof BasicFileChooserUI)
219 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
220 final String name = fcui.getFileName().trim();
222 if ((name == null) || (name.length() == 0))
227 EventQueue.invokeLater(new Thread()
232 String currentName = fcui.getFileName();
233 if ((currentName == null) || (currentName.length() == 0))
235 fcui.setFileName(name);
240 } catch (Exception ex)
242 ex.printStackTrace();
243 // Some platforms do not have BasicFileChooserUI
248 * Returns the selected file format, or null if none selected
252 public FileFormatI getSelectedFormat()
254 if (getFileFilter() == null)
260 * logic here depends on option description being formatted as
261 * formatName (extension, extension...)
262 * or the 'no option selected' value
264 * @see JalviewFileFilter.getDescription
266 String format = getFileFilter().getDescription();
267 int parenPos = format.indexOf("(");
270 format = format.substring(0, parenPos).trim();
273 return FileFormats.getInstance().forName(format);
274 } catch (IllegalArgumentException e)
276 System.err.println("Unexpected format: " + format);
282 File ourselectedFile = null;
285 public File getSelectedFile()
287 File selfile = super.getSelectedFile();
288 if (selfile == null && ourselectedFile != null)
290 return ourselectedFile;
296 public int showSaveDialog(Component parent) throws HeadlessException
298 this.setAccessory(null);
300 setDialogType(SAVE_DIALOG);
302 this.setSelectedFile(null);
303 int ret = showDialog(parent, MessageManager.getString("action.save"));
304 ourselectedFile = getSelectedFile();
306 if (getSelectedFile() == null)
308 // Workaround for Java 9,10 on OSX - no selected file, but there is a
312 String filename = ((BasicFileChooserUI) getUI()).getFileName();
313 if (filename != null && filename.length() > 0)
315 ourselectedFile = new File(getCurrentDirectory(), filename);
317 } catch (Throwable x)
320 "Unexpected exception when trying to get filename.");
324 if (ourselectedFile == null)
326 return JalviewFileChooser.CANCEL_OPTION;
328 if (getFileFilter() instanceof JalviewFileFilter)
330 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
332 if (!jvf.accept(ourselectedFile))
334 String withExtension = getSelectedFile().getName() + "."
335 + jvf.getAcceptableExtension();
336 ourselectedFile = (new File(getCurrentDirectory(), withExtension));
337 setSelectedFile(ourselectedFile);
340 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
341 // USER PROMPTED FOR A NEW FILENAME
342 if ((ret == JalviewFileChooser.APPROVE_OPTION)
343 && ourselectedFile.exists())
345 int confirm = JvOptionPane.showConfirmDialog(parent,
346 MessageManager.getString("label.overwrite_existing_file"),
347 MessageManager.getString("label.file_already_exists"),
348 JvOptionPane.YES_NO_OPTION);
350 if (confirm != JvOptionPane.YES_OPTION)
352 ret = JalviewFileChooser.CANCEL_OPTION;
355 rollBackupFiles(ourselectedFile);
362 // attempts to roll backup files for this file (before overwriting). Returns
363 // true if it rolled all the files, false otherwise.
364 private static boolean rollBackupFiles(File file)
373 // split filename up to insert suffix template in the right place. template
374 // and backupMax can be set in .jalview_properties
375 String backupSuffixTemplate = jalview.bin.Cache
376 .getDefault("BACKUP_SUFFIX", "-bak-%n");
377 int backupMax = jalview.bin.Cache.getDefault("BACKUP_MAX", 10);
381 File dirFile = file.getParentFile();
382 dir = dirFile.getCanonicalPath();
383 } catch (Exception e)
386 "Could not get canonical path for file '" + file + "'");
389 String filename = file.getName();
390 String basename = filename;
391 String extension = "";
392 int dotchar = filename.lastIndexOf('.');
393 // don't split of filenames with the last '.' at the very beginning or
394 // very end of the filename
395 if ((dotchar > 0) && (dotchar < filename.length() - 1))
397 basename = filename.substring(0, dotchar);
398 extension = filename.substring(dotchar); // NOTE this includes the '.'
404 // Create/move backups up one
405 String numString = null;
406 File lastfile = null;
407 for (int n = backupMax; n > 0; n--)
409 numString = String.format("%02d", n);
410 String backupSuffix = backupSuffixTemplate.replaceAll("%n",
412 String backupfilename = dir + File.separatorChar + basename
413 + backupSuffix + extension;
414 File backupfile_n = new File(backupfilename);
416 if (! backupfile_n.exists()) {
417 lastfile = backupfile_n;
421 if (n == backupMax-1)
422 { // Move the max backup to /tmp instead of deleting (Just In
424 String tmpfile = "tmp-" + backupfilename;
427 File tmpFile = File.createTempFile(tmpfile, ".tmp");
428 ret = ret && backupfile_n.renameTo(tmpFile);
429 } catch (IOException e)
432 "Could not create temp file '" + tmpfile + ".tmp'");
437 ret = ret && backupfile_n.renameTo(lastfile);
440 lastfile = backupfile_n;
443 // now actually backup the important file!
444 ret = ret && file.renameTo(lastfile);
450 void recentListSelectionChanged(Object selection)
452 setSelectedFile(null);
453 if (selection != null)
455 File file = new File((String) selection);
456 if (getFileFilter() instanceof JalviewFileFilter)
458 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
460 if (!jvf.accept(file))
462 setFileFilter(getChoosableFileFilters()[0]);
466 setSelectedFile(file);
470 class RecentlyOpened extends JPanel
474 public RecentlyOpened()
477 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
479 Vector recent = new Vector();
481 if (historyItems != null)
483 st = new StringTokenizer(historyItems, "\t");
485 while (st.hasMoreTokens())
487 recent.addElement(st.nextElement());
491 list = new JList(recent);
493 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
494 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
495 list.setCellRenderer(dlcr);
497 list.addMouseListener(new MouseAdapter()
500 public void mousePressed(MouseEvent evt)
502 recentListSelectionChanged(list.getSelectedValue());
506 this.setBorder(new javax.swing.border.TitledBorder(
507 MessageManager.getString("label.recently_opened")));
509 final JScrollPane scroller = new JScrollPane(list);
511 SpringLayout layout = new SpringLayout();
512 layout.putConstraint(SpringLayout.WEST, scroller, 5,
513 SpringLayout.WEST, this);
514 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
515 SpringLayout.NORTH, this);
517 if (new Platform().isAMac())
519 scroller.setPreferredSize(new Dimension(500, 100));
523 scroller.setPreferredSize(new Dimension(130, 200));
528 javax.swing.SwingUtilities.invokeLater(new Runnable()
533 scroller.getHorizontalScrollBar()
534 .setValue(scroller.getHorizontalScrollBar().getMaximum());