2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 //////////////////////////////////////////////////////////////////
\r
25 import javax.swing.*;
\r
26 import java.awt.event.*;
\r
29 public class JalviewFileChooser
\r
30 extends JFileChooser
\r
33 public JalviewFileChooser(String dir)
\r
36 setAccessory(new RecentlyOpened());
\r
39 public JalviewFileChooser(String dir,
\r
46 init( suffix, desc, selected, selectAll);
\r
49 public JalviewFileChooser(String dir,
\r
55 init( suffix, desc, selected, true);
\r
58 void init(String[] suffix,
\r
64 JalviewFileFilter chosen = null;
\r
66 //SelectAllFilter needs to be set first before adding further
\r
67 //file filters to fix bug on Mac OSX
\r
68 setAcceptAllFileFilterUsed(selectAll);
\r
70 for (int i = 0; i < suffix.length; i++)
\r
72 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
\r
73 addChoosableFileFilter(jvf);
\r
75 if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))
\r
83 setFileFilter(chosen);
\r
86 setAccessory(new RecentlyOpened());
\r
90 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
\r
92 super.setFileFilter(filter);
\r
96 if(getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
\r
98 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.
\r
99 basic.BasicFileChooserUI) getUI();
\r
100 final String name = ui.getFileName().trim();
\r
102 if ( (name == null) || (name.length() == 0))
\r
107 EventQueue.invokeLater(new Thread()
\r
111 String currentName = ui.getFileName();
\r
112 if ( (currentName == null) || (currentName.length() == 0))
\r
114 ui.setFileName(name);
\r
119 }catch(Exception ex)
\r
121 ex.printStackTrace();
\r
122 // Some platforms do not have BasicFileChooserUI
\r
126 public String getSelectedFormat()
\r
128 if(getFileFilter()==null)
\r
133 String format = getFileFilter().getDescription();
\r
135 if (format.toUpperCase().startsWith("JALVIEW"))
\r
137 format = "Jalview";
\r
139 else if (format.toUpperCase().startsWith("FASTA"))
\r
143 else if (format.toUpperCase().startsWith("MSF"))
\r
147 else if (format.toUpperCase().startsWith("CLUSTAL"))
\r
149 format = "CLUSTAL";
\r
151 else if (format.toUpperCase().startsWith("BLC"))
\r
155 else if (format.toUpperCase().startsWith("PIR"))
\r
159 else if (format.toUpperCase().startsWith("PFAM"))
\r
167 public int showSaveDialog(Component parent)
\r
168 throws HeadlessException
\r
170 this.setAccessory(null);
\r
173 setDialogType(SAVE_DIALOG);
\r
175 int ret = showDialog(parent, "Save");
\r
177 if (getFileFilter() instanceof JalviewFileFilter)
\r
179 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
\r
181 if (!jvf.accept(getSelectedFile()))
\r
183 String withExtension = getSelectedFile() + "." +
\r
184 jvf.getAcceptableExtension();
\r
185 setSelectedFile(new File(withExtension));
\r
189 if ( (ret == JalviewFileChooser.APPROVE_OPTION) &&
\r
190 getSelectedFile().exists())
\r
192 int confirm = JOptionPane.showConfirmDialog(parent,
\r
193 "Overwrite existing file?",
\r
195 JOptionPane.YES_NO_OPTION);
\r
197 if (confirm != JOptionPane.YES_OPTION)
\r
199 ret = JalviewFileChooser.CANCEL_OPTION;
\r
206 void recentListSelectionChanged(String selection)
\r
208 setSelectedFile(null);
\r
210 File file = new File(selection);
\r
211 if (getFileFilter() instanceof JalviewFileFilter)
\r
213 JalviewFileFilter jvf = (JalviewFileFilter)this.getFileFilter();
\r
215 if (!jvf.accept(file))
\r
217 setFileFilter(getChoosableFileFilters()[0]);
\r
221 setSelectedFile( file );
\r
224 class RecentlyOpened extends JPanel
\r
227 public RecentlyOpened()
\r
229 String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
\r
230 StringTokenizer st;
\r
231 Vector recent = new Vector();
\r
233 if (historyItems != null)
\r
235 st = new StringTokenizer(historyItems, "\t");
\r
237 while (st.hasMoreTokens())
\r
239 recent.addElement(st.nextElement());
\r
243 list = new JList(recent);
\r
245 DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
\r
246 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
\r
247 list.setCellRenderer(dlcr);
\r
250 list.addMouseListener(new MouseAdapter()
\r
252 public void mousePressed(MouseEvent evt)
\r
254 recentListSelectionChanged(list.getSelectedValue().toString());
\r
258 this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
\r
260 final JScrollPane scroller = new JScrollPane(list);
\r
261 scroller.setPreferredSize(new Dimension(130, 200));
\r
262 this.add(scroller);
\r
264 javax.swing.SwingUtilities.invokeLater(new Runnable()
\r
268 scroller.getHorizontalScrollBar().setValue(
\r
269 scroller.getHorizontalScrollBar().getMaximum());
\r