2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 //////////////////////////////////////////////////////////////////
25 import java.awt.event.*;
29 * Enhanced file chooser dialog box.
31 * NOTE: bug on Windows systems when filechooser opened on directory to view
32 * files with colons in title.
37 public class JalviewFileChooser extends JFileChooser
39 public JalviewFileChooser(String dir)
42 setAccessory(new RecentlyOpened());
45 private static File safePath(String dir)
52 File f = new File(dir);
53 if (f.getName().indexOf(':') > -1)
60 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
61 String selected, boolean selectAll)
64 init(suffix, desc, selected, selectAll);
67 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
71 init(suffix, desc, selected, true);
74 void init(String[] suffix, String[] desc, String selected,
78 JalviewFileFilter chosen = null;
80 // SelectAllFilter needs to be set first before adding further
81 // file filters to fix bug on Mac OSX
82 setAcceptAllFileFilterUsed(selectAll);
84 for (int i = 0; i < suffix.length; i++)
86 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
87 addChoosableFileFilter(jvf);
88 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
96 setFileFilter(chosen);
99 setAccessory(new RecentlyOpened());
102 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
104 super.setFileFilter(filter);
108 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
110 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
111 final String name = ui.getFileName().trim();
113 if ((name == null) || (name.length() == 0))
118 EventQueue.invokeLater(new Thread()
122 String currentName = ui.getFileName();
123 if ((currentName == null) || (currentName.length() == 0))
125 ui.setFileName(name);
130 } catch (Exception ex)
132 ex.printStackTrace();
133 // Some platforms do not have BasicFileChooserUI
137 public String getSelectedFormat()
139 if (getFileFilter() == null)
144 String format = getFileFilter().getDescription();
146 if (format.toUpperCase().startsWith("JALVIEW"))
150 else if (format.toUpperCase().startsWith("FASTA"))
154 else if (format.toUpperCase().startsWith("MSF"))
158 else if (format.toUpperCase().startsWith("CLUSTAL"))
162 else if (format.toUpperCase().startsWith("BLC"))
166 else if (format.toUpperCase().startsWith("PIR"))
170 else if (format.toUpperCase().startsWith("PFAM"))
178 public int showSaveDialog(Component parent) throws HeadlessException
180 this.setAccessory(null);
182 setDialogType(SAVE_DIALOG);
184 int ret = showDialog(parent, "Save");
186 if (getFileFilter() instanceof JalviewFileFilter)
188 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
190 if (!jvf.accept(getSelectedFile()))
192 String withExtension = getSelectedFile() + "."
193 + jvf.getAcceptableExtension();
194 setSelectedFile(new File(withExtension));
197 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
198 // USER PROMPTED FOR A NEW FILENAME
199 if ((ret == JalviewFileChooser.APPROVE_OPTION)
200 && getSelectedFile().exists())
202 int confirm = JOptionPane.showConfirmDialog(parent,
203 "Overwrite existing file?", "File exists",
204 JOptionPane.YES_NO_OPTION);
206 if (confirm != JOptionPane.YES_OPTION)
208 ret = JalviewFileChooser.CANCEL_OPTION;
215 void recentListSelectionChanged(String selection)
217 setSelectedFile(null);
219 File file = new File(selection);
220 if (getFileFilter() instanceof JalviewFileFilter)
222 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
224 if (!jvf.accept(file))
226 setFileFilter(getChoosableFileFilters()[0]);
230 setSelectedFile(file);
233 class RecentlyOpened extends JPanel
237 public RecentlyOpened()
239 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
241 Vector recent = new Vector();
243 if (historyItems != null)
245 st = new StringTokenizer(historyItems, "\t");
247 while (st.hasMoreTokens())
249 recent.addElement(st.nextElement());
253 list = new JList(recent);
255 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
256 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
257 list.setCellRenderer(dlcr);
259 list.addMouseListener(new MouseAdapter()
261 public void mousePressed(MouseEvent evt)
263 recentListSelectionChanged(list.getSelectedValue().toString());
267 this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
269 final JScrollPane scroller = new JScrollPane(list);
270 scroller.setPreferredSize(new Dimension(130, 200));
273 javax.swing.SwingUtilities.invokeLater(new Runnable()
277 scroller.getHorizontalScrollBar().setValue(
278 scroller.getHorizontalScrollBar().getMaximum());