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 java.awt.Component;
25 import java.awt.Dimension;
26 import java.awt.EventQueue;
27 import java.awt.HeadlessException;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.MouseAdapter;
31 import java.awt.event.MouseEvent;
32 import java.beans.PropertyChangeEvent;
33 import java.beans.PropertyChangeListener;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
42 import javax.swing.BoxLayout;
43 import javax.swing.JCheckBox;
44 import javax.swing.JDialog;
45 import javax.swing.JFileChooser;
46 import javax.swing.JLabel;
47 import javax.swing.JList;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.ListCellRenderer;
52 import javax.swing.SpringLayout;
53 import javax.swing.SwingConstants;
54 import javax.swing.SwingUtilities;
55 import javax.swing.border.TitledBorder;
56 import javax.swing.filechooser.FileFilter;
57 import javax.swing.plaf.basic.BasicFileChooserUI;
59 import jalview.bin.Cache;
60 import jalview.gui.JvOptionPane;
61 import jalview.util.ChannelProperties;
62 import jalview.util.MessageManager;
63 import jalview.util.Platform;
64 import jalview.util.dialogrunner.DialogRunnerI;
67 * Enhanced file chooser dialog box.
69 * NOTE: bug on Windows systems when filechooser opened on directory to view
70 * files with colons in title.
75 public class JalviewFileChooser extends JFileChooser
76 implements DialogRunnerI, PropertyChangeListener
78 private static final long serialVersionUID = 1L;
80 private Map<Object, Runnable> callbacks = new HashMap<>();
82 File selectedFile = null;
85 * backupfilesCheckBox = "Include backup files" checkbox includeBackupfiles =
86 * flag set by checkbox
88 private JCheckBox backupfilesCheckBox = null;
90 protected boolean includeBackupFiles = false;
93 * Factory method to return a file chooser that offers readable alignment file
100 public static JalviewFileChooser forRead(String directory,
103 return JalviewFileChooser.forRead(directory, selected, false);
106 public static JalviewFileChooser forRead(String directory,
107 String selected, boolean allowBackupFiles)
109 List<String> extensions = new ArrayList<>();
110 List<String> descs = new ArrayList<>();
111 for (FileFormatI format : FileFormats.getInstance().getFormats())
113 if (format.isReadable())
115 extensions.add(format.getExtensions());
116 descs.add(format.getName());
120 return new JalviewFileChooser(directory,
121 extensions.toArray(new String[extensions.size()]),
122 descs.toArray(new String[descs.size()]), selected, true,
127 * Factory method to return a file chooser that offers writable alignment file
134 public static JalviewFileChooser forWrite(String directory,
137 // TODO in Java 8, forRead and forWrite can be a single method
138 // with a lambda expression parameter for isReadable/isWritable
139 List<String> extensions = new ArrayList<>();
140 List<String> descs = new ArrayList<>();
141 for (FileFormatI format : FileFormats.getInstance().getFormats())
143 if (format.isWritable())
145 extensions.add(format.getExtensions());
146 descs.add(format.getName());
149 return new JalviewFileChooser(directory,
150 extensions.toArray(new String[extensions.size()]),
151 descs.toArray(new String[descs.size()]), selected, false);
154 public JalviewFileChooser(String dir)
156 super(safePath(dir));
157 setAccessory(new RecentlyOpened());
160 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
163 this(dir, suffix, desc, selected, true);
167 * Constructor for a single choice of file extension and description
172 public JalviewFileChooser(String extension, String desc)
174 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
176 { desc }, desc, true);
179 JalviewFileChooser(String dir, String[] extensions, String[] descs,
180 String selected, boolean acceptAny)
182 this(dir, extensions, descs, selected, acceptAny, false);
185 public JalviewFileChooser(String dir, String[] extensions, String[] descs,
186 String selected, boolean acceptAny, boolean allowBackupFiles)
188 super(safePath(dir));
189 if (extensions.length == descs.length)
191 List<String[]> formats = new ArrayList<>();
192 for (int i = 0; i < extensions.length; i++)
194 formats.add(new String[] { extensions[i], descs[i] });
196 init(formats, selected, acceptAny, allowBackupFiles);
200 jalview.bin.Console.errPrintln("JalviewFileChooser arguments mismatch: "
201 + extensions + ", " + descs);
205 private static File safePath(String dir)
212 File f = new File(dir);
213 if (f.getName().indexOf(':') > -1)
221 * Overridden for JalviewJS compatibility: only one thread in Javascript, so
222 * we can't wait for user choice in another thread and then perform the
226 public int showOpenDialog(Component parent)
228 int value = super.showOpenDialog(this);
230 if (!Platform.isJS())
238 * code here is not run in JalviewJS, instead
239 * propertyChange() is called for dialog action
241 handleResponse(value);
249 * a list of {extensions, description} for each file format
252 * if true, 'any format' option is included
254 void init(List<String[]> formats, String selected, boolean acceptAny)
256 init(formats, selected, acceptAny, false);
259 void init(List<String[]> formats, String selected, boolean acceptAny,
260 boolean allowBackupFiles)
263 JalviewFileFilter chosen = null;
265 // SelectAllFilter needs to be set first before adding further
266 // file filters to fix bug on Mac OSX
267 setAcceptAllFileFilterUsed(acceptAny);
269 // add a "All known alignment files" option
270 List<String> allExtensions = new ArrayList<>();
271 for (String[] format : formats)
273 String[] extensions = format[0].split(",");
274 for (String ext : extensions)
276 if (!allExtensions.contains(ext))
278 allExtensions.add(ext);
282 allExtensions.sort(null);
283 JalviewFileFilter alljvf = new JalviewFileFilter(
284 allExtensions.toArray(new String[] {}),
285 MessageManager.getString("label.all_known_alignment_files"));
286 alljvf.setExtensionListInDescription(false);
287 addChoosableFileFilter(alljvf);
289 if (selected == null)
294 for (String[] format : formats)
296 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
297 if (allowBackupFiles)
299 jvf.setParentJFC(this);
301 addChoosableFileFilter(jvf);
302 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
310 setFileFilter(chosen);
313 if (allowBackupFiles)
315 JPanel multi = new JPanel();
316 multi.setLayout(new BoxLayout(multi, BoxLayout.PAGE_AXIS));
317 if (backupfilesCheckBox == null)
321 includeBackupFiles = Boolean.parseBoolean(
322 Cache.getProperty(BackupFiles.NS + "_FC_INCLUDE"));
323 } catch (Exception e)
325 includeBackupFiles = false;
327 backupfilesCheckBox = new JCheckBox(
328 MessageManager.getString("label.include_backup_files"),
330 backupfilesCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT);
331 JalviewFileChooser jfc = this;
332 backupfilesCheckBox.addActionListener(new ActionListener()
335 public void actionPerformed(ActionEvent e)
337 includeBackupFiles = backupfilesCheckBox.isSelected();
338 Cache.setProperty(BackupFiles.NS + "_FC_INCLUDE",
339 String.valueOf(includeBackupFiles));
341 FileFilter f = jfc.getFileFilter();
342 // deselect the selected file if it's no longer choosable
343 File selectedFile = jfc.getSelectedFile();
344 if (selectedFile != null && !f.accept(selectedFile))
346 jfc.setSelectedFile(null);
348 // fake the OK button changing (to force it to upate)
349 String s = jfc.getApproveButtonText();
350 jfc.firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY,
352 // fake the file filter changing (its behaviour actually has)
353 jfc.firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, null, f);
355 jfc.rescanCurrentDirectory();
361 multi.add(new RecentlyOpened());
362 multi.add(backupfilesCheckBox);
367 // set includeBackupFiles=false to avoid other file choosers from picking
368 // up backup files (Just In Case)
369 includeBackupFiles = false;
370 setAccessory(new RecentlyOpened());
375 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
377 super.setFileFilter(filter);
381 if (getUI() instanceof BasicFileChooserUI)
383 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
384 final String name = fcui.getFileName().trim();
386 if ((name == null) || (name.length() == 0))
391 EventQueue.invokeLater(new Thread()
396 String currentName = fcui.getFileName();
397 if ((currentName == null) || (currentName.length() == 0))
399 fcui.setFileName(name);
404 } catch (Exception ex)
406 ex.printStackTrace();
407 // Some platforms do not have BasicFileChooserUI
412 * Returns the selected file format, or null if none selected
416 public FileFormatI getSelectedFormat()
418 if (getFileFilter() == null)
424 * logic here depends on option description being formatted as
425 * formatName (extension, extension...)
426 * or the 'no option selected' value
428 * @see JalviewFileFilter.getDescription
430 String format = getFileFilter().getDescription();
431 int parenPos = format.indexOf("(");
434 format = format.substring(0, parenPos).trim();
437 return FileFormats.getInstance().forName(format);
438 } catch (IllegalArgumentException e)
440 jalview.bin.Console.errPrintln("Unexpected format: " + format);
447 public File getSelectedFile()
449 File f = super.getSelectedFile();
450 return f == null ? selectedFile : f;
454 public int showSaveDialog(Component parent) throws HeadlessException
456 this.setAccessory(null);
457 // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
458 this.setSelectedFile(null);
460 return super.showSaveDialog(parent);
464 * If doing a Save, and an existing file is chosen or entered, prompt for
465 * confirmation of overwrite. Proceed if Yes, else leave the file chooser
468 * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
471 public void approveSelection()
473 if (getDialogType() != SAVE_DIALOG)
475 super.approveSelection();
479 selectedFile = getSelectedFile();
481 if (selectedFile == null)
483 // Workaround for Java 9,10 on OSX - no selected file, but there is a
487 String filename = ((BasicFileChooserUI) getUI()).getFileName();
488 if (filename != null && filename.length() > 0)
490 selectedFile = new File(getCurrentDirectory(), filename);
492 } catch (Throwable x)
494 jalview.bin.Console.errPrintln(
495 "Unexpected exception when trying to get filename.");
498 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
500 // USER PROMPTED FOR A NEW FILENAME
503 if (selectedFile == null)
508 if (getFileFilter() instanceof JalviewFileFilter)
510 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
512 if (!jvf.accept(selectedFile))
514 String withExtension = getSelectedFile().getName() + "."
515 + jvf.getAcceptableExtension();
516 selectedFile = (new File(getCurrentDirectory(), withExtension));
517 setSelectedFile(selectedFile);
521 if (selectedFile.exists())
523 int confirm = Cache.getDefault("CONFIRM_OVERWRITE_FILE", true)
524 ? JvOptionPane.showConfirmDialog(this,
526 .getString("label.overwrite_existing_file"),
527 MessageManager.getString("label.file_already_exists"),
528 JvOptionPane.YES_NO_OPTION)
529 : JOptionPane.YES_OPTION;
531 if (confirm != JvOptionPane.YES_OPTION)
537 super.approveSelection();
540 void recentListSelectionChanged(Object selection)
542 setSelectedFile(null);
543 if (selection != null)
545 File file = new File((String) selection);
546 if (getFileFilter() instanceof JalviewFileFilter)
548 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
550 if (!jvf.accept(file))
552 setFileFilter(getChoosableFileFilters()[0]);
556 if (!file.isAbsolute() && file.exists())
558 file = file.getAbsoluteFile();
561 setSelectedFile(file);
565 class RecentlyOpened extends JPanel
567 private static final long serialVersionUID = 1L;
573 setPreferredSize(new Dimension(300, 100));
574 String historyItems = Cache.getProperty("RECENT_FILE");
576 Vector<String> recent = new Vector<>();
578 if (historyItems != null)
580 st = new StringTokenizer(historyItems, "\t");
582 while (st.hasMoreTokens())
584 recent.addElement(st.nextToken());
588 list = new JList<>(recent);
589 list.setCellRenderer(new recentlyOpenedCellRenderer());
591 list.addMouseListener(new MouseAdapter()
594 public void mousePressed(MouseEvent evt)
596 recentListSelectionChanged(list.getSelectedValue());
600 TitledBorder recentlyOpenedBorder = new TitledBorder(
601 MessageManager.getString("label.recently_opened"));
602 recentlyOpenedBorder.setTitleFont(
603 recentlyOpenedBorder.getTitleFont().deriveFont(10f));
604 this.setBorder(recentlyOpenedBorder);
606 final JScrollPane scroller = new JScrollPane(list);
608 SpringLayout layout = new SpringLayout();
609 layout.putConstraint(SpringLayout.WEST, scroller, 5,
610 SpringLayout.WEST, this);
611 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
612 SpringLayout.NORTH, this);
614 // one size okay for all
615 scroller.setPreferredSize(new Dimension(280, 105));
618 SwingUtilities.invokeLater(new Runnable()
623 scroller.getHorizontalScrollBar()
624 .setValue(scroller.getHorizontalScrollBar().getMaximum());
632 class recentlyOpenedCellRenderer extends JLabel
633 implements ListCellRenderer<String>
635 private final static int maxChars = 46;
637 private final static String ellipsis = "...";
640 public Component getListCellRendererComponent(
641 JList<? extends String> list, String value, int index,
642 boolean isSelected, boolean cellHasFocus)
644 String filename = value.toString();
645 String displayFilename;
646 if (filename.length() > maxChars)
648 StringBuilder displayFileSB = new StringBuilder();
649 File file = new File(filename);
650 displayFileSB.append(file.getName());
651 if (file.getParent() != null)
654 boolean spaceleft = true;
655 while (spaceleft && parent.getParent() != null)
657 parent = parent.getParentFile();
658 String name = parent.getName();
659 displayFileSB.insert(0, File.separator);
660 if (displayFileSB.length() + name.length() < maxChars - 1)
662 displayFileSB.insert(0, name);
666 displayFileSB.insert(0, ellipsis);
670 if (spaceleft && filename.startsWith(File.separator)
671 && !(displayFileSB.charAt(0) == File.separatorChar))
673 displayFileSB.insert(0, File.separator);
676 displayFilename = displayFileSB.toString();
680 displayFilename = filename;
682 this.setText(displayFilename.toString());
683 this.setToolTipText(filename);
686 setBackground(list.getSelectionBackground());
687 setForeground(list.getSelectionForeground());
691 setBackground(list.getBackground());
692 setForeground(list.getForeground());
694 this.setHorizontalAlignment(SwingConstants.TRAILING);
695 this.setEnabled(list.isEnabled());
696 this.setFont(list.getFont().deriveFont(12f));
697 this.setOpaque(true);
705 public JalviewFileChooser setResponseHandler(Object response,
708 callbacks.put(response, new Callable<Void>()
722 public DialogRunnerI setResponseHandler(Object response, Runnable action)
724 callbacks.put(response, action);
729 public void handleResponse(Object response)
732 * this test is for NaN in Chrome
734 if (response != null && !response.equals(response))
738 Runnable action = callbacks.get(response);
744 } catch (Exception e)
752 * JalviewJS signals file selection by a property change event for property
753 * "SelectedFile". This methods responds to that by running the response
754 * action for 'OK' in the dialog.
759 public void propertyChange(PropertyChangeEvent evt)
761 // TODO other properties need runners...
762 switch (evt.getPropertyName())
765 * property name here matches that used in JFileChooser.js
768 handleResponse(APPROVE_OPTION);
774 protected JDialog createDialog(Component parent) throws HeadlessException
776 JDialog dialog = super.createDialog(parent);
777 dialog.setIconImages(ChannelProperties.getIconList());