JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / ws / rest / RestClient.java
index a71b70d..a5b68d7 100644 (file)
@@ -21,6 +21,8 @@
 package jalview.ws.rest;
 
 import jalview.bin.Cache;
+import jalview.bin.ApplicationSingletonProvider;
+import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
 import jalview.datamodel.AlignmentView;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
@@ -49,14 +51,11 @@ import javax.swing.event.MenuListener;
  * 
  */
 public class RestClient extends WSClient
-        implements WSClientI, WSMenuEntryProviderI
+        implements WSClientI, WSMenuEntryProviderI, ApplicationSingletonI
 {
-  RestServiceDescription service;
+  public static final String RSBS_SERVICES = "RSBS_SERVICES";
 
-  public RestClient(RestServiceDescription rsd)
-  {
-    service = rsd;
-  }
+  RestServiceDescription service;
 
   /**
    * parent alignframe for this job
@@ -68,6 +67,118 @@ public class RestClient extends WSClient
    */
   AlignViewport av;
 
+  boolean headless = false;
+
+  protected Vector<String> services = null;
+
+  private static RestClient getInstance()
+  {
+    return (RestClient) ApplicationSingletonProvider.getInstance(RestClient.class);
+  }
+
+  public static RestClient makeShmmrRestClient()
+  {
+    String action = "Analysis",
+            description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)",
+            name = MessageManager.getString("label.multiharmony");
+    Hashtable<String, InputType> iparams = new Hashtable<>();
+    // jalview.ws.rest.params.JobConstant toolp;
+    // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
+    // iparams.put(toolp.token, toolp);
+    // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
+    // iparams.put(toolp.token, toolp);
+    // toolp = new
+    // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
+    // iparams.put(toolp.token, toolp);
+    // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
+    // iparams.put(toolp.token, toolp);
+    // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
+    // iparams.put(toolp.token, toolp);
+
+    jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
+    // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
+    // so we always upload our files.
+    aliinput.token = "ali_file";
+    aliinput.writeAsFile = true;
+    iparams.put(aliinput.token, aliinput);
+    jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
+    sgroups.setMinsize(2);
+    sgroups.min = 2;// need at least two group defined to make a partition
+    iparams.put("groups", sgroups);
+    sgroups.token = "groups";
+    sgroups.sep = " ";
+    RestServiceDescription shmrService = new RestServiceDescription(action,
+            description, name,
+            "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview", // ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
+            "?tool=jalview", iparams, true, false, '-');
+    // a priori knowledge of the data returned from the service
+    shmrService.addResultDatatype(JvDataType.ANNOTATION);
+    return new RestClient(shmrService);
+  }
+
+  public static RestClient[] getRestClients()
+  {
+    RestClient c = getInstance();
+
+    if (c.services == null)
+    {
+      c.services = new Vector<>();
+      try
+      {
+        for (RestServiceDescription descr : RestServiceDescription
+                .parseDescriptions(
+                        jalview.bin.Cache.getDefault(RSBS_SERVICES,
+                                makeShmmrRestClient().service.toString())))
+        {
+          c.services.add(descr.toString());
+        }
+      } catch (Exception ex)
+      {
+        System.err.println(
+                "Serious - RSBS descriptions in user preferences are corrupt!");
+        ex.printStackTrace();
+      }
+
+    }
+    RestClient[] lst = new RestClient[c.services.size()];
+    int i = 0;
+    for (String svc : c.services)
+    {
+      lst[i++] = new RestClient(new RestServiceDescription(svc));
+    }
+    return lst;
+  }
+
+  public static Vector<String> getRsbsDescriptions()
+  {
+    Vector<String> rsbsDescrs = new Vector<>();
+    for (RestClient rsbs : getRestClients())
+    {
+      rsbsDescrs.add(rsbs.getRestDescription().toString());
+    }
+    return rsbsDescrs;
+  }
+
+  public static void setRsbsServices(Vector<String> rsbsUrls)
+  {
+    if (rsbsUrls != null)
+    {
+      // TODO: consider validating services ?
+      RestClient c = getInstance();
+      c.services = new Vector<>(rsbsUrls);
+      StringBuffer sprop = new StringBuffer();
+      for (String s : c.services)
+      {
+        sprop.append(s);
+      }
+      Cache.setProperty(RSBS_SERVICES, sprop.toString());
+    }
+    else
+    {
+      Cache.removeProperty(RSBS_SERVICES);
+    }
+  }
+
   /**
    * get the alignFrame for the associated input data if it exists.
    * 
@@ -78,13 +189,21 @@ public class RestClient extends WSClient
     return jalview.gui.Desktop.getAlignFrameFor(av);
   }
 
+  private RestClient()
+  {
+
+  }
+
+  public RestClient(RestServiceDescription rsd)
+  {
+    service = rsd;
+  }
+
   public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
   {
     this(service2, alignFrame, false);
   }
 
-  boolean headless = false;
-
   public RestClient(RestServiceDescription service2, AlignFrame alignFrame,
           boolean nogui)
   {
@@ -329,7 +448,7 @@ public class RestClient extends WSClient
     else
     {
       // TODO: try to tell the user why the job couldn't be started.
-      JvOptionPane.showMessageDialog(Desktop.desktop,
+      JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
               (jobsthread.hasWarnings() ? jobsthread.getWarnings()
                       : MessageManager.getString(
                               "label.job_couldnt_be_started_check_input")),
@@ -339,46 +458,6 @@ public class RestClient extends WSClient
     }
   }
 
-  public static RestClient makeShmmrRestClient()
-  {
-    String action = "Analysis",
-            description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)",
-            name = MessageManager.getString("label.multiharmony");
-    Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
-    jalview.ws.rest.params.JobConstant toolp;
-    // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
-    // iparams.put(toolp.token, toolp);
-    // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
-    // iparams.put(toolp.token, toolp);
-    // toolp = new
-    // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
-    // iparams.put(toolp.token, toolp);
-    // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
-    // iparams.put(toolp.token, toolp);
-    // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
-    // iparams.put(toolp.token, toolp);
-
-    jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
-    // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
-    // so we always upload our files.
-    aliinput.token = "ali_file";
-    aliinput.writeAsFile = true;
-    iparams.put(aliinput.token, aliinput);
-    jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
-    sgroups.setMinsize(2);
-    sgroups.min = 2;// need at least two group defined to make a partition
-    iparams.put("groups", sgroups);
-    sgroups.token = "groups";
-    sgroups.sep = " ";
-    RestServiceDescription shmrService = new RestServiceDescription(action,
-            description, name,
-            "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview", // ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
-            "?tool=jalview", iparams, true, false, '-');
-    // a priori knowledge of the data returned from the service
-    shmrService.addResultDatatype(JvDataType.ANNOTATION);
-    return new RestClient(shmrService);
-  }
-
   public AlignmentPanel recoverAlignPanelForView()
   {
     AlignmentPanel[] aps = Desktop
@@ -399,41 +478,6 @@ public class RestClient extends WSClient
     return true;
   }
 
-  protected static Vector<String> services = null;
-
-  public static final String RSBS_SERVICES = "RSBS_SERVICES";
-
-  public static RestClient[] getRestClients()
-  {
-    if (services == null)
-    {
-      services = new Vector<String>();
-      try
-      {
-        for (RestServiceDescription descr : RestServiceDescription
-                .parseDescriptions(
-                        jalview.bin.Cache.getDefault(RSBS_SERVICES,
-                                makeShmmrRestClient().service.toString())))
-        {
-          services.add(descr.toString());
-        }
-      } catch (Exception ex)
-      {
-        System.err.println(
-                "Serious - RSBS descriptions in user preferences are corrupt!");
-        ex.printStackTrace();
-      }
-
-    }
-    RestClient[] lst = new RestClient[services.size()];
-    int i = 0;
-    for (String svc : services)
-    {
-      lst[i++] = new RestClient(new RestServiceDescription(svc));
-    }
-    return lst;
-  }
-
   public String getAction()
   {
     return service.details.Action;
@@ -444,33 +488,4 @@ public class RestClient extends WSClient
     return service;
   }
 
-  public static Vector<String> getRsbsDescriptions()
-  {
-    Vector<String> rsbsDescrs = new Vector<String>();
-    for (RestClient rsbs : getRestClients())
-    {
-      rsbsDescrs.add(rsbs.getRestDescription().toString());
-    }
-    return rsbsDescrs;
-  }
-
-  public static void setRsbsServices(Vector<String> rsbsUrls)
-  {
-    if (rsbsUrls != null)
-    {
-      // TODO: consider validating services ?
-      services = new Vector<String>(rsbsUrls);
-      StringBuffer sprop = new StringBuffer();
-      for (String s : services)
-      {
-        sprop.append(s);
-      }
-      Cache.setProperty(RSBS_SERVICES, sprop.toString());
-    }
-    else
-    {
-      Cache.removeProperty(RSBS_SERVICES);
-    }
-  }
-
 }