JAL-3878 Add javadocs to created classes and reformat code.
[jalview.git] / src / jalview / ws2 / WebServiceInfoUpdater.java
1 package jalview.ws2;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.util.Objects;
6
7 import jalview.gui.WebserviceInfo;
8
9 /**
10  * A helper class that can be attached as a listener to the {@link WSJob}
11  * object. It updates the job status in the {@link jalview.gui.WebServiceInfo}
12  * window according to the state changes of the job object.
13  * 
14  * The {@link WebServiceInfoUpdater} object allows to decouple GUI updates
15  * from the web service worker logic.
16  * 
17  * @author mmwarowny
18  *
19  */
20 public class WebServiceInfoUpdater implements PropertyChangeListener
21 {
22   private final WebServiceWorkerI worker;
23   private final WebserviceInfo wsInfo;
24
25   private String outputHeader = "";
26
27   public WebServiceInfoUpdater(WebServiceWorkerI worker, WebserviceInfo wsInfo)
28   {
29     this.worker = worker;
30     this.wsInfo = wsInfo;
31   }
32
33   public String getOutputHeader()
34   {
35     return outputHeader;
36   }
37
38   public void setOutputHeader(String header)
39   {
40     this.outputHeader = header;
41   }
42
43   @Override
44   public void propertyChange(PropertyChangeEvent evt)
45   {
46     switch (evt.getPropertyName())
47     {
48     case "status":
49       statusChanged(evt);
50       break;
51     case "log":
52       logChanged(evt);
53       break;
54     case "errorLog":
55       errorLogChanged(evt);
56       break;
57     }
58   }
59
60   private void statusChanged(PropertyChangeEvent evt)
61   {
62     WSJob job = (WSJob) evt.getSource();
63     WSJobStatus status = (WSJobStatus) evt.getNewValue();
64     int wsInfoStatus = 0;
65     switch (status)
66     {
67     case READY:
68     case SUBMITTED:
69     case QUEUED:
70       wsInfoStatus = WebserviceInfo.STATE_QUEUING;
71       break;
72     case RUNNING:
73       wsInfoStatus = WebserviceInfo.STATE_RUNNING;
74       break;
75     case FINISHED:
76       wsInfoStatus = WebserviceInfo.STATE_STOPPED_OK;
77       break;
78     case CANCELLED:
79       wsInfoStatus = WebserviceInfo.STATE_CANCELLED_OK;
80       break;
81     case INVALID:
82     case BROKEN:
83     case FAILED:
84     case UNKNOWN:
85       wsInfoStatus = WebserviceInfo.STATE_STOPPED_ERROR;
86       break;
87     case SERVER_ERROR:
88       wsInfoStatus = WebserviceInfo.STATE_STOPPED_SERVERERROR;
89       break;
90     }
91     wsInfo.setStatus(job.getJobNum(), wsInfoStatus);
92     updateWSInfoGlobalStatus();
93   }
94
95   private void logChanged(PropertyChangeEvent evt)
96   {
97     WSJob job = (WSJob) evt.getSource();
98     String oldLog = (String) evt.getOldValue();
99     String newLog = (String) evt.getNewValue();
100     wsInfo.appendProgressText(job.getJobNum(),
101             newLog.substring(oldLog.length()));
102   }
103
104   private void errorLogChanged(PropertyChangeEvent evt)
105   {
106     WSJob job = (WSJob) evt.getSource();
107     String oldLog = (String) evt.getOldValue();
108     String newLog = (String) evt.getNewValue();
109     wsInfo.appendProgressText(job.getJobNum(),
110             newLog.substring(oldLog.length()));
111   }
112
113
114   private void updateWSInfoGlobalStatus()
115   {
116     var jobs = worker.getJobs();
117     if (jobs.countRunning() > 0)
118     {
119       wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
120     }
121     else if (jobs.countQueuing() > 0
122             || jobs.countSubmitted() < jobs.size())
123     {
124       wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
125     }
126     else
127     {
128       if (jobs.countSuccessful() > 0)
129       {
130         wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
131       }
132       else if (jobs.countCancelled() > 0)
133       {
134         wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
135       }
136       else if (jobs.countFailed() > 0)
137       {
138         wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
139       }
140     }
141   }
142 }