2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
25 import java.awt.image.*;
28 import jalview.jbgui.*;
32 * Base class for web service client thread and gui
37 public class WebserviceInfo extends GWebserviceInfo
41 public static final int STATE_QUEUING = 0;
44 public static final int STATE_RUNNING = 1;
46 /** Job has finished with no errors */
47 public static final int STATE_STOPPED_OK = 2;
49 /** Job has been cancelled with no errors */
50 public static final int STATE_CANCELLED_OK = 3;
52 /** job has stopped because of some error */
53 public static final int STATE_STOPPED_ERROR = 4;
55 /** job has failed because of some unavoidable service interruption */
56 public static final int STATE_STOPPED_SERVERERROR = 5;
57 int currentStatus = STATE_QUEUING;
61 jalview.ws.WSClientI thisService;
62 boolean serviceIsCancellable;
64 JTabbedPane subjobs=null;
65 java.util.Vector jobPanes = null;
66 private boolean serviceCanMergeResults = false;
67 private boolean viewResultsImmediatly = true;
69 public synchronized int addJobPane() {
70 JScrollPane jobpane = new JScrollPane();
71 JTextArea progressText = new JTextArea();
72 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
73 progressText.setBorder(null);
74 progressText.setEditable(false);
75 progressText.setText("WS Job");
76 progressText.setLineWrap(true);
77 progressText.setWrapStyleWord(true);
78 jobpane.setName("JobPane");
79 jobpane.getViewport().add(progressText, null);
80 jobpane.setBorder(null);
82 jobPanes = new Vector();
84 int newpane = jobPanes.size();
85 jobPanes.add(jobpane);
88 this.add(jobpane, BorderLayout.CENTER);
91 // revert to a tabbed pane.
92 JScrollPane firstpane;
93 this.remove(firstpane=(JScrollPane) jobPanes.get(0));
94 subjobs=new JTabbedPane();
95 this.add(subjobs, BorderLayout.CENTER);
96 subjobs.add(firstpane);
97 subjobs.setTitleAt(0, firstpane.getName());
101 return newpane; // index for accessor methods below
104 * Creates a new WebserviceInfo object.
106 * @param title short name and job type
107 * @param info reference or other human readable description
109 public WebserviceInfo(String title, String info)
111 init(title, info, 520, 500);
115 * Creates a new WebserviceInfo object.
117 * @param title DOCUMENT ME!
118 * @param info DOCUMENT ME!
119 * @param width DOCUMENT ME!
120 * @param height DOCUMENT ME!
122 public WebserviceInfo(String title, String info, int width, int height)
124 init(title, info, width, height);
130 * @return DOCUMENT ME!
132 public jalview.ws.WSClientI getthisService()
140 * @param newservice DOCUMENT ME!
142 public void setthisService(jalview.ws.WSClientI newservice)
144 thisService = newservice;
145 serviceIsCancellable = newservice.isCancellable();
146 frame.setClosable(!serviceIsCancellable);
147 serviceCanMergeResults = newservice.canMergeResults();
153 * @param title DOCUMENT ME!
154 * @param info DOCUMENT ME!
155 * @param width DOCUMENT ME!
156 * @param height DOCUMENT ME!
158 void init(String title, String info, int width, int height)
160 frame = new JInternalFrame();
161 frame.setContentPane(this);
162 Desktop.addInternalFrame(frame, title, width, height);
163 frame.setClosable(false);
168 java.net.URL url = getClass().getResource("/images/logo.gif");
169 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
171 MediaTracker mt = new MediaTracker(this);
172 mt.addImage(image, 0);
182 AnimatedPanel ap = new AnimatedPanel();
183 titlePanel.add(ap, BorderLayout.CENTER);
185 Thread thread = new Thread(ap);
192 * @param status integer status from state constants
194 public void setStatus(int status)
196 currentStatus = status;
199 * subjob status indicator
203 public void setStatus(int jobpane, int status) {
204 if (jobpane<0 || jobpane>=jobPanes.size()) {
205 throw new Error("setStatus called for non-existent job pane."+jobpane);
209 setProgressName(jobpane+" - QUEUED", jobpane);
212 setProgressName(jobpane+" - RUNNING", jobpane);
214 case STATE_STOPPED_OK:
215 setProgressName(jobpane+" - FINISHED", jobpane);
217 case STATE_CANCELLED_OK:
218 setProgressName(jobpane+" - CANCELLED", jobpane);
220 case STATE_STOPPED_ERROR:
221 setProgressName(jobpane+" - BROKEN",jobpane);
223 case STATE_STOPPED_SERVERERROR:
224 setProgressName(jobpane+" - ALERT", jobpane);
227 setProgressName(jobpane+" - UNKNOWN STATE", jobpane);
233 * @return DOCUMENT ME!
235 public String getInfoText()
237 return infoText.getText();
243 * @param text DOCUMENT ME!
245 public void setInfoText(String text)
247 infoText.setText(text);
253 * @param text DOCUMENT ME!
255 public void appendInfoText(String text)
257 infoText.append(text);
262 * @return DOCUMENT ME!
264 public String getProgressText(int which)
266 if (jobPanes == null)
268 return ( (JTextArea) ( (JScrollPane) jobPanes.get(which)).getViewport().
269 getComponent(0)).getText();
274 * @param text DOCUMENT ME!
276 public void setProgressText(int which, String text)
278 if (jobPanes == null)
280 ( (JTextArea) ( (JScrollPane) jobPanes.get(which)).getViewport().
281 getComponent(0)).setText(text);
287 * @param text DOCUMENT ME!
289 public void appendProgressText(int which, String text)
291 if (jobPanes == null)
293 ( (JTextArea) ( (JScrollPane) jobPanes.get(which)).getViewport().
294 getComponent(0)).append(text);
297 * setProgressText(0, text)
299 public void setProgressText(String text)
301 setProgressText(0, text);
304 * appendProgressText(0, text)
306 public void appendProgressText(String text)
308 appendProgressText(0, text);
313 public String getProgressText()
315 return getProgressText(0);
318 * get the tab title for a subjob
322 public String getProgressName(int which) {
326 return subjobs.getTitleAt(which);
328 return ((JScrollPane) jobPanes.get(which)).getViewport().getComponent(0).getName();
331 * set the tab title for a subjob
335 public void setProgressName(String name, int which) {
337 subjobs.setTitleAt(which, name);
338 subjobs.revalidate();
341 JScrollPane c=(JScrollPane) jobPanes.get(which);
342 c.getViewport().getComponent(0).setName(name);
347 * Gui action for cancelling the current job, if possible.
349 * @param e DOCUMENT ME!
351 protected void cancel_actionPerformed(ActionEvent e)
353 if (!serviceIsCancellable)
355 JOptionPane.showInternalMessageDialog(Desktop.desktop,
356 "This job cannot be cancelled.\nJust close the window.", "Cancel job",
357 JOptionPane.WARNING_MESSAGE);
361 thisService.cancelJob();
363 frame.setClosable(true);
366 * Set up GUI for user to get at results - and possibly automatically display
367 * them if viewResultsImmediatly is set.
369 public void setResultsReady()
371 frame.setClosable(true);
372 buttonPanel.remove(cancel);
373 buttonPanel.add(showResultsNewFrame);
374 if (serviceCanMergeResults)
376 buttonPanel.add(mergeResults);
377 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
379 buttonPanel.validate();
381 if (viewResultsImmediatly)
382 showResultsNewFrame.doClick();
386 * called when job has finished but no result objects can be passed back to user
388 public void setFinishedNoResults()
390 frame.setClosable(true);
391 buttonPanel.remove(cancel);
392 buttonPanel.validate();
396 class AnimatedPanel extends JPanel implements Runnable
399 BufferedImage offscreen;
403 startTime = System.currentTimeMillis();
406 while (currentStatus < STATE_STOPPED_OK)
412 int units = (int) ( (System.currentTimeMillis() - startTime) /
416 startTime = System.currentTimeMillis();
418 if (offscreen == null || offscreen.getWidth(this) != getWidth()
419 || offscreen.getHeight(this) != getHeight())
421 offscreen = new BufferedImage(getWidth(), getHeight(),
422 BufferedImage.TYPE_INT_ARGB);
423 g = (Graphics2D) offscreen.getGraphics();
426 g.setColor(Color.white);
427 g.fillRect(0, 0, getWidth(), getHeight());
429 g.setFont(new Font("Arial", Font.BOLD, 12));
430 g.setColor(Color.black);
432 switch (currentStatus)
435 g.drawString(title.concat(" - queuing"), 60, 30);
440 g.drawString(title.concat(" - running"), 60, 30);
444 case STATE_STOPPED_OK:
445 g.drawString(title.concat(" - complete"), 60, 30);
449 case STATE_CANCELLED_OK:
450 g.drawString(title.concat(" - job cancelled!"), 60, 30);
454 case STATE_STOPPED_ERROR:
455 g.drawString(title.concat(" - job error!"), 60, 30);
459 case STATE_STOPPED_SERVERERROR:
460 g.drawString(title.concat(" - Server Error! (try later)"),
468 if (currentStatus >= STATE_STOPPED_OK)
473 g.rotate(Math.toRadians(angle), 28, 28);
474 g.drawImage(image, 10, 10, this);
475 g.rotate( -Math.toRadians(angle), 28, 28);
486 cancel.setEnabled(false);
489 public void paintComponent(Graphics g1)
491 g1.drawImage(offscreen, 0,0,this);