package jalview.ws2; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; 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(WebServiceWorkerI worker, WebserviceInfo wsInfo) { this.worker = worker; this.wsInfo = wsInfo; } public String getOutputHeader() { return outputHeader; } public void setOutputHeader(String header) { this.outputHeader = header; } @Override public void propertyChange(PropertyChangeEvent evt) { switch (evt.getPropertyName()) { case "status": statusChanged(evt); break; case "log": logChanged(evt); break; case "errorLog": errorLogChanged(evt); break; } } private void statusChanged(PropertyChangeEvent evt) { WSJob job = (WSJob) evt.getSource(); WSJobStatus status = (WSJobStatus) evt.getNewValue(); int wsInfoStatus = 0; switch (status) { case READY: case SUBMITTED: case QUEUED: wsInfoStatus = WebserviceInfo.STATE_QUEUING; break; case RUNNING: wsInfoStatus = WebserviceInfo.STATE_RUNNING; break; case FINISHED: wsInfoStatus = WebserviceInfo.STATE_STOPPED_OK; break; case CANCELLED: wsInfoStatus = WebserviceInfo.STATE_CANCELLED_OK; break; case INVALID: case BROKEN: case FAILED: case UNKNOWN: wsInfoStatus = WebserviceInfo.STATE_STOPPED_ERROR; break; case SERVER_ERROR: wsInfoStatus = WebserviceInfo.STATE_STOPPED_SERVERERROR; break; } wsInfo.setStatus(job.getJobNum(), wsInfoStatus); updateWSInfoGlobalStatus(); } private void logChanged(PropertyChangeEvent evt) { WSJob job = (WSJob) evt.getSource(); String oldLog = (String) evt.getOldValue(); String newLog = (String) evt.getNewValue(); wsInfo.appendProgressText(job.getJobNum(), newLog.substring(oldLog.length())); } private void errorLogChanged(PropertyChangeEvent evt) { WSJob job = (WSJob) evt.getSource(); String oldLog = (String) evt.getOldValue(); String newLog = (String) evt.getNewValue(); wsInfo.appendProgressText(job.getJobNum(), 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); } } } }