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.DefaultListCellRenderer;
44 import javax.swing.JCheckBox;
45 import javax.swing.JDialog;
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.filechooser.FileFilter;
52 import javax.swing.plaf.basic.BasicFileChooserUI;
54 import jalview.bin.Cache;
55 import jalview.gui.JvOptionPane;
56 import jalview.util.ChannelProperties;
57 import jalview.util.MessageManager;
58 import jalview.util.Platform;
59 import jalview.util.dialogrunner.DialogRunnerI;
62 * Enhanced file chooser dialog box.
64 * NOTE: bug on Windows systems when filechooser opened on directory to view
65 * files with colons in title.
70 public class JalviewFileChooser extends JFileChooser
71 implements DialogRunnerI, PropertyChangeListener
73 private static final long serialVersionUID = 1L;
75 private Map<Object, Runnable> callbacks = new HashMap<>();
77 File selectedFile = null;
80 * backupfilesCheckBox = "Include backup files" checkbox includeBackupfiles =
81 * flag set by checkbox
83 private JCheckBox backupfilesCheckBox = null;
85 protected boolean includeBackupFiles = false;
88 * Factory method to return a file chooser that offers readable alignment file
95 public static JalviewFileChooser forRead(String directory,
98 return JalviewFileChooser.forRead(directory, selected, false);
101 public static JalviewFileChooser forRead(String directory,
102 String selected, boolean allowBackupFiles)
104 List<String> extensions = new ArrayList<>();
105 List<String> descs = new ArrayList<>();
106 for (FileFormatI format : FileFormats.getInstance().getFormats())
108 if (format.isReadable())
110 extensions.add(format.getExtensions());
111 descs.add(format.getName());
115 return new JalviewFileChooser(directory,
116 extensions.toArray(new String[extensions.size()]),
117 descs.toArray(new String[descs.size()]), selected, true,
122 * Factory method to return a file chooser that offers writable alignment file
129 public static JalviewFileChooser forWrite(String directory,
132 // TODO in Java 8, forRead and forWrite can be a single method
133 // with a lambda expression parameter for isReadable/isWritable
134 List<String> extensions = new ArrayList<>();
135 List<String> descs = new ArrayList<>();
136 for (FileFormatI format : FileFormats.getInstance().getFormats())
138 if (format.isWritable())
140 extensions.add(format.getExtensions());
141 descs.add(format.getName());
144 return new JalviewFileChooser(directory,
145 extensions.toArray(new String[extensions.size()]),
146 descs.toArray(new String[descs.size()]), selected, false);
149 public JalviewFileChooser(String dir)
151 super(safePath(dir));
152 setAccessory(new RecentlyOpened());
155 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
158 this(dir, suffix, desc, selected, true);
162 * Constructor for a single choice of file extension and description
167 public JalviewFileChooser(String extension, String desc)
169 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
171 { desc }, desc, true);
174 JalviewFileChooser(String dir, String[] extensions, String[] descs,
175 String selected, boolean acceptAny)
177 this(dir, extensions, descs, selected, acceptAny, false);
180 public JalviewFileChooser(String dir, String[] extensions, String[] descs,
181 String selected, boolean acceptAny, boolean allowBackupFiles)
183 super(safePath(dir));
184 if (extensions.length == descs.length)
186 List<String[]> formats = new ArrayList<>();
187 for (int i = 0; i < extensions.length; i++)
189 formats.add(new String[] { extensions[i], descs[i] });
191 init(formats, selected, acceptAny, allowBackupFiles);
195 System.err.println("JalviewFileChooser arguments mismatch: "
196 + extensions + ", " + descs);
200 private static File safePath(String dir)
207 File f = new File(dir);
208 if (f.getName().indexOf(':') > -1)
216 * Overridden for JalviewJS compatibility: only one thread in Javascript, so
217 * we can't wait for user choice in another thread and then perform the
221 public int showOpenDialog(Component parent)
223 int value = super.showOpenDialog(this);
225 if (!Platform.isJS())
233 * code here is not run in JalviewJS, instead
234 * propertyChange() is called for dialog action
236 handleResponse(value);
244 * a list of {extensions, description} for each file format
247 * if true, 'any format' option is included
249 void init(List<String[]> formats, String selected, boolean acceptAny)
251 init(formats, selected, acceptAny, false);
254 void init(List<String[]> formats, String selected, boolean acceptAny,
255 boolean allowBackupFiles)
258 JalviewFileFilter chosen = null;
260 // SelectAllFilter needs to be set first before adding further
261 // file filters to fix bug on Mac OSX
262 setAcceptAllFileFilterUsed(acceptAny);
264 for (String[] format : formats)
266 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
267 if (allowBackupFiles)
269 jvf.setParentJFC(this);
271 addChoosableFileFilter(jvf);
272 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
280 setFileFilter(chosen);
283 if (allowBackupFiles)
285 JPanel multi = new JPanel();
286 multi.setLayout(new BoxLayout(multi, BoxLayout.PAGE_AXIS));
287 if (backupfilesCheckBox == null)
291 includeBackupFiles = Boolean.parseBoolean(
292 Cache.getProperty(BackupFiles.NS + "_FC_INCLUDE"));
293 } catch (Exception e)
295 includeBackupFiles = false;
297 backupfilesCheckBox = new JCheckBox(
298 MessageManager.getString("label.include_backup_files"),
300 backupfilesCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT);
301 JalviewFileChooser jfc = this;
302 backupfilesCheckBox.addActionListener(new ActionListener()
305 public void actionPerformed(ActionEvent e)
307 includeBackupFiles = backupfilesCheckBox.isSelected();
308 Cache.setProperty(BackupFiles.NS + "_FC_INCLUDE",
309 String.valueOf(includeBackupFiles));
311 FileFilter f = jfc.getFileFilter();
312 // deselect the selected file if it's no longer choosable
313 File selectedFile = jfc.getSelectedFile();
314 if (selectedFile != null && !f.accept(selectedFile))
316 jfc.setSelectedFile(null);
318 // fake the OK button changing (to force it to upate)
319 String s = jfc.getApproveButtonText();
320 jfc.firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY,
322 // fake the file filter changing (its behaviour actually has)
323 jfc.firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, null, f);
325 jfc.rescanCurrentDirectory();
331 multi.add(new RecentlyOpened());
332 multi.add(backupfilesCheckBox);
337 // set includeBackupFiles=false to avoid other file choosers from picking
338 // up backup files (Just In Case)
339 includeBackupFiles = false;
340 setAccessory(new RecentlyOpened());
345 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
347 super.setFileFilter(filter);
351 if (getUI() instanceof BasicFileChooserUI)
353 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
354 final String name = fcui.getFileName().trim();
356 if ((name == null) || (name.length() == 0))
361 EventQueue.invokeLater(new Thread()
366 String currentName = fcui.getFileName();
367 if ((currentName == null) || (currentName.length() == 0))
369 fcui.setFileName(name);
374 } catch (Exception ex)
376 ex.printStackTrace();
377 // Some platforms do not have BasicFileChooserUI
382 * Returns the selected file format, or null if none selected
386 public FileFormatI getSelectedFormat()
388 if (getFileFilter() == null)
394 * logic here depends on option description being formatted as
395 * formatName (extension, extension...)
396 * or the 'no option selected' value
398 * @see JalviewFileFilter.getDescription
400 String format = getFileFilter().getDescription();
401 int parenPos = format.indexOf("(");
404 format = format.substring(0, parenPos).trim();
407 return FileFormats.getInstance().forName(format);
408 } catch (IllegalArgumentException e)
410 System.err.println("Unexpected format: " + format);
417 public File getSelectedFile()
419 File f = super.getSelectedFile();
420 return f == null ? selectedFile : f;
424 public int showSaveDialog(Component parent) throws HeadlessException
426 this.setAccessory(null);
427 // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
428 this.setSelectedFile(null);
430 return super.showSaveDialog(parent);
434 * If doing a Save, and an existing file is chosen or entered, prompt for
435 * confirmation of overwrite. Proceed if Yes, else leave the file chooser
438 * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
441 public void approveSelection()
443 if (getDialogType() != SAVE_DIALOG)
445 super.approveSelection();
449 selectedFile = getSelectedFile();
451 if (selectedFile == null)
453 // Workaround for Java 9,10 on OSX - no selected file, but there is a
457 String filename = ((BasicFileChooserUI) getUI()).getFileName();
458 if (filename != null && filename.length() > 0)
460 selectedFile = new File(getCurrentDirectory(), filename);
462 } catch (Throwable x)
465 "Unexpected exception when trying to get filename.");
468 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
470 // USER PROMPTED FOR A NEW FILENAME
473 if (selectedFile == null)
478 if (getFileFilter() instanceof JalviewFileFilter)
480 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
482 if (!jvf.accept(selectedFile))
484 String withExtension = getSelectedFile().getName() + "."
485 + jvf.getAcceptableExtension();
486 selectedFile = (new File(getCurrentDirectory(), withExtension));
487 setSelectedFile(selectedFile);
491 if (selectedFile.exists())
493 int confirm = JvOptionPane.showConfirmDialog(this,
494 MessageManager.getString("label.overwrite_existing_file"),
495 MessageManager.getString("label.file_already_exists"),
496 JvOptionPane.YES_NO_OPTION);
498 if (confirm != JvOptionPane.YES_OPTION)
504 super.approveSelection();
507 void recentListSelectionChanged(Object selection)
509 setSelectedFile(null);
510 if (selection != null)
512 File file = new File((String) selection);
513 if (getFileFilter() instanceof JalviewFileFilter)
515 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
517 if (!jvf.accept(file))
519 setFileFilter(getChoosableFileFilters()[0]);
523 setSelectedFile(file);
527 class RecentlyOpened extends JPanel
529 private static final long serialVersionUID = 1L;
535 setPreferredSize(new Dimension(300, 100));
536 String historyItems = Cache.getProperty("RECENT_FILE");
538 Vector<String> recent = new Vector<>();
540 if (historyItems != null)
542 st = new StringTokenizer(historyItems, "\t");
544 while (st.hasMoreTokens())
546 recent.addElement(st.nextToken());
550 list = new JList<>(recent);
552 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
553 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
554 list.setCellRenderer(dlcr);
556 list.addMouseListener(new MouseAdapter()
559 public void mousePressed(MouseEvent evt)
561 recentListSelectionChanged(list.getSelectedValue());
565 this.setBorder(new javax.swing.border.TitledBorder(
566 MessageManager.getString("label.recently_opened")));
568 final JScrollPane scroller = new JScrollPane(list);
570 SpringLayout layout = new SpringLayout();
571 layout.putConstraint(SpringLayout.WEST, scroller, 5,
572 SpringLayout.WEST, this);
573 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
574 SpringLayout.NORTH, this);
576 if (Platform.isAMacAndNotJS())
578 scroller.setPreferredSize(new Dimension(500, 100));
582 scroller.setPreferredSize(new Dimension(530, 200));
587 javax.swing.SwingUtilities.invokeLater(new Runnable()
592 scroller.getHorizontalScrollBar()
593 .setValue(scroller.getHorizontalScrollBar().getMaximum());
602 public DialogRunnerI setResponseHandler(Object response, Runnable action)
604 callbacks.put(response, action);
609 public void handleResponse(Object response)
612 * this test is for NaN in Chrome
614 if (response != null && !response.equals(response))
618 Runnable action = callbacks.get(response);
626 * JalviewJS signals file selection by a property change event for property
627 * "SelectedFile". This methods responds to that by running the response
628 * action for 'OK' in the dialog.
633 public void propertyChange(PropertyChangeEvent evt)
635 // TODO other properties need runners...
636 switch (evt.getPropertyName())
639 * property name here matches that used in JFileChooser.js
642 handleResponse(APPROVE_OPTION);
648 protected JDialog createDialog(Component parent) throws HeadlessException
650 JDialog dialog = super.createDialog(parent);
651 dialog.setIconImages(ChannelProperties.getIconList());