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
27 public class JalviewFileChooser
\r
28 extends JFileChooser
\r
31 public JalviewFileChooser(String dir)
\r
36 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
\r
41 JalviewFileFilter chosen = null;
\r
43 for (int i = 0; i < suffix.length; i++)
\r
45 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
\r
46 addChoosableFileFilter(jvf);
\r
48 if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))
\r
56 setFileFilter(chosen);
\r
60 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
\r
62 super.setFileFilter(filter);
\r
65 if(getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
\r
67 final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.
\r
68 basic.BasicFileChooserUI) getUI();
\r
69 final String name = ui.getFileName().trim();
\r
71 if ( (name == null) || (name.length() == 0))
\r
76 EventQueue.invokeLater(new Thread()
\r
80 String currentName = ui.getFileName();
\r
81 if ( (currentName == null) || (currentName.length() == 0))
\r
83 ui.setFileName(name);
\r
88 }catch(Exception ex)
\r
90 // Some platforms do not have BasicFileChooserUI
\r
95 public String getSelectedFormat()
\r
97 if(getFileFilter()==null)
\r
102 String format = getFileFilter().getDescription();
\r
104 if (format.toUpperCase().startsWith("JALVIEW"))
\r
106 format = "Jalview";
\r
108 else if (format.toUpperCase().startsWith("FASTA"))
\r
112 else if (format.toUpperCase().startsWith("MSF"))
\r
116 else if (format.toUpperCase().startsWith("CLUSTAL"))
\r
118 format = "CLUSTAL";
\r
120 else if (format.toUpperCase().startsWith("BLC"))
\r
124 else if (format.toUpperCase().startsWith("PIR"))
\r
128 else if (format.toUpperCase().startsWith("PFAM"))
\r
136 public int showSaveDialog(Component parent)
\r
137 throws HeadlessException
\r
139 setDialogType(SAVE_DIALOG);
\r
141 int ret = showDialog(parent, null);
\r
143 if (getFileFilter() instanceof JalviewFileFilter)
\r
145 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
\r
147 if (!jvf.accept(getSelectedFile()))
\r
149 String withExtension = getSelectedFile() + "." +
\r
150 jvf.getAcceptableExtension();
\r
151 setSelectedFile(new File(withExtension));
\r
155 if ( (ret == JalviewFileChooser.APPROVE_OPTION) &&
\r
156 getSelectedFile().exists())
\r
158 int confirm = JOptionPane.showConfirmDialog(parent,
\r
159 "Overwrite existing file?",
\r
161 JOptionPane.YES_NO_OPTION);
\r
163 if (confirm != JOptionPane.YES_OPTION)
\r
165 ret = JalviewFileChooser.CANCEL_OPTION;
\r