2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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;
30 import java.awt.event.*;
34 * Enhanced file chooser dialog box.
36 * NOTE: bug on Windows systems when filechooser opened on directory to view
37 * files with colons in title.
42 public class JalviewFileChooser extends JFileChooser
44 public JalviewFileChooser(String dir)
47 setAccessory(new RecentlyOpened());
50 private static File safePath(String dir)
57 File f = new File(dir);
58 if (f.getName().indexOf(':') > -1)
65 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
66 String selected, boolean selectAll)
69 init(suffix, desc, selected, selectAll);
72 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
76 init(suffix, desc, selected, true);
79 void init(String[] suffix, String[] desc, String selected,
83 JalviewFileFilter chosen = null;
85 // SelectAllFilter needs to be set first before adding further
86 // file filters to fix bug on Mac OSX
87 setAcceptAllFileFilterUsed(selectAll);
89 for (int i = 0; i < suffix.length; i++)
91 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
92 addChoosableFileFilter(jvf);
93 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
101 setFileFilter(chosen);
104 setAccessory(new RecentlyOpened());
107 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
109 super.setFileFilter(filter);
113 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
115 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
116 final String name = ui.getFileName().trim();
118 if ((name == null) || (name.length() == 0))
123 EventQueue.invokeLater(new Thread()
127 String currentName = ui.getFileName();
128 if ((currentName == null) || (currentName.length() == 0))
130 ui.setFileName(name);
135 } catch (Exception ex)
137 ex.printStackTrace();
138 // Some platforms do not have BasicFileChooserUI
142 public String getSelectedFormat()
144 if (getFileFilter() == null)
149 String format = getFileFilter().getDescription();
151 if (format.toUpperCase().startsWith("JALVIEW"))
155 else if (format.toUpperCase().startsWith("FASTA"))
159 else if (format.toUpperCase().startsWith("MSF"))
163 else if (format.toUpperCase().startsWith("CLUSTAL"))
167 else if (format.toUpperCase().startsWith("BLC"))
171 else if (format.toUpperCase().startsWith("PIR"))
175 else if (format.toUpperCase().startsWith("PFAM"))
183 public int showSaveDialog(Component parent) throws HeadlessException
185 this.setAccessory(null);
187 setDialogType(SAVE_DIALOG);
189 int ret = showDialog(parent, MessageManager.getString("action.save"));
191 if (getFileFilter() instanceof JalviewFileFilter)
193 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
195 if (!jvf.accept(getSelectedFile()))
197 String withExtension = getSelectedFile() + "."
198 + jvf.getAcceptableExtension();
199 setSelectedFile(new File(withExtension));
202 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
203 // USER PROMPTED FOR A NEW FILENAME
204 if ((ret == JalviewFileChooser.APPROVE_OPTION)
205 && getSelectedFile().exists())
207 int confirm = JOptionPane.showConfirmDialog(parent,
208 "Overwrite existing file?", "File exists",
209 JOptionPane.YES_NO_OPTION);
211 if (confirm != JOptionPane.YES_OPTION)
213 ret = JalviewFileChooser.CANCEL_OPTION;
220 void recentListSelectionChanged(Object selection)
222 setSelectedFile(null);
223 if (selection != null)
225 File file = new File((String) selection);
226 if (getFileFilter() instanceof JalviewFileFilter)
228 JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
230 if (!jvf.accept(file))
232 setFileFilter(getChoosableFileFilters()[0]);
236 setSelectedFile(file);
240 class RecentlyOpened extends JPanel
244 public RecentlyOpened()
246 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
248 Vector recent = new Vector();
250 if (historyItems != null)
252 st = new StringTokenizer(historyItems, "\t");
254 while (st.hasMoreTokens())
256 recent.addElement(st.nextElement());
260 list = new JList(recent);
262 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
263 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
264 list.setCellRenderer(dlcr);
266 list.addMouseListener(new MouseAdapter()
268 public void mousePressed(MouseEvent evt)
270 recentListSelectionChanged(list.getSelectedValue());
274 this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
276 final JScrollPane scroller = new JScrollPane(list);
277 scroller.setPreferredSize(new Dimension(130, 200));
280 javax.swing.SwingUtilities.invokeLater(new Runnable()
284 scroller.getHorizontalScrollBar().setValue(
285 scroller.getHorizontalScrollBar().getMaximum());