SwingJS 3.2.4.05 should fix JFileChooser synthetic ref issue
[jalview.git] / src / jalview / io / JalviewFileChooser.java
index 809985d..b7eeaab 100755 (executable)
@@ -25,6 +25,7 @@ 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;
@@ -63,8 +64,8 @@ import javax.swing.plaf.basic.BasicFileChooserUI;
 public class JalviewFileChooser extends JFileChooser
         implements PropertyChangeListener, DialogRunnerI
 {
-  jalview.util.dialogrunner.DialogRunner<JalviewFileChooser> runner = new jalview.util.dialogrunner.DialogRunner<>(
-          this);
+  DialogRunner<JalviewFileChooser> runner = new DialogRunner<>(this);
+  
   /**
    * Factory method to return a file chooser that offers readable alignment file
    * formats
@@ -191,16 +192,23 @@ public class JalviewFileChooser extends JFileChooser
     return f;
   }
 
-  public void openDialog(Component parent)
+  /**
+   * 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 = showOpenDialog(this);
+    int value = super.showOpenDialog(this);
     /**
      * @j2sNative
      */
     {
       runner.firstRun(value);
     }
+    return value;
   }
 
   /**
@@ -309,6 +317,19 @@ public class JalviewFileChooser extends JFileChooser
     }
     return null;
   }
+  
+  File ourselectedFile = null;
+
+  @Override
+  public File getSelectedFile()
+  {
+    File selfile = super.getSelectedFile();
+    if (selfile == null && ourselectedFile != null)
+    {
+      return ourselectedFile;
+    }
+    return selfile;
+  }
 
   Component saveparent;
   RunResponse overwriteCheck = new RunResponse(
@@ -317,6 +338,31 @@ public class JalviewFileChooser extends JFileChooser
     @Override
     public void run()
     {
+    ourselectedFile = 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)
+      {
+        System.err.println(
+                "Unexpected exception when trying to get filename.");
+        x.printStackTrace();
+      }
+    }
+    if (ourselectedFile == null)
+    {
+      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)
@@ -358,6 +404,11 @@ 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 showSaveDialog(Component parent) throws HeadlessException
   {
@@ -378,6 +429,10 @@ public class JalviewFileChooser extends JFileChooser
     }
 
     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"));
@@ -412,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)
       {
@@ -427,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()
@@ -457,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);
@@ -483,9 +539,9 @@ public class JalviewFileChooser extends JFileChooser
   }
 
   @Override
-  public JalviewFileChooser response(RunResponse action)
+  public JalviewFileChooser addResponse(RunResponse action)
   {
-    return runner.response(action);
+    return runner.addResponse(action);
   }
 
 }