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.bin.Cache;
25 import jalview.util.MessageManager;
26 import jalview.util.Platform;
28 import java.awt.Component;
29 import java.awt.Dimension;
30 import java.awt.EventQueue;
31 import java.awt.HeadlessException;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
35 import java.util.StringTokenizer;
36 import java.util.Vector;
38 import javax.swing.DefaultListCellRenderer;
39 import javax.swing.JFileChooser;
40 import javax.swing.JList;
41 import javax.swing.JOptionPane;
42 import javax.swing.JPanel;
43 import javax.swing.JScrollPane;
44 import javax.swing.SpringLayout;
47 * Enhanced file chooser dialog box.
49 * NOTE: bug on Windows systems when filechooser opened on directory to view
50 * files with colons in title.
55 public class JalviewFileChooser extends JFileChooser
57 public JalviewFileChooser(String dir)
60 setAccessory(new RecentlyOpened());
63 private static File safePath(String dir)
70 File f = new File(dir);
71 if (f.getName().indexOf(':') > -1)
78 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
79 String selected, boolean selectAll)
82 init(suffix, desc, selected, selectAll);
85 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
89 init(suffix, desc, selected, true);
92 void init(String[] suffix, String[] desc, String selected,
96 JalviewFileFilter chosen = null;
98 // SelectAllFilter needs to be set first before adding further
99 // file filters to fix bug on Mac OSX
100 setAcceptAllFileFilterUsed(selectAll);
102 for (int i = 0; i < suffix.length; i++)
104 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
105 addChoosableFileFilter(jvf);
106 if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
114 setFileFilter(chosen);
117 setAccessory(new RecentlyOpened());
121 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
123 super.setFileFilter(filter);
127 if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
129 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
130 final String name = ui.getFileName().trim();
132 if ((name == null) || (name.length() == 0))
137 EventQueue.invokeLater(new Thread()
142 String currentName = ui.getFileName();
143 if ((currentName == null) || (currentName.length() == 0))
145 ui.setFileName(name);
150 } catch (Exception ex)
152 ex.printStackTrace();
153 // Some platforms do not have BasicFileChooserUI
157 public String getSelectedFormat()
159 if (getFileFilter() == null)
164 String format = getFileFilter().getDescription();
166 if (format.toUpperCase().startsWith("JALVIEW"))
170 else if (format.toUpperCase().startsWith("FASTA"))
174 else if (format.toUpperCase().startsWith("MSF"))
178 else if (format.toUpperCase().startsWith("CLUSTAL"))
182 else if (format.toUpperCase().startsWith("BLC"))
186 else if (format.toUpperCase().startsWith("PIR"))
190 else if (format.toUpperCase().startsWith("PFAM"))
194 else if (format.toUpperCase().startsWith(PhylipFile.FILE_DESC))
196 format = PhylipFile.FILE_DESC;
203 public int showSaveDialog(Component parent) throws HeadlessException
205 this.setAccessory(null);
207 setDialogType(SAVE_DIALOG);
209 int ret = showDialog(parent, MessageManager.getString("action.save"));
211 if (getFileFilter() instanceof JalviewFileFilter)
213 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
215 if (!jvf.accept(getSelectedFile()))
217 String withExtension = getSelectedFile() + "."
218 + jvf.getAcceptableExtension();
219 setSelectedFile(new File(withExtension));
222 // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
223 // USER PROMPTED FOR A NEW FILENAME
224 if ((ret == JalviewFileChooser.APPROVE_OPTION)
225 && getSelectedFile().exists())
227 int confirm = JOptionPane.showConfirmDialog(parent,
228 MessageManager.getString("label.overwrite_existing_file"), 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 = 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.getString("label.recently_opened")));
298 final JScrollPane scroller = new JScrollPane(list);
300 SpringLayout layout = new SpringLayout();
301 layout.putConstraint(SpringLayout.WEST, scroller, 5,
302 SpringLayout.WEST, this);
303 layout.putConstraint(SpringLayout.NORTH, scroller, 5,
304 SpringLayout.NORTH, this);
306 if (new Platform().isAMac())
308 scroller.setPreferredSize(new Dimension(500, 100));
312 scroller.setPreferredSize(new Dimension(130, 200));
317 javax.swing.SwingUtilities.invokeLater(new Runnable()
322 scroller.getHorizontalScrollBar().setValue(
323 scroller.getHorizontalScrollBar().getMaximum());