JAL-3878 Add javadocs to created classes and reformat code.
[jalview.git] / src / jalview / ws2 / WebServiceInfoUpdater.java
index 8152abf..a4d0f70 100644 (file)
@@ -6,14 +6,27 @@ import java.util.Objects;
 
 import jalview.gui.WebserviceInfo;
 
+/**
+ * A helper class that can be attached as a listener to the {@link WSJob}
+ * object. It updates the job status in the {@link jalview.gui.WebServiceInfo}
+ * window according to the state changes of the job object.
+ * 
+ * The {@link WebServiceInfoUpdater} object allows to decouple GUI updates
+ * from the web service worker logic.
+ * 
+ * @author mmwarowny
+ *
+ */
 public class WebServiceInfoUpdater implements PropertyChangeListener
 {
+  private final WebServiceWorkerI worker;
   private final WebserviceInfo wsInfo;
 
   private String outputHeader = "";
 
-  public WebServiceInfoUpdater(WebserviceInfo wsInfo)
+  public WebServiceInfoUpdater(WebServiceWorkerI worker, WebserviceInfo wsInfo)
   {
+    this.worker = worker;
     this.wsInfo = wsInfo;
   }
 
@@ -76,6 +89,7 @@ public class WebServiceInfoUpdater implements PropertyChangeListener
       break;
     }
     wsInfo.setStatus(job.getJobNum(), wsInfoStatus);
+    updateWSInfoGlobalStatus();
   }
 
   private void logChanged(PropertyChangeEvent evt)
@@ -96,4 +110,33 @@ public class WebServiceInfoUpdater implements PropertyChangeListener
             newLog.substring(oldLog.length()));
   }
 
+
+  private void updateWSInfoGlobalStatus()
+  {
+    var jobs = worker.getJobs();
+    if (jobs.countRunning() > 0)
+    {
+      wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
+    }
+    else if (jobs.countQueuing() > 0
+            || jobs.countSubmitted() < jobs.size())
+    {
+      wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
+    }
+    else
+    {
+      if (jobs.countSuccessful() > 0)
+      {
+        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
+      }
+      else if (jobs.countCancelled() > 0)
+      {
+        wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
+      }
+      else if (jobs.countFailed() > 0)
+      {
+        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+      }
+    }
+  }
 }