Merge branch 'feature/JAL-3169cancelOverwrite' into develop
[jalview.git] / src / jalview / io / JalviewFileChooser.java
index 8deaa33..7fbe801 100755 (executable)
@@ -48,6 +48,8 @@ import javax.swing.JList;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.SpringLayout;
+import javax.swing.SwingUtilities;
+import javax.swing.border.TitledBorder;
 import javax.swing.filechooser.FileFilter;
 import javax.swing.plaf.basic.BasicFileChooserUI;
 
@@ -389,14 +391,29 @@ public class JalviewFileChooser extends JFileChooser
   public int showSaveDialog(Component parent) throws HeadlessException
   {
     this.setAccessory(null);
+    this.setSelectedFile(null);
+    return super.showSaveDialog(parent);
+  }
 
-    setDialogType(SAVE_DIALOG);
+  /**
+   * If doing a Save, and an existing file is chosen or entered, prompt for
+   * confirmation of overwrite. Proceed if Yes, else leave the file chooser
+   * open.
+   * 
+   * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
+   */
+  @Override
+  public void approveSelection()
+  {
+    if (getDialogType() != SAVE_DIALOG)
+    {
+      super.approveSelection();
+      return;
+    }
 
-    this.setSelectedFile(null);
-    int ret = showDialog(parent, MessageManager.getString("action.save"));
     ourselectedFile = getSelectedFile();
 
-    if (getSelectedFile() == null)
+    if (ourselectedFile == null)
     {
       // Workaround for Java 9,10 on OSX - no selected file, but there is a
       // filename typed in
@@ -413,11 +430,15 @@ public class JalviewFileChooser extends JFileChooser
                 "Unexpected exception when trying to get filename.");
         x.printStackTrace();
       }
+      // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
+      // THE
+      // USER PROMPTED FOR A NEW FILENAME
     }
     if (ourselectedFile == null)
     {
-      return JalviewFileChooser.CANCEL_OPTION;
+      return;
     }
+
     if (getFileFilter() instanceof JalviewFileFilter)
     {
       JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
@@ -431,26 +452,20 @@ public class JalviewFileChooser extends JFileChooser
       }
     }
 
-    // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
-    // USER PROMPTED FOR A NEW FILENAME.
-    // DO NOT need to confirm file overwrite if using backup files (the files
-    // aren't being overwritten!)
-    if ((ret == JalviewFileChooser.APPROVE_OPTION)
-            && ourselectedFile.exists() && (!BackupFiles.getEnabled()))
+    if (ourselectedFile.exists())
     {
-      int confirm = JvOptionPane.showConfirmDialog(parent,
+      int confirm = JvOptionPane.showConfirmDialog(this,
               MessageManager.getString("label.overwrite_existing_file"),
               MessageManager.getString("label.file_already_exists"),
               JvOptionPane.YES_NO_OPTION);
 
       if (confirm != JvOptionPane.YES_OPTION)
       {
-        ret = JalviewFileChooser.CANCEL_OPTION;
+        return;
       }
-
     }
 
-    return ret;
+    super.approveSelection();
   }
 
   void recentListSelectionChanged(Object selection)
@@ -473,28 +488,35 @@ public class JalviewFileChooser extends JFileChooser
     }
   }
 
+  /**
+   * A panel to set as the 'accessory' component to the file chooser dialog,
+   * holding a list of recently opened files (if any). These are held as a
+   * tab-separated list of file paths under key <code>RECENT_FILE</code> in
+   * <code>.jalview_properties</code>. A click in the list calls a method in
+   * JalviewFileChooser to set the chosen file as the selection.
+   */
   class RecentlyOpened extends JPanel
   {
-    JList list;
+    private static final long serialVersionUID = 1L;
 
-    public RecentlyOpened()
-    {
+    private JList<String> list;
 
-      String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
+    RecentlyOpened()
+    {
+      String historyItems = Cache.getProperty("RECENT_FILE");
       StringTokenizer st;
-      Vector recent = new Vector();
+      Vector<String> recent = new Vector<>();
 
       if (historyItems != null)
       {
         st = new StringTokenizer(historyItems, "\t");
-
         while (st.hasMoreTokens())
         {
-          recent.addElement(st.nextElement());
+          recent.addElement(st.nextToken());
         }
       }
 
-      list = new JList(recent);
+      list = new JList<>(recent);
 
       DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
       dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
@@ -509,7 +531,7 @@ public class JalviewFileChooser extends JFileChooser
         }
       });
 
-      this.setBorder(new javax.swing.border.TitledBorder(
+      this.setBorder(new TitledBorder(
               MessageManager.getString("label.recently_opened")));
 
       final JScrollPane scroller = new JScrollPane(list);
@@ -520,7 +542,7 @@ public class JalviewFileChooser extends JFileChooser
       layout.putConstraint(SpringLayout.NORTH, scroller, 5,
               SpringLayout.NORTH, this);
 
-      if (new Platform().isAMac())
+      if (Platform.isAMac())
       {
         scroller.setPreferredSize(new Dimension(500, 100));
       }
@@ -531,7 +553,7 @@ public class JalviewFileChooser extends JFileChooser
 
       this.add(scroller);
 
-      javax.swing.SwingUtilities.invokeLater(new Runnable()
+      SwingUtilities.invokeLater(new Runnable()
       {
         @Override
         public void run()
@@ -540,8 +562,6 @@ public class JalviewFileChooser extends JFileChooser
                   .setValue(scroller.getHorizontalScrollBar().getMaximum());
         }
       });
-
     }
-
   }
 }