2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 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);
89 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
97 setFileFilter(chosen);
100 setAccessory(new RecentlyOpened());
103 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
105 super.setFileFilter(filter);
109 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
111 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
112 final String name = ui.getFileName().trim();
114 if ((name == null) || (name.length() == 0))
119 EventQueue.invokeLater(new Thread()
123 String currentName = ui.getFileName();
124 if ((currentName == null) || (currentName.length() == 0))
126 ui.setFileName(name);
131 } catch (Exception ex)
133 ex.printStackTrace();
134 // Some platforms do not have BasicFileChooserUI
138 public String getSelectedFormat()
140 if (getFileFilter() == null)
145 String format = getFileFilter().getDescription();
147 if (format.toUpperCase().startsWith("JALVIEW"))
151 else if (format.toUpperCase().startsWith("FASTA"))
155 else if (format.toUpperCase().startsWith("MSF"))
159 else if (format.toUpperCase().startsWith("CLUSTAL"))
163 else if (format.toUpperCase().startsWith("BLC"))
167 else if (format.toUpperCase().startsWith("PIR"))
171 else if (format.toUpperCase().startsWith("PFAM"))
179 public int showSaveDialog(Component parent) throws HeadlessException
181 this.setAccessory(null);
183 setDialogType(SAVE_DIALOG);
185 int ret = showDialog(parent, "Save");
187 if (getFileFilter() instanceof JalviewFileFilter)
189 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
191 if (!jvf.accept(getSelectedFile()))
193 String withExtension = getSelectedFile() + "."
194 + jvf.getAcceptableExtension();
195 setSelectedFile(new File(withExtension));
198 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
199 // USER PROMPTED FOR A NEW FILENAME
200 if ((ret == JalviewFileChooser.APPROVE_OPTION)
201 && getSelectedFile().exists())
203 int confirm = JOptionPane.showConfirmDialog(parent,
204 "Overwrite existing file?", "File exists",
205 JOptionPane.YES_NO_OPTION);
207 if (confirm != JOptionPane.YES_OPTION)
209 ret = JalviewFileChooser.CANCEL_OPTION;
216 void recentListSelectionChanged(String selection)
218 setSelectedFile(null);
220 File file = new File(selection);
221 if (getFileFilter() instanceof JalviewFileFilter)
223 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
225 if (!jvf.accept(file))
227 setFileFilter(getChoosableFileFilters()[0]);
231 setSelectedFile(file);
234 class RecentlyOpened extends JPanel
238 public RecentlyOpened()
240 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
242 Vector recent = new Vector();
244 if (historyItems != null)
246 st = new StringTokenizer(historyItems, "\t");
248 while (st.hasMoreTokens())
250 recent.addElement(st.nextElement());
254 list = new JList(recent);
256 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
257 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
258 list.setCellRenderer(dlcr);
260 list.addMouseListener(new MouseAdapter()
262 public void mousePressed(MouseEvent evt)
264 recentListSelectionChanged(list.getSelectedValue().toString());
268 this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
270 final JScrollPane scroller = new JScrollPane(list);
271 scroller.setPreferredSize(new Dimension(130, 200));
274 javax.swing.SwingUtilities.invokeLater(new Runnable()
278 scroller.getHorizontalScrollBar().setValue(
279 scroller.getHorizontalScrollBar().getMaximum());