2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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.*;
31 * Base class for web service client thread and gui
36 public class WebserviceInfo
37 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()
71 JScrollPane jobpane = new JScrollPane();
72 JTextArea progressText = new JTextArea();
73 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
74 progressText.setBorder(null);
75 progressText.setEditable(false);
76 progressText.setText("WS Job");
77 progressText.setLineWrap(true);
78 progressText.setWrapStyleWord(true);
79 jobpane.setName("JobPane");
80 jobpane.getViewport().add(progressText, null);
81 jobpane.setBorder(null);
84 jobPanes = new Vector();
86 int newpane = jobPanes.size();
87 jobPanes.add(jobpane);
91 this.add(jobpane, BorderLayout.CENTER);
97 // revert to a tabbed pane.
98 JScrollPane firstpane;
99 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
100 subjobs = new JTabbedPane();
101 this.add(subjobs, BorderLayout.CENTER);
102 subjobs.add(firstpane);
103 subjobs.setTitleAt(0, firstpane.getName());
105 subjobs.add(jobpane);
107 return newpane; // index for accessor methods below
111 * Creates a new WebserviceInfo object.
113 * @param title short name and job type
114 * @param info reference or other human readable description
116 public WebserviceInfo(String title, String info)
118 init(title, info, 520, 500);
122 * Creates a new WebserviceInfo object.
124 * @param title DOCUMENT ME!
125 * @param info DOCUMENT ME!
126 * @param width DOCUMENT ME!
127 * @param height DOCUMENT ME!
129 public WebserviceInfo(String title, String info, int width, int height)
131 init(title, info, width, height);
137 * @return DOCUMENT ME!
139 public jalview.ws.WSClientI getthisService()
147 * @param newservice DOCUMENT ME!
149 public void setthisService(jalview.ws.WSClientI newservice)
151 thisService = newservice;
152 serviceIsCancellable = newservice.isCancellable();
153 frame.setClosable(!serviceIsCancellable);
154 serviceCanMergeResults = newservice.canMergeResults();
160 * @param title DOCUMENT ME!
161 * @param info DOCUMENT ME!
162 * @param width DOCUMENT ME!
163 * @param height DOCUMENT ME!
165 void init(String title, String info, int width, int height)
167 frame = new JInternalFrame();
168 frame.setContentPane(this);
169 Desktop.addInternalFrame(frame, title, width, height);
170 frame.setClosable(false);
175 java.net.URL url = getClass().getResource("/images/logo.gif");
176 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
178 MediaTracker mt = new MediaTracker(this);
179 mt.addImage(image, 0);
189 AnimatedPanel ap = new AnimatedPanel();
190 titlePanel.add(ap, BorderLayout.CENTER);
192 Thread thread = new Thread(ap);
199 * @param status integer status from state constants
201 public void setStatus(int status)
203 currentStatus = status;
207 * subjob status indicator
211 public void setStatus(int jobpane, int status)
213 if (jobpane < 0 || jobpane >= jobPanes.size())
215 throw new Error("setStatus called for non-existent job pane." + jobpane);
220 setProgressName(jobpane + " - QUEUED", jobpane);
223 setProgressName(jobpane + " - RUNNING", jobpane);
225 case STATE_STOPPED_OK:
226 setProgressName(jobpane + " - FINISHED", jobpane);
228 case STATE_CANCELLED_OK:
229 setProgressName(jobpane + " - CANCELLED", jobpane);
231 case STATE_STOPPED_ERROR:
232 setProgressName(jobpane + " - BROKEN", jobpane);
234 case STATE_STOPPED_SERVERERROR:
235 setProgressName(jobpane + " - ALERT", jobpane);
238 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
245 * @return DOCUMENT ME!
247 public String getInfoText()
249 return infoText.getText();
255 * @param text DOCUMENT ME!
257 public void setInfoText(String text)
259 infoText.setText(text);
265 * @param text DOCUMENT ME!
267 public void appendInfoText(String text)
269 infoText.append(text);
275 * @return DOCUMENT ME!
277 public String getProgressText(int which)
279 if (jobPanes == null)
283 return ( (JTextArea) ( (JScrollPane) jobPanes.get(which)).getViewport().
284 getComponent(0)).getText();
290 * @param text DOCUMENT ME!
292 public void setProgressText(int which, String text)
294 if (jobPanes == null)
298 ( (JTextArea) ( (JScrollPane) jobPanes.get(which)).getViewport().
299 getComponent(0)).setText(text);
305 * @param text DOCUMENT ME!
307 public void appendProgressText(int which, String text)
309 if (jobPanes == null)
313 ( (JTextArea) ( (JScrollPane) jobPanes.get(which)).getViewport().
314 getComponent(0)).append(text);
318 * setProgressText(0, text)
320 public void setProgressText(String text)
322 setProgressText(0, text);
326 * appendProgressText(0, text)
328 public void appendProgressText(String text)
330 appendProgressText(0, text);
336 public String getProgressText()
338 return getProgressText(0);
342 * get the tab title for a subjob
346 public String getProgressName(int which)
348 if (jobPanes == null)
354 return subjobs.getTitleAt(which);
358 return ( (JScrollPane) jobPanes.get(which)).getViewport().getComponent(0).
364 * set the tab title for a subjob
368 public void setProgressName(String name, int which)
372 subjobs.setTitleAt(which, name);
373 subjobs.revalidate();
376 JScrollPane c = (JScrollPane) jobPanes.get(which);
377 c.getViewport().getComponent(0).setName(name);
382 * Gui action for cancelling the current job, if possible.
384 * @param e DOCUMENT ME!
386 protected void cancel_actionPerformed(ActionEvent e)
388 if (!serviceIsCancellable)
390 JOptionPane.showInternalMessageDialog(Desktop.desktop,
391 "This job cannot be cancelled.\nJust close the window.",
393 JOptionPane.WARNING_MESSAGE);
397 thisService.cancelJob();
399 frame.setClosable(true);
403 * Set up GUI for user to get at results - and possibly automatically display
404 * them if viewResultsImmediatly is set.
406 public void setResultsReady()
408 frame.setClosable(true);
409 buttonPanel.remove(cancel);
410 buttonPanel.add(showResultsNewFrame);
411 if (serviceCanMergeResults)
413 buttonPanel.add(mergeResults);
414 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
416 buttonPanel.validate();
418 if (viewResultsImmediatly)
420 showResultsNewFrame.doClick();
425 * called when job has finished but no result objects can be passed back to user
427 public void setFinishedNoResults()
429 frame.setClosable(true);
430 buttonPanel.remove(cancel);
431 buttonPanel.validate();
436 extends JPanel implements Runnable
439 BufferedImage offscreen;
443 startTime = System.currentTimeMillis();
445 while (currentStatus < STATE_STOPPED_OK)
451 int units = (int) ( (System.currentTimeMillis() - startTime) /
455 startTime = System.currentTimeMillis();
457 if (currentStatus >= STATE_STOPPED_OK)
469 cancel.setEnabled(false);
474 if (offscreen == null || offscreen.getWidth(this) != getWidth()
475 || offscreen.getHeight(this) != getHeight())
477 offscreen = new BufferedImage(getWidth(), getHeight(),
478 BufferedImage.TYPE_INT_ARGB);
481 Graphics2D g = (Graphics2D) offscreen.getGraphics();
483 g.setColor(Color.white);
484 g.fillRect(0, 0, getWidth(), getHeight());
486 g.setFont(new Font("Arial", Font.BOLD, 12));
487 g.setColor(Color.black);
489 switch (currentStatus)
492 g.drawString(title.concat(" - queuing"), 60, 30);
497 g.drawString(title.concat(" - running"), 60, 30);
501 case STATE_STOPPED_OK:
502 g.drawString(title.concat(" - complete"), 60, 30);
506 case STATE_CANCELLED_OK:
507 g.drawString(title.concat(" - job cancelled!"), 60, 30);
511 case STATE_STOPPED_ERROR:
512 g.drawString(title.concat(" - job error!"), 60, 30);
516 case STATE_STOPPED_SERVERERROR:
517 g.drawString(title.concat(" - Server Error! (try later)"),
526 g.rotate(Math.toRadians(angle), 28, 28);
527 g.drawImage(image, 10, 10, this);
528 g.rotate( -Math.toRadians(angle), 28, 28);
532 public void paintComponent(Graphics g1)
536 g1.drawImage(offscreen, 0, 0, this);