2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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;
81 * @param flag to indicate if results will be shown in a new window as soon as they are available.
83 public boolean isViewResultsImmediatly()
85 return viewResultsImmediatly;
90 * @param flag to indicate if results will be shown in a new window as soon as they are available.
92 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
94 this.viewResultsImmediatly = viewResultsImmediatly;
98 public synchronized int addJobPane()
100 JScrollPane jobpane = new JScrollPane();
101 JTextArea progressText = new JTextArea();
102 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
103 progressText.setBorder(null);
104 progressText.setEditable(false);
105 progressText.setText("WS Job");
106 progressText.setLineWrap(true);
107 progressText.setWrapStyleWord(true);
108 jobpane.setName("JobPane");
109 jobpane.getViewport().add(progressText, null);
110 jobpane.setBorder(null);
111 if (jobPanes == null)
113 jobPanes = new Vector();
115 int newpane = jobPanes.size();
116 jobPanes.add(jobpane);
120 this.add(jobpane, BorderLayout.CENTER);
126 // revert to a tabbed pane.
127 JScrollPane firstpane;
128 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
129 subjobs = new JTabbedPane();
130 this.add(subjobs, BorderLayout.CENTER);
131 subjobs.add(firstpane);
132 subjobs.setTitleAt(0, firstpane.getName());
134 subjobs.add(jobpane);
136 return newpane; // index for accessor methods below
140 * Creates a new WebserviceInfo object.
143 * short name and job type
145 * reference or other human readable description
147 public WebserviceInfo(String title, String info)
149 init(title, info, 520, 500);
153 * Creates a new WebserviceInfo object.
164 public WebserviceInfo(String title, String info, int width, int height)
166 init(title, info, width, height);
172 * @return DOCUMENT ME!
174 public jalview.ws.WSClientI getthisService()
180 * Update state of GUI based on client capabilities (like whether the job is
181 * cancellable, whether the 'merge results' button is shown.
184 * service client to query for capabilities
186 public void setthisService(jalview.ws.WSClientI newservice)
188 thisService = newservice;
189 serviceIsCancellable = newservice.isCancellable();
190 frame.setClosable(!serviceIsCancellable);
191 serviceCanMergeResults = newservice.canMergeResults();
192 rebuildButtonPanel();
195 private void rebuildButtonPanel()
197 if (buttonPanel != null)
199 buttonPanel.removeAll();
200 if (serviceIsCancellable)
202 buttonPanel.add(cancel);
203 frame.setClosable(false);
207 frame.setClosable(true);
224 void init(String title, String info, int width, int height)
226 frame = new JInternalFrame();
227 frame.setContentPane(this);
228 Desktop.addInternalFrame(frame, title, width, height);
229 frame.setClosable(false);
234 java.net.URL url = getClass().getResource("/images/logo.gif");
235 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
237 MediaTracker mt = new MediaTracker(this);
238 mt.addImage(image, 0);
243 } catch (Exception ex)
247 AnimatedPanel ap = new AnimatedPanel();
248 titlePanel.add(ap, BorderLayout.CENTER);
250 Thread thread = new Thread(ap);
252 final WebserviceInfo thisinfo = this;
253 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
255 public void internalFrameClosed(
256 javax.swing.event.InternalFrameEvent evt)
258 // System.out.println("Shutting down webservice client");
259 WSClientI service = thisinfo.getthisService();
260 if (service != null && service.isCancellable())
274 * integer status from state constants
276 public void setStatus(int status)
278 currentStatus = status;
282 * subjob status indicator
287 public void setStatus(int jobpane, int status)
289 if (jobpane < 0 || jobpane >= jobPanes.size())
291 throw new Error("setStatus called for non-existent job pane."
297 setProgressName(jobpane + " - QUEUED", jobpane);
300 setProgressName(jobpane + " - RUNNING", jobpane);
302 case STATE_STOPPED_OK:
303 setProgressName(jobpane + " - FINISHED", jobpane);
305 case STATE_CANCELLED_OK:
306 setProgressName(jobpane + " - CANCELLED", jobpane);
308 case STATE_STOPPED_ERROR:
309 setProgressName(jobpane + " - BROKEN", jobpane);
311 case STATE_STOPPED_SERVERERROR:
312 setProgressName(jobpane + " - ALERT", jobpane);
315 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
322 * @return DOCUMENT ME!
324 public String getInfoText()
326 return infoText.getText();
335 public void setInfoText(String text)
337 infoText.setText(text);
346 public void appendInfoText(String text)
348 infoText.append(text);
354 * @return DOCUMENT ME!
356 public String getProgressText(int which)
358 if (jobPanes == null)
362 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
363 .getComponent(0)).getText();
372 public void setProgressText(int which, String text)
374 if (jobPanes == null)
378 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
379 .getComponent(0)).setText(text);
388 public void appendProgressText(int which, String text)
390 if (jobPanes == null)
394 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
395 .getComponent(0)).append(text);
399 * setProgressText(0, text)
401 public void setProgressText(String text)
403 setProgressText(0, text);
407 * appendProgressText(0, text)
409 public void appendProgressText(String text)
411 appendProgressText(0, text);
417 public String getProgressText()
419 return getProgressText(0);
423 * get the tab title for a subjob
429 public String getProgressName(int which)
431 if (jobPanes == null)
437 return subjobs.getTitleAt(which);
441 return ((JScrollPane) jobPanes.get(which)).getViewport()
442 .getComponent(0).getName();
447 * set the tab title for a subjob
454 public void setProgressName(String name, int which)
458 subjobs.setTitleAt(which, name);
459 subjobs.revalidate();
462 JScrollPane c = (JScrollPane) jobPanes.get(which);
463 c.getViewport().getComponent(0).setName(name);
468 * Gui action for cancelling the current job, if possible.
473 protected void cancel_actionPerformed(ActionEvent e)
475 if (!serviceIsCancellable)
477 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
478 // anyhow - it has to stop threads and clean up
479 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
481 warnUser("This job cannot be cancelled.\nJust close the window.",
486 thisService.cancelJob();
488 frame.setClosable(true);
492 * Spawns a thread that pops up a warning dialog box with the given message
498 public void warnUser(final String message, final String title)
500 javax.swing.SwingUtilities.invokeLater(new Runnable()
504 JOptionPane.showInternalMessageDialog(Desktop.desktop, message,
505 title, JOptionPane.WARNING_MESSAGE);
512 * Set up GUI for user to get at results - and possibly automatically display
513 * them if viewResultsImmediatly is set.
515 public void setResultsReady()
517 frame.setClosable(true);
518 buttonPanel.remove(cancel);
519 buttonPanel.add(showResultsNewFrame);
520 if (serviceCanMergeResults)
522 buttonPanel.add(mergeResults);
523 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
525 buttonPanel.validate();
527 if (viewResultsImmediatly)
529 showResultsNewFrame.doClick();
534 * called when job has finished but no result objects can be passed back to
537 public void setFinishedNoResults()
539 frame.setClosable(true);
540 buttonPanel.remove(cancel);
541 buttonPanel.validate();
545 class AnimatedPanel extends JPanel implements Runnable
549 BufferedImage offscreen;
553 startTime = System.currentTimeMillis();
555 while (currentStatus < STATE_STOPPED_OK)
561 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
564 startTime = System.currentTimeMillis();
566 if (currentStatus >= STATE_STOPPED_OK)
572 } catch (Exception ex)
577 cancel.setEnabled(false);
582 if (offscreen == null || offscreen.getWidth(this) != getWidth()
583 || offscreen.getHeight(this) != getHeight())
585 offscreen = new BufferedImage(getWidth(), getHeight(),
586 BufferedImage.TYPE_INT_ARGB);
589 Graphics2D g = (Graphics2D) offscreen.getGraphics();
591 g.setColor(Color.white);
592 g.fillRect(0, 0, getWidth(), getHeight());
594 g.setFont(new Font("Arial", Font.BOLD, 12));
595 g.setColor(Color.black);
597 switch (currentStatus)
600 g.drawString(title.concat(" - queuing"), 60, 30);
605 g.drawString(title.concat(" - running"), 60, 30);
609 case STATE_STOPPED_OK:
610 g.drawString(title.concat(" - complete"), 60, 30);
614 case STATE_CANCELLED_OK:
615 g.drawString(title.concat(" - job cancelled!"), 60, 30);
619 case STATE_STOPPED_ERROR:
620 g.drawString(title.concat(" - job error!"), 60, 30);
624 case STATE_STOPPED_SERVERERROR:
625 g.drawString(title.concat(" - Server Error! (try later)"), 60, 30);
632 g.rotate(Math.toRadians(angle), 28, 28);
633 g.drawImage(image, 10, 10, this);
634 g.rotate(-Math.toRadians(angle), 28, 28);
638 public void paintComponent(Graphics g1)
642 g1.drawImage(offscreen, 0, 0, this);