GPL license added
[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 \r
20 package jalview.gui;\r
21 \r
22 import java.awt.*;\r
23 import java.awt.event.*;\r
24 import javax.swing.*;\r
25 import jalview.jbgui.GWebserviceInfo;\r
26 \r
27 public class WebserviceInfo extends GWebserviceInfo\r
28 {\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 \r
40   String title="";\r
41   jalview.ws.WSClientI thisService;\r
42   boolean serviceIsCancellable;\r
43   public jalview.ws.WSClientI getthisService() {\r
44     return thisService;\r
45   }\r
46   public void setthisService(jalview.ws.WSClientI newservice) {\r
47     thisService = newservice;\r
48     serviceIsCancellable = newservice.isCancellable();\r
49   }\r
50 \r
51   public WebserviceInfo(String title, String info)\r
52   {\r
53     init(title, info, 520, 500);\r
54   }\r
55 \r
56   public WebserviceInfo(String title, String info, int width, int height)\r
57   {\r
58     init(title, info, width, height);\r
59   }\r
60 \r
61   void init(String title, String info, int width, int height)\r
62   {\r
63     JInternalFrame frame = new JInternalFrame();\r
64     frame.setContentPane(this);\r
65     Desktop.addInternalFrame(frame, title, width, height);\r
66 \r
67     this.title = title;\r
68     setInfoText(info);\r
69     java.net.URL url = getClass().getResource("/images/logo.gif");\r
70     image = java.awt.Toolkit.getDefaultToolkit().createImage(url);\r
71     MediaTracker mt = new MediaTracker(this);\r
72     mt.addImage(image, 0);\r
73     try\r
74     {\r
75       mt.waitForID(0);\r
76     }\r
77     catch (Exception ex)\r
78     {}\r
79 \r
80 \r
81     AnimatedPanel ap = new AnimatedPanel();\r
82     titlePanel.add(ap, BorderLayout.CENTER);\r
83     Thread thread = new Thread(ap);\r
84     thread.start();\r
85   }\r
86 \r
87   public void setStatus(int status)\r
88   {\r
89     currentStatus = status;\r
90   }\r
91 \r
92   public String getInfoText()\r
93   {   return infoText.getText();  }\r
94 \r
95   public void setInfoText(String text)\r
96   {   infoText.setText(text);     }\r
97 \r
98   public void appendInfoText(String text)\r
99   {  infoText.append(text);  }\r
100 \r
101   public String getProgressText()\r
102   {   return progressText.getText();  }\r
103 \r
104   public void setProgressText(String text)\r
105   {   progressText.setText(text);   }\r
106 \r
107   public void appendProgressText(String text)\r
108   {   progressText.append(text);  }\r
109 \r
110   protected void cancel_actionPerformed(ActionEvent e)\r
111   {\r
112     if (!serviceIsCancellable)\r
113       JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
114                                             "This job cannot be cancelled." ,\r
115                                             "Cancel job"\r
116                                             ,JOptionPane.WARNING_MESSAGE);\r
117     else\r
118       thisService.cancelJob();\r
119   }\r
120 \r
121 \r
122   class AnimatedPanel extends JPanel implements Runnable\r
123   {\r
124     long startTime=0;\r
125     public void run()\r
126     {\r
127       startTime = System.currentTimeMillis();\r
128       while(currentStatus<STATE_STOPPED_OK)\r
129       try\r
130       {\r
131         Thread.sleep(50);\r
132 \r
133         int units =  (int)((System.currentTimeMillis()-startTime)/10f);\r
134         angle += units;\r
135         angle %= 360;\r
136         startTime = System.currentTimeMillis();\r
137         repaint();\r
138 \r
139       }\r
140       catch (Exception ex)\r
141       {}\r
142       angle = 0;\r
143       cancel.setEnabled(false);\r
144 \r
145     }\r
146 \r
147    synchronized public void  paintComponent(Graphics g1)\r
148     {\r
149       Graphics2D g = (Graphics2D) g1;\r
150       g.setColor(Color.white);\r
151       g.fillRect(0, 0, getWidth(), getHeight());\r
152       if (image != null)\r
153       {\r
154         g.rotate(Math.toRadians(angle), 28, 28);\r
155         g.drawImage(image, 10, 10, this);\r
156         g.rotate( -Math.toRadians(angle), 28, 28);\r
157       }\r
158       g.setFont(new Font("Arial", Font.BOLD, 12));\r
159       g.setColor(Color.black);\r
160       switch(currentStatus)\r
161       {\r
162         case STATE_QUEUING:\r
163           g.drawString(title.concat(" - queuing"), 60, 30);\r
164           break;\r
165         case STATE_RUNNING:\r
166           g.drawString(title.concat(" - running"), 60, 30);\r
167           break;\r
168         case STATE_STOPPED_OK:\r
169           g.drawString(title.concat(" - complete"), 60, 30);\r
170           break;\r
171 \r
172         case STATE_CANCELLED_OK:\r
173           g.drawString(title.concat(" - job cancelled!"), 60, 30);\r
174           break;\r
175 \r
176         case STATE_STOPPED_ERROR:\r
177           g.drawString(title.concat(" - job error!"), 60, 30);\r
178           break;\r
179         case STATE_STOPPED_SERVERERROR:\r
180           g.drawString(title.concat(" - Server Error! (try later)"), 60, 30);\r
181           break;\r
182       }\r
183 \r
184     }\r
185   }\r
186 }\r