Formatted source
[jalview.git] / src / jalview / gui / WebserviceInfo.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import java.awt.*;\r
22 import java.awt.event.*;\r
23 import javax.swing.*;\r
24 \r
25 import jalview.jbgui.*;\r
26 \r
27 public class WebserviceInfo\r
28     extends GWebserviceInfo\r
29 {\r
30   public static final int STATE_QUEUING = 0;\r
31   public static final int STATE_RUNNING = 1;\r
32   public static final int STATE_STOPPED_OK = 2;\r
33   public static final int STATE_CANCELLED_OK = 3;\r
34   public static final int STATE_STOPPED_ERROR = 4;\r
35   public static final int STATE_STOPPED_SERVERERROR = 5;\r
36   int currentStatus = STATE_QUEUING;\r
37   Image image;\r
38   int angle = 0;\r
39   String title = "";\r
40   jalview.ws.WSClientI thisService;\r
41   boolean serviceIsCancellable;\r
42 \r
43   public WebserviceInfo(String title, String info)\r
44   {\r
45     init(title, info, 520, 500);\r
46   }\r
47 \r
48   public WebserviceInfo(String title, String info, int width, int height)\r
49   {\r
50     init(title, info, width, height);\r
51   }\r
52 \r
53   public jalview.ws.WSClientI getthisService()\r
54   {\r
55     return thisService;\r
56   }\r
57 \r
58   public void setthisService(jalview.ws.WSClientI newservice)\r
59   {\r
60     thisService = newservice;\r
61     serviceIsCancellable = newservice.isCancellable();\r
62   }\r
63 \r
64   void init(String title, String info, int width, int height)\r
65   {\r
66     JInternalFrame frame = new JInternalFrame();\r
67     frame.setContentPane(this);\r
68     Desktop.addInternalFrame(frame, title, width, height);\r
69 \r
70     this.title = title;\r
71     setInfoText(info);\r
72 \r
73     java.net.URL url = getClass().getResource("/images/logo.gif");\r
74     image = java.awt.Toolkit.getDefaultToolkit().createImage(url);\r
75 \r
76     MediaTracker mt = new MediaTracker(this);\r
77     mt.addImage(image, 0);\r
78 \r
79     try\r
80     {\r
81       mt.waitForID(0);\r
82     }\r
83     catch (Exception ex)\r
84     {\r
85     }\r
86 \r
87     AnimatedPanel ap = new AnimatedPanel();\r
88     titlePanel.add(ap, BorderLayout.CENTER);\r
89 \r
90     Thread thread = new Thread(ap);\r
91     thread.start();\r
92   }\r
93 \r
94   public void setStatus(int status)\r
95   {\r
96     currentStatus = status;\r
97   }\r
98 \r
99   public String getInfoText()\r
100   {\r
101     return infoText.getText();\r
102   }\r
103 \r
104   public void setInfoText(String text)\r
105   {\r
106     infoText.setText(text);\r
107   }\r
108 \r
109   public void appendInfoText(String text)\r
110   {\r
111     infoText.append(text);\r
112   }\r
113 \r
114   public String getProgressText()\r
115   {\r
116     return progressText.getText();\r
117   }\r
118 \r
119   public void setProgressText(String text)\r
120   {\r
121     progressText.setText(text);\r
122   }\r
123 \r
124   public void appendProgressText(String text)\r
125   {\r
126     progressText.append(text);\r
127   }\r
128 \r
129   protected void cancel_actionPerformed(ActionEvent e)\r
130   {\r
131     if (!serviceIsCancellable)\r
132     {\r
133       JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
134                                             "This job cannot be cancelled.",\r
135                                             "Cancel job",\r
136                                             JOptionPane.WARNING_MESSAGE);\r
137     }\r
138     else\r
139     {\r
140       thisService.cancelJob();\r
141     }\r
142   }\r
143 \r
144   class AnimatedPanel\r
145       extends JPanel implements Runnable\r
146   {\r
147     long startTime = 0;\r
148 \r
149     public void run()\r
150     {\r
151       startTime = System.currentTimeMillis();\r
152 \r
153       while (currentStatus < STATE_STOPPED_OK)\r
154       {\r
155 \r
156         try\r
157         {\r
158           Thread.sleep(50);\r
159 \r
160           int units = (int) ( (System.currentTimeMillis() - startTime) / 10f);\r
161           angle += units;\r
162           angle %= 360;\r
163           startTime = System.currentTimeMillis();\r
164           repaint();\r
165         }\r
166         catch (Exception ex)\r
167         {\r
168         }\r
169       }\r
170 \r
171       angle = 0;\r
172       cancel.setEnabled(false);\r
173     }\r
174 \r
175     synchronized public void paintComponent(Graphics g1)\r
176     {\r
177       Graphics2D g = (Graphics2D) g1;\r
178       g.setColor(Color.white);\r
179       g.fillRect(0, 0, getWidth(), getHeight());\r
180 \r
181       if (image != null)\r
182       {\r
183         g.rotate(Math.toRadians(angle), 28, 28);\r
184         g.drawImage(image, 10, 10, this);\r
185         g.rotate( -Math.toRadians(angle), 28, 28);\r
186       }\r
187 \r
188       g.setFont(new Font("Arial", Font.BOLD, 12));\r
189       g.setColor(Color.black);\r
190 \r
191       switch (currentStatus)\r
192       {\r
193         case STATE_QUEUING:\r
194           g.drawString(title.concat(" - queuing"), 60, 30);\r
195 \r
196           break;\r
197 \r
198         case STATE_RUNNING:\r
199           g.drawString(title.concat(" - running"), 60, 30);\r
200 \r
201           break;\r
202 \r
203         case STATE_STOPPED_OK:\r
204           g.drawString(title.concat(" - complete"), 60, 30);\r
205 \r
206           break;\r
207 \r
208         case STATE_CANCELLED_OK:\r
209           g.drawString(title.concat(" - job cancelled!"), 60, 30);\r
210 \r
211           break;\r
212 \r
213         case STATE_STOPPED_ERROR:\r
214           g.drawString(title.concat(" - job error!"), 60, 30);\r
215 \r
216           break;\r
217 \r
218         case STATE_STOPPED_SERVERERROR:\r
219           g.drawString(title.concat(" - Server Error! (try later)"), 60,\r
220                        30);\r
221 \r
222           break;\r
223       }\r
224     }\r
225   }\r
226 }\r