4 import java.awt.event.*;
9 import javax.swing.plaf.*;
10 import javax.swing.plaf.basic.*;
11 import javax.swing.plaf.metal.*;
12 import javax.swing.table.*;
14 public class JalviewFileChooser extends JFileChooser
17 private static final int COLUMN_FILENAME = 0;
18 private static final int COLUMN_FILESIZE = 1;
19 private static final int COLUMN_FILETYPE = 2;
20 private static final int COLUMN_FILEDATE = 3;
21 private static final int COLUMN_FILEATTR = 4;
22 private static final int COLUMN_COLCOUNT = 5;
23 private static String[] COLUMNS = null;
25 public JalviewFileChooser(String dir)
30 Locale l = getLocale();
31 COLUMNS = new String[]{
32 UIManager.getString("FileChooser.fileNameHeaderText",l),
33 UIManager.getString("FileChooser.fileSizeHeaderText",l),
34 UIManager.getString("FileChooser.fileTypeHeaderText",l),
35 UIManager.getString("FileChooser.fileDateHeaderText",l),
36 UIManager.getString("FileChooser.fileAttrHeaderText",l)
42 public JalviewFileChooser()
46 Locale l = getLocale();
47 COLUMNS = new String[]{
48 UIManager.getString("FileChooser.fileNameHeaderText",l),
49 UIManager.getString("FileChooser.fileSizeHeaderText",l),
50 UIManager.getString("FileChooser.fileTypeHeaderText",l),
51 UIManager.getString("FileChooser.fileDateHeaderText",l),
52 UIManager.getString("FileChooser.fileAttrHeaderText",l)
58 /**************************************************************************
59 * Always create the local UI
61 *************************************************************************/
62 public final void setUI(ComponentUI comp)
64 super.setUI(new UI(this));
67 /**************************************************************************
68 * Internal implementation of Metal LookAndFeel to create the table sorting
70 *************************************************************************/
71 private final static class UI extends MetalFileChooserUI
73 private DirectoryModel model;
75 /**************************************************************************
76 * Must be overridden to extend
78 *************************************************************************/
79 public UI(JFileChooser e)
84 /**************************************************************************
85 * Overridden to create our own model
86 *************************************************************************/
87 protected final void createModel()
89 model = new DirectoryModel(getFileChooser());
92 /**************************************************************************
93 * Overridden to get our own model
95 *************************************************************************/
96 public final BasicDirectoryModel getModel() { return model; }
98 /**************************************************************************
99 * Calls the default method then adds a MouseListener to the JTable
102 *************************************************************************/
103 protected final JPanel createDetailsView(JFileChooser chooser)
105 final JPanel panel = super.createDetailsView(chooser);
107 //Since we can't access MetalFileChooserUI's private member detailsTable
108 //directly, we have to find it in the JPanel
109 final JTable tbl = findJTable(panel.getComponents());
112 //Fix the columns so they can't be rearranged, if we don't do this
113 //we would need to keep track when each column is moved
114 tbl.getTableHeader().setReorderingAllowed(false);
116 //Add a mouselistener to listen for clicks on column headers
117 tbl.getTableHeader().addMouseListener(new MouseAdapter(){
118 public void mouseClicked(MouseEvent e)
120 //Only process single clicks
121 if (e.getClickCount() > 1) return;
123 final int col = tbl.getTableHeader().columnAtPoint(e.getPoint());
124 if (col == COLUMN_FILENAME || col == COLUMN_FILESIZE ||
125 col == COLUMN_FILEDATE)
133 /**************************************************************************
134 * Finds the JTable in the panel so we can add MouseListener
137 *************************************************************************/
138 private final static JTable findJTable(Component[] comp)
140 for (int i=0;i<comp.length;i++)
142 if (comp[i] instanceof JTable)
144 return (JTable)comp[i];
146 if (comp[i] instanceof Container)
148 JTable tbl = findJTable(((Container)comp[i]).getComponents());
149 if (tbl != null) return tbl;
156 /***************************************************************************
157 * Implementation of BasicDirectoryModel that sorts the Files by column
158 **************************************************************************/
159 private final static class DirectoryModel extends BasicDirectoryModel
164 /**************************************************************************
165 * Must be overridden to extend BasicDirectoryModel
167 *************************************************************************/
168 DirectoryModel(JFileChooser chooser)
174 /**************************************************************************
175 * Supposedly this is not used anymore, hopefully not. We implemented
176 * some basic attempt at sorting just in case
180 *************************************************************************/
181 protected final boolean lt(File a, File b)
183 System.out.println("LT called?");
184 boolean less = false;
187 case COLUMN_FILEDATE:
188 less = a.lastModified() > b.lastModified();
190 case COLUMN_FILESIZE:
191 less = a.length() > b.length();
194 less = a.getName().compareToIgnoreCase(b.getName()) > 0;
197 if (ascending) return less = !less;
201 /**************************************************************************
202 * Resorts the JFileChooser table based on new column
204 *************************************************************************/
205 protected final void sort(int c, JTable tbl)
207 //Set column and order
209 ascending = !ascending;
210 String indicator = " (^)";
214 final JTableHeader th = tbl.getTableHeader();
215 final TableColumnModel tcm = th.getColumnModel();
217 for (int i=0;i<JalviewFileChooser.COLUMN_COLCOUNT;i++)
219 final TableColumn tc = tcm.getColumn( i ); // the column to change
220 tc.setHeaderValue( COLUMNS[i] );
223 final TableColumn tc = tcm.getColumn( col ); // the column to change
224 tc.setHeaderValue( COLUMNS[col] + indicator );
228 //Requery the file listing
232 /**************************************************************************
233 * Sorts the data based on current column setting
235 *************************************************************************/
236 protected final void sort(Vector data)
240 case COLUMN_FILEDATE:
241 Collections.sort(data,new Comparator(){
242 public int compare(Object o1,Object o2)
245 final File a = (File)o1;
246 final File b = (File)o2;
247 if (a.lastModified() > b.lastModified())
249 else if (a.lastModified() == b.lastModified())
259 case COLUMN_FILESIZE:
260 Collections.sort(data,new Comparator(){
261 public int compare(Object o1,Object o2)
264 final File a = (File)o1;
265 final File b = (File)o2;
266 if (a.length() > b.length())
268 else if (a.length() == b.length())
278 case COLUMN_FILENAME:
279 Collections.sort(data,new Comparator(){
280 public int compare(Object o1,Object o2)
282 final File a = (File)o1;
283 final File b = (File)o2;
285 return a.getName().compareToIgnoreCase(b.getName());
287 return -1 * a.getName().compareToIgnoreCase(b.getName());