X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FJalviewFileChooser.java;h=1f44c99ab356211672316d96cc86757872313840;hb=b8d09897dacc7b0ad203982b4578e2c1d8929142;hp=03daa17a89ed5e7a722115bbb235e3638e8c1b9a;hpb=02417ff502a66dbbf96e55ce3cfb2aec1469e11d;p=jalview.git diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 03daa17..1f44c99 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -1,122 +1,177 @@ - +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ /*/////////////////////////////////////////////////////////////////// // This file was taken from java forum // Re: JFileChooser functioning like normal Windows Apps FileChooser // Author: ddanimal // http://forum.java.sun.com/thread.jspa?forumID=57&threadID=327712 -*/////////////////////////////////////////////////////////////////// +*/ +////////////////////////////////////////////////////////////////// package jalview.io; -import java.awt.*; -import java.awt.event.*; import java.io.*; import java.util.*; +import java.awt.*; +import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.*; import javax.swing.plaf.basic.*; import javax.swing.plaf.metal.*; import javax.swing.table.*; -public class JalviewFileChooser extends JFileChooser +public class JalviewFileChooser + extends JFileChooser { - private static final int COLUMN_FILENAME = 0; - private static final int COLUMN_FILESIZE = 1; - private static final int COLUMN_FILETYPE = 2; - private static final int COLUMN_FILEDATE = 3; - private static final int COLUMN_FILEATTR = 4; - private static final int COLUMN_COLCOUNT = 5; - private static String[] COLUMNS = null; - - public JalviewFileChooser(String dir, String [] suffix, String [] desc, String selected) + public JalviewFileChooser(String dir, String[] suffix, String[] desc, + String selected) { super(dir); JalviewFileFilter chosen = null; - for(int i=0; i 1) return; - e.consume(); - final int col = tbl.getTableHeader().columnAtPoint(e.getPoint()); - if (col == COLUMN_FILENAME || col == COLUMN_FILESIZE || - col == COLUMN_FILEDATE) - model.sort(col,tbl); - } - }); - } - return panel; - } - - /************************************************************************** - * Finds the JTable in the panel so we can add MouseListener - * @param comp - * @return - *************************************************************************/ - private final static JTable findJTable(Component[] comp) - { - for (int i=0;i b.lastModified(); - break; - case COLUMN_FILESIZE: - less = a.length() > b.length(); - break; - default: - less = a.getName().compareToIgnoreCase(b.getName()) > 0; - break; - } - if (ascending) return less = !less; - return less; - } - - /************************************************************************** - * Resorts the JFileChooser table based on new column - * @param c - *************************************************************************/ - protected final void sort(int c, JTable tbl) - { - //Set column and order - col = c; - ascending = !ascending; - String indicator = " (^)"; - if (ascending) - indicator = " (v)"; - - final JTableHeader th = tbl.getTableHeader(); - final TableColumnModel tcm = th.getColumnModel(); - - for (int i=0;i b.lastModified()) - ret = -1; - else if (a.lastModified() == b.lastModified()) - ret = 0; - - if (ascending) - ret *= -1; - return ret; - } - - }); - break; - case COLUMN_FILESIZE: - Collections.sort(data,new Comparator(){ - public int compare(Object o1,Object o2) - { - int ret = 1; - final File a = (File)o1; - final File b = (File)o2; - if (a.length() > b.length()) - ret = -1; - else if (a.length() == b.length()) - ret = 0; - - if (ascending) - ret *= -1; - return ret; - } - - }); - break; - case COLUMN_FILENAME: - Collections.sort(data,new Comparator(){ - public int compare(Object o1,Object o2) - { - final File a = (File)o1; - final File b = (File)o2; - if (ascending) - return a.getName().compareToIgnoreCase(b.getName()); - else - return -1 * a.getName().compareToIgnoreCase(b.getName()); - } - - }); - break; - } - } + private final class DirectoryModel + extends BasicDirectoryModel + { + String sortingColumnName=null; + int col = 0; + boolean ascending; + + /************************************************************************** + * Must be overridden to extend BasicDirectoryModel + * @param chooser + *************************************************************************/ + DirectoryModel(JFileChooser chooser) + { + super(chooser); + } + + + /************************************************************************** + * Resorts the JFileChooser table based on new column + * @param c + *************************************************************************/ + protected final void sort(int c, JTable tbl) + { + + + //Set column and order + col = c; + ascending = !ascending; + + String indicator = " (^)"; + + if (ascending) + { + indicator = " (v)"; + } + + final JTableHeader th = tbl.getTableHeader(); + final TableColumnModel tcm = th.getColumnModel(); + TableColumn tc = null; + String title = null; + + + //Remove indicator from old sort, and add to new + for(int i=0; i b.lastModified()) + { + ret = -1; + } + else if (a.lastModified() == b.lastModified()) + { + ret = 0; + } + + if (ascending) + { + ret *= -1; + } + + return ret; + } + }); + } + else if(sortingColumnName.equals(UIManager.getString("FileChooser.fileSizeHeaderText"))) + { + + Collections.sort(data, + new Comparator() + { + + public int compare(Object o1, Object o2) + { + int ret = 1; + final File a = (File) o1; + final File b = (File) o2; + + if (a.length() > b.length()) + { + ret = -1; + } + else if (a.length() == b.length()) + { + ret = 0; + } + + if (ascending) + { + ret *= -1; + } + + return ret; + } + }); + } + else if(sortingColumnName.equals(UIManager.getString("FileChooser.fileTypeHeaderText"))) + { + Collections.sort(data, + new Comparator() + { + public int compare(Object o1, Object o2) + { + final String a = getFileSystemView().getSystemTypeDescription( (File)o1 ); + final String b = getFileSystemView().getSystemTypeDescription( (File)o2 ); + if (ascending) + { + return a.compareToIgnoreCase(b); + } + else + { + return -1 * a.compareToIgnoreCase(b); + } + } + }); + } + else if(sortingColumnName.equals(UIManager.getString("FileChooser.fileNameHeaderText"))) +{ + Collections.sort(data, + new Comparator() + { + public int compare(Object o1, Object o2) + { + final String a = ((File) o1).getName(); + final String b = ((File) o2).getName(); + + if (ascending) + { + return a.compareToIgnoreCase(b); + } + else + { + return -1 * a.compareToIgnoreCase(b); + } + } + }); } -} + } + } +}