SwingJS 3.2.4.05 should fix JFileChooser synthetic ref issue
[jalview.git] / src / jalview / io / JalviewFileChooser.java
index ae6c9eb..b7eeaab 100755 (executable)
@@ -25,6 +25,10 @@ import jalview.bin.Cache;
 import jalview.gui.JvOptionPane;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
+import jalview.util.dialogrunner.DialogRunner;
+import jalview.util.dialogrunner.DialogRunnerI;
+import jalview.util.dialogrunner.Response;
+import jalview.util.dialogrunner.RunResponse;
 
 import java.awt.Component;
 import java.awt.Dimension;
@@ -58,8 +62,10 @@ import javax.swing.plaf.basic.BasicFileChooserUI;
  *
  */
 public class JalviewFileChooser extends JFileChooser
-        implements PropertyChangeListener
+        implements PropertyChangeListener, DialogRunnerI
 {
+  DialogRunner<JalviewFileChooser> runner = new DialogRunner<>(this);
+  
   /**
    * Factory method to return a file chooser that offers readable alignment file
    * formats
@@ -159,25 +165,14 @@ public class JalviewFileChooser extends JFileChooser
     }
   }
 
-  private Runnable theCallback;
-
-  public void setCallback(Runnable callback)
-  {
-    this.theCallback = callback;
-  }
-
-  public Runnable getCallback()
-  {
-    return theCallback;
-  }
-
   @Override
   public void propertyChange(PropertyChangeEvent evt)
   {
+    // TODO other properties need runners...
     switch (evt.getPropertyName())
     {
     case "SelectedFile": 
-      theCallback.run();
+      runner.run(APPROVE_OPTION);
       break;
     }
   }
@@ -198,6 +193,25 @@ public class JalviewFileChooser extends JFileChooser
   }
 
   /**
+   * Overridden for JalviewJS compatibility: only one thread in Javascript, 
+   * so we can't wait for user choice in another thread and then perform the 
+   * desired action
+   */
+  @Override
+  public int showOpenDialog(Component parent)
+  {
+    runner.resetResponses();
+    int value = super.showOpenDialog(this);
+    /**
+     * @j2sNative
+     */
+    {
+      runner.firstRun(value);
+    }
+    return value;
+  }
+
+  /**
    * 
    * @param formats
    *          a list of {extensions, description} for each file format
@@ -303,45 +317,132 @@ public class JalviewFileChooser extends JFileChooser
     }
     return null;
   }
+  
+  File ourselectedFile = null;
 
   @Override
-  public int showSaveDialog(Component parent) throws HeadlessException
+  public File getSelectedFile()
   {
-    this.setAccessory(null);
-
-    setDialogType(SAVE_DIALOG);
-
-    int ret = showDialog(parent, MessageManager.getString("action.save"));
-
+    File selfile = super.getSelectedFile();
+    if (selfile == null && ourselectedFile != null)
+    {
+      return ourselectedFile;
+    }
+    return selfile;
+  }
 
-    if (getFileFilter() instanceof JalviewFileFilter)
+  Component saveparent;
+  RunResponse overwriteCheck = new RunResponse(
+          JalviewFileChooser.APPROVE_OPTION)
+  {
+    @Override
+    public void run()
     {
-      JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
+    ourselectedFile = getSelectedFile();
 
-      if (!jvf.accept(getSelectedFile()))
+    if (getSelectedFile() == null)
+    {
+      // Workaround for Java 9,10 on OSX - no selected file, but there is a
+      // filename typed in
+      try
+      {
+        String filename = ((BasicFileChooserUI) getUI()).getFileName();
+        if (filename != null && filename.length() > 0)
+        {
+          ourselectedFile = new File(getCurrentDirectory(), filename);
+        }
+      } catch (Throwable x)
       {
-        String withExtension = getSelectedFile() + "."
-                + jvf.getAcceptableExtension();
-        setSelectedFile(new File(withExtension));
+        System.err.println(
+                "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 ((ret == JalviewFileChooser.APPROVE_OPTION)
-            && getSelectedFile().exists())
+    if (ourselectedFile == null)
     {
-      int confirm = JvOptionPane.showConfirmDialog(parent,
-              MessageManager.getString("label.overwrite_existing_file"),
-              MessageManager.getString("label.file_already_exists"),
-              JvOptionPane.YES_NO_OPTION);
+      returned = new Response(JalviewFileChooser.CANCEL_OPTION);
+      return;
+    }
+      // JBP Note - this code was executed regardless of 'SAVE' being pressed
+      // need to see if there were side effects
+      if (getFileFilter() instanceof JalviewFileFilter)
+      {
+        JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
 
-      if (confirm != JvOptionPane.YES_OPTION)
+        if (!jvf.accept(getSelectedFile()))
+        {
+          String withExtension = getSelectedFile() + "."
+                  + jvf.getAcceptableExtension();
+          setSelectedFile(new File(withExtension));
+        }
+      }
+      // All good, so we continue to save
+      returned = new Response(JalviewFileChooser.APPROVE_OPTION);
+
+      // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
+      // USER PROMPTED FOR A NEW FILENAME
+      /**
+       * @j2sNative
+       */
       {
-        ret = JalviewFileChooser.CANCEL_OPTION;
+        if (getSelectedFile().exists())
+        {
+          // JAL-3048 - may not need to raise this for browser saves
+
+          // yes/no cancel
+          int confirm = JvOptionPane.showConfirmDialog(saveparent,
+                  MessageManager.getString("label.overwrite_existing_file"),
+                  MessageManager.getString("label.file_already_exists"),
+                  JvOptionPane.YES_NO_OPTION);
+
+          if (confirm != JvOptionPane.YES_OPTION)
+          {
+            returned = new Response(JalviewFileChooser.CANCEL_OPTION);
+          }
+        }
       }
+    };
+  };
+
+  /**
+   * Overridden for JalviewJS compatibility: only one thread in Javascript, 
+   * so we can't wait for user choice in another thread and then perform the 
+   * desired action
+   */
+  @Override
+  public int showSaveDialog(Component parent) throws HeadlessException
+  {
+    this.setAccessory(null);
+
+    /*
+     * Save dialog is opened until user picks a file format 
+     */
+    if (!runner.isRegistered(overwriteCheck))
+    {
+      // first call for this instance
+      runner.firstResponse(overwriteCheck);
+    }
+    else
+    {
+      // reset response flags
+      runner.resetResponses();
     }
 
-    return ret;
+    setDialogType(SAVE_DIALOG);
+    
+    // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
+    this.setSelectedFile(null);
+    saveparent = parent;
+
+    int value = showDialog(parent, MessageManager.getString("action.save"));
+    /**
+     * @j2sNative
+     */
+    {
+      runner.firstRun(value);
+    }
+    return value;
   }
 
   void recentListSelectionChanged(Object selection)
@@ -366,14 +467,15 @@ public class JalviewFileChooser extends JFileChooser
 
   class RecentlyOpened extends JPanel
   {
-    JList list;
+    JList<String> list;
 
     public RecentlyOpened()
     {
 
+      setPreferredSize(new Dimension(300,100));
       String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
       StringTokenizer st;
-      Vector recent = new Vector();
+      Vector<String> recent = new Vector<>();
 
       if (historyItems != null)
       {
@@ -381,14 +483,14 @@ public class JalviewFileChooser extends JFileChooser
 
         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);
+//      dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
       list.setCellRenderer(dlcr);
 
       list.addMouseListener(new MouseAdapter()
@@ -411,13 +513,13 @@ 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));
       }
       else
       {
-        scroller.setPreferredSize(new Dimension(130, 200));
+        scroller.setPreferredSize(new Dimension(530, 200));
       }
 
       this.add(scroller);
@@ -436,4 +538,10 @@ public class JalviewFileChooser extends JFileChooser
 
   }
 
+  @Override
+  public JalviewFileChooser addResponse(RunResponse action)
+  {
+    return runner.addResponse(action);
+  }
+
 }