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.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 implements DialogRunnerI,
65 PropertyChangeListener
67 private DialogRunnerI runner = new DialogRunner();
69 File selectedFile = null;
72 * On user selecting a file to save to, this response is run to check if the
73 * file already exists, and if so show a dialog to prompt for confirmation of
76 RunResponse overwriteCheck = new RunResponse(JalviewFileChooser.APPROVE_OPTION)
81 selectedFile = getSelectedFile();
83 if (selectedFile == null)
85 // Workaround for Java 9,10 on OSX - no selected file, but there is a
87 // TODO is this needed in Java 8 or 11?
90 String filename = ((BasicFileChooserUI) getUI()).getFileName();
91 if (filename != null && filename.length() > 0)
93 selectedFile = new File(getCurrentDirectory(), filename);
98 "Unexpected exception when trying to get filename.");
102 if (selectedFile == null)
104 setReturnValue(JalviewFileChooser.CANCEL_OPTION);
107 // JBP Note - this code was executed regardless of 'SAVE' being pressed
108 // need to see if there were side effects
109 if (getFileFilter() instanceof JalviewFileFilter)
111 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
113 if (!jvf.accept(getSelectedFile()))
115 String withExtension = getSelectedFile() + "."
116 + jvf.getAcceptableExtension();
117 setSelectedFile(new File(withExtension));
120 // All good, so we continue to save
121 setReturnValue(JalviewFileChooser.APPROVE_OPTION);
123 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
124 // USER PROMPTED FOR A NEW FILENAME
127 if (getSelectedFile().exists())
129 // JAL-3048 - may not need to raise this for browser saves
131 int confirm = JvOptionPane.showConfirmDialog(JalviewFileChooser.this,
132 MessageManager.getString("label.overwrite_existing_file"),
133 MessageManager.getString("label.file_already_exists"),
134 JvOptionPane.YES_NO_OPTION);
136 if (confirm != JvOptionPane.YES_OPTION)
138 setReturnValue(JalviewFileChooser.CANCEL_OPTION);
146 * Factory method to return a file chooser that offers readable alignment file
153 public static JalviewFileChooser forRead(String directory,
156 List<String> extensions = new ArrayList<>();
157 List<String> descs = new ArrayList<>();
158 for (FileFormatI format : FileFormats.getInstance().getFormats())
160 if (format.isReadable())
162 extensions.add(format.getExtensions());
163 descs.add(format.getName());
166 return new JalviewFileChooser(directory,
167 extensions.toArray(new String[extensions.size()]),
168 descs.toArray(new String[descs.size()]), selected, true);
172 * Factory method to return a file chooser that offers writable alignment file
179 public static JalviewFileChooser forWrite(String directory,
182 // TODO in Java 8, forRead and forWrite can be a single method
183 // with a lambda expression parameter for isReadable/isWritable
184 List<String> extensions = new ArrayList<>();
185 List<String> descs = new ArrayList<>();
186 for (FileFormatI format : FileFormats.getInstance().getFormats())
188 if (format.isWritable())
190 extensions.add(format.getExtensions());
191 descs.add(format.getName());
194 return new JalviewFileChooser(directory,
195 extensions.toArray(new String[extensions.size()]),
196 descs.toArray(new String[descs.size()]), selected, false);
199 public JalviewFileChooser(String dir)
201 super(safePath(dir));
202 setAccessory(new RecentlyOpened());
205 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
208 this(dir, suffix, desc, selected, true);
212 * Constructor for a single choice of file extension and description
217 public JalviewFileChooser(String extension, String desc)
219 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
221 { desc }, desc, true);
224 JalviewFileChooser(String dir, String[] extensions, String[] descs,
225 String selected, boolean acceptAny)
227 super(safePath(dir));
228 if (extensions.length == descs.length)
230 List<String[]> formats = new ArrayList<>();
231 for (int i = 0; i < extensions.length; i++)
233 formats.add(new String[] { extensions[i], descs[i] });
235 init(formats, selected, acceptAny);
239 System.err.println("JalviewFileChooser arguments mismatch: "
240 + extensions + ", " + descs);
244 private static File safePath(String dir)
251 File f = new File(dir);
252 if (f.getName().indexOf(':') > -1)
260 * Overridden for JalviewJS compatibility: only one thread in Javascript,
261 * so we can't wait for user choice in another thread and then perform the
265 public int showOpenDialog(Component parent)
267 // runner.resetResponses();
268 int value = super.showOpenDialog(this);
271 runner.handleResponse(value);
279 * a list of {extensions, description} for each file format
282 * if true, 'any format' option is included
284 void init(List<String[]> formats, String selected, boolean acceptAny)
287 JalviewFileFilter chosen = null;
289 // SelectAllFilter needs to be set first before adding further
290 // file filters to fix bug on Mac OSX
291 setAcceptAllFileFilterUsed(acceptAny);
293 for (String[] format : formats)
295 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
296 addChoosableFileFilter(jvf);
297 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
305 setFileFilter(chosen);
308 setAccessory(new RecentlyOpened());
312 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
314 super.setFileFilter(filter);
318 if (getUI() instanceof BasicFileChooserUI)
320 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
321 final String name = fcui.getFileName().trim();
323 if ((name == null) || (name.length() == 0))
328 EventQueue.invokeLater(new Thread()
333 String currentName = fcui.getFileName();
334 if ((currentName == null) || (currentName.length() == 0))
336 fcui.setFileName(name);
341 } catch (Exception ex)
343 ex.printStackTrace();
344 // Some platforms do not have BasicFileChooserUI
349 * Returns the selected file format, or null if none selected
353 public FileFormatI getSelectedFormat()
355 if (getFileFilter() == null)
361 * logic here depends on option description being formatted as
362 * formatName (extension, extension...)
363 * or the 'no option selected' value
365 * @see JalviewFileFilter.getDescription
367 String format = getFileFilter().getDescription();
368 int parenPos = format.indexOf("(");
371 format = format.substring(0, parenPos).trim();
374 return FileFormats.getInstance().forName(format);
375 } catch (IllegalArgumentException e)
377 System.err.println("Unexpected format: " + format);
384 public File getSelectedFile()
386 File f = super.getSelectedFile();
387 return f == null ? selectedFile : f;
391 * Overridden for JalviewJS compatibility: only one thread in Javascript,
392 * so we can't wait for user choice in another thread and then perform the
396 public int showSaveDialog(Component parent) throws HeadlessException
398 this.setAccessory(null);
401 * Save dialog is opened until user picks a file format
404 if (!runner.isRegistered(overwriteCheck))
406 // first call for this instance
407 runner.setFirstResponse(overwriteCheck);
411 // reset response flags
412 runner.resetResponses();
415 // runner.addResponse(overwriteCheck);
416 // setDialogType(SAVE_DIALOG);
418 // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
419 this.setSelectedFile(null);
421 int value = super.showSaveDialog(parent);//, MessageManager.getString("action.save"));
424 runner.handleResponse(value);
429 void recentListSelectionChanged(Object selection)
431 setSelectedFile(null);
432 if (selection != null)
434 File file = new File((String) selection);
435 if (getFileFilter() instanceof JalviewFileFilter)
437 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
439 if (!jvf.accept(file))
441 setFileFilter(getChoosableFileFilters()[0]);
445 setSelectedFile(file);
449 class RecentlyOpened extends JPanel
453 public RecentlyOpened()
456 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
458 Vector<String> recent = new Vector<>();
460 if (historyItems != null)
462 st = new StringTokenizer(historyItems, "\t");
464 while (st.hasMoreTokens())
466 recent.addElement(st.nextToken());
470 list = new JList<>(recent);
472 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
473 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
474 list.setCellRenderer(dlcr);
476 list.addMouseListener(new MouseAdapter()
479 public void mousePressed(MouseEvent evt)
481 recentListSelectionChanged(list.getSelectedValue());
485 this.setBorder(new javax.swing.border.TitledBorder(
486 MessageManager.getString("label.recently_opened")));
488 final JScrollPane scroller = new JScrollPane(list);
490 SpringLayout layout = new SpringLayout();
491 layout.putConstraint(SpringLayout.WEST, scroller, 5,
492 SpringLayout.WEST, this);
493 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
494 SpringLayout.NORTH, this);
496 if (Platform.isAMac())
498 scroller.setPreferredSize(new Dimension(500, 100));
502 scroller.setPreferredSize(new Dimension(130, 200));
507 javax.swing.SwingUtilities.invokeLater(new Runnable()
512 scroller.getHorizontalScrollBar()
513 .setValue(scroller.getHorizontalScrollBar().getMaximum());
522 public DialogRunnerI addResponse(RunResponse action)
524 return runner.addResponse(action);
528 * JalviewJS signals file selection by a property change event
529 * for property "SelectedFile". This methods responds to that by
530 * running the response action for 'OK' in the dialog.
535 public void propertyChange(PropertyChangeEvent evt)
537 // TODO other properties need runners...
538 switch (evt.getPropertyName())
541 runner.handleResponse(APPROVE_OPTION);
547 public void approveSelection()
549 if (getDialogType() == SAVE_DIALOG && !Jalview.isJS())
551 File selectedFile = getSelectedFile();
552 if ((selectedFile != null) && selectedFile.exists())
554 int confirm = JvOptionPane.showConfirmDialog(this,
555 MessageManager.getString("label.overwrite_existing_file"),
556 MessageManager.getString("label.file_already_exists"), JvOptionPane.YES_NO_OPTION);
558 if (confirm != JvOptionPane.YES_OPTION)
564 super.approveSelection();