2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
24 import java.awt.image.*;
27 import jalview.jbgui.*;
28 import jalview.ws.WSClientI;
31 * Base class for web service client thread and gui
36 public class WebserviceInfo extends GWebserviceInfo
40 public static final int STATE_QUEUING = 0;
43 public static final int STATE_RUNNING = 1;
45 /** Job has finished with no errors */
46 public static final int STATE_STOPPED_OK = 2;
48 /** Job has been cancelled with no errors */
49 public static final int STATE_CANCELLED_OK = 3;
51 /** job has stopped because of some error */
52 public static final int STATE_STOPPED_ERROR = 4;
54 /** job has failed because of some unavoidable service interruption */
55 public static final int STATE_STOPPED_SERVERERROR = 5;
57 int currentStatus = STATE_QUEUING;
65 jalview.ws.WSClientI thisService;
67 boolean serviceIsCancellable;
71 JTabbedPane subjobs = null;
73 java.util.Vector jobPanes = null;
75 private boolean serviceCanMergeResults = false;
77 private boolean viewResultsImmediatly = true;
80 public synchronized int addJobPane()
82 JScrollPane jobpane = new JScrollPane();
83 JTextArea progressText = new JTextArea();
84 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
85 progressText.setBorder(null);
86 progressText.setEditable(false);
87 progressText.setText("WS Job");
88 progressText.setLineWrap(true);
89 progressText.setWrapStyleWord(true);
90 jobpane.setName("JobPane");
91 jobpane.getViewport().add(progressText, null);
92 jobpane.setBorder(null);
95 jobPanes = new Vector();
97 int newpane = jobPanes.size();
98 jobPanes.add(jobpane);
102 this.add(jobpane, BorderLayout.CENTER);
108 // revert to a tabbed pane.
109 JScrollPane firstpane;
110 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
111 subjobs = new JTabbedPane();
112 this.add(subjobs, BorderLayout.CENTER);
113 subjobs.add(firstpane);
114 subjobs.setTitleAt(0, firstpane.getName());
116 subjobs.add(jobpane);
118 return newpane; // index for accessor methods below
122 * Creates a new WebserviceInfo object.
125 * short name and job type
127 * reference or other human readable description
129 public WebserviceInfo(String title, String info)
131 init(title, info, 520, 500);
135 * Creates a new WebserviceInfo object.
146 public WebserviceInfo(String title, String info, int width, int height)
148 init(title, info, width, height);
154 * @return DOCUMENT ME!
156 public jalview.ws.WSClientI getthisService()
162 * Update state of GUI based on client capabilities (like whether the job is
163 * cancellable, whether the 'merge results' button is shown.
166 * service client to query for capabilities
168 public void setthisService(jalview.ws.WSClientI newservice)
170 thisService = newservice;
171 serviceIsCancellable = newservice.isCancellable();
172 frame.setClosable(!serviceIsCancellable);
173 serviceCanMergeResults = newservice.canMergeResults();
174 rebuildButtonPanel();
177 private void rebuildButtonPanel()
179 if (buttonPanel != null)
181 buttonPanel.removeAll();
182 if (serviceIsCancellable)
184 buttonPanel.add(cancel);
185 frame.setClosable(false);
189 frame.setClosable(true);
206 void init(String title, String info, int width, int height)
208 frame = new JInternalFrame();
209 frame.setContentPane(this);
210 Desktop.addInternalFrame(frame, title, width, height);
211 frame.setClosable(false);
216 java.net.URL url = getClass().getResource("/images/logo.gif");
217 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
219 MediaTracker mt = new MediaTracker(this);
220 mt.addImage(image, 0);
225 } catch (Exception ex)
229 AnimatedPanel ap = new AnimatedPanel();
230 titlePanel.add(ap, BorderLayout.CENTER);
232 Thread thread = new Thread(ap);
234 final WebserviceInfo thisinfo = this;
236 .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
238 public void internalFrameClosed(
239 javax.swing.event.InternalFrameEvent evt)
241 // System.out.println("Shutting down webservice client");
242 WSClientI service = thisinfo.getthisService();
243 if (service != null && service.isCancellable())
256 * integer status from state constants
258 public void setStatus(int status)
260 currentStatus = status;
264 * subjob status indicator
269 public void setStatus(int jobpane, int status)
271 if (jobpane < 0 || jobpane >= jobPanes.size())
273 throw new Error("setStatus called for non-existent job pane."
279 setProgressName(jobpane + " - QUEUED", jobpane);
282 setProgressName(jobpane + " - RUNNING", jobpane);
284 case STATE_STOPPED_OK:
285 setProgressName(jobpane + " - FINISHED", jobpane);
287 case STATE_CANCELLED_OK:
288 setProgressName(jobpane + " - CANCELLED", jobpane);
290 case STATE_STOPPED_ERROR:
291 setProgressName(jobpane + " - BROKEN", jobpane);
293 case STATE_STOPPED_SERVERERROR:
294 setProgressName(jobpane + " - ALERT", jobpane);
297 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
304 * @return DOCUMENT ME!
306 public String getInfoText()
308 return infoText.getText();
317 public void setInfoText(String text)
319 infoText.setText(text);
328 public void appendInfoText(String text)
330 infoText.append(text);
336 * @return DOCUMENT ME!
338 public String getProgressText(int which)
340 if (jobPanes == null)
344 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
345 .getComponent(0)).getText();
354 public void setProgressText(int which, String text)
356 if (jobPanes == null)
360 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
361 .getComponent(0)).setText(text);
370 public void appendProgressText(int which, String text)
372 if (jobPanes == null)
376 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
377 .getComponent(0)).append(text);
381 * setProgressText(0, text)
383 public void setProgressText(String text)
385 setProgressText(0, text);
389 * appendProgressText(0, text)
391 public void appendProgressText(String text)
393 appendProgressText(0, text);
399 public String getProgressText()
401 return getProgressText(0);
405 * get the tab title for a subjob
411 public String getProgressName(int which)
413 if (jobPanes == null)
419 return subjobs.getTitleAt(which);
423 return ((JScrollPane) jobPanes.get(which)).getViewport()
424 .getComponent(0).getName();
429 * set the tab title for a subjob
436 public void setProgressName(String name, int which)
440 subjobs.setTitleAt(which, name);
441 subjobs.revalidate();
444 JScrollPane c = (JScrollPane) jobPanes.get(which);
445 c.getViewport().getComponent(0).setName(name);
450 * Gui action for cancelling the current job, if possible.
455 protected void cancel_actionPerformed(ActionEvent e)
457 if (!serviceIsCancellable)
459 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
460 // anyhow - it has to stop threads and clean up
461 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
463 JOptionPane.showInternalMessageDialog(Desktop.desktop,
464 "This job cannot be cancelled.\nJust close the window.",
465 "Cancel job", JOptionPane.WARNING_MESSAGE);
469 thisService.cancelJob();
471 frame.setClosable(true);
475 * Set up GUI for user to get at results - and possibly automatically display
476 * them if viewResultsImmediatly is set.
478 public void setResultsReady()
480 frame.setClosable(true);
481 buttonPanel.remove(cancel);
482 buttonPanel.add(showResultsNewFrame);
483 if (serviceCanMergeResults)
485 buttonPanel.add(mergeResults);
486 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
488 buttonPanel.validate();
490 if (viewResultsImmediatly)
492 showResultsNewFrame.doClick();
497 * called when job has finished but no result objects can be passed back to
500 public void setFinishedNoResults()
502 frame.setClosable(true);
503 buttonPanel.remove(cancel);
504 buttonPanel.validate();
508 class AnimatedPanel extends JPanel implements Runnable
512 BufferedImage offscreen;
516 startTime = System.currentTimeMillis();
518 while (currentStatus < STATE_STOPPED_OK)
524 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
527 startTime = System.currentTimeMillis();
529 if (currentStatus >= STATE_STOPPED_OK)
535 } catch (Exception ex)
540 cancel.setEnabled(false);
545 if (offscreen == null || offscreen.getWidth(this) != getWidth()
546 || offscreen.getHeight(this) != getHeight())
548 offscreen = new BufferedImage(getWidth(), getHeight(),
549 BufferedImage.TYPE_INT_ARGB);
552 Graphics2D g = (Graphics2D) offscreen.getGraphics();
554 g.setColor(Color.white);
555 g.fillRect(0, 0, getWidth(), getHeight());
557 g.setFont(new Font("Arial", Font.BOLD, 12));
558 g.setColor(Color.black);
560 switch (currentStatus)
563 g.drawString(title.concat(" - queuing"), 60, 30);
568 g.drawString(title.concat(" - running"), 60, 30);
572 case STATE_STOPPED_OK:
573 g.drawString(title.concat(" - complete"), 60, 30);
577 case STATE_CANCELLED_OK:
578 g.drawString(title.concat(" - job cancelled!"), 60, 30);
582 case STATE_STOPPED_ERROR:
583 g.drawString(title.concat(" - job error!"), 60, 30);
587 case STATE_STOPPED_SERVERERROR:
588 g.drawString(title.concat(" - Server Error! (try later)"), 60, 30);
595 g.rotate(Math.toRadians(angle), 28, 28);
596 g.drawImage(image, 10, 10, this);
597 g.rotate(-Math.toRadians(angle), 28, 28);
601 public void paintComponent(Graphics g1)
605 g1.drawImage(offscreen, 0, 0, this);