2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 //////////////////////////////////////////////////////////////////
26 import java.awt.event.*;
30 * Enhanced file chooser dialog box.
32 * NOTE: bug on Windows systems when filechooser opened on directory to view
33 * files with colons in title.
38 public class JalviewFileChooser extends JFileChooser
40 public JalviewFileChooser(String dir)
43 setAccessory(new RecentlyOpened());
46 private static File safePath(String dir)
53 File f = new File(dir);
54 if (f.getName().indexOf(':') > -1)
61 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
62 String selected, boolean selectAll)
65 init(suffix, desc, selected, selectAll);
68 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
72 init(suffix, desc, selected, true);
75 void init(String[] suffix, String[] desc, String selected,
79 JalviewFileFilter chosen = null;
81 // SelectAllFilter needs to be set first before adding further
82 // file filters to fix bug on Mac OSX
83 setAcceptAllFileFilterUsed(selectAll);
85 for (int i = 0; i < suffix.length; i++)
87 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
88 addChoosableFileFilter(jvf);
90 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
98 setFileFilter(chosen);
101 setAccessory(new RecentlyOpened());
104 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
106 super.setFileFilter(filter);
110 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
112 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
113 final String name = ui.getFileName().trim();
115 if ((name == null) || (name.length() == 0))
120 EventQueue.invokeLater(new Thread()
124 String currentName = ui.getFileName();
125 if ((currentName == null) || (currentName.length() == 0))
127 ui.setFileName(name);
132 } catch (Exception ex)
134 ex.printStackTrace();
135 // Some platforms do not have BasicFileChooserUI
139 public String getSelectedFormat()
141 if (getFileFilter() == null)
146 String format = getFileFilter().getDescription();
148 if (format.toUpperCase().startsWith("JALVIEW"))
152 else if (format.toUpperCase().startsWith("FASTA"))
156 else if (format.toUpperCase().startsWith("MSF"))
160 else if (format.toUpperCase().startsWith("CLUSTAL"))
164 else if (format.toUpperCase().startsWith("BLC"))
168 else if (format.toUpperCase().startsWith("PIR"))
172 else if (format.toUpperCase().startsWith("PFAM"))
180 public int showSaveDialog(Component parent) throws HeadlessException
182 this.setAccessory(null);
184 setDialogType(SAVE_DIALOG);
186 int ret = showDialog(parent, "Save");
188 if (getFileFilter() instanceof JalviewFileFilter)
190 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
192 if (!jvf.accept(getSelectedFile()))
194 String withExtension = getSelectedFile() + "."
195 + jvf.getAcceptableExtension();
196 setSelectedFile(new File(withExtension));
199 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
200 // USER PROMPTED FOR A NEW FILENAME
201 if ((ret == JalviewFileChooser.APPROVE_OPTION)
202 && getSelectedFile().exists())
204 int confirm = JOptionPane.showConfirmDialog(parent,
205 "Overwrite existing file?", "File exists",
206 JOptionPane.YES_NO_OPTION);
208 if (confirm != JOptionPane.YES_OPTION)
210 ret = JalviewFileChooser.CANCEL_OPTION;
217 void recentListSelectionChanged(String selection)
219 setSelectedFile(null);
221 File file = new File(selection);
222 if (getFileFilter() instanceof JalviewFileFilter)
224 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
226 if (!jvf.accept(file))
228 setFileFilter(getChoosableFileFilters()[0]);
232 setSelectedFile(file);
235 class RecentlyOpened extends JPanel
239 public RecentlyOpened()
241 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
243 Vector recent = new Vector();
245 if (historyItems != null)
247 st = new StringTokenizer(historyItems, "\t");
249 while (st.hasMoreTokens())
251 recent.addElement(st.nextElement());
255 list = new JList(recent);
257 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
258 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
259 list.setCellRenderer(dlcr);
261 list.addMouseListener(new MouseAdapter()
263 public void mousePressed(MouseEvent evt)
265 recentListSelectionChanged(list.getSelectedValue().toString());
270 .setBorder(new javax.swing.border.TitledBorder(
273 final JScrollPane scroller = new JScrollPane(list);
274 scroller.setPreferredSize(new Dimension(130, 200));
277 javax.swing.SwingUtilities.invokeLater(new Runnable()
281 scroller.getHorizontalScrollBar().setValue(
282 scroller.getHorizontalScrollBar().getMaximum());