JAL-1551 JAL-3048 formatting
[jalview.git] / src / jalview / gui / Desktop.java
index 3ebbb2d..774dd4c 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;
@@ -193,6 +194,13 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   public static MyDesktopPane desktop;
 
+  public static MyDesktopPane getDesktop()
+  {
+    // BH 2018 could use currentThread() here as a reference to a
+    // Hashtable<Thread, MyDesktopPane> in JavaScript
+    return desktop;
+  }
+
   static int openFrameCount = 0;
 
   static final int xOffset = 30;
@@ -336,9 +344,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   public Desktop()
   {
     /**
-     * A note to implementors. It is ESSENTIAL that any activities that might
-     * block are spawned off as threads rather than waited for during this
-     * constructor.
+     * A note to implementors. It is ESSENTIAL that any activities that might block
+     * are spawned off as threads rather than waited for during this constructor.
      */
     instance = this;
     doVamsasClientCheck();
@@ -363,13 +370,12 @@ public class Desktop extends jalview.jbgui.GDesktop
 
     // This line prevents Windows Look&Feel resizing all new windows to maximum
     // if previous window was maximised
-    desktop.setDesktopManager(
-            new MyDesktopManager(
-                    (Platform.isWindows() ? new DefaultDesktopManager()
-                            : Platform.isAMac()
-                                    ? new AquaInternalFrameManager(
-                                            desktop.getDesktopManager())
-                                    : desktop.getDesktopManager())));
+    desktop.setDesktopManager(new MyDesktopManager(
+            (Platform.isWindows() ? new DefaultDesktopManager()
+                    : Platform.isAMac()
+                            ? new AquaInternalFrameManager(
+                                    desktop.getDesktopManager())
+                            : desktop.getDesktopManager())));
 
     Rectangle dims = getLastKnownDimensions("");
     if (dims != null)
@@ -379,8 +385,9 @@ public class Desktop extends jalview.jbgui.GDesktop
     else
     {
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-      setBounds((screenSize.width - 900) / 2, (screenSize.height - 650) / 2,
-              900, 650);
+      int xPos = Math.max(5, (screenSize.width - 900) / 2);
+      int yPos = Math.max(5, (screenSize.height - 650) / 2);
+      setBounds(xPos, yPos, 900, 650);
     }
     /**
      * BH 2018
@@ -412,49 +419,50 @@ public class Desktop extends jalview.jbgui.GDesktop
 
       checkURLLinks();
 
-    this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
-    // Spawn a thread that shows the splashscreen
+      // Spawn a thread that shows the splashscreen
 
-    SwingUtilities.invokeLater(new Runnable()
-    {
-      @Override
-      public void run()
+      SwingUtilities.invokeLater(new Runnable()
       {
-        new SplashScreen();
-      }
-    });
+        @Override
+        public void run()
+        {
+          new SplashScreen();
+        }
+      });
 
-    // Thread off a new instance of the file chooser - this reduces the time it
-    // takes to open it later on.
-    new Thread(new Runnable()
-    {
-      @Override
-      public void run()
+      // Thread off a new instance of the file chooser - this reduces the time it
+      // takes to open it later on.
+      new Thread(new Runnable()
       {
-        Cache.log.debug("Filechooser init thread started.");
-        String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
-        JalviewFileChooser.forRead(Cache.getProperty("LAST_DIRECTORY"),
-                fileFormat);
-        Cache.log.debug("Filechooser init thread finished.");
-      }
-    }).start();
-    // Add the service change listener
-    changeSupport.addJalviewPropertyChangeListener("services",
-            new PropertyChangeListener()
-            {
-
-              @Override
-              public void propertyChange(PropertyChangeEvent evt)
+        @Override
+        public void run()
+        {
+          Cache.log.debug("Filechooser init thread started.");
+          String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
+          JalviewFileChooser.forRead(Cache.getProperty("LAST_DIRECTORY"),
+                  fileFormat);
+          Cache.log.debug("Filechooser init thread finished.");
+        }
+      }).start();
+      // Add the service change listener
+      changeSupport.addJalviewPropertyChangeListener("services",
+              new PropertyChangeListener()
               {
-                Cache.log.debug("Firing service changed event for "
-                        + evt.getNewValue());
-                JalviewServicesChanged(evt);
-              }
 
-            });
+                @Override
+                public void propertyChange(PropertyChangeEvent evt)
+                {
+                  Cache.log.debug("Firing service changed event for "
+                          + evt.getNewValue());
+                  JalviewServicesChanged(evt);
+                }
+
+              });
 
     } // end BH 2018 ignore
 
+    this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
+
     this.addWindowListener(new WindowAdapter()
     {
       @Override
@@ -533,10 +541,10 @@ public class Desktop extends jalview.jbgui.GDesktop
      * @j2sNative
      */
     {
-    final Desktop me = this;
-    // Thread off the news reader, in case there are connection problems.
-    addDialogThread(new Runnable()
-    {
+      final Desktop me = this;
+      // Thread off the news reader, in case there are connection problems.
+      addDialogThread(new Runnable()
+      {
         @Override
         public void run()
         {
@@ -546,7 +554,7 @@ public class Desktop extends jalview.jbgui.GDesktop
           showNews.setVisible(true);
           Cache.log.debug("Completed news thread.");
         }
-    });
+      });
     }
   }
 
@@ -973,8 +981,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   }
 
   /**
-   * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close
-   * the window
+   * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close the
+   * window
    * 
    * @param frame
    */
@@ -1053,7 +1061,7 @@ public class Desktop extends jalview.jbgui.GDesktop
     // Java's Transferable for native dnd
     evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
     Transferable t = evt.getTransferable();
-    List<String> files = new ArrayList<>();
+    List<Object> files = new ArrayList<>();
     List<DataSourceType> protocols = new ArrayList<>();
 
     try
@@ -1071,13 +1079,15 @@ public class Desktop extends jalview.jbgui.GDesktop
       {
         for (int i = 0; i < files.size(); i++)
         {
-          String file = files.get(i).toString();
+          // BH 2018 File or String
+          Object file = files.get(i);
+          String fileName = file.toString();
           DataSourceType protocol = (protocols == null)
                   ? DataSourceType.FILE
                   : protocols.get(i);
           FileFormatI format = null;
 
-          if (file.endsWith(".jar"))
+          if (fileName.endsWith(".jar"))
           {
             format = FileFormat.Jalview;
 
@@ -1087,7 +1097,7 @@ public class Desktop extends jalview.jbgui.GDesktop
             format = new IdentifyFile().identify(file, protocol);
           }
 
-          new FileLoader().LoadFile(file, protocol, format);
+          new FileLoader().LoadFile(null, file, protocol, format);
 
         }
       } catch (Exception ex)
@@ -1109,7 +1119,7 @@ public class Desktop extends jalview.jbgui.GDesktop
   public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
   {
     String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
-    JalviewFileChooser chooser = JalviewFileChooser
+    final JalviewFileChooser chooser = JalviewFileChooser
             .forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat);
 
     chooser.setFileView(new JalviewFileView());
@@ -1117,42 +1127,38 @@ public class Desktop extends jalview.jbgui.GDesktop
             MessageManager.getString("label.open_local_file"));
     chooser.setToolTipText(MessageManager.getString("action.open"));
 
-    int value = chooser.showOpenDialog(this);
-
-    if (value == JalviewFileChooser.APPROVE_OPTION)
+    chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION)
     {
-      String choice = chooser.getSelectedFile().getPath();
-      Cache.setProperty("LAST_DIRECTORY",
-              chooser.getSelectedFile().getParent());
 
-      FileFormatI format = chooser.getSelectedFormat();
-
-      /*
-       * Call IdentifyFile to verify the file contains what its extension implies.
-       * Skip this step for dynamically added file formats, because
-       * IdentifyFile does not know how to recognise them.
-       */
-      if (FileFormats.getInstance().isIdentifiable(format))
+      @Override
+      public void run()
       {
-        try
-        {
-          format = new IdentifyFile().identify(choice, DataSourceType.FILE);
-        } catch (FileFormatException e)
+        File selectedFile = chooser.getSelectedFile();
+        Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
+
+        FileFormatI format = chooser.getSelectedFormat();
+
+        /*
+         * Call IdentifyFile to verify the file contains what its extension implies.
+         * Skip this step for dynamically added file formats, because
+         * IdentifyFile does not know how to recognise them.
+         */
+        if (FileFormats.getInstance().isIdentifiable(format))
         {
-          // format = null; //??
+          try
+          {
+            format = new IdentifyFile().identify(selectedFile,
+                    DataSourceType.FILE);
+          } catch (FileFormatException e)
+          {
+            // format = null; //??
+          }
         }
-      }
 
-      if (viewport != null)
-      {
-        new FileLoader().LoadFile(viewport, choice, DataSourceType.FILE,
-                format);
+        new FileLoader().LoadFile(viewport, selectedFile,
+                DataSourceType.FILE, format);
       }
-      else
-      {
-        new FileLoader().LoadFile(choice, DataSourceType.FILE, format);
-      }
-    }
+    }).openDialog(this);
   }
 
   /**
@@ -1192,19 +1198,25 @@ public class Desktop extends jalview.jbgui.GDesktop
     }
 
     // BH 2018 -- providing a callback for SwingJS
+    // dialogOption is just a simple way to provide
+    // context for the modal-like response.
+    // The only requirement is that desktop implement
+    // PropertyChangeListener, which is used already in Java
+    // for changes in input value and such within the dialogs.
+
     String dialogOption = "label.input_alignment_from_url";
     desktop.dialogData = new Object[] { dialogOption, viewport, history };
-    desktop.onDialogReturn(
-            JvOptionPane.showInternalConfirmDialog(desktop, panel,
-            MessageManager.getString(dialogOption),
+    desktop.onDialogReturn(JvOptionPane.showInternalConfirmDialog(desktop,
+            panel, MessageManager.getString(dialogOption),
             JvOptionPane.OK_CANCEL_OPTION));
 
     // no code may follow this, as SwingJS will not block
-    // callback in JavaScript comes via a property change event
+    // callback in JavaScript comes via a property change event,
+    // thus going into desktop.onDialogReturn(int) just the same as
+    // in Java.
 
   }
 
-
   /**
    * Opens the CutAndPaste window for the user to paste an alignment in to
    * 
@@ -1588,6 +1600,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   @Override
   public void saveState_actionPerformed(ActionEvent e)
   {
+    // TODO: JAL-3048 not needed for Jalview-JS
+
     JalviewFileChooser chooser = new JalviewFileChooser("jvp",
             "Jalview Project");
 
@@ -1661,6 +1675,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   @Override
   public void loadState_actionPerformed(ActionEvent e)
   {
+    // TODO: JAL-3048 not needed for Jalview-JS
+
     JalviewFileChooser chooser = new JalviewFileChooser(
             Cache.getProperty("LAST_DIRECTORY"), new String[]
             { "jvp", "jar" },
@@ -1720,7 +1736,7 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   ArrayList<JPanel> fileLoadingPanels = new ArrayList<>();
 
-  public void startLoading(final String fileName)
+  public void startLoading(final Object fileName)
   {
     if (fileLoadingCount == 0)
     {
@@ -1922,9 +1938,9 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   /**
    * Gather expanded views (separate AlignFrame's) with the same sequence set
-   * identifier back in to this frame as additional views, and close the
-   * expanded views. Note the expanded frames may themselves have multiple
-   * views. We take the lot.
+   * identifier back in to this frame as additional views, and close the expanded
+   * views. Note the expanded frames may themselves have multiple views. We take
+   * the lot.
    * 
    * @param source
    */
@@ -1968,6 +1984,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   @Override
   public void vamsasImport_actionPerformed(ActionEvent e)
   {
+    // TODO: JAL-3048 not needed for Jalview-JS
+
     if (v_client == null)
     {
       // Load and try to start a session.
@@ -2244,6 +2262,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   @Override
   public void vamsasSave_actionPerformed(ActionEvent e)
   {
+    // TODO: JAL-3048 not needed for Jalview-JS
+
     if (v_client != null)
     {
       // TODO: VAMSAS DOCUMENT EXTENSION is VDJ
@@ -2421,8 +2441,7 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   /**
    * Proxy class for JDesktopPane which optionally displays the current memory
-   * usage and highlights the desktop area with a red bar if free memory runs
-   * low.
+   * usage and highlights the desktop area with a red bar if free memory runs low.
    * 
    * @author AMW
    */
@@ -2471,12 +2490,9 @@ public class Desktop extends jalview.jbgui.GDesktop
         switch (name)
         {
         case "SelectedFile":
-          File file = (File) val;
-          byte[] array = (val == null ? null
-                  : /** @j2sNative file._bytes || */
-                  null);
-          onDialogReturn("fileName is '" + file.getName() + "'\n\n"
-                  + new String(array));
+          // in JavaScript, this File object will have a _bytes property,
+          // because the file data has already been loaded
+          onDialogReturn(new Object[] { (File) val });
           return;
         }
         break;
@@ -2486,9 +2502,19 @@ public class Desktop extends jalview.jbgui.GDesktop
     }
 
     // JSCOmponent.DialogCaller interface
-    private void onDialogReturn(Object value)
+    void onDialogReturn(Object value)
     {
-      System.out.println("not implemented");
+      switch ((String) dialogData[0])
+      {
+      case "SelectedFile":
+      case "runnable":
+        dialogData[0] = value;
+        ((Runnable) dialogData[1]).run();
+        break;
+      case "label.select_feature_colour":
+        ((FeatureRenderer) dialogData[1]).processColorDialog((Color) value);
+        break;
+      }
     }
 
     // JSCOmponent.DialogCaller interface
@@ -2502,6 +2528,10 @@ public class Desktop extends jalview.jbgui.GDesktop
 
       switch ((String) dialogData[0])
       {
+      case "runnable":
+        dialogData[0] = Integer.valueOf(value);
+        ((Runnable) dialogData[1]).run();
+        break;
       case "label.input_alignment_from_url":
         // reconstruct the parameter data
         int reply = value;
@@ -2545,7 +2575,7 @@ public class Desktop extends jalview.jbgui.GDesktop
             JvOptionPane.showInternalMessageDialog(Desktop.desktop,
                     MessageManager.formatMessage("label.couldnt_locate",
                             new Object[]
-                    { url }),
+                            { url }),
                     MessageManager.getString("label.url_not_found"),
                     JvOptionPane.WARNING_MESSAGE);
 
@@ -2656,7 +2686,6 @@ public class Desktop extends jalview.jbgui.GDesktop
     }
   }
 
-
   /**
    * Accessor method to quickly get all the AlignmentFrames loaded.
    * 
@@ -2804,8 +2833,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   }
 
   /**
-   * Bind Ctrl/Cmd-Q to Quit - for reset as Groovy Console takes over this
-   * binding when opened
+   * Bind Ctrl/Cmd-Q to Quit - for reset as Groovy Console takes over this binding
+   * when opened
    */
   protected void addQuitHandler()
   {
@@ -2938,9 +2967,8 @@ public class Desktop extends jalview.jbgui.GDesktop
   }
 
   /**
-   * This will return the first AlignFrame holding the given viewport instance.
-   * It will break if there are more than one AlignFrames viewing a particular
-   * av.
+   * This will return the first AlignFrame holding the given viewport instance. It
+   * will break if there are more than one AlignFrames viewing a particular av.
    * 
    * @param viewport
    * @return alignFrame for viewport
@@ -3306,8 +3334,8 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   /**
    * Explode the views in the given SplitFrame into separate SplitFrame windows.
-   * This respects (remembers) any previous 'exploded geometry' i.e. the size
-   * and location last time the view was expanded (if any). However it does not
+   * This respects (remembers) any previous 'exploded geometry' i.e. the size and
+   * location last time the view was expanded (if any). However it does not
    * remember the split pane divider location - this is set to match the
    * 'exploding' frame.
    * 
@@ -3473,11 +3501,31 @@ public class Desktop extends jalview.jbgui.GDesktop
    *          - the payload from the drop event
    * @throws Exception
    */
-  public static void transferFromDropTarget(List<String> files,
+  public static void transferFromDropTarget(List<Object> files,
           List<DataSourceType> protocols, DropTargetDropEvent evt,
           Transferable t) throws Exception
   {
 
+    // BH 2018 changed List<String> to List<Object> to allow for File from SwingJS
+
+    // DataFlavor[] flavors = t.getTransferDataFlavors();
+    // for (int i = 0; i < flavors.length; i++) {
+    // if (flavors[i].isFlavorJavaFileListType()) {
+    // evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
+    // List<File> list = (List<File>) t.getTransferData(flavors[i]);
+    // for (int j = 0; j < list.size(); j++) {
+    // File file = (File) list.get(j);
+    // byte[] data = getDroppedFileBytes(file);
+    // fileName.setText(file.getName() + " - " + data.length + " " +
+    // evt.getLocation());
+    // JTextArea target = (JTextArea) ((DropTarget) evt.getSource()).getComponent();
+    // target.setText(new String(data));
+    // }
+    // dtde.dropComplete(true);
+    // return;
+    // }
+    //
+
     DataFlavor uriListFlavor = new DataFlavor(
             "text/uri-list;class=java.lang.String"), urlFlavour = null;
     try
@@ -3494,15 +3542,15 @@ public class Desktop extends jalview.jbgui.GDesktop
 
       try
       {
-      java.net.URL url = (URL) t.getTransferData(urlFlavour);
+        java.net.URL url = (URL) t.getTransferData(urlFlavour);
         // nb: java 8 osx bug https://bugs.openjdk.java.net/browse/JDK-8156099
         // means url may be null.
-      if (url != null)
-      {
-        protocols.add(DataSourceType.URL);
-        files.add(url.toString());
-        Cache.log.debug("Drop handled as URL dataflavor "
-                + files.get(files.size() - 1));
+        if (url != null)
+        {
+          protocols.add(DataSourceType.URL);
+          files.add(url.toString());
+          Cache.log.debug("Drop handled as URL dataflavor "
+                  + files.get(files.size() - 1));
           return;
         }
         else
@@ -3513,7 +3561,7 @@ public class Desktop extends jalview.jbgui.GDesktop
                     "Please ignore plist error - occurs due to problem with java 8 on OSX");
           }
           ;
-      }
+        }
       } catch (Throwable ex)
       {
         Cache.log.debug("URL drop handler failed.", ex);
@@ -3526,7 +3574,7 @@ public class Desktop extends jalview.jbgui.GDesktop
       for (Object file : (List) t
               .getTransferData(DataFlavor.javaFileListFlavor))
       {
-        files.add(((File) file).toString());
+        files.add(file);
         protocols.add(DataSourceType.FILE);
       }
     }
@@ -3632,13 +3680,16 @@ public class Desktop extends jalview.jbgui.GDesktop
       // resolve any .lnk files in the file drop
       for (int f = 0; f < files.size(); f++)
       {
-        String source = files.get(f).toLowerCase();
+        String source = files.get(f).toString().toLowerCase();
         if (protocols.get(f).equals(DataSourceType.FILE)
                 && (source.endsWith(".lnk") || source.endsWith(".url")
                         || source.endsWith(".site")))
         {
-          try {
-            File lf = new File(files.get(f));
+          try
+          {
+            Object obj = files.get(f);
+            File lf = (obj instanceof File ? (File) obj
+                    : new File((String) obj));
             // process link file to get a URL
             Cache.log.debug("Found potential link file: " + lf);
             WindowsShortcut wscfile = new WindowsShortcut(lf);
@@ -3647,10 +3698,11 @@ public class Desktop extends jalview.jbgui.GDesktop
             files.set(f, fullname);
             Cache.log.debug("Parsed real filename " + fullname
                     + " to extract protocol: " + protocols.get(f));
-          }
-          catch (Exception ex)
+          } catch (Exception ex)
           {
-            Cache.log.error("Couldn't parse "+files.get(f)+" as a link file.",ex);
+            Cache.log.error(
+                    "Couldn't parse " + files.get(f) + " as a link file.",
+                    ex);
           }
         }
       }
@@ -3668,10 +3720,10 @@ public class Desktop extends jalview.jbgui.GDesktop
   }
 
   /**
-   * Answers a (possibly empty) list of any structure viewer frames (currently
-   * for either Jmol or Chimera) which are currently open. This may optionally
-   * be restricted to viewers of a specified class, or viewers linked to a
-   * specified alignment panel.
+   * Answers a (possibly empty) list of any structure viewer frames (currently for
+   * either Jmol or Chimera) which are currently open. This may optionally be
+   * restricted to viewers of a specified class, or viewers linked to a specified
+   * alignment panel.
    * 
    * @param apanel
    *          if not null, only return viewers linked to this panel