2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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.*;
29 import jalview.ws.WSClientI;
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;
58 int currentStatus = STATE_QUEUING;
66 jalview.ws.WSClientI thisService;
68 boolean serviceIsCancellable;
72 JTabbedPane subjobs = null;
74 java.util.Vector jobPanes = null;
76 private boolean serviceCanMergeResults = false;
78 private boolean viewResultsImmediatly = true;
81 public synchronized int addJobPane()
83 JScrollPane jobpane = new JScrollPane();
84 JTextArea progressText = new JTextArea();
85 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
86 progressText.setBorder(null);
87 progressText.setEditable(false);
88 progressText.setText("WS Job");
89 progressText.setLineWrap(true);
90 progressText.setWrapStyleWord(true);
91 jobpane.setName("JobPane");
92 jobpane.getViewport().add(progressText, null);
93 jobpane.setBorder(null);
96 jobPanes = new Vector();
98 int newpane = jobPanes.size();
99 jobPanes.add(jobpane);
103 this.add(jobpane, BorderLayout.CENTER);
109 // revert to a tabbed pane.
110 JScrollPane firstpane;
111 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
112 subjobs = new JTabbedPane();
113 this.add(subjobs, BorderLayout.CENTER);
114 subjobs.add(firstpane);
115 subjobs.setTitleAt(0, firstpane.getName());
117 subjobs.add(jobpane);
119 return newpane; // index for accessor methods below
123 * Creates a new WebserviceInfo object.
126 * short name and job type
128 * reference or other human readable description
130 public WebserviceInfo(String title, String info)
132 init(title, info, 520, 500);
136 * Creates a new WebserviceInfo object.
147 public WebserviceInfo(String title, String info, int width, int height)
149 init(title, info, width, height);
155 * @return DOCUMENT ME!
157 public jalview.ws.WSClientI getthisService()
163 * Update state of GUI based on client capabilities (like whether the job is
164 * cancellable, whether the 'merge results' button is shown.
167 * service client to query for capabilities
169 public void setthisService(jalview.ws.WSClientI newservice)
171 thisService = newservice;
172 serviceIsCancellable = newservice.isCancellable();
173 frame.setClosable(!serviceIsCancellable);
174 serviceCanMergeResults = newservice.canMergeResults();
175 rebuildButtonPanel();
178 private void rebuildButtonPanel()
180 if (buttonPanel != null)
182 buttonPanel.removeAll();
183 if (serviceIsCancellable)
185 buttonPanel.add(cancel);
186 frame.setClosable(false);
190 frame.setClosable(true);
207 void init(String title, String info, int width, int height)
209 frame = new JInternalFrame();
210 frame.setContentPane(this);
211 Desktop.addInternalFrame(frame, title, width, height);
212 frame.setClosable(false);
217 java.net.URL url = getClass().getResource("/images/logo.gif");
218 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
220 MediaTracker mt = new MediaTracker(this);
221 mt.addImage(image, 0);
226 } catch (Exception ex)
230 AnimatedPanel ap = new AnimatedPanel();
231 titlePanel.add(ap, BorderLayout.CENTER);
233 Thread thread = new Thread(ap);
235 final WebserviceInfo thisinfo = this;
237 .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
239 public void internalFrameClosed(
240 javax.swing.event.InternalFrameEvent evt)
242 // System.out.println("Shutting down webservice client");
243 WSClientI service = thisinfo.getthisService();
244 if (service != null && service.isCancellable())
257 * integer status from state constants
259 public void setStatus(int status)
261 currentStatus = status;
265 * subjob status indicator
270 public void setStatus(int jobpane, int status)
272 if (jobpane < 0 || jobpane >= jobPanes.size())
274 throw new Error("setStatus called for non-existent job pane."
280 setProgressName(jobpane + " - QUEUED", jobpane);
283 setProgressName(jobpane + " - RUNNING", jobpane);
285 case STATE_STOPPED_OK:
286 setProgressName(jobpane + " - FINISHED", jobpane);
288 case STATE_CANCELLED_OK:
289 setProgressName(jobpane + " - CANCELLED", jobpane);
291 case STATE_STOPPED_ERROR:
292 setProgressName(jobpane + " - BROKEN", jobpane);
294 case STATE_STOPPED_SERVERERROR:
295 setProgressName(jobpane + " - ALERT", jobpane);
298 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
305 * @return DOCUMENT ME!
307 public String getInfoText()
309 return infoText.getText();
318 public void setInfoText(String text)
320 infoText.setText(text);
329 public void appendInfoText(String text)
331 infoText.append(text);
337 * @return DOCUMENT ME!
339 public String getProgressText(int which)
341 if (jobPanes == null)
345 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
346 .getComponent(0)).getText();
355 public void setProgressText(int which, String text)
357 if (jobPanes == null)
361 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
362 .getComponent(0)).setText(text);
371 public void appendProgressText(int which, String text)
373 if (jobPanes == null)
377 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
378 .getComponent(0)).append(text);
382 * setProgressText(0, text)
384 public void setProgressText(String text)
386 setProgressText(0, text);
390 * appendProgressText(0, text)
392 public void appendProgressText(String text)
394 appendProgressText(0, text);
400 public String getProgressText()
402 return getProgressText(0);
406 * get the tab title for a subjob
412 public String getProgressName(int which)
414 if (jobPanes == null)
420 return subjobs.getTitleAt(which);
424 return ((JScrollPane) jobPanes.get(which)).getViewport()
425 .getComponent(0).getName();
430 * set the tab title for a subjob
437 public void setProgressName(String name, int which)
441 subjobs.setTitleAt(which, name);
442 subjobs.revalidate();
445 JScrollPane c = (JScrollPane) jobPanes.get(which);
446 c.getViewport().getComponent(0).setName(name);
451 * Gui action for cancelling the current job, if possible.
456 protected void cancel_actionPerformed(ActionEvent e)
458 if (!serviceIsCancellable)
460 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
461 // anyhow - it has to stop threads and clean up
462 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
464 JOptionPane.showInternalMessageDialog(Desktop.desktop,
465 "This job cannot be cancelled.\nJust close the window.",
466 "Cancel job", JOptionPane.WARNING_MESSAGE);
470 thisService.cancelJob();
472 frame.setClosable(true);
476 * Set up GUI for user to get at results - and possibly automatically display
477 * them if viewResultsImmediatly is set.
479 public void setResultsReady()
481 frame.setClosable(true);
482 buttonPanel.remove(cancel);
483 buttonPanel.add(showResultsNewFrame);
484 if (serviceCanMergeResults)
486 buttonPanel.add(mergeResults);
487 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
489 buttonPanel.validate();
491 if (viewResultsImmediatly)
493 showResultsNewFrame.doClick();
498 * called when job has finished but no result objects can be passed back to
501 public void setFinishedNoResults()
503 frame.setClosable(true);
504 buttonPanel.remove(cancel);
505 buttonPanel.validate();
509 class AnimatedPanel extends JPanel implements Runnable
513 BufferedImage offscreen;
517 startTime = System.currentTimeMillis();
519 while (currentStatus < STATE_STOPPED_OK)
525 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
528 startTime = System.currentTimeMillis();
530 if (currentStatus >= STATE_STOPPED_OK)
536 } catch (Exception ex)
541 cancel.setEnabled(false);
546 if (offscreen == null || offscreen.getWidth(this) != getWidth()
547 || offscreen.getHeight(this) != getHeight())
549 offscreen = new BufferedImage(getWidth(), getHeight(),
550 BufferedImage.TYPE_INT_ARGB);
553 Graphics2D g = (Graphics2D) offscreen.getGraphics();
555 g.setColor(Color.white);
556 g.fillRect(0, 0, getWidth(), getHeight());
558 g.setFont(new Font("Arial", Font.BOLD, 12));
559 g.setColor(Color.black);
561 switch (currentStatus)
564 g.drawString(title.concat(" - queuing"), 60, 30);
569 g.drawString(title.concat(" - running"), 60, 30);
573 case STATE_STOPPED_OK:
574 g.drawString(title.concat(" - complete"), 60, 30);
578 case STATE_CANCELLED_OK:
579 g.drawString(title.concat(" - job cancelled!"), 60, 30);
583 case STATE_STOPPED_ERROR:
584 g.drawString(title.concat(" - job error!"), 60, 30);
588 case STATE_STOPPED_SERVERERROR:
589 g.drawString(title.concat(" - Server Error! (try later)"), 60, 30);
596 g.rotate(Math.toRadians(angle), 28, 28);
597 g.drawImage(image, 10, 10, this);
598 g.rotate(-Math.toRadians(angle), 28, 28);
602 public void paintComponent(Graphics g1)
606 g1.drawImage(offscreen, 0, 0, this);