2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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.*;
29 * Enhanced file chooser dialog box.
31 * NOTE: bug on Windows systems when filechooser opened on directory to view files with colons in title.
36 public class JalviewFileChooser
39 public JalviewFileChooser(String dir)
42 setAccessory(new RecentlyOpened());
45 private static File safePath(String dir)
51 File f = new File(dir);
52 if (f.getName().indexOf(':')>-1)
59 public JalviewFileChooser(String dir,
66 init(suffix, desc, selected, selectAll);
69 public JalviewFileChooser(String dir,
75 init(suffix, desc, selected, true);
78 void init(String[] suffix,
84 JalviewFileFilter chosen = null;
86 //SelectAllFilter needs to be set first before adding further
87 //file filters to fix bug on Mac OSX
88 setAcceptAllFileFilterUsed(selectAll);
90 for (int i = 0; i < suffix.length; i++)
92 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
93 addChoosableFileFilter(jvf);
95 if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))
103 setFileFilter(chosen);
106 setAccessory(new RecentlyOpened());
109 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
111 super.setFileFilter(filter);
115 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
117 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.
118 basic.BasicFileChooserUI) getUI();
119 final String name = ui.getFileName().trim();
121 if ( (name == null) || (name.length() == 0))
126 EventQueue.invokeLater(new Thread()
130 String currentName = ui.getFileName();
131 if ( (currentName == null) || (currentName.length() == 0))
133 ui.setFileName(name);
141 ex.printStackTrace();
142 // Some platforms do not have BasicFileChooserUI
146 public String getSelectedFormat()
148 if (getFileFilter() == null)
153 String format = getFileFilter().getDescription();
155 if (format.toUpperCase().startsWith("JALVIEW"))
159 else if (format.toUpperCase().startsWith("FASTA"))
163 else if (format.toUpperCase().startsWith("MSF"))
167 else if (format.toUpperCase().startsWith("CLUSTAL"))
171 else if (format.toUpperCase().startsWith("BLC"))
175 else if (format.toUpperCase().startsWith("PIR"))
179 else if (format.toUpperCase().startsWith("PFAM"))
187 public int showSaveDialog(Component parent)
188 throws HeadlessException
190 this.setAccessory(null);
192 setDialogType(SAVE_DIALOG);
194 int ret = showDialog(parent, "Save");
196 if (getFileFilter() instanceof JalviewFileFilter)
198 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
200 if (!jvf.accept(getSelectedFile()))
202 String withExtension = getSelectedFile() + "." +
203 jvf.getAcceptableExtension();
204 setSelectedFile(new File(withExtension));
207 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE USER PROMPTED FOR A NEW FILENAME
208 if ( (ret == JalviewFileChooser.APPROVE_OPTION) &&
209 getSelectedFile().exists())
211 int confirm = JOptionPane.showConfirmDialog(parent,
212 "Overwrite existing file?",
214 JOptionPane.YES_NO_OPTION);
216 if (confirm != JOptionPane.YES_OPTION)
218 ret = JalviewFileChooser.CANCEL_OPTION;
225 void recentListSelectionChanged(String selection)
227 setSelectedFile(null);
229 File file = new File(selection);
230 if (getFileFilter() instanceof JalviewFileFilter)
232 JalviewFileFilter jvf = (JalviewFileFilter)this.getFileFilter();
234 if (!jvf.accept(file))
236 setFileFilter(getChoosableFileFilters()[0]);
240 setSelectedFile(file);
247 public RecentlyOpened()
249 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
251 Vector recent = new Vector();
253 if (historyItems != null)
255 st = new StringTokenizer(historyItems, "\t");
257 while (st.hasMoreTokens())
259 recent.addElement(st.nextElement());
263 list = new JList(recent);
265 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
266 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
267 list.setCellRenderer(dlcr);
269 list.addMouseListener(new MouseAdapter()
271 public void mousePressed(MouseEvent evt)
273 recentListSelectionChanged(list.getSelectedValue().toString());
277 this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
279 final JScrollPane scroller = new JScrollPane(list);
280 scroller.setPreferredSize(new Dimension(130, 200));
283 javax.swing.SwingUtilities.invokeLater(new Runnable()
287 scroller.getHorizontalScrollBar().setValue(
288 scroller.getHorizontalScrollBar().getMaximum());