update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / JalviewFileChooser.java
index 2bac0de..b128997 100755 (executable)
-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2005 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.io.*;\r
-import java.awt.*;\r
-import javax.swing.*;\r
-\r
-public class JalviewFileChooser\r
-    extends JFileChooser\r
-{\r
-\r
-  public JalviewFileChooser(String dir)\r
-  {\r
-    super(dir);\r
-  }\r
-\r
-  public JalviewFileChooser(String dir, String[] suffix, String[] desc,\r
-                            String selected)\r
-  {\r
-    super(dir);\r
-\r
-    JalviewFileFilter chosen = null;\r
-\r
-    for (int i = 0; i < suffix.length; i++)\r
-    {\r
-      JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);\r
-      addChoosableFileFilter(jvf);\r
-\r
-      if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))\r
-      {\r
-        chosen = jvf;\r
-      }\r
-    }\r
-\r
-    if (chosen != null)\r
-    {\r
-      setFileFilter(chosen);\r
-    }\r
-  }\r
-\r
-  public void setFileFilter(javax.swing.filechooser.FileFilter filter)\r
-  {\r
-    super.setFileFilter(filter);\r
-\r
-    try{\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
-    }catch(Exception ex)\r
-    {\r
-      // Some platforms do not have BasicFileChooserUI\r
-    }\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
-\r
-  public int showSaveDialog(Component parent)\r
-      throws HeadlessException\r
-  {\r
-    setDialogType(SAVE_DIALOG);\r
-\r
-    int ret = showDialog(parent, null);\r
-\r
-    if (getFileFilter() instanceof JalviewFileFilter)\r
-    {\r
-      JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();\r
-\r
-      if (!jvf.accept(getSelectedFile()))\r
-      {\r
-        String withExtension = getSelectedFile() + "." +\r
-            jvf.getAcceptableExtension();\r
-        setSelectedFile(new File(withExtension));\r
-      }\r
-    }\r
-\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
-        ret = JalviewFileChooser.CANCEL_OPTION;\r
-      }\r
-    }\r
-\r
-    return ret;\r
-  }\r
-}\r
-\r
-\r
-\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview 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 3 of the License, or (at your option) any later version.
+ * 
+ * Jalview 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 Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ */
+//////////////////////////////////////////////////////////////////
+package jalview.io;
+
+import java.io.*;
+import java.util.*;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+
+/**
+ * Enhanced file chooser dialog box.
+ * 
+ * NOTE: bug on Windows systems when filechooser opened on directory to view
+ * files with colons in title.
+ * 
+ * @author AMW
+ * 
+ */
+public class JalviewFileChooser extends JFileChooser
+{
+  public JalviewFileChooser(String dir)
+  {
+    super(safePath(dir));
+    setAccessory(new RecentlyOpened());
+  }
+
+  private static File safePath(String dir)
+  {
+    if (dir == null)
+    {
+      return null;
+    }
+
+    File f = new File(dir);
+    if (f.getName().indexOf(':') > -1)
+    {
+      return null;
+    }
+    return f;
+  }
+
+  public JalviewFileChooser(String dir, String[] suffix, String[] desc,
+          String selected, boolean selectAll)
+  {
+    super(safePath(dir));
+    init(suffix, desc, selected, selectAll);
+  }
+
+  public JalviewFileChooser(String dir, String[] suffix, String[] desc,
+          String selected)
+  {
+    super(safePath(dir));
+    init(suffix, desc, selected, true);
+  }
+
+  void init(String[] suffix, String[] desc, String selected,
+          boolean selectAll)
+  {
+
+    JalviewFileFilter chosen = null;
+
+    // SelectAllFilter needs to be set first before adding further
+    // file filters to fix bug on Mac OSX
+    setAcceptAllFileFilterUsed(selectAll);
+    
+    for (int i = 0; i < suffix.length; i++)
+    {
+      JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
+      addChoosableFileFilter(jvf);
+      if ((selected != null) && selected.equalsIgnoreCase(desc[i]))
+      {
+        chosen = jvf;
+      }
+    }
+
+    if (chosen != null)
+    {
+      setFileFilter(chosen);
+    }
+
+    setAccessory(new RecentlyOpened());
+  }
+
+  public void setFileFilter(javax.swing.filechooser.FileFilter filter)
+  {
+    super.setFileFilter(filter);
+
+    try
+    {
+      if (getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
+      {
+        final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.basic.BasicFileChooserUI) getUI();
+        final String name = ui.getFileName().trim();
+
+        if ((name == null) || (name.length() == 0))
+        {
+          return;
+        }
+
+        EventQueue.invokeLater(new Thread()
+        {
+          public void run()
+          {
+            String currentName = ui.getFileName();
+            if ((currentName == null) || (currentName.length() == 0))
+            {
+              ui.setFileName(name);
+            }
+          }
+        });
+      }
+    } catch (Exception ex)
+    {
+      ex.printStackTrace();
+      // Some platforms do not have BasicFileChooserUI
+    }
+  }
+
+  public String getSelectedFormat()
+  {
+    if (getFileFilter() == null)
+    {
+      return null;
+    }
+
+    String format = getFileFilter().getDescription();
+
+    if (format.toUpperCase().startsWith("JALVIEW"))
+    {
+      format = "Jalview";
+    }
+    else if (format.toUpperCase().startsWith("FASTA"))
+    {
+      format = "FASTA";
+    }
+    else if (format.toUpperCase().startsWith("MSF"))
+    {
+      format = "MSF";
+    }
+    else if (format.toUpperCase().startsWith("CLUSTAL"))
+    {
+      format = "CLUSTAL";
+    }
+    else if (format.toUpperCase().startsWith("BLC"))
+    {
+      format = "BLC";
+    }
+    else if (format.toUpperCase().startsWith("PIR"))
+    {
+      format = "PIR";
+    }
+    else if (format.toUpperCase().startsWith("PFAM"))
+    {
+      format = "PFAM";
+    }
+
+    return format;
+  }
+
+  public int showSaveDialog(Component parent) throws HeadlessException
+  {
+    this.setAccessory(null);
+
+    setDialogType(SAVE_DIALOG);
+
+    int ret = showDialog(parent, "Save");
+
+    if (getFileFilter() instanceof JalviewFileFilter)
+    {
+      JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
+
+      if (!jvf.accept(getSelectedFile()))
+      {
+        String withExtension = getSelectedFile() + "."
+                + jvf.getAcceptableExtension();
+        setSelectedFile(new File(withExtension));
+      }
+    }
+    // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
+    // USER PROMPTED FOR A NEW FILENAME
+    if ((ret == JalviewFileChooser.APPROVE_OPTION)
+            && getSelectedFile().exists())
+    {
+      int confirm = JOptionPane.showConfirmDialog(parent,
+              "Overwrite existing file?", "File exists",
+              JOptionPane.YES_NO_OPTION);
+
+      if (confirm != JOptionPane.YES_OPTION)
+      {
+        ret = JalviewFileChooser.CANCEL_OPTION;
+      }
+    }
+
+    return ret;
+  }
+
+  void recentListSelectionChanged(String selection)
+  {
+    setSelectedFile(null);
+
+    File file = new File(selection);
+    if (getFileFilter() instanceof JalviewFileFilter)
+    {
+      JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
+
+      if (!jvf.accept(file))
+      {
+        setFileFilter(getChoosableFileFilters()[0]);
+      }
+    }
+
+    setSelectedFile(file);
+  }
+
+  class RecentlyOpened extends JPanel
+  {
+    JList list;
+
+    public RecentlyOpened()
+    {
+      String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
+      StringTokenizer st;
+      Vector recent = new Vector();
+
+      if (historyItems != null)
+      {
+        st = new StringTokenizer(historyItems, "\t");
+
+        while (st.hasMoreTokens())
+        {
+          recent.addElement(st.nextElement());
+        }
+      }
+
+      list = new JList(recent);
+
+      DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
+      dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
+      list.setCellRenderer(dlcr);
+
+      list.addMouseListener(new MouseAdapter()
+      {
+        public void mousePressed(MouseEvent evt)
+        {
+          recentListSelectionChanged(list.getSelectedValue().toString());
+        }
+      });
+
+      this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
+
+      final JScrollPane scroller = new JScrollPane(list);
+      scroller.setPreferredSize(new Dimension(130, 200));
+      this.add(scroller);
+
+      javax.swing.SwingUtilities.invokeLater(new Runnable()
+      {
+        public void run()
+        {
+          scroller.getHorizontalScrollBar().setValue(
+                  scroller.getHorizontalScrollBar().getMaximum());
+        }
+      });
+
+    }
+
+  }
+}