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
19 /*///////////////////////////////////////////////////////////////////
\r
20 // This file was taken from java forum
\r
21 // Re: JFileChooser functioning like normal Windows Apps FileChooser
\r
23 // http://forum.java.sun.com/thread.jspa?forumID=57&threadID=327712
\r
26 //////////////////////////////////////////////////////////////////
\r
33 import java.awt.event.*;
\r
34 import javax.swing.*;
\r
35 import javax.swing.plaf.*;
\r
36 import javax.swing.plaf.basic.*;
\r
37 import javax.swing.plaf.metal.*;
\r
38 import javax.swing.table.*;
\r
40 public class JalviewFileChooser
\r
41 extends JFileChooser
\r
45 public JalviewFileChooser(String dir, String[] suffix, String[] desc,
\r
50 JalviewFileFilter chosen = null;
\r
52 for (int i = 0; i < suffix.length; i++)
\r
54 JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
\r
55 addChoosableFileFilter(jvf);
\r
57 if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))
\r
65 setFileFilter(chosen);
\r
71 public void setFileFilter(javax.swing.filechooser.FileFilter filter)
\r
73 super.setFileFilter(filter);
\r
75 if (!(getUI() instanceof BasicFileChooserUI)) {
\r
79 final BasicFileChooserUI ui = (BasicFileChooserUI) getUI();
\r
80 final String name = ui.getFileName().trim();
\r
82 if ((name == null) || (name.length() == 0)) {
\r
86 EventQueue.invokeLater(new Thread() {
\r
88 String currentName = ui.getFileName();
\r
89 if ((currentName == null) || (currentName.length() == 0)) {
\r
90 ui.setFileName(name);
\r
97 public JalviewFileChooser(String dir)
\r
102 public String getSelectedFormat()
\r
104 String format = getFileFilter().getDescription();
\r
106 if (format.toUpperCase().startsWith("JALVIEW"))
\r
108 format = "Jalview";
\r
110 else if (format.toUpperCase().startsWith("FASTA"))
\r
114 else if (format.toUpperCase().startsWith("MSF"))
\r
118 else if (format.toUpperCase().startsWith("CLUSTAL"))
\r
120 format = "CLUSTAL";
\r
122 else if (format.toUpperCase().startsWith("BLC"))
\r
126 else if (format.toUpperCase().startsWith("PIR"))
\r
130 else if (format.toUpperCase().startsWith("PFAM"))
\r
138 public int showSaveDialog(Component parent)
\r
139 throws HeadlessException
\r
141 setDialogType(SAVE_DIALOG);
\r
143 int ret = showDialog(parent, null);
\r
145 if (getFileFilter() instanceof JalviewFileFilter)
\r
147 JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
\r
149 if (!jvf.accept(getSelectedFile()))
\r
151 String withExtension = getSelectedFile() + "." +
\r
152 jvf.getAcceptableExtension();
\r
153 setSelectedFile(new File(withExtension));
\r
157 if ( (ret == JalviewFileChooser.APPROVE_OPTION) &&
\r
158 getSelectedFile().exists())
\r
160 int confirm = JOptionPane.showConfirmDialog(parent,
\r
161 "Overwrite existing file?",
\r
163 JOptionPane.YES_NO_OPTION);
\r
165 if (confirm != JOptionPane.YES_OPTION)
\r
167 ret = this.CANCEL_OPTION;
\r
174 /**************************************************************************
\r
175 * Always create the local UI
\r
177 *************************************************************************/
\r
178 public final void setUI(ComponentUI comp)
\r
180 super.setUI(new UI(this));
\r
183 /**************************************************************************
\r
184 * Internal implementation of Metal LookAndFeel to create the table sorting
\r
186 *************************************************************************/
\r
188 extends MetalFileChooserUI
\r
190 private DirectoryModel model;
\r
192 /**************************************************************************
\r
193 * Must be overridden to extend
\r
195 *************************************************************************/
\r
196 public UI(JFileChooser e)
\r
201 /**************************************************************************
\r
202 * Overridden to create our own model
\r
203 *************************************************************************/
\r
204 protected final void createModel()
\r
206 model = new DirectoryModel(getFileChooser());
\r
209 /**************************************************************************
\r
210 * Overridden to get our own model
\r
212 *************************************************************************/
\r
213 public final BasicDirectoryModel getModel()
\r
218 /**************************************************************************
\r
219 * Calls the default method then adds a MouseListener to the JTable
\r
222 *************************************************************************/
\r
223 protected final JPanel createDetailsView(JFileChooser chooser)
\r
225 final JPanel panel = super.createDetailsView(chooser);
\r
227 //Since we can't access MetalFileChooserUI's private member detailsTable
\r
228 //directly, we have to find it in the JPanel
\r
229 final JTable tbl = findJTable(panel.getComponents());
\r
233 //Add a mouselistener to listen for presses on column headers
\r
234 tbl.getTableHeader().addMouseListener(new MouseAdapter()
\r
236 public void mousePressed(MouseEvent e)
\r
241 final int col = tbl.getTableHeader().columnAtPoint(e.getPoint());
\r
242 model.sort(col, tbl);
\r
251 /**************************************************************************
\r
252 * Finds the JTable in the panel so we can add MouseListener
\r
255 *************************************************************************/
\r
256 private JTable findJTable(Component[] comp)
\r
258 for (int i = 0; i < comp.length; i++)
\r
260 if (comp[i] instanceof JTable)
\r
262 return (JTable) comp[i];
\r
265 if (comp[i] instanceof Container)
\r
267 JTable tbl = findJTable( ( (Container) comp[i]).getComponents());
\r
280 /***************************************************************************
\r
281 * Implementation of BasicDirectoryModel that sorts the Files by column
\r
282 **************************************************************************/
\r
283 private final class DirectoryModel
\r
284 extends BasicDirectoryModel
\r
286 String sortingColumnName=null;
\r
290 /**************************************************************************
\r
291 * Must be overridden to extend BasicDirectoryModel
\r
293 *************************************************************************/
\r
294 DirectoryModel(JFileChooser chooser)
\r
300 /**************************************************************************
\r
301 * Resorts the JFileChooser table based on new column
\r
303 *************************************************************************/
\r
304 protected final void sort(int c, JTable tbl)
\r
308 //Set column and order
\r
310 ascending = !ascending;
\r
312 String indicator = " (^)";
\r
316 indicator = " (v)";
\r
319 final JTableHeader th = tbl.getTableHeader();
\r
320 final TableColumnModel tcm = th.getColumnModel();
\r
321 TableColumn tc = null;
\r
322 String title = null;
\r
325 //Remove indicator from old sort, and add to new
\r
326 for(int i=0; i<tcm.getColumnCount(); i++)
\r
328 tc = tcm.getColumn(i);
\r
329 title = tc.getHeaderValue().toString();
\r
330 if (title.endsWith(" (^)") || title.endsWith(" (v)"))
\r
332 title = title.substring(0, title.length() - 4);
\r
337 sortingColumnName = title;
\r
338 title = title + indicator;
\r
341 tc.setHeaderValue(title);
\r
344 //Requery the file listing
\r
345 validateFileCache();
\r
348 /**************************************************************************
\r
349 * Sorts the data based on current column setting
\r
351 *************************************************************************/
\r
352 protected final void sort(Vector data)
\r
355 UIManager.getString("FileChooser.fileTypeHeaderText", l),
\r
356 UIManager.getString("FileChooser.fileAttrHeaderText", l)
\r
358 if(sortingColumnName==null)
\r
362 if(sortingColumnName.equals(UIManager.getString("FileChooser.fileDateHeaderText")))
\r
364 Collections.sort(data,
\r
368 public int compare(Object o1, Object o2)
\r
371 final File a = (File) o1;
\r
372 final File b = (File) o2;
\r
374 if (a.lastModified() > b.lastModified())
\r
378 else if (a.lastModified() == b.lastModified())
\r
392 else if(sortingColumnName.equals(UIManager.getString("FileChooser.fileSizeHeaderText")))
\r
395 Collections.sort(data,
\r
399 public int compare(Object o1, Object o2)
\r
402 final File a = (File) o1;
\r
403 final File b = (File) o2;
\r
405 if (a.length() > b.length())
\r
409 else if (a.length() == b.length())
\r
423 else if(sortingColumnName.equals(UIManager.getString("FileChooser.fileTypeHeaderText")))
\r
425 Collections.sort(data,
\r
428 public int compare(Object o1, Object o2)
\r
430 final String a = getFileSystemView().getSystemTypeDescription( (File)o1 );
\r
431 final String b = getFileSystemView().getSystemTypeDescription( (File)o2 );
\r
434 return a.compareToIgnoreCase(b);
\r
438 return -1 * a.compareToIgnoreCase(b);
\r
443 else if(sortingColumnName.equals(UIManager.getString("FileChooser.fileNameHeaderText")))
\r
445 Collections.sort(data,
\r
448 public int compare(Object o1, Object o2)
\r
450 final String a = ((File) o1).getName();
\r
451 final String b = ((File) o2).getName();
\r
455 return a.compareToIgnoreCase(b);
\r
459 return -1 * a.compareToIgnoreCase(b);
\r