JAL-3048 refactored Desktop->File->Open to use jalview.utils.dialogrunner.DialogRunner
authorJim Procter <jprocter@issues.jalview.org>
Fri, 29 Jun 2018 15:21:35 +0000 (16:21 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Fri, 29 Jun 2018 15:21:35 +0000 (16:21 +0100)
src/jalview/gui/Desktop.java
src/jalview/io/JalviewFileChooser.java

index 06c3b36..65e936c 100644 (file)
@@ -44,6 +44,7 @@ import jalview.util.ImageMaker;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.util.UrlConstants;
+import jalview.util.dialogrunner.RunResponse;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.ws.params.ParamManager;
 import jalview.ws.utils.UrlDownloadClient;
@@ -105,7 +106,6 @@ import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JComponent;
 import javax.swing.JDesktopPane;
-import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JInternalFrame;
 import javax.swing.JLabel;
@@ -1129,7 +1129,7 @@ public class Desktop extends jalview.jbgui.GDesktop
             MessageManager.getString("label.open_local_file"));
     chooser.setToolTipText(MessageManager.getString("action.open"));
 
-    chooser.setCallback(new Runnable()
+    chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION)
     {
 
       @Override
@@ -1160,14 +1160,7 @@ public class Desktop extends jalview.jbgui.GDesktop
         new FileLoader().LoadFile(viewport, selectedFile,
                 DataSourceType.FILE, format);
       }
-    });
-
-    int value = chooser.showOpenDialog(this);
-    if (value == JFileChooser.APPROVE_OPTION)
-    {
-      chooser.getCallback().run();
-    }
-
+    }).openDialog(this);
   }
 
   /**
index ae6c9eb..65cb603 100755 (executable)
@@ -25,6 +25,8 @@ import jalview.bin.Cache;
 import jalview.gui.JvOptionPane;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
+import jalview.util.dialogrunner.DialogRunnerI;
+import jalview.util.dialogrunner.RunResponse;
 
 import java.awt.Component;
 import java.awt.Dimension;
@@ -58,8 +60,10 @@ import javax.swing.plaf.basic.BasicFileChooserUI;
  *
  */
 public class JalviewFileChooser extends JFileChooser
-        implements PropertyChangeListener
+        implements PropertyChangeListener, DialogRunnerI
 {
+  jalview.util.dialogrunner.DialogRunner<JalviewFileChooser> runner = new jalview.util.dialogrunner.DialogRunner<>(
+          this);
   /**
    * Factory method to return a file chooser that offers readable alignment file
    * formats
@@ -159,25 +163,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;
     }
   }
@@ -197,6 +190,12 @@ public class JalviewFileChooser extends JFileChooser
     return f;
   }
 
+  public void openDialog(Component parent)
+  {
+    int value = showOpenDialog(this);
+    runner.run(value);
+  }
+
   /**
    * 
    * @param formats
@@ -436,4 +435,10 @@ public class JalviewFileChooser extends JFileChooser
 
   }
 
+  @Override
+  public JalviewFileChooser response(RunResponse action)
+  {
+    return runner.response(action);
+  }
+
 }