2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
20 //////////////////////////////////////////////////////////////////
27 import java.awt.event.*;
30 * Enhanced file chooser dialog box.
32 * NOTE: bug on Windows systems when filechooser opened on directory to view files with colons in title.
37 public class JalviewFileChooser
40 public JalviewFileChooser(String dir)
43 setAccessory(new RecentlyOpened());
46 private static File safePath(String dir)
52 File f = new File(dir);
53 if (f.getName().indexOf(':')>-1)
60 public JalviewFileChooser(String dir,
67 init(suffix, desc, selected, selectAll);
70 public JalviewFileChooser(String dir,
76 init(suffix, desc, selected, true);
79 void init(String[] suffix,
85 JalviewFileFilter chosen = null;
87 //SelectAllFilter needs to be set first before adding further
88 //file filters to fix bug on Mac OSX
89 setAcceptAllFileFilterUsed(selectAll);
91 for (int i = 0; i < suffix.length; i++)
93 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
94 addChoosableFileFilter(jvf);
96 if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))
104 setFileFilter(chosen);
107 setAccessory(new RecentlyOpened());
110 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
112 super.setFileFilter(filter);
116 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
118 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.
119 basic.BasicFileChooserUI) getUI();
120 final String name = ui.getFileName().trim();
122 if ( (name == null) || (name.length() == 0))
127 EventQueue.invokeLater(new Thread()
131 String currentName = ui.getFileName();
132 if ( (currentName == null) || (currentName.length() == 0))
134 ui.setFileName(name);
142 ex.printStackTrace();
143 // Some platforms do not have BasicFileChooserUI
147 public String getSelectedFormat()
149 if (getFileFilter() == null)
154 String format = getFileFilter().getDescription();
156 if (format.toUpperCase().startsWith("JALVIEW"))
160 else if (format.toUpperCase().startsWith("FASTA"))
164 else if (format.toUpperCase().startsWith("MSF"))
168 else if (format.toUpperCase().startsWith("CLUSTAL"))
172 else if (format.toUpperCase().startsWith("BLC"))
176 else if (format.toUpperCase().startsWith("PIR"))
180 else if (format.toUpperCase().startsWith("PFAM"))
188 public int showSaveDialog(Component parent)
189 throws HeadlessException
191 this.setAccessory(null);
193 setDialogType(SAVE_DIALOG);
195 int ret = showDialog(parent, "Save");
197 if (getFileFilter() instanceof JalviewFileFilter)
199 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
201 if (!jvf.accept(getSelectedFile()))
203 String withExtension = getSelectedFile() + "." +
204 jvf.getAcceptableExtension();
205 setSelectedFile(new File(withExtension));
208 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE USER PROMPTED FOR A NEW FILENAME
209 if ( (ret == JalviewFileChooser.APPROVE_OPTION) &&
210 getSelectedFile().exists())
212 int confirm = JOptionPane.showConfirmDialog(parent,
213 "Overwrite existing file?",
215 JOptionPane.YES_NO_OPTION);
217 if (confirm != JOptionPane.YES_OPTION)
219 ret = JalviewFileChooser.CANCEL_OPTION;
226 void recentListSelectionChanged(String selection)
228 setSelectedFile(null);
230 File file = new File(selection);
231 if (getFileFilter() instanceof JalviewFileFilter)
233 JalviewFileFilter jvf = (JalviewFileFilter)this.getFileFilter();
235 if (!jvf.accept(file))
237 setFileFilter(getChoosableFileFilters()[0]);
241 setSelectedFile(file);
248 public RecentlyOpened()
250 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
252 Vector recent = new Vector();
254 if (historyItems != null)
256 st = new StringTokenizer(historyItems, "\t");
258 while (st.hasMoreTokens())
260 recent.addElement(st.nextElement());
264 list = new JList(recent);
266 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
267 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
268 list.setCellRenderer(dlcr);
270 list.addMouseListener(new MouseAdapter()
272 public void mousePressed(MouseEvent evt)
274 recentListSelectionChanged(list.getSelectedValue().toString());
278 this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
280 final JScrollPane scroller = new JScrollPane(list);
281 scroller.setPreferredSize(new Dimension(130, 200));
284 javax.swing.SwingUtilities.invokeLater(new Runnable()
288 scroller.getHorizontalScrollBar().setValue(
289 scroller.getHorizontalScrollBar().getMaximum());