2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 //////////////////////////////////////////////////////////////////
24 import jalview.util.MessageManager;
25 import jalview.util.Platform;
27 import java.awt.Component;
28 import java.awt.Dimension;
29 import java.awt.EventQueue;
30 import java.awt.HeadlessException;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
37 import javax.swing.DefaultListCellRenderer;
38 import javax.swing.JFileChooser;
39 import javax.swing.JList;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.SpringLayout;
46 * Enhanced file chooser dialog box.
48 * NOTE: bug on Windows systems when filechooser opened on directory to view
49 * files with colons in title.
54 public class JalviewFileChooser extends JFileChooser
56 public JalviewFileChooser(String dir)
59 setAccessory(new RecentlyOpened());
62 private static File safePath(String dir)
69 File f = new File(dir);
70 if (f.getName().indexOf(':') > -1)
77 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
78 String selected, boolean selectAll)
81 init(suffix, desc, selected, selectAll);
84 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
88 init(suffix, desc, selected, true);
91 void init(String[] suffix, String[] desc, String selected,
95 JalviewFileFilter chosen = null;
97 // SelectAllFilter needs to be set first before adding further
98 // file filters to fix bug on Mac OSX
99 setAcceptAllFileFilterUsed(selectAll);
101 for (int i = 0; i < suffix.length; i++)
103 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
104 addChoosableFileFilter(jvf);
105 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
113 setFileFilter(chosen);
116 setAccessory(new RecentlyOpened());
120 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
122 super.setFileFilter(filter);
126 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
128 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
129 final String name = ui.getFileName().trim();
131 if ((name == null) || (name.length() == 0))
136 EventQueue.invokeLater(new Thread()
141 String currentName = ui.getFileName();
142 if ((currentName == null) || (currentName.length() == 0))
144 ui.setFileName(name);
149 } catch (Exception ex)
151 ex.printStackTrace();
152 // Some platforms do not have BasicFileChooserUI
156 public String getSelectedFormat()
158 if (getFileFilter() == null)
163 String format = getFileFilter().getDescription();
165 if (format.toUpperCase().startsWith("JALVIEW"))
169 else if (format.toUpperCase().startsWith("FASTA"))
173 else if (format.toUpperCase().startsWith("MSF"))
177 else if (format.toUpperCase().startsWith("CLUSTAL"))
181 else if (format.toUpperCase().startsWith("BLC"))
185 else if (format.toUpperCase().startsWith("PIR"))
189 else if (format.toUpperCase().startsWith("PFAM"))
193 else if (format.toUpperCase().startsWith(PhylipFile.FILE_DESC))
195 format = PhylipFile.FILE_DESC;
202 public int showSaveDialog(Component parent) throws HeadlessException
204 this.setAccessory(null);
206 setDialogType(SAVE_DIALOG);
208 int ret = showDialog(parent, MessageManager.getString("action.save"));
210 if (getFileFilter() instanceof JalviewFileFilter)
212 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
214 if (!jvf.accept(getSelectedFile()))
216 String withExtension = getSelectedFile() + "."
217 + jvf.getAcceptableExtension();
218 setSelectedFile(new File(withExtension));
221 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
222 // USER PROMPTED FOR A NEW FILENAME
223 if ((ret == JalviewFileChooser.APPROVE_OPTION)
224 && getSelectedFile().exists())
226 int confirm = JOptionPane.showConfirmDialog(parent,
227 MessageManager.getString("label.overwrite_existing_file"),
228 MessageManager.getString("label.file_already_exists"),
229 JOptionPane.YES_NO_OPTION);
231 if (confirm != JOptionPane.YES_OPTION)
233 ret = JalviewFileChooser.CANCEL_OPTION;
240 void recentListSelectionChanged(Object selection)
242 setSelectedFile(null);
243 if (selection != null)
245 File file = new File((String) selection);
246 if (getFileFilter() instanceof JalviewFileFilter)
248 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
250 if (!jvf.accept(file))
252 setFileFilter(getChoosableFileFilters()[0]);
256 setSelectedFile(file);
260 class RecentlyOpened extends JPanel
264 public RecentlyOpened()
267 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
269 Vector recent = new Vector();
271 if (historyItems != null)
273 st = new StringTokenizer(historyItems, "\t");
275 while (st.hasMoreTokens())
277 recent.addElement(st.nextElement());
281 list = new JList(recent);
283 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
284 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
285 list.setCellRenderer(dlcr);
287 list.addMouseListener(new MouseAdapter()
290 public void mousePressed(MouseEvent evt)
292 recentListSelectionChanged(list.getSelectedValue());
296 this.setBorder(new javax.swing.border.TitledBorder(MessageManager
297 .getString("label.recently_opened")));
299 final JScrollPane scroller = new JScrollPane(list);
301 SpringLayout layout = new SpringLayout();
302 layout.putConstraint(SpringLayout.WEST, scroller, 5,
303 SpringLayout.WEST, this);
304 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
305 SpringLayout.NORTH, this);
307 if (new Platform().isAMac())
309 scroller.setPreferredSize(new Dimension(500, 100));
313 scroller.setPreferredSize(new Dimension(130, 200));
318 javax.swing.SwingUtilities.invokeLater(new Runnable()
323 scroller.getHorizontalScrollBar().setValue(
324 scroller.getHorizontalScrollBar().getMaximum());