2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 //////////////////////////////////////////////////////////////////
22 import jalview.util.MessageManager;
28 import java.awt.event.*;
32 * Enhanced file chooser dialog box.
34 * NOTE: bug on Windows systems when filechooser opened on directory to view
35 * files with colons in title.
40 public class JalviewFileChooser extends JFileChooser
42 public JalviewFileChooser(String dir)
45 setAccessory(new RecentlyOpened());
48 private static File safePath(String dir)
55 File f = new File(dir);
56 if (f.getName().indexOf(':') > -1)
63 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
64 String selected, boolean selectAll)
67 init(suffix, desc, selected, selectAll);
70 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
74 init(suffix, desc, selected, true);
77 void init(String[] suffix, String[] desc, String selected,
81 JalviewFileFilter chosen = null;
83 // SelectAllFilter needs to be set first before adding further
84 // file filters to fix bug on Mac OSX
85 setAcceptAllFileFilterUsed(selectAll);
87 for (int i = 0; i < suffix.length; i++)
89 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
90 addChoosableFileFilter(jvf);
91 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
99 setFileFilter(chosen);
102 setAccessory(new RecentlyOpened());
105 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
107 super.setFileFilter(filter);
111 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
113 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
114 final String name = ui.getFileName().trim();
116 if ((name == null) || (name.length() == 0))
121 EventQueue.invokeLater(new Thread()
125 String currentName = ui.getFileName();
126 if ((currentName == null) || (currentName.length() == 0))
128 ui.setFileName(name);
133 } catch (Exception ex)
135 ex.printStackTrace();
136 // Some platforms do not have BasicFileChooserUI
140 public String getSelectedFormat()
142 if (getFileFilter() == null)
147 String format = getFileFilter().getDescription();
149 if (format.toUpperCase().startsWith("JALVIEW"))
153 else if (format.toUpperCase().startsWith("FASTA"))
157 else if (format.toUpperCase().startsWith("MSF"))
161 else if (format.toUpperCase().startsWith("CLUSTAL"))
165 else if (format.toUpperCase().startsWith("BLC"))
169 else if (format.toUpperCase().startsWith("PIR"))
173 else if (format.toUpperCase().startsWith("PFAM"))
181 public int showSaveDialog(Component parent) throws HeadlessException
183 this.setAccessory(null);
185 setDialogType(SAVE_DIALOG);
187 int ret = showDialog(parent, MessageManager.getString("action.save"));
189 if (getFileFilter() instanceof JalviewFileFilter)
191 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
193 if (!jvf.accept(getSelectedFile()))
195 String withExtension = getSelectedFile() + "."
196 + jvf.getAcceptableExtension();
197 setSelectedFile(new File(withExtension));
200 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
201 // USER PROMPTED FOR A NEW FILENAME
202 if ((ret == JalviewFileChooser.APPROVE_OPTION)
203 && getSelectedFile().exists())
205 int confirm = JOptionPane.showConfirmDialog(parent,
206 "Overwrite existing file?", "File exists",
207 JOptionPane.YES_NO_OPTION);
209 if (confirm != JOptionPane.YES_OPTION)
211 ret = JalviewFileChooser.CANCEL_OPTION;
218 void recentListSelectionChanged(String selection)
220 setSelectedFile(null);
222 File file = new File(selection);
223 if (getFileFilter() instanceof JalviewFileFilter)
225 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
227 if (!jvf.accept(file))
229 setFileFilter(getChoosableFileFilters()[0]);
233 setSelectedFile(file);
236 class RecentlyOpened extends JPanel
240 public RecentlyOpened()
242 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
244 Vector recent = new Vector();
246 if (historyItems != null)
248 st = new StringTokenizer(historyItems, "\t");
250 while (st.hasMoreTokens())
252 recent.addElement(st.nextElement());
256 list = new JList(recent);
258 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
259 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
260 list.setCellRenderer(dlcr);
262 list.addMouseListener(new MouseAdapter()
264 public void mousePressed(MouseEvent evt)
266 recentListSelectionChanged(list.getSelectedValue().toString());
270 this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
272 final JScrollPane scroller = new JScrollPane(list);
273 scroller.setPreferredSize(new Dimension(130, 200));
276 javax.swing.SwingUtilities.invokeLater(new Runnable()
280 scroller.getHorizontalScrollBar().setValue(
281 scroller.getHorizontalScrollBar().getMaximum());