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.DialogRunner;
29 import jalview.util.dialogrunner.DialogRunnerI;
30 import jalview.util.dialogrunner.Response;
31 import jalview.util.dialogrunner.RunResponse;
33 import java.awt.Component;
34 import java.awt.Dimension;
35 import java.awt.EventQueue;
36 import java.awt.HeadlessException;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.beans.PropertyChangeEvent;
40 import java.beans.PropertyChangeListener;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.StringTokenizer;
45 import java.util.Vector;
47 import javax.swing.DefaultListCellRenderer;
48 import javax.swing.JFileChooser;
49 import javax.swing.JList;
50 import javax.swing.JPanel;
51 import javax.swing.JScrollPane;
52 import javax.swing.SpringLayout;
53 import javax.swing.plaf.basic.BasicFileChooserUI;
56 * Enhanced file chooser dialog box.
58 * NOTE: bug on Windows systems when filechooser opened on directory to view
59 * files with colons in title.
64 public class JalviewFileChooser extends JFileChooser
65 implements PropertyChangeListener, DialogRunnerI
67 DialogRunner<JalviewFileChooser> runner = new DialogRunner<>(this);
70 * Factory method to return a file chooser that offers readable alignment file
77 public static JalviewFileChooser forRead(String directory,
80 List<String> extensions = new ArrayList<>();
81 List<String> descs = new ArrayList<>();
82 for (FileFormatI format : FileFormats.getInstance().getFormats())
84 if (format.isReadable())
86 extensions.add(format.getExtensions());
87 descs.add(format.getName());
90 return new JalviewFileChooser(directory,
91 extensions.toArray(new String[extensions.size()]),
92 descs.toArray(new String[descs.size()]), selected, true);
96 * Factory method to return a file chooser that offers writable alignment file
103 public static JalviewFileChooser forWrite(String directory,
106 // TODO in Java 8, forRead and forWrite can be a single method
107 // with a lambda expression parameter for isReadable/isWritable
108 List<String> extensions = new ArrayList<>();
109 List<String> descs = new ArrayList<>();
110 for (FileFormatI format : FileFormats.getInstance().getFormats())
112 if (format.isWritable())
114 extensions.add(format.getExtensions());
115 descs.add(format.getName());
118 return new JalviewFileChooser(directory,
119 extensions.toArray(new String[extensions.size()]),
120 descs.toArray(new String[descs.size()]), selected, false);
123 public JalviewFileChooser(String dir)
125 super(safePath(dir));
126 setAccessory(new RecentlyOpened());
129 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
132 this(dir, suffix, desc, selected, true);
136 * Constructor for a single choice of file extension and description
141 public JalviewFileChooser(String extension, String desc)
143 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
145 { desc }, desc, true);
148 JalviewFileChooser(String dir, String[] extensions, String[] descs,
149 String selected, boolean allFiles)
151 super(safePath(dir));
152 if (extensions.length == descs.length)
154 List<String[]> formats = new ArrayList<>();
155 for (int i = 0; i < extensions.length; i++)
157 formats.add(new String[] { extensions[i], descs[i] });
159 init(formats, selected, allFiles);
163 System.err.println("JalviewFileChooser arguments mismatch: "
164 + extensions + ", " + descs);
169 public void propertyChange(PropertyChangeEvent evt)
171 // TODO other properties need runners...
172 switch (evt.getPropertyName())
175 runner.run(APPROVE_OPTION);
180 private static File safePath(String dir)
187 File f = new File(dir);
188 if (f.getName().indexOf(':') > -1)
196 * Overridden for JalviewJS compatibility: only one thread in Javascript,
197 * so we can't wait for user choice in another thread and then perform the
201 public int showOpenDialog(Component parent)
203 runner.resetResponses();
204 int value = super.showOpenDialog(this);
209 runner.firstRun(value);
217 * a list of {extensions, description} for each file format
220 * if true, 'any format' option is included
222 void init(List<String[]> formats, String selected, boolean allFiles)
225 JalviewFileFilter chosen = null;
227 // SelectAllFilter needs to be set first before adding further
228 // file filters to fix bug on Mac OSX
229 setAcceptAllFileFilterUsed(allFiles);
231 for (String[] format : formats)
233 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
234 addChoosableFileFilter(jvf);
235 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
243 setFileFilter(chosen);
246 setAccessory(new RecentlyOpened());
250 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
252 super.setFileFilter(filter);
256 if (getUI() instanceof BasicFileChooserUI)
258 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
259 final String name = fcui.getFileName().trim();
261 if ((name == null) || (name.length() == 0))
266 EventQueue.invokeLater(new Thread()
271 String currentName = fcui.getFileName();
272 if ((currentName == null) || (currentName.length() == 0))
274 fcui.setFileName(name);
279 } catch (Exception ex)
281 ex.printStackTrace();
282 // Some platforms do not have BasicFileChooserUI
287 * Returns the selected file format, or null if none selected
291 public FileFormatI getSelectedFormat()
293 if (getFileFilter() == null)
299 * logic here depends on option description being formatted as
300 * formatName (extension, extension...)
301 * or the 'no option selected' value
303 * @see JalviewFileFilter.getDescription
305 String format = getFileFilter().getDescription();
306 int parenPos = format.indexOf("(");
309 format = format.substring(0, parenPos).trim();
312 return FileFormats.getInstance().forName(format);
313 } catch (IllegalArgumentException e)
315 System.err.println("Unexpected format: " + format);
321 File ourselectedFile = null;
324 public File getSelectedFile()
326 File selfile = super.getSelectedFile();
327 if (selfile == null && ourselectedFile != null)
329 return ourselectedFile;
334 Component saveparent;
335 RunResponse overwriteCheck = new RunResponse(
336 JalviewFileChooser.APPROVE_OPTION)
341 ourselectedFile = getSelectedFile();
343 if (getSelectedFile() == null)
345 // Workaround for Java 9,10 on OSX - no selected file, but there is a
349 String filename = ((BasicFileChooserUI) getUI()).getFileName();
350 if (filename != null && filename.length() > 0)
352 ourselectedFile = new File(getCurrentDirectory(), filename);
354 } catch (Throwable x)
357 "Unexpected exception when trying to get filename.");
361 if (ourselectedFile == null)
363 returned = new Response(JalviewFileChooser.CANCEL_OPTION);
366 // JBP Note - this code was executed regardless of 'SAVE' being pressed
367 // need to see if there were side effects
368 if (getFileFilter() instanceof JalviewFileFilter)
370 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
372 if (!jvf.accept(getSelectedFile()))
374 String withExtension = getSelectedFile() + "."
375 + jvf.getAcceptableExtension();
376 setSelectedFile(new File(withExtension));
379 // All good, so we continue to save
380 returned = new Response(JalviewFileChooser.APPROVE_OPTION);
382 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
383 // USER PROMPTED FOR A NEW FILENAME
388 if (getSelectedFile().exists())
390 // JAL-3048 - may not need to raise this for browser saves
393 int confirm = JvOptionPane.showConfirmDialog(saveparent,
394 MessageManager.getString("label.overwrite_existing_file"),
395 MessageManager.getString("label.file_already_exists"),
396 JvOptionPane.YES_NO_OPTION);
398 if (confirm != JvOptionPane.YES_OPTION)
400 returned = new Response(JalviewFileChooser.CANCEL_OPTION);
408 * Overridden for JalviewJS compatibility: only one thread in Javascript,
409 * so we can't wait for user choice in another thread and then perform the
413 public int showSaveDialog(Component parent) throws HeadlessException
415 this.setAccessory(null);
418 * Save dialog is opened until user picks a file format
420 if (!runner.isRegistered(overwriteCheck))
422 // first call for this instance
423 runner.firstResponse(overwriteCheck);
427 // reset response flags
428 runner.resetResponses();
431 setDialogType(SAVE_DIALOG);
433 // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
434 this.setSelectedFile(null);
438 int value = showDialog(parent, MessageManager.getString("action.save"));
443 runner.firstRun(value);
448 void recentListSelectionChanged(Object selection)
450 setSelectedFile(null);
451 if (selection != null)
453 File file = new File((String) selection);
454 if (getFileFilter() instanceof JalviewFileFilter)
456 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
458 if (!jvf.accept(file))
460 setFileFilter(getChoosableFileFilters()[0]);
464 setSelectedFile(file);
468 class RecentlyOpened extends JPanel
472 public RecentlyOpened()
475 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
477 Vector<String> recent = new Vector<>();
479 if (historyItems != null)
481 st = new StringTokenizer(historyItems, "\t");
483 while (st.hasMoreTokens())
485 recent.addElement(st.nextToken());
489 list = new JList<>(recent);
491 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
492 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
493 list.setCellRenderer(dlcr);
495 list.addMouseListener(new MouseAdapter()
498 public void mousePressed(MouseEvent evt)
500 recentListSelectionChanged(list.getSelectedValue());
504 this.setBorder(new javax.swing.border.TitledBorder(
505 MessageManager.getString("label.recently_opened")));
507 final JScrollPane scroller = new JScrollPane(list);
509 SpringLayout layout = new SpringLayout();
510 layout.putConstraint(SpringLayout.WEST, scroller, 5,
511 SpringLayout.WEST, this);
512 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
513 SpringLayout.NORTH, this);
515 if (Platform.isAMac())
517 scroller.setPreferredSize(new Dimension(500, 100));
521 scroller.setPreferredSize(new Dimension(130, 200));
526 javax.swing.SwingUtilities.invokeLater(new Runnable()
531 scroller.getHorizontalScrollBar()
532 .setValue(scroller.getHorizontalScrollBar().getMaximum());
541 public JalviewFileChooser response(RunResponse action)
543 return runner.response(action);