Formatting
[jalview.git] / src / jalview / io / JalviewFileChooser.java
index e731ddc..556bb72 100755 (executable)
-\r
-/*///////////////////////////////////////////////////////////////////\r
-// This file was taken from java forum\r
-// Re: JFileChooser functioning like normal Windows Apps FileChooser\r
-// Author: ddanimal\r
-// http://forum.java.sun.com/thread.jspa?forumID=57&threadID=327712\r
-*///////////////////////////////////////////////////////////////////\r
-\r
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ */\r
+\r
+//////////////////////////////////////////////////////////////////\r
 package jalview.io;\r
 \r
-import java.awt.*;\r
-import java.awt.event.*;\r
 import java.io.*;\r
 import java.util.*;\r
 \r
+import java.awt.*;\r
+import java.awt.event.*;\r
 import javax.swing.*;\r
-import javax.swing.plaf.*;\r
-import javax.swing.plaf.basic.*;\r
-import javax.swing.plaf.metal.*;\r
-import javax.swing.table.*;\r
 \r
-public class JalviewFileChooser extends JFileChooser\r
+public class JalviewFileChooser\r
+    extends JFileChooser\r
 {\r
 \r
-  private static final int COLUMN_FILENAME = 0;\r
-  private static final int COLUMN_FILESIZE = 1;\r
-  private static final int COLUMN_FILETYPE = 2;\r
-  private static final int COLUMN_FILEDATE = 3;\r
-  private static final int COLUMN_FILEATTR = 4;\r
-  private static final int COLUMN_COLCOUNT = 5;\r
-  private static String[] COLUMNS = null;\r
+  public JalviewFileChooser(String dir)\r
+  {\r
+    super(dir);\r
+    setAccessory(new RecentlyOpened());\r
+  }\r
 \r
+  public JalviewFileChooser(String dir,\r
+                            String[] suffix,\r
+                            String[] desc,\r
+                            String selected,\r
+                            boolean selectAll)\r
+  {\r
+    super(dir);\r
+    init(suffix, desc, selected, selectAll);\r
+  }\r
 \r
-  public JalviewFileChooser(String dir, String [] suffix, String [] desc, String selected)\r
+  public JalviewFileChooser(String dir,\r
+                            String[] suffix,\r
+                            String[] desc,\r
+                            String selected)\r
   {\r
     super(dir);\r
+    init(suffix, desc, selected, true);\r
+  }\r
+\r
+  void init(String[] suffix,\r
+            String[] desc,\r
+            String selected,\r
+            boolean selectAll)\r
+  {\r
 \r
     JalviewFileFilter chosen = null;\r
-    for(int i=0; i<suffix.length; i++)\r
+\r
+    //SelectAllFilter needs to be set first before adding further\r
+    //file filters to fix bug on Mac OSX\r
+    setAcceptAllFileFilterUsed(selectAll);\r
+\r
+    for (int i = 0; i < suffix.length; i++)\r
     {\r
       JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);\r
       addChoosableFileFilter(jvf);\r
-      if(selected!=null && selected.equals(desc[i]))\r
+\r
+      if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))\r
+      {\r
         chosen = jvf;\r
+      }\r
     }\r
-    if(chosen!=null)\r
+\r
+    if (chosen != null)\r
+    {\r
       setFileFilter(chosen);\r
-    initColumns();\r
+    }\r
+\r
+    setAccessory(new RecentlyOpened());\r
+  }\r
+\r
+  public void setFileFilter(javax.swing.filechooser.FileFilter filter)\r
+  {\r
+    super.setFileFilter(filter);\r
+\r
+    try\r
+    {\r
+      if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)\r
+      {\r
+        final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.\r
+            basic.BasicFileChooserUI) getUI();\r
+        final String name = ui.getFileName().trim();\r
+\r
+        if ( (name == null) || (name.length() == 0))\r
+        {\r
+          return;\r
+        }\r
+\r
+        EventQueue.invokeLater(new Thread()\r
+        {\r
+          public void run()\r
+          {\r
+            String currentName = ui.getFileName();\r
+            if ( (currentName == null) || (currentName.length() == 0))\r
+            {\r
+              ui.setFileName(name);\r
+            }\r
+          }\r
+        });\r
+      }\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+      ex.printStackTrace();\r
+      // Some platforms do not have BasicFileChooserUI\r
+    }\r
   }\r
 \r
   public String getSelectedFormat()\r
   {\r
+    if (getFileFilter() == null)\r
+    {\r
+      return null;\r
+    }\r
 \r
     String format = getFileFilter().getDescription();\r
 \r
     if (format.toUpperCase().startsWith("JALVIEW"))\r
+    {\r
       format = "Jalview";\r
+    }\r
     else if (format.toUpperCase().startsWith("FASTA"))\r
+    {\r
       format = "FASTA";\r
+    }\r
     else if (format.toUpperCase().startsWith("MSF"))\r
+    {\r
       format = "MSF";\r
+    }\r
     else if (format.toUpperCase().startsWith("CLUSTAL"))\r
+    {\r
       format = "CLUSTAL";\r
+    }\r
     else if (format.toUpperCase().startsWith("BLC"))\r
+    {\r
       format = "BLC";\r
+    }\r
     else if (format.toUpperCase().startsWith("PIR"))\r
+    {\r
       format = "PIR";\r
+    }\r
     else if (format.toUpperCase().startsWith("PFAM"))\r
+    {\r
       format = "PFAM";\r
+    }\r
 \r
     return format;\r
   }\r
-  public JalviewFileChooser(String dir)\r
+\r
+  public int showSaveDialog(Component parent)\r
+      throws HeadlessException\r
   {\r
-    super(dir);\r
-    initColumns();\r
-  }\r
+    this.setAccessory(null);\r
+\r
+    setDialogType(SAVE_DIALOG);\r
+\r
+    int ret = showDialog(parent, "Save");\r
 \r
-  public int showSaveDialog(Component parent) throws HeadlessException {\r
-      setDialogType(SAVE_DIALOG);\r
-      int ret = showDialog(parent, null);\r
+    if (getFileFilter() instanceof JalviewFileFilter)\r
+    {\r
+      JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();\r
 \r
-      JalviewFileFilter jvf = (JalviewFileFilter)getFileFilter();\r
-      if( !jvf.accept( getSelectedFile() ) )\r
+      if (!jvf.accept(getSelectedFile()))\r
       {\r
-        String withExtension = getSelectedFile()+"."+jvf.getAcceptableExtension();\r
+        String withExtension = getSelectedFile() + "." +\r
+            jvf.getAcceptableExtension();\r
         setSelectedFile(new File(withExtension));\r
       }\r
+    }\r
 \r
-      if(ret == JalviewFileChooser.APPROVE_OPTION  &&  getSelectedFile().exists() )\r
+    if ( (ret == JalviewFileChooser.APPROVE_OPTION) &&\r
+        getSelectedFile().exists())\r
+    {\r
+      int confirm = JOptionPane.showConfirmDialog(parent,\r
+                                                  "Overwrite existing file?",\r
+                                                  "File exists",\r
+                                                  JOptionPane.YES_NO_OPTION);\r
+\r
+      if (confirm != JOptionPane.YES_OPTION)\r
       {\r
-       int confirm =  JOptionPane.showConfirmDialog(parent,\r
-                                      "Overwrite existing file?",\r
-                                      "File exists",\r
-                                      JOptionPane.YES_NO_OPTION);\r
-       if(confirm!=JOptionPane.YES_OPTION)\r
-         ret = this.CANCEL_OPTION;\r
+        ret = JalviewFileChooser.CANCEL_OPTION;\r
       }\r
-      return ret;\r
-  }\r
+    }\r
 \r
+    return ret;\r
+  }\r
 \r
-  void initColumns()\r
+  void recentListSelectionChanged(String selection)\r
   {\r
-    if (COLUMNS == null)\r
+    setSelectedFile(null);\r
+\r
+    File file = new File(selection);\r
+    if (getFileFilter() instanceof JalviewFileFilter)\r
     {\r
-        Locale l = getLocale();\r
-        COLUMNS = new String[]{\r
-        UIManager.getString("FileChooser.fileNameHeaderText",l),\r
-        UIManager.getString("FileChooser.fileSizeHeaderText",l),\r
-        UIManager.getString("FileChooser.fileTypeHeaderText",l),\r
-        UIManager.getString("FileChooser.fileDateHeaderText",l),\r
-        UIManager.getString("FileChooser.fileAttrHeaderText",l)\r
-      };\r
+      JalviewFileFilter jvf = (JalviewFileFilter)this.getFileFilter();\r
+\r
+      if (!jvf.accept(file))\r
+      {\r
+        setFileFilter(getChoosableFileFilters()[0]);\r
+      }\r
     }\r
+\r
+    setSelectedFile(file);\r
   }\r
 \r
-  /**************************************************************************\r
-   * Always create the local UI\r
-   * @param comp\r
-   *************************************************************************/\r
-  public final void setUI(ComponentUI comp)\r
+  class RecentlyOpened\r
+      extends JPanel\r
   {\r
-    super.setUI(new UI(this));\r
-  }\r
+    JList list;\r
+    public RecentlyOpened()\r
+    {\r
+      String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");\r
+      StringTokenizer st;\r
+      Vector recent = new Vector();\r
+\r
+      if (historyItems != null)\r
+      {\r
+        st = new StringTokenizer(historyItems, "\t");\r
+\r
+        while (st.hasMoreTokens())\r
+        {\r
+          recent.addElement(st.nextElement());\r
+        }\r
+      }\r
+\r
+      list = new JList(recent);\r
+\r
+      DefaultListCellRenderer dlcr = new DefaultListCellRenderer();\r
+      dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);\r
+      list.setCellRenderer(dlcr);\r
+\r
+      list.addMouseListener(new MouseAdapter()\r
+      {\r
+        public void mousePressed(MouseEvent evt)\r
+        {\r
+          recentListSelectionChanged(list.getSelectedValue().toString());\r
+        }\r
+      });\r
+\r
+      this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));\r
+\r
+      final JScrollPane scroller = new JScrollPane(list);\r
+      scroller.setPreferredSize(new Dimension(130, 200));\r
+      this.add(scroller);\r
+\r
+      javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+      {\r
+        public void run()\r
+        {\r
+          scroller.getHorizontalScrollBar().setValue(\r
+              scroller.getHorizontalScrollBar().getMaximum());\r
+        }\r
+      });\r
+\r
+    }\r
 \r
-   /**************************************************************************\r
-    * Internal implementation of Metal LookAndFeel to create the table sorting\r
-    * ability.\r
-    *************************************************************************/\r
-   private final static class UI extends MetalFileChooserUI\r
-   {\r
-     private DirectoryModel model;\r
-\r
-     /**************************************************************************\r
-      * Must be overridden to extend\r
-      * @param e\r
-      *************************************************************************/\r
-     public UI(JFileChooser e)\r
-     {\r
-       super(e);\r
-     }\r
-\r
-     /**************************************************************************\r
-      * Overridden to create our own model\r
-      *************************************************************************/\r
-     protected final void createModel()\r
-     {\r
-       model = new DirectoryModel(getFileChooser());\r
-     }\r
-\r
-     /**************************************************************************\r
-      * Overridden to get our own model\r
-      * @return\r
-      *************************************************************************/\r
-     public final BasicDirectoryModel getModel() {       return model;     }\r
-\r
-     /**************************************************************************\r
-      * Calls the default method then adds a MouseListener to the JTable\r
-      * @param chooser\r
-      * @return\r
-      *************************************************************************/\r
-     protected final JPanel createDetailsView(JFileChooser chooser)\r
-     {\r
-       final JPanel panel = super.createDetailsView(chooser);\r
-\r
-       //Since we can't access MetalFileChooserUI's private member detailsTable\r
-       //directly, we have to find it in the JPanel\r
-       final JTable tbl = findJTable(panel.getComponents());\r
-       if (tbl != null)\r
-       {\r
-         //Fix the columns so they can't be rearranged, if we don't do this\r
-         //we would need to keep track when each column is moved\r
-         tbl.getTableHeader().setReorderingAllowed(false);\r
-\r
-         //Add a mouselistener to listen for clicks on column headers\r
-         tbl.getTableHeader().addMouseListener(new MouseAdapter(){\r
-           public void mouseClicked(MouseEvent e)\r
-           {\r
-             //Only process single clicks\r
-             if (e.getClickCount() > 1) return;\r
-             e.consume();\r
-             final int col = tbl.getTableHeader().columnAtPoint(e.getPoint());\r
-             if (col == COLUMN_FILENAME || col == COLUMN_FILESIZE ||\r
-                 col == COLUMN_FILEDATE)\r
-               model.sort(col,tbl);\r
-           }\r
-         });\r
-       }\r
-       return panel;\r
-     }\r
-\r
-     /**************************************************************************\r
-      * Finds the JTable in the panel so we can add MouseListener\r
-      * @param comp\r
-      * @return\r
-      *************************************************************************/\r
-     private final static JTable findJTable(Component[] comp)\r
-     {\r
-       for (int i=0;i<comp.length;i++)\r
-       {\r
-         if (comp[i] instanceof JTable)\r
-         {\r
-           return (JTable)comp[i];\r
-         }\r
-         if (comp[i] instanceof Container)\r
-         {\r
-           JTable tbl = findJTable(((Container)comp[i]).getComponents());\r
-           if (tbl != null) return tbl;\r
-         }\r
-       }\r
-       return null;\r
-     }\r
-   }\r
-\r
-  /***************************************************************************\r
-   * Implementation of BasicDirectoryModel that sorts the Files by column\r
-   **************************************************************************/\r
-  private final static class DirectoryModel extends BasicDirectoryModel\r
-   {\r
-     int col = 0;\r
-     boolean ascending;\r
-\r
-     /**************************************************************************\r
-      * Must be overridden to extend BasicDirectoryModel\r
-      * @param chooser\r
-      *************************************************************************/\r
-     DirectoryModel(JFileChooser chooser)\r
-     {\r
-       super(chooser);\r
-     }\r
-\r
-\r
-     /**************************************************************************\r
-      * Supposedly this is not used anymore, hopefully not.  We implemented\r
-      * some basic attempt at sorting just in case\r
-      * @param a\r
-      * @param b\r
-      * @return\r
-      *************************************************************************/\r
-     protected final boolean lt(File a, File b)\r
-     {\r
-       System.out.println("LT called?");\r
-       boolean less = false;\r
-       switch (col)\r
-       {\r
-         case COLUMN_FILEDATE:\r
-           less = a.lastModified() > b.lastModified();\r
-           break;\r
-         case COLUMN_FILESIZE:\r
-           less = a.length() > b.length();\r
-           break;\r
-         default:\r
-           less = a.getName().compareToIgnoreCase(b.getName()) > 0;\r
-         break;\r
-       }\r
-       if (ascending) return less = !less;\r
-       return less;\r
-     }\r
-\r
-     /**************************************************************************\r
-      * Resorts the JFileChooser table based on new column\r
-      * @param c\r
-      *************************************************************************/\r
-     protected final void sort(int c, JTable tbl)\r
-     {\r
-       //Set column and order\r
-       col = c;\r
-       ascending = !ascending;\r
-       String indicator = " (^)";\r
-       if (ascending)\r
-         indicator = " (v)";\r
-\r
-       final JTableHeader th = tbl.getTableHeader();\r
-       final TableColumnModel tcm = th.getColumnModel();\r
-\r
-       for (int i=0;i<JalviewFileChooser.COLUMN_COLCOUNT;i++)\r
-       {\r
-         final TableColumn tc = tcm.getColumn( i ); // the column to change\r
-         tc.setHeaderValue( COLUMNS[i] );\r
-       }\r
-\r
-       final TableColumn tc = tcm.getColumn( col ); // the column to change\r
-       tc.setHeaderValue( COLUMNS[col] + indicator );\r
-\r
-       th.repaint();\r
-\r
-       //Requery the file listing\r
-       validateFileCache();\r
-     }\r
-\r
-     /**************************************************************************\r
-      * Sorts the data based on current column setting\r
-      * @param data\r
-      *************************************************************************/\r
-     protected final void sort(Vector data)\r
-     {\r
-       switch (col)\r
-       {\r
-         case COLUMN_FILEDATE:\r
-           Collections.sort(data,new Comparator(){\r
-             public int compare(Object o1,Object o2)\r
-             {\r
-               int ret = 1;\r
-               final File a = (File)o1;\r
-               final File b = (File)o2;\r
-               if (a.lastModified() > b.lastModified())\r
-                 ret = -1;\r
-               else if (a.lastModified() == b.lastModified())\r
-                 ret = 0;\r
-\r
-               if (ascending)\r
-                 ret *= -1;\r
-               return ret;\r
-             }\r
-\r
-           });\r
-           break;\r
-         case COLUMN_FILESIZE:\r
-           Collections.sort(data,new Comparator(){\r
-             public int compare(Object o1,Object o2)\r
-             {\r
-               int ret = 1;\r
-               final File a = (File)o1;\r
-               final File b = (File)o2;\r
-               if (a.length() > b.length())\r
-                 ret = -1;\r
-               else if (a.length() == b.length())\r
-                 ret = 0;\r
-\r
-               if (ascending)\r
-                 ret *= -1;\r
-               return ret;\r
-             }\r
-\r
-           });\r
-           break;\r
-         case COLUMN_FILENAME:\r
-           Collections.sort(data,new Comparator(){\r
-             public int compare(Object o1,Object o2)\r
-             {\r
-               final File a = (File)o1;\r
-               final File b = (File)o2;\r
-               if (ascending)\r
-                 return a.getName().compareToIgnoreCase(b.getName());\r
-               else\r
-                 return -1 * a.getName().compareToIgnoreCase(b.getName());\r
-             }\r
-\r
-           });\r
-           break;\r
-       }\r
-     }\r
   }\r
 }\r
-\r