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;
28 import jalview.util.dialogrunner.DialogRunnerI;
30 import java.awt.Component;
31 import java.awt.Dimension;
32 import java.awt.EventQueue;
33 import java.awt.HeadlessException;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.beans.PropertyChangeEvent;
39 import java.beans.PropertyChangeListener;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
45 import java.util.StringTokenizer;
46 import java.util.Vector;
47 import java.util.concurrent.Callable;
49 import javax.swing.BoxLayout;
50 import javax.swing.DefaultListCellRenderer;
51 import javax.swing.JCheckBox;
52 import javax.swing.JDialog;
53 import javax.swing.JFileChooser;
54 import javax.swing.JList;
55 import javax.swing.JOptionPane;
56 import javax.swing.JPanel;
57 import javax.swing.JScrollPane;
58 import javax.swing.SpringLayout;
59 import javax.swing.filechooser.FileFilter;
60 import javax.swing.plaf.basic.BasicFileChooserUI;
62 import jalview.bin.Cache;
63 import jalview.gui.JvOptionPane;
64 import jalview.util.ChannelProperties;
65 import jalview.util.MessageManager;
66 import jalview.util.Platform;
67 import jalview.util.dialogrunner.DialogRunnerI;
70 * Enhanced file chooser dialog box.
72 * NOTE: bug on Windows systems when filechooser opened on directory to view
73 * files with colons in title.
78 public class JalviewFileChooser extends JFileChooser
79 implements DialogRunnerI, PropertyChangeListener
81 private static final long serialVersionUID = 1L;
83 private Map<Object, Callable> callbacks = new HashMap<>();
85 File selectedFile = null;
88 * backupfilesCheckBox = "Include backup files" checkbox includeBackupfiles =
89 * flag set by checkbox
91 private JCheckBox backupfilesCheckBox = null;
93 protected boolean includeBackupFiles = false;
96 * Factory method to return a file chooser that offers readable alignment file
103 public static JalviewFileChooser forRead(String directory,
106 return JalviewFileChooser.forRead(directory, selected, false);
109 public static JalviewFileChooser forRead(String directory,
110 String selected, boolean allowBackupFiles)
112 List<String> extensions = new ArrayList<>();
113 List<String> descs = new ArrayList<>();
114 for (FileFormatI format : FileFormats.getInstance().getFormats())
116 if (format.isReadable())
118 extensions.add(format.getExtensions());
119 descs.add(format.getName());
123 return new JalviewFileChooser(directory,
124 extensions.toArray(new String[extensions.size()]),
125 descs.toArray(new String[descs.size()]), selected, true,
130 * Factory method to return a file chooser that offers writable alignment file
137 public static JalviewFileChooser forWrite(String directory,
140 // TODO in Java 8, forRead and forWrite can be a single method
141 // with a lambda expression parameter for isReadable/isWritable
142 List<String> extensions = new ArrayList<>();
143 List<String> descs = new ArrayList<>();
144 for (FileFormatI format : FileFormats.getInstance().getFormats())
146 if (format.isWritable())
148 extensions.add(format.getExtensions());
149 descs.add(format.getName());
152 return new JalviewFileChooser(directory,
153 extensions.toArray(new String[extensions.size()]),
154 descs.toArray(new String[descs.size()]), selected, false);
157 public JalviewFileChooser(String dir)
159 super(safePath(dir));
160 setAccessory(new RecentlyOpened());
163 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
166 this(dir, suffix, desc, selected, true);
170 * Constructor for a single choice of file extension and description
175 public JalviewFileChooser(String extension, String desc)
177 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
179 { desc }, desc, true);
182 JalviewFileChooser(String dir, String[] extensions, String[] descs,
183 String selected, boolean acceptAny)
185 this(dir, extensions, descs, selected, acceptAny, false);
188 public JalviewFileChooser(String dir, String[] extensions, String[] descs,
189 String selected, boolean acceptAny, boolean allowBackupFiles)
191 super(safePath(dir));
192 if (extensions.length == descs.length)
194 List<String[]> formats = new ArrayList<>();
195 for (int i = 0; i < extensions.length; i++)
197 formats.add(new String[] { extensions[i], descs[i] });
199 init(formats, selected, acceptAny, allowBackupFiles);
203 System.err.println("JalviewFileChooser arguments mismatch: "
204 + extensions + ", " + descs);
208 private static File safePath(String dir)
215 File f = new File(dir);
216 if (f.getName().indexOf(':') > -1)
224 * Overridden for JalviewJS compatibility: only one thread in Javascript, so
225 * we can't wait for user choice in another thread and then perform the
229 public int showOpenDialog(Component parent)
231 int value = super.showOpenDialog(this);
233 if (!Platform.isJS())
241 * code here is not run in JalviewJS, instead
242 * propertyChange() is called for dialog action
244 handleResponse(value);
252 * a list of {extensions, description} for each file format
255 * if true, 'any format' option is included
257 void init(List<String[]> formats, String selected, boolean acceptAny)
259 init(formats, selected, acceptAny, false);
262 void init(List<String[]> formats, String selected, boolean acceptAny,
263 boolean allowBackupFiles)
266 JalviewFileFilter chosen = null;
268 // SelectAllFilter needs to be set first before adding further
269 // file filters to fix bug on Mac OSX
270 setAcceptAllFileFilterUsed(acceptAny);
272 for (String[] format : formats)
274 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
275 if (allowBackupFiles)
277 jvf.setParentJFC(this);
279 addChoosableFileFilter(jvf);
280 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
288 setFileFilter(chosen);
291 if (allowBackupFiles)
293 JPanel multi = new JPanel();
294 multi.setLayout(new BoxLayout(multi, BoxLayout.PAGE_AXIS));
295 if (backupfilesCheckBox == null)
299 includeBackupFiles = Boolean.parseBoolean(
300 Cache.getProperty(BackupFiles.NS + "_FC_INCLUDE"));
301 } catch (Exception e)
303 includeBackupFiles = false;
305 backupfilesCheckBox = new JCheckBox(
306 MessageManager.getString("label.include_backup_files"),
308 backupfilesCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT);
309 JalviewFileChooser jfc = this;
310 backupfilesCheckBox.addActionListener(new ActionListener()
313 public void actionPerformed(ActionEvent e)
315 includeBackupFiles = backupfilesCheckBox.isSelected();
316 Cache.setProperty(BackupFiles.NS + "_FC_INCLUDE",
317 String.valueOf(includeBackupFiles));
319 FileFilter f = jfc.getFileFilter();
320 // deselect the selected file if it's no longer choosable
321 File selectedFile = jfc.getSelectedFile();
322 if (selectedFile != null && !f.accept(selectedFile))
324 jfc.setSelectedFile(null);
326 // fake the OK button changing (to force it to upate)
327 String s = jfc.getApproveButtonText();
328 jfc.firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY,
330 // fake the file filter changing (its behaviour actually has)
331 jfc.firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, null, f);
333 jfc.rescanCurrentDirectory();
339 multi.add(new RecentlyOpened());
340 multi.add(backupfilesCheckBox);
345 // set includeBackupFiles=false to avoid other file choosers from picking
346 // up backup files (Just In Case)
347 includeBackupFiles = false;
348 setAccessory(new RecentlyOpened());
353 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
355 super.setFileFilter(filter);
359 if (getUI() instanceof BasicFileChooserUI)
361 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
362 final String name = fcui.getFileName().trim();
364 if ((name == null) || (name.length() == 0))
369 EventQueue.invokeLater(new Thread()
374 String currentName = fcui.getFileName();
375 if ((currentName == null) || (currentName.length() == 0))
377 fcui.setFileName(name);
382 } catch (Exception ex)
384 ex.printStackTrace();
385 // Some platforms do not have BasicFileChooserUI
390 * Returns the selected file format, or null if none selected
394 public FileFormatI getSelectedFormat()
396 if (getFileFilter() == null)
402 * logic here depends on option description being formatted as
403 * formatName (extension, extension...)
404 * or the 'no option selected' value
406 * @see JalviewFileFilter.getDescription
408 String format = getFileFilter().getDescription();
409 int parenPos = format.indexOf("(");
412 format = format.substring(0, parenPos).trim();
415 return FileFormats.getInstance().forName(format);
416 } catch (IllegalArgumentException e)
418 System.err.println("Unexpected format: " + format);
425 public File getSelectedFile()
427 File f = super.getSelectedFile();
428 return f == null ? selectedFile : f;
432 public int showSaveDialog(Component parent) throws HeadlessException
434 this.setAccessory(null);
435 // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
436 this.setSelectedFile(null);
438 return super.showSaveDialog(parent);
442 * If doing a Save, and an existing file is chosen or entered, prompt for
443 * confirmation of overwrite. Proceed if Yes, else leave the file chooser
446 * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
449 public void approveSelection()
451 if (getDialogType() != SAVE_DIALOG)
453 super.approveSelection();
457 selectedFile = getSelectedFile();
459 if (selectedFile == null)
461 // Workaround for Java 9,10 on OSX - no selected file, but there is a
465 String filename = ((BasicFileChooserUI) getUI()).getFileName();
466 if (filename != null && filename.length() > 0)
468 selectedFile = new File(getCurrentDirectory(), filename);
470 } catch (Throwable x)
473 "Unexpected exception when trying to get filename.");
476 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
478 // USER PROMPTED FOR A NEW FILENAME
481 if (selectedFile == null)
486 if (getFileFilter() instanceof JalviewFileFilter)
488 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
490 if (!jvf.accept(selectedFile))
492 String withExtension = getSelectedFile().getName() + "."
493 + jvf.getAcceptableExtension();
494 selectedFile = (new File(getCurrentDirectory(), withExtension));
495 setSelectedFile(selectedFile);
499 if (selectedFile.exists())
501 int confirm = Cache.getDefault("CONFIRM_OVERWRITE_FILE", true)
502 ? JvOptionPane.showConfirmDialog(this,
504 .getString("label.overwrite_existing_file"),
505 MessageManager.getString("label.file_already_exists"),
506 JvOptionPane.YES_NO_OPTION)
507 : JOptionPane.YES_OPTION;
509 if (confirm != JvOptionPane.YES_OPTION)
515 super.approveSelection();
518 void recentListSelectionChanged(Object selection)
520 setSelectedFile(null);
521 if (selection != null)
523 File file = new File((String) selection);
524 if (getFileFilter() instanceof JalviewFileFilter)
526 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
528 if (!jvf.accept(file))
530 setFileFilter(getChoosableFileFilters()[0]);
534 setSelectedFile(file);
538 class RecentlyOpened extends JPanel
540 private static final long serialVersionUID = 1L;
546 setPreferredSize(new Dimension(300, 100));
547 String historyItems = Cache.getProperty("RECENT_FILE");
549 Vector<String> recent = new Vector<>();
551 if (historyItems != null)
553 st = new StringTokenizer(historyItems, "\t");
555 while (st.hasMoreTokens())
557 recent.addElement(st.nextToken());
561 list = new JList<>(recent);
563 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
564 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
565 list.setCellRenderer(dlcr);
567 list.addMouseListener(new MouseAdapter()
570 public void mousePressed(MouseEvent evt)
572 recentListSelectionChanged(list.getSelectedValue());
576 this.setBorder(new javax.swing.border.TitledBorder(
577 MessageManager.getString("label.recently_opened")));
579 final JScrollPane scroller = new JScrollPane(list);
581 SpringLayout layout = new SpringLayout();
582 layout.putConstraint(SpringLayout.WEST, scroller, 5,
583 SpringLayout.WEST, this);
584 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
585 SpringLayout.NORTH, this);
587 if (Platform.isAMacAndNotJS())
589 scroller.setPreferredSize(new Dimension(500, 100));
593 scroller.setPreferredSize(new Dimension(530, 200));
598 javax.swing.SwingUtilities.invokeLater(new Runnable()
603 scroller.getHorizontalScrollBar()
604 .setValue(scroller.getHorizontalScrollBar().getMaximum());
614 public JalviewFileChooser setResponseHandler(Object response,
617 callbacks.put(response, new Callable<Void>()
631 public DialogRunnerI setResponseHandler(Object response, Callable action)
633 callbacks.put(response, action);
638 public void handleResponse(Object response)
641 * this test is for NaN in Chrome
643 if (response != null && !response.equals(response))
647 Callable action = callbacks.get(response);
653 } catch (Exception e)
661 * JalviewJS signals file selection by a property change event for property
662 * "SelectedFile". This methods responds to that by running the response
663 * action for 'OK' in the dialog.
668 public void propertyChange(PropertyChangeEvent evt)
670 // TODO other properties need runners...
671 switch (evt.getPropertyName())
674 * property name here matches that used in JFileChooser.js
677 handleResponse(APPROVE_OPTION);
683 protected JDialog createDialog(Component parent) throws HeadlessException
685 JDialog dialog = super.createDialog(parent);
686 dialog.setIconImages(ChannelProperties.getIconList());