2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.util.MessageManager;
26 import java.awt.Component;
27 import java.awt.Dimension;
28 import java.awt.EventQueue;
29 import java.awt.HeadlessException;
30 import java.awt.event.MouseAdapter;
31 import java.awt.event.MouseEvent;
33 import java.util.StringTokenizer;
34 import java.util.Vector;
36 import javax.swing.DefaultListCellRenderer;
37 import javax.swing.JFileChooser;
38 import javax.swing.JList;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
44 * Enhanced file chooser dialog box.
46 * NOTE: bug on Windows systems when filechooser opened on directory to view
47 * files with colons in title.
52 public class JalviewFileChooser extends JFileChooser
54 public JalviewFileChooser(String dir)
57 setAccessory(new RecentlyOpened());
60 private static File safePath(String dir)
67 File f = new File(dir);
68 if (f.getName().indexOf(':') > -1)
75 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
76 String selected, boolean selectAll)
79 init(suffix, desc, selected, selectAll);
82 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
86 init(suffix, desc, selected, true);
89 void init(String[] suffix, String[] desc, String selected,
93 JalviewFileFilter chosen = null;
95 // SelectAllFilter needs to be set first before adding further
96 // file filters to fix bug on Mac OSX
97 setAcceptAllFileFilterUsed(selectAll);
99 for (int i = 0; i < suffix.length; i++)
101 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
102 addChoosableFileFilter(jvf);
103 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
111 setFileFilter(chosen);
114 setAccessory(new RecentlyOpened());
118 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
120 super.setFileFilter(filter);
124 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
126 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
127 final String name = ui.getFileName().trim();
129 if ((name == null) || (name.length() == 0))
134 EventQueue.invokeLater(new Thread()
139 String currentName = ui.getFileName();
140 if ((currentName == null) || (currentName.length() == 0))
142 ui.setFileName(name);
147 } catch (Exception ex)
149 ex.printStackTrace();
150 // Some platforms do not have BasicFileChooserUI
154 public String getSelectedFormat()
156 if (getFileFilter() == null)
161 String format = getFileFilter().getDescription();
163 if (format.toUpperCase().startsWith("JALVIEW"))
167 else if (format.toUpperCase().startsWith("FASTA"))
171 else if (format.toUpperCase().startsWith("MSF"))
175 else if (format.toUpperCase().startsWith("CLUSTAL"))
179 else if (format.toUpperCase().startsWith("BLC"))
183 else if (format.toUpperCase().startsWith("PIR"))
187 else if (format.toUpperCase().startsWith("PFAM"))
191 else if (format.toUpperCase().startsWith(PhylipFile.FILE_DESC))
193 format = PhylipFile.FILE_DESC;
200 public int showSaveDialog(Component parent) throws HeadlessException
202 this.setAccessory(null);
204 setDialogType(SAVE_DIALOG);
206 int ret = showDialog(parent, MessageManager.getString("action.save"));
208 if (getFileFilter() instanceof JalviewFileFilter)
210 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
212 if (!jvf.accept(getSelectedFile()))
214 String withExtension = getSelectedFile() + "."
215 + jvf.getAcceptableExtension();
216 setSelectedFile(new File(withExtension));
219 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
220 // USER PROMPTED FOR A NEW FILENAME
221 if ((ret == JalviewFileChooser.APPROVE_OPTION)
222 && getSelectedFile().exists())
224 int confirm = JOptionPane.showConfirmDialog(parent,
225 MessageManager.getString("label.overwrite_existing_file"), MessageManager.getString("label.file_already_exists"),
226 JOptionPane.YES_NO_OPTION);
228 if (confirm != JOptionPane.YES_OPTION)
230 ret = JalviewFileChooser.CANCEL_OPTION;
237 void recentListSelectionChanged(Object selection)
239 setSelectedFile(null);
240 if (selection != null)
242 File file = new File((String) selection);
243 if (getFileFilter() instanceof JalviewFileFilter)
245 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
247 if (!jvf.accept(file))
249 setFileFilter(getChoosableFileFilters()[0]);
253 setSelectedFile(file);
257 class RecentlyOpened extends JPanel
261 public RecentlyOpened()
263 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
265 Vector recent = new Vector();
267 if (historyItems != null)
269 st = new StringTokenizer(historyItems, "\t");
271 while (st.hasMoreTokens())
273 recent.addElement(st.nextElement());
277 list = new JList(recent);
279 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
280 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
281 list.setCellRenderer(dlcr);
283 list.addMouseListener(new MouseAdapter()
286 public void mousePressed(MouseEvent evt)
288 recentListSelectionChanged(list.getSelectedValue());
292 this.setBorder(new javax.swing.border.TitledBorder(MessageManager.getString("label.recently_opened")));
294 final JScrollPane scroller = new JScrollPane(list);
295 scroller.setPreferredSize(new Dimension(130, 200));
298 javax.swing.SwingUtilities.invokeLater(new Runnable()
303 scroller.getHorizontalScrollBar().setValue(
304 scroller.getHorizontalScrollBar().getMaximum());