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.*;
26 import javax.swing.event.HyperlinkEvent;
27 import javax.swing.event.HyperlinkListener;
28 import javax.swing.event.HyperlinkEvent.EventType;
29 import javax.swing.text.html.HTMLEditorKit;
30 import javax.swing.text.html.StyleSheet;
32 import jalview.jbgui.*;
33 import jalview.ws.WSClientI;
36 * Base class for web service client thread and gui TODO: create StAX parser to
37 * extract html body content reliably when preparing html formatted job statuses
42 public class WebserviceInfo extends GWebserviceInfo implements
47 public static final int STATE_QUEUING = 0;
50 public static final int STATE_RUNNING = 1;
52 /** Job has finished with no errors */
53 public static final int STATE_STOPPED_OK = 2;
55 /** Job has been cancelled with no errors */
56 public static final int STATE_CANCELLED_OK = 3;
58 /** job has stopped because of some error */
59 public static final int STATE_STOPPED_ERROR = 4;
61 /** job has failed because of some unavoidable service interruption */
62 public static final int STATE_STOPPED_SERVERERROR = 5;
64 int currentStatus = STATE_QUEUING;
72 jalview.ws.WSClientI thisService;
74 boolean serviceIsCancellable;
78 JTabbedPane subjobs = null;
80 java.util.Vector jobPanes = null;
82 private boolean serviceCanMergeResults = false;
84 private boolean viewResultsImmediatly = true;
90 * to indicate if results will be shown in a new window as soon as
93 public boolean isViewResultsImmediatly()
95 return viewResultsImmediatly;
102 * to indicate if results will be shown in a new window as soon as
103 * they are available.
105 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
107 this.viewResultsImmediatly = viewResultsImmediatly;
110 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
113 // Copied blatantly from
114 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
115 StyleSheet myStyleSheet = new StyleSheet();
117 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
119 editorKit.setStyleSheet(myStyleSheet);
122 * Set the style sheet rules here by reading them from the constants
126 * for (int ix=0; ix<CSS_RULES.length; ix++) {
128 * myStyleSheet.addRule(CSS_RULES[ix]);
137 public synchronized int addJobPane()
139 JScrollPane jobpane = new JScrollPane();
140 JComponent _progressText;
143 JEditorPane progressText = new JEditorPane("text/html", "");
144 progressText.addHyperlinkListener(this);
145 _progressText = progressText;
146 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
147 // progressText.setBorder(null);
148 progressText.setEditable(false);
150 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
152 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
154 * HTMLDocument tipDocument = (HTMLDocument)
155 * (myEditorKit.createDefaultDocument());
157 * progressText.setDocument(tipDocument);
158 */progressText.setText("<html><h1>WS Job</h1></html>");
162 JTextArea progressText = new JTextArea();
163 _progressText = progressText;
165 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
166 progressText.setBorder(null);
167 progressText.setEditable(false);
168 progressText.setText("WS Job");
169 progressText.setLineWrap(true);
170 progressText.setWrapStyleWord(true);
172 jobpane.setName("JobPane");
173 jobpane.getViewport().add(_progressText, null);
174 jobpane.setBorder(null);
175 if (jobPanes == null)
177 jobPanes = new Vector();
179 int newpane = jobPanes.size();
180 jobPanes.add(jobpane);
184 this.add(jobpane, BorderLayout.CENTER);
190 // revert to a tabbed pane.
191 JScrollPane firstpane;
192 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
193 subjobs = new JTabbedPane();
194 this.add(subjobs, BorderLayout.CENTER);
195 subjobs.add(firstpane);
196 subjobs.setTitleAt(0, firstpane.getName());
198 subjobs.add(jobpane);
200 return newpane; // index for accessor methods below
204 * Creates a new WebserviceInfo object.
207 * short name and job type
209 * reference or other human readable description
211 public WebserviceInfo(String title, String info)
213 init(title, info, 520, 500);
217 * Creates a new WebserviceInfo object.
228 public WebserviceInfo(String title, String info, int width, int height)
230 init(title, info, width, height);
236 * @return DOCUMENT ME!
238 public jalview.ws.WSClientI getthisService()
244 * Update state of GUI based on client capabilities (like whether the job is
245 * cancellable, whether the 'merge results' button is shown.
248 * service client to query for capabilities
250 public void setthisService(jalview.ws.WSClientI newservice)
252 thisService = newservice;
253 serviceIsCancellable = newservice.isCancellable();
254 frame.setClosable(!serviceIsCancellable);
255 serviceCanMergeResults = newservice.canMergeResults();
256 rebuildButtonPanel();
259 private void rebuildButtonPanel()
261 if (buttonPanel != null)
263 buttonPanel.removeAll();
264 if (serviceIsCancellable)
266 buttonPanel.add(cancel);
267 frame.setClosable(false);
271 frame.setClosable(true);
288 void init(String title, String info, int width, int height)
290 frame = new JInternalFrame();
291 frame.setContentPane(this);
292 Desktop.addInternalFrame(frame, title, width, height);
293 frame.setClosable(false);
298 java.net.URL url = getClass().getResource("/images/logo.gif");
299 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
301 MediaTracker mt = new MediaTracker(this);
302 mt.addImage(image, 0);
307 } catch (Exception ex)
311 AnimatedPanel ap = new AnimatedPanel();
312 titlePanel.add(ap, BorderLayout.CENTER);
314 Thread thread = new Thread(ap);
316 final WebserviceInfo thisinfo = this;
317 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
319 public void internalFrameClosed(
320 javax.swing.event.InternalFrameEvent evt)
322 // System.out.println("Shutting down webservice client");
323 WSClientI service = thisinfo.getthisService();
324 if (service != null && service.isCancellable())
338 * integer status from state constants
340 public void setStatus(int status)
342 currentStatus = status;
346 * subjob status indicator
351 public void setStatus(int jobpane, int status)
353 if (jobpane < 0 || jobpane >= jobPanes.size())
355 throw new Error("setStatus called for non-existent job pane."
361 setProgressName(jobpane + " - QUEUED", jobpane);
364 setProgressName(jobpane + " - RUNNING", jobpane);
366 case STATE_STOPPED_OK:
367 setProgressName(jobpane + " - FINISHED", jobpane);
369 case STATE_CANCELLED_OK:
370 setProgressName(jobpane + " - CANCELLED", jobpane);
372 case STATE_STOPPED_ERROR:
373 setProgressName(jobpane + " - BROKEN", jobpane);
375 case STATE_STOPPED_SERVERERROR:
376 setProgressName(jobpane + " - ALERT", jobpane);
379 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
386 * @return DOCUMENT ME!
388 public String getInfoText()
390 return infoText.getText();
399 public void setInfoText(String text)
401 infoText.setText(text);
410 public void appendInfoText(String text)
412 infoText.append(text);
418 * @return DOCUMENT ME!
420 public String getProgressText(int which)
422 if (jobPanes == null)
428 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
429 .getViewport().getComponent(0)).getText();
433 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
434 .getComponent(0)).getText();
444 public void setProgressText(int which, String text)
446 if (jobPanes == null)
452 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
453 .getComponent(0)).setText(ensureHtmlTagged(text));
457 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
458 .getComponent(0)).setText(text);
463 * extract content from <body> content </body>
467 * - set to leave the initial html tag intact
469 * - set to leave the final html tag intact
472 private String getHtmlFragment(String text, boolean leaveFirst,
479 String lowertxt = text.toLowerCase();
480 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
482 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
483 int htmlpose = lowertxt.indexOf(">", htmlpos), htmlende = lowertxt
484 .indexOf(">", htmlend);
485 if (htmlend == -1 && htmlpos == -1)
491 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
493 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
497 * very simple routine for adding/ensuring html tags are present in text.
500 * @return properly html tag enclosed text
502 private String ensureHtmlTagged(String text)
508 String lowertxt = text.toLowerCase();
509 int htmlpos = lowertxt.indexOf("<body");
510 int htmlend = lowertxt.indexOf("</body");
511 int doctype = lowertxt.indexOf("<!doctype");
512 int xmltype = lowertxt.indexOf("<?xml");
515 text = text + "</body></html>";
519 if ((doctype > -1 && htmlpos > doctype)
520 || (xmltype > -1 && htmlpos > xmltype))
522 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
527 text = "<html><head></head><body>\n" + text;
529 if (text.indexOf("<meta") > -1)
531 System.err.println("HTML COntent: \n" + text
532 + "<< END HTML CONTENT\n");
544 public void appendProgressText(int which, String text)
546 if (jobPanes == null)
552 String txt = getHtmlFragment(
553 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
554 .getViewport().getComponent(0)).getText(), true,
556 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
557 .getComponent(0)).setText(ensureHtmlTagged(txt
558 + getHtmlFragment(text, false, true)));
562 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
563 .getComponent(0)).append(text);
568 * setProgressText(0, text)
570 public void setProgressText(String text)
572 setProgressText(0, text);
576 * appendProgressText(0, text)
578 public void appendProgressText(String text)
580 appendProgressText(0, text);
586 public String getProgressText()
588 return getProgressText(0);
592 * get the tab title for a subjob
598 public String getProgressName(int which)
600 if (jobPanes == null)
606 return subjobs.getTitleAt(which);
610 return ((JScrollPane) jobPanes.get(which)).getViewport()
611 .getComponent(0).getName();
616 * set the tab title for a subjob
623 public void setProgressName(String name, int which)
627 subjobs.setTitleAt(which, name);
628 subjobs.revalidate();
631 JScrollPane c = (JScrollPane) jobPanes.get(which);
632 c.getViewport().getComponent(0).setName(name);
637 * Gui action for cancelling the current job, if possible.
642 protected void cancel_actionPerformed(ActionEvent e)
644 if (!serviceIsCancellable)
646 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
647 // anyhow - it has to stop threads and clean up
648 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
650 warnUser("This job cannot be cancelled.\nJust close the window.",
655 thisService.cancelJob();
657 frame.setClosable(true);
661 * Spawns a thread that pops up a warning dialog box with the given message
667 public void warnUser(final String message, final String title)
669 javax.swing.SwingUtilities.invokeLater(new Runnable()
673 JOptionPane.showInternalMessageDialog(Desktop.desktop, message,
674 title, JOptionPane.WARNING_MESSAGE);
681 * Set up GUI for user to get at results - and possibly automatically display
682 * them if viewResultsImmediatly is set.
684 public void setResultsReady()
686 frame.setClosable(true);
687 buttonPanel.remove(cancel);
688 buttonPanel.add(showResultsNewFrame);
689 if (serviceCanMergeResults)
691 buttonPanel.add(mergeResults);
692 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
694 buttonPanel.validate();
696 if (viewResultsImmediatly)
698 showResultsNewFrame.doClick();
703 * called when job has finished but no result objects can be passed back to
706 public void setFinishedNoResults()
708 frame.setClosable(true);
709 buttonPanel.remove(cancel);
710 buttonPanel.validate();
714 class AnimatedPanel extends JPanel implements Runnable
718 BufferedImage offscreen;
722 startTime = System.currentTimeMillis();
724 while (currentStatus < STATE_STOPPED_OK)
730 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
733 startTime = System.currentTimeMillis();
735 if (currentStatus >= STATE_STOPPED_OK)
741 } catch (Exception ex)
746 cancel.setEnabled(false);
751 if (offscreen == null || offscreen.getWidth(this) != getWidth()
752 || offscreen.getHeight(this) != getHeight())
754 offscreen = new BufferedImage(getWidth(), getHeight(),
755 BufferedImage.TYPE_INT_ARGB);
758 Graphics2D g = (Graphics2D) offscreen.getGraphics();
760 g.setColor(Color.white);
761 g.fillRect(0, 0, getWidth(), getHeight());
763 g.setFont(new Font("Arial", Font.BOLD, 12));
764 g.setColor(Color.black);
766 switch (currentStatus)
769 g.drawString(title.concat(" - queuing"), 60, 30);
774 g.drawString(title.concat(" - running"), 60, 30);
778 case STATE_STOPPED_OK:
779 g.drawString(title.concat(" - complete"), 60, 30);
783 case STATE_CANCELLED_OK:
784 g.drawString(title.concat(" - job cancelled!"), 60, 30);
788 case STATE_STOPPED_ERROR:
789 g.drawString(title.concat(" - job error!"), 60, 30);
793 case STATE_STOPPED_SERVERERROR:
794 g.drawString(title.concat(" - Server Error! (try later)"), 60, 30);
801 g.rotate(Math.toRadians(angle), 28, 28);
802 g.drawImage(image, 10, 10, this);
803 g.rotate(-Math.toRadians(angle), 28, 28);
807 public void paintComponent(Graphics g1)
811 g1.drawImage(offscreen, 0, 0, this);
815 boolean renderAsHtml = false;
817 public void setRenderAsHtml(boolean b)
822 public void hyperlinkUpdate(HyperlinkEvent e)
824 if (e.getEventType() == EventType.ACTIVATED)
828 final String url = e.getURL().toString();
829 Desktop.showUrl(url);
830 } catch (Exception x)
832 // ignore any exceptions due to dud links.