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.DialogRunnerI;
29 import jalview.util.dialogrunner.Response;
30 import jalview.util.dialogrunner.RunResponse;
32 import java.awt.Component;
33 import java.awt.Dimension;
34 import java.awt.EventQueue;
35 import java.awt.HeadlessException;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.beans.PropertyChangeEvent;
39 import java.beans.PropertyChangeListener;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.StringTokenizer;
44 import java.util.Vector;
46 import javax.swing.DefaultListCellRenderer;
47 import javax.swing.JFileChooser;
48 import javax.swing.JList;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.SpringLayout;
52 import javax.swing.plaf.basic.BasicFileChooserUI;
55 * Enhanced file chooser dialog box.
57 * NOTE: bug on Windows systems when filechooser opened on directory to view
58 * files with colons in title.
63 public class JalviewFileChooser extends JFileChooser
64 implements PropertyChangeListener, DialogRunnerI
66 jalview.util.dialogrunner.DialogRunner<JalviewFileChooser> runner = new jalview.util.dialogrunner.DialogRunner<>(
69 * Factory method to return a file chooser that offers readable alignment file
76 public static JalviewFileChooser forRead(String directory,
79 List<String> extensions = new ArrayList<>();
80 List<String> descs = new ArrayList<>();
81 for (FileFormatI format : FileFormats.getInstance().getFormats())
83 if (format.isReadable())
85 extensions.add(format.getExtensions());
86 descs.add(format.getName());
89 return new JalviewFileChooser(directory,
90 extensions.toArray(new String[extensions.size()]),
91 descs.toArray(new String[descs.size()]), selected, true);
95 * Factory method to return a file chooser that offers writable alignment file
102 public static JalviewFileChooser forWrite(String directory,
105 // TODO in Java 8, forRead and forWrite can be a single method
106 // with a lambda expression parameter for isReadable/isWritable
107 List<String> extensions = new ArrayList<>();
108 List<String> descs = new ArrayList<>();
109 for (FileFormatI format : FileFormats.getInstance().getFormats())
111 if (format.isWritable())
113 extensions.add(format.getExtensions());
114 descs.add(format.getName());
117 return new JalviewFileChooser(directory,
118 extensions.toArray(new String[extensions.size()]),
119 descs.toArray(new String[descs.size()]), selected, false);
122 public JalviewFileChooser(String dir)
124 super(safePath(dir));
125 setAccessory(new RecentlyOpened());
128 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
131 this(dir, suffix, desc, selected, true);
135 * Constructor for a single choice of file extension and description
140 public JalviewFileChooser(String extension, String desc)
142 this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
144 { desc }, desc, true);
147 JalviewFileChooser(String dir, String[] extensions, String[] descs,
148 String selected, boolean allFiles)
150 super(safePath(dir));
151 if (extensions.length == descs.length)
153 List<String[]> formats = new ArrayList<>();
154 for (int i = 0; i < extensions.length; i++)
156 formats.add(new String[] { extensions[i], descs[i] });
158 init(formats, selected, allFiles);
162 System.err.println("JalviewFileChooser arguments mismatch: "
163 + extensions + ", " + descs);
168 public void propertyChange(PropertyChangeEvent evt)
170 // TODO other properties need runners...
171 switch (evt.getPropertyName())
174 runner.run(APPROVE_OPTION);
179 private static File safePath(String dir)
186 File f = new File(dir);
187 if (f.getName().indexOf(':') > -1)
194 public void openDialog(Component parent)
196 runner.resetResponses();
197 int value = showOpenDialog(this);
202 runner.firstRun(value);
209 * a list of {extensions, description} for each file format
212 * if true, 'any format' option is included
214 void init(List<String[]> formats, String selected, boolean allFiles)
217 JalviewFileFilter chosen = null;
219 // SelectAllFilter needs to be set first before adding further
220 // file filters to fix bug on Mac OSX
221 setAcceptAllFileFilterUsed(allFiles);
223 for (String[] format : formats)
225 JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
226 addChoosableFileFilter(jvf);
227 if ((selected != null) && selected.equalsIgnoreCase(format[1]))
235 setFileFilter(chosen);
238 setAccessory(new RecentlyOpened());
242 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
244 super.setFileFilter(filter);
248 if (getUI() instanceof BasicFileChooserUI)
250 final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
251 final String name = fcui.getFileName().trim();
253 if ((name == null) || (name.length() == 0))
258 EventQueue.invokeLater(new Thread()
263 String currentName = fcui.getFileName();
264 if ((currentName == null) || (currentName.length() == 0))
266 fcui.setFileName(name);
271 } catch (Exception ex)
273 ex.printStackTrace();
274 // Some platforms do not have BasicFileChooserUI
279 * Returns the selected file format, or null if none selected
283 public FileFormatI getSelectedFormat()
285 if (getFileFilter() == null)
291 * logic here depends on option description being formatted as
292 * formatName (extension, extension...)
293 * or the 'no option selected' value
295 * @see JalviewFileFilter.getDescription
297 String format = getFileFilter().getDescription();
298 int parenPos = format.indexOf("(");
301 format = format.substring(0, parenPos).trim();
304 return FileFormats.getInstance().forName(format);
305 } catch (IllegalArgumentException e)
307 System.err.println("Unexpected format: " + format);
313 Component saveparent;
314 RunResponse overwriteCheck = new RunResponse(
315 JalviewFileChooser.APPROVE_OPTION)
320 // JBP Note - this code was executed regardless of 'SAVE' being pressed
321 // need to see if there were side effects
322 if (getFileFilter() instanceof JalviewFileFilter)
324 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
326 if (!jvf.accept(getSelectedFile()))
328 String withExtension = getSelectedFile() + "."
329 + jvf.getAcceptableExtension();
330 setSelectedFile(new File(withExtension));
333 // All good, so we continue to save
334 returned = new Response(JalviewFileChooser.APPROVE_OPTION);
336 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
337 // USER PROMPTED FOR A NEW FILENAME
342 if (getSelectedFile().exists())
344 // JAL-3048 - may not need to raise this for browser saves
347 int confirm = JvOptionPane.showConfirmDialog(saveparent,
348 MessageManager.getString("label.overwrite_existing_file"),
349 MessageManager.getString("label.file_already_exists"),
350 JvOptionPane.YES_NO_OPTION);
352 if (confirm != JvOptionPane.YES_OPTION)
354 returned = new Response(JalviewFileChooser.CANCEL_OPTION);
362 public int showSaveDialog(Component parent) throws HeadlessException
364 this.setAccessory(null);
367 * Save dialog is opened until user picks a file format
369 if (!runner.isRegistered(overwriteCheck))
371 // first call for this instance
372 runner.firstResponse(overwriteCheck);
376 // reset response flags
377 runner.resetResponses();
380 setDialogType(SAVE_DIALOG);
383 int value = showDialog(parent, MessageManager.getString("action.save"));
388 runner.firstRun(value);
393 void recentListSelectionChanged(Object selection)
395 setSelectedFile(null);
396 if (selection != null)
398 File file = new File((String) selection);
399 if (getFileFilter() instanceof JalviewFileFilter)
401 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
403 if (!jvf.accept(file))
405 setFileFilter(getChoosableFileFilters()[0]);
409 setSelectedFile(file);
413 class RecentlyOpened extends JPanel
417 public RecentlyOpened()
420 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
422 Vector recent = new Vector();
424 if (historyItems != null)
426 st = new StringTokenizer(historyItems, "\t");
428 while (st.hasMoreTokens())
430 recent.addElement(st.nextElement());
434 list = new JList(recent);
436 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
437 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
438 list.setCellRenderer(dlcr);
440 list.addMouseListener(new MouseAdapter()
443 public void mousePressed(MouseEvent evt)
445 recentListSelectionChanged(list.getSelectedValue());
449 this.setBorder(new javax.swing.border.TitledBorder(
450 MessageManager.getString("label.recently_opened")));
452 final JScrollPane scroller = new JScrollPane(list);
454 SpringLayout layout = new SpringLayout();
455 layout.putConstraint(SpringLayout.WEST, scroller, 5,
456 SpringLayout.WEST, this);
457 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
458 SpringLayout.NORTH, this);
460 if (new Platform().isAMac())
462 scroller.setPreferredSize(new Dimension(500, 100));
466 scroller.setPreferredSize(new Dimension(130, 200));
471 javax.swing.SwingUtilities.invokeLater(new Runnable()
476 scroller.getHorizontalScrollBar()
477 .setValue(scroller.getHorizontalScrollBar().getMaximum());
486 public JalviewFileChooser response(RunResponse action)
488 return runner.response(action);