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.bin.Jalview;
26 import jalview.gui.JvOptionPane;
27 import jalview.util.MessageManager;
28 import jalview.util.Platform;
29 import jalview.util.dialogrunner.DialogRunner;
30 import jalview.util.dialogrunner.DialogRunnerI;
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.SwingUtilities;
54 import javax.swing.border.TitledBorder;
55 import javax.swing.plaf.basic.BasicFileChooserUI;
58 * Enhanced file chooser dialog box.
60 * NOTE: bug on Windows systems when filechooser opened on directory to view
61 * files with colons in title.
66 public class JalviewFileChooser extends JFileChooser implements DialogRunnerI,
67 PropertyChangeListener
69 private static final long serialVersionUID = 1L;
71 private DialogRunnerI runner = new DialogRunner();
73 File selectedFile = null;
76 * On user selecting a file to save to, this response is run to check if the
77 * file already exists, and if so show a dialog to prompt for confirmation of
80 RunResponse overwriteCheck = new RunResponse(JalviewFileChooser.APPROVE_OPTION)
85 selectedFile = getSelectedFile();
87 if (selectedFile == null)
89 // Workaround for Java 9,10 on OSX - no selected file, but there is a
91 // TODO is this needed in Java 8 or 11?
94 String filename = ((BasicFileChooserUI) getUI()).getFileName();
95 if (filename != null && filename.length() > 0)
97 selectedFile = new File(getCurrentDirectory(), filename);
102 "Unexpected exception when trying to get filename.");
106 if (selectedFile == null)
108 setReturnValue(JalviewFileChooser.CANCEL_OPTION);
111 // JBP Note - this code was executed regardless of 'SAVE' being pressed
112 // need to see if there were side effects
113 if (getFileFilter() instanceof JalviewFileFilter)
115 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
117 if (!jvf.accept(getSelectedFile()))
119 String withExtension = getSelectedFile() + "."
120 + jvf.getAcceptableExtension();
121 setSelectedFile(new File(withExtension));
124 // All good, so we continue to save
125 setReturnValue(JalviewFileChooser.APPROVE_OPTION);
127 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
128 // USER PROMPTED FOR A NEW FILENAME
131 if (getSelectedFile().exists())
133 // JAL-3048 - may not need to raise this for browser saves
135 int confirm = JvOptionPane.showConfirmDialog(JalviewFileChooser.this,
136 MessageManager.getString("label.overwrite_existing_file"),
137 MessageManager.getString("label.file_already_exists"),
138 JvOptionPane.YES_NO_OPTION);
140 if (confirm != JvOptionPane.YES_OPTION)
142 setReturnValue(JalviewFileChooser.CANCEL_OPTION);
150 * Factory method to return a file chooser that offers readable alignment file
157 public static JalviewFileChooser forRead(String directory,
160 List<String> extensions = new ArrayList<>();
161 List<String> descs = new ArrayList<>();
162 for (FileFormatI format : FileFormats.getInstance().getFormats())
164 if (format.isReadable())
166 extensions.add(format.getExtensions());
167 descs.add(format.getName());
170 return new JalviewFileChooser(directory,
171 extensions.toArray(new String[extensions.size()]),
172 descs.toArray(new String[descs.size()]), selected, true);
176 * Factory method to return a file chooser that offers writable alignment file
183 public static JalviewFileChooser forWrite(String directory,
186 // TODO in Java 8, forRead and forWrite can be a single method
187 // with a lambda expression parameter for isReadable/isWritable
188 List<String> extensions = new ArrayList<>();
189 List<String> descs = new ArrayList<>();
190 for (FileFormatI format : FileFormats.getInstance().getFormats())
192 if (format.isWritable())
194 extensions.add(format.getExtensions());
195 descs.add(format.getName());
198 return new JalviewFileChooser(directory,
199 extensions.toArray(new String[extensions.size()]),
200 descs.toArray(new String[descs.size()]), selected, false);
203 public JalviewFileChooser(String dir)
205 super(safePath(dir));
206 setAccessory(new RecentlyOpened());
209 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
212 this(dir, suffix, desc, selected, true);
216 * Constructor for a single choice of file extension and description
221 public JalviewFileChooser(String extension, String desc)
223 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
225 { desc }, desc, true);
228 JalviewFileChooser(String dir, String[] extensions, String[] descs,
229 String selected, boolean acceptAny)
231 super(safePath(dir));
232 if (extensions.length == descs.length)
234 List<String[]> formats = new ArrayList<>();
235 for (int i = 0; i < extensions.length; i++)
237 formats.add(new String[] { extensions[i], descs[i] });
239 init(formats, selected, acceptAny);
243 System.err.println("JalviewFileChooser arguments mismatch: "
244 + extensions + ", " + descs);
248 private static File safePath(String dir)
255 File f = new File(dir);
256 if (f.getName().indexOf(':') > -1)
264 * Overridden for JalviewJS compatibility: only one thread in Javascript,
265 * so we can't wait for user choice in another thread and then perform the
269 public int showOpenDialog(Component parent)
271 // runner.resetResponses();
272 int value = super.showOpenDialog(this);
275 runner.handleResponse(value);
283 * a list of {extensions, description} for each file format
286 * if true, 'any format' option is included
288 void init(List<String[]> formats, String selected, boolean acceptAny)
291 JalviewFileFilter chosen = null;
293 // SelectAllFilter needs to be set first before adding further
294 // file filters to fix bug on Mac OSX
295 setAcceptAllFileFilterUsed(acceptAny);
297 for (String[] format : formats)
299 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
300 addChoosableFileFilter(jvf);
301 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
309 setFileFilter(chosen);
312 setAccessory(new RecentlyOpened());
316 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
318 super.setFileFilter(filter);
322 if (getUI() instanceof BasicFileChooserUI)
324 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
325 final String name = fcui.getFileName().trim();
327 if ((name == null) || (name.length() == 0))
332 EventQueue.invokeLater(new Thread()
337 String currentName = fcui.getFileName();
338 if ((currentName == null) || (currentName.length() == 0))
340 fcui.setFileName(name);
345 } catch (Exception ex)
347 ex.printStackTrace();
348 // Some platforms do not have BasicFileChooserUI
353 * Returns the selected file format, or null if none selected
357 public FileFormatI getSelectedFormat()
359 if (getFileFilter() == null)
365 * logic here depends on option description being formatted as
366 * formatName (extension, extension...)
367 * or the 'no option selected' value
369 * @see JalviewFileFilter.getDescription
371 String format = getFileFilter().getDescription();
372 int parenPos = format.indexOf("(");
375 format = format.substring(0, parenPos).trim();
378 return FileFormats.getInstance().forName(format);
379 } catch (IllegalArgumentException e)
381 System.err.println("Unexpected format: " + format);
388 public File getSelectedFile()
390 File f = super.getSelectedFile();
391 return f == null ? selectedFile : f;
395 * Overridden for JalviewJS compatibility: only one thread in Javascript,
396 * so we can't wait for user choice in another thread and then perform the
400 public int showSaveDialog(Component parent) throws HeadlessException
402 this.setAccessory(null);
403 this.setSelectedFile(null);
404 return super.showSaveDialog(parent);
408 * If doing a Save, and an existing file is chosen or entered, prompt for
409 * confirmation of overwrite. Proceed if Yes, else leave the file chooser
412 * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
415 public void approveSelection()
417 if (getDialogType() != SAVE_DIALOG)
419 super.approveSelection();
423 selectedFile = getSelectedFile();
425 if (selectedFile == null)
427 // Workaround for Java 9,10 on OSX - no selected file, but there is a
431 String filename = ((BasicFileChooserUI) getUI()).getFileName();
432 if (filename != null && filename.length() > 0)
434 selectedFile = new File(getCurrentDirectory(), filename);
436 } catch (Throwable x)
439 "Unexpected exception when trying to get filename.");
442 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
444 // USER PROMPTED FOR A NEW FILENAME
447 if (selectedFile == null)
452 if (getFileFilter() instanceof JalviewFileFilter)
454 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
456 if (!jvf.accept(selectedFile))
458 String withExtension = getSelectedFile().getName() + "."
459 + jvf.getAcceptableExtension();
460 selectedFile = (new File(getCurrentDirectory(), withExtension));
461 setSelectedFile(selectedFile);
465 if (selectedFile.exists())
467 int confirm = JvOptionPane.showConfirmDialog(this,
468 MessageManager.getString("label.overwrite_existing_file"),
469 MessageManager.getString("label.file_already_exists"),
470 JvOptionPane.YES_NO_OPTION);
471 if (confirm != JvOptionPane.YES_OPTION)
477 super.approveSelection();
480 void recentListSelectionChanged(Object selection)
482 setSelectedFile(null);
483 if (selection != null)
485 File file = new File((String) selection);
486 if (getFileFilter() instanceof JalviewFileFilter)
488 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
490 if (!jvf.accept(file))
492 setFileFilter(getChoosableFileFilters()[0]);
496 setSelectedFile(file);
501 * A panel to set as the 'accessory' component to the file chooser dialog,
502 * holding a list of recently opened files (if any). These are held as a
503 * tab-separated list of file paths under key <code>RECENT_FILE</code> in
504 * <code>.jalview_properties</code>. A click in the list calls a method in
505 * JalviewFileChooser to set the chosen file as the selection.
507 class RecentlyOpened extends JPanel
509 private static final long serialVersionUID = 1L;
515 String historyItems = Cache.getProperty("RECENT_FILE");
517 Vector<String> recent = new Vector<>();
519 if (historyItems != null)
521 st = new StringTokenizer(historyItems, "\t");
522 while (st.hasMoreTokens())
524 recent.addElement(st.nextToken());
528 list = new JList<>(recent);
530 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
531 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
532 list.setCellRenderer(dlcr);
534 list.addMouseListener(new MouseAdapter()
537 public void mousePressed(MouseEvent evt)
539 recentListSelectionChanged(list.getSelectedValue());
543 this.setBorder(new TitledBorder(
544 MessageManager.getString("label.recently_opened")));
546 final JScrollPane scroller = new JScrollPane(list);
548 SpringLayout layout = new SpringLayout();
549 layout.putConstraint(SpringLayout.WEST, scroller, 5,
550 SpringLayout.WEST, this);
551 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
552 SpringLayout.NORTH, this);
554 if (Platform.isAMac())
556 scroller.setPreferredSize(new Dimension(500, 100));
560 scroller.setPreferredSize(new Dimension(130, 200));
565 SwingUtilities.invokeLater(new Runnable()
570 scroller.getHorizontalScrollBar()
571 .setValue(scroller.getHorizontalScrollBar().getMaximum());
578 public DialogRunnerI addResponse(RunResponse action)
580 return runner.addResponse(action);
584 * JalviewJS signals file selection by a property change event
585 * for property "SelectedFile". This methods responds to that by
586 * running the response action for 'OK' in the dialog.
591 public void propertyChange(PropertyChangeEvent evt)
593 // TODO other properties need runners...
594 switch (evt.getPropertyName())
597 runner.handleResponse(APPROVE_OPTION);