2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 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.bin.Cache;
33 import jalview.jbgui.*;
34 import jalview.ws.WSClientI;
37 * Base class for web service client thread and gui TODO: create StAX parser to
38 * extract html body content reliably when preparing html formatted job statuses
43 public class WebserviceInfo extends GWebserviceInfo implements
48 public static final int STATE_QUEUING = 0;
51 public static final int STATE_RUNNING = 1;
53 /** Job has finished with no errors */
54 public static final int STATE_STOPPED_OK = 2;
56 /** Job has been cancelled with no errors */
57 public static final int STATE_CANCELLED_OK = 3;
59 /** job has stopped because of some error */
60 public static final int STATE_STOPPED_ERROR = 4;
62 /** job has failed because of some unavoidable service interruption */
63 public static final int STATE_STOPPED_SERVERERROR = 5;
65 int currentStatus = STATE_QUEUING;
73 jalview.ws.WSClientI thisService;
75 boolean serviceIsCancellable;
79 JTabbedPane subjobs = null;
81 java.util.Vector jobPanes = null;
83 private boolean serviceCanMergeResults = false;
85 private boolean viewResultsImmediatly = true;
91 * to indicate if results will be shown in a new window as soon as
94 public boolean isViewResultsImmediatly()
96 return viewResultsImmediatly;
103 * to indicate if results will be shown in a new window as soon as
104 * they are available.
106 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
108 this.viewResultsImmediatly = viewResultsImmediatly;
111 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
114 // Copied blatantly from
115 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
116 StyleSheet myStyleSheet = new StyleSheet();
118 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
120 editorKit.setStyleSheet(myStyleSheet);
123 * Set the style sheet rules here by reading them from the constants
127 * for (int ix=0; ix<CSS_RULES.length; ix++) {
129 * myStyleSheet.addRule(CSS_RULES[ix]);
138 public synchronized int addJobPane()
140 JScrollPane jobpane = new JScrollPane();
141 JComponent _progressText;
144 JEditorPane progressText = new JEditorPane("text/html", "");
145 progressText.addHyperlinkListener(this);
146 _progressText = progressText;
147 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
148 // progressText.setBorder(null);
149 progressText.setEditable(false);
151 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
153 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
155 * HTMLDocument tipDocument = (HTMLDocument)
156 * (myEditorKit.createDefaultDocument());
158 * progressText.setDocument(tipDocument);
159 */progressText.setText("<html><h1>WS Job</h1></html>");
163 JTextArea progressText = new JTextArea();
164 _progressText = progressText;
166 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
167 progressText.setBorder(null);
168 progressText.setEditable(false);
169 progressText.setText("WS Job");
170 progressText.setLineWrap(true);
171 progressText.setWrapStyleWord(true);
173 jobpane.setName("JobPane");
174 jobpane.getViewport().add(_progressText, null);
175 jobpane.setBorder(null);
176 if (jobPanes == null)
178 jobPanes = new Vector();
180 int newpane = jobPanes.size();
181 jobPanes.add(jobpane);
185 this.add(jobpane, BorderLayout.CENTER);
191 // revert to a tabbed pane.
192 JScrollPane firstpane;
193 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
194 subjobs = new JTabbedPane();
195 this.add(subjobs, BorderLayout.CENTER);
196 subjobs.add(firstpane);
197 subjobs.setTitleAt(0, firstpane.getName());
199 subjobs.add(jobpane);
201 return newpane; // index for accessor methods below
205 * Creates a new WebserviceInfo object.
208 * short name and job type
210 * reference or other human readable description
212 public WebserviceInfo(String title, String info)
214 init(title, info, 520, 500);
218 * Creates a new WebserviceInfo object.
229 public WebserviceInfo(String title, String info, int width, int height)
231 init(title, info, width, height);
237 * @return DOCUMENT ME!
239 public jalview.ws.WSClientI getthisService()
245 * Update state of GUI based on client capabilities (like whether the job is
246 * cancellable, whether the 'merge results' button is shown.
249 * service client to query for capabilities
251 public void setthisService(jalview.ws.WSClientI newservice)
253 thisService = newservice;
254 serviceIsCancellable = newservice.isCancellable();
255 frame.setClosable(!serviceIsCancellable);
256 serviceCanMergeResults = newservice.canMergeResults();
257 rebuildButtonPanel();
260 private void rebuildButtonPanel()
262 if (buttonPanel != null)
264 buttonPanel.removeAll();
265 if (serviceIsCancellable)
267 buttonPanel.add(cancel);
268 frame.setClosable(false);
272 frame.setClosable(true);
289 void init(String title, String info, int width, int height)
291 frame = new JInternalFrame();
292 frame.setContentPane(this);
293 Desktop.addInternalFrame(frame, title, width, height);
294 frame.setClosable(false);
299 java.net.URL url = getClass().getResource("/images/logo.gif");
300 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
302 MediaTracker mt = new MediaTracker(this);
303 mt.addImage(image, 0);
308 } catch (Exception ex)
312 AnimatedPanel ap = new AnimatedPanel();
313 titlePanel.add(ap, BorderLayout.CENTER);
315 Thread thread = new Thread(ap);
317 final WebserviceInfo thisinfo = this;
318 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
320 public void internalFrameClosed(
321 javax.swing.event.InternalFrameEvent evt)
323 // System.out.println("Shutting down webservice client");
324 WSClientI service = thisinfo.getthisService();
325 if (service != null && service.isCancellable())
339 * integer status from state constants
341 public void setStatus(int status)
343 currentStatus = status;
347 * subjob status indicator
352 public void setStatus(int jobpane, int status)
354 if (jobpane < 0 || jobpane >= jobPanes.size())
356 throw new Error("setStatus called for non-existent job pane."
362 setProgressName(jobpane + " - QUEUED", jobpane);
365 setProgressName(jobpane + " - RUNNING", jobpane);
367 case STATE_STOPPED_OK:
368 setProgressName(jobpane + " - FINISHED", jobpane);
370 case STATE_CANCELLED_OK:
371 setProgressName(jobpane + " - CANCELLED", jobpane);
373 case STATE_STOPPED_ERROR:
374 setProgressName(jobpane + " - BROKEN", jobpane);
376 case STATE_STOPPED_SERVERERROR:
377 setProgressName(jobpane + " - ALERT", jobpane);
380 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
387 * @return DOCUMENT ME!
389 public String getInfoText()
391 return infoText.getText();
400 public void setInfoText(String text)
402 infoText.setText(text);
411 public void appendInfoText(String text)
413 infoText.append(text);
419 * @return DOCUMENT ME!
421 public String getProgressText(int which)
423 if (jobPanes == null)
429 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
430 .getViewport().getComponent(0)).getText();
434 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
435 .getComponent(0)).getText();
445 public void setProgressText(int which, String text)
447 if (jobPanes == null)
453 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
454 .getComponent(0)).setText(ensureHtmlTagged(text));
458 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
459 .getComponent(0)).setText(text);
464 * extract content from <body> content </body>
468 * - set to leave the initial html tag intact
470 * - set to leave the final html tag intact
473 private String getHtmlFragment(String text, boolean leaveFirst,
480 String lowertxt = text.toLowerCase();
481 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
483 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
484 int htmlpose = lowertxt.indexOf(">", htmlpos), htmlende = lowertxt
485 .indexOf(">", htmlend);
486 if (htmlend == -1 && htmlpos == -1)
492 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
494 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
498 * very simple routine for adding/ensuring html tags are present in text.
501 * @return properly html tag enclosed text
503 private String ensureHtmlTagged(String text)
509 String lowertxt = text.toLowerCase();
510 int htmlpos = lowertxt.indexOf("<body");
511 int htmlend = lowertxt.indexOf("</body");
512 int doctype = lowertxt.indexOf("<!doctype");
513 int xmltype = lowertxt.indexOf("<?xml");
516 text = text + "</body></html>";
520 if ((doctype > -1 && htmlpos > doctype)
521 || (xmltype > -1 && htmlpos > xmltype))
523 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
528 text = "<html><head></head><body>\n" + text;
530 if (text.indexOf("<meta") > -1)
532 System.err.println("HTML COntent: \n" + text
533 + "<< END HTML CONTENT\n");
545 public void appendProgressText(int which, String text)
547 if (jobPanes == null)
553 String txt = getHtmlFragment(
554 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
555 .getViewport().getComponent(0)).getText(), true,
557 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
558 .getComponent(0)).setText(ensureHtmlTagged(txt
559 + getHtmlFragment(text, false, true)));
563 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
564 .getComponent(0)).append(text);
569 * setProgressText(0, text)
571 public void setProgressText(String text)
573 setProgressText(0, text);
577 * appendProgressText(0, text)
579 public void appendProgressText(String text)
581 appendProgressText(0, text);
587 public String getProgressText()
589 return getProgressText(0);
593 * get the tab title for a subjob
599 public String getProgressName(int which)
601 if (jobPanes == null)
607 return subjobs.getTitleAt(which);
611 return ((JScrollPane) jobPanes.get(which)).getViewport()
612 .getComponent(0).getName();
617 * set the tab title for a subjob
624 public void setProgressName(String name, int which)
628 subjobs.setTitleAt(which, name);
629 subjobs.revalidate();
632 JScrollPane c = (JScrollPane) jobPanes.get(which);
633 c.getViewport().getComponent(0).setName(name);
638 * Gui action for cancelling the current job, if possible.
643 protected void cancel_actionPerformed(ActionEvent e)
645 if (!serviceIsCancellable)
647 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
648 // anyhow - it has to stop threads and clean up
649 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
651 warnUser("This job cannot be cancelled.\nJust close the window.",
656 thisService.cancelJob();
658 frame.setClosable(true);
662 * Spawns a thread that pops up a warning dialog box with the given message
668 public void warnUser(final String message, final String title)
670 javax.swing.SwingUtilities.invokeLater(new Runnable()
674 JOptionPane.showInternalMessageDialog(Desktop.desktop, message,
675 title, JOptionPane.WARNING_MESSAGE);
682 * Set up GUI for user to get at results - and possibly automatically display
683 * them if viewResultsImmediatly is set.
685 public void setResultsReady()
687 frame.setClosable(true);
688 buttonPanel.remove(cancel);
689 buttonPanel.add(showResultsNewFrame);
690 if (serviceCanMergeResults)
692 buttonPanel.add(mergeResults);
693 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
695 buttonPanel.validate();
697 if (viewResultsImmediatly)
699 showResultsNewFrame.doClick();
704 * called when job has finished but no result objects can be passed back to
707 public void setFinishedNoResults()
709 frame.setClosable(true);
710 buttonPanel.remove(cancel);
711 buttonPanel.validate();
715 class AnimatedPanel extends JPanel implements Runnable
719 BufferedImage offscreen;
723 startTime = System.currentTimeMillis();
725 while (currentStatus < STATE_STOPPED_OK)
731 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
734 startTime = System.currentTimeMillis();
736 if (currentStatus >= STATE_STOPPED_OK)
742 } catch (Exception ex)
747 cancel.setEnabled(false);
752 if (offscreen == null || offscreen.getWidth(this) != getWidth()
753 || offscreen.getHeight(this) != getHeight())
755 offscreen = new BufferedImage(getWidth(), getHeight(),
756 BufferedImage.TYPE_INT_ARGB);
759 Graphics2D g = (Graphics2D) offscreen.getGraphics();
761 g.setColor(Color.white);
762 g.fillRect(0, 0, getWidth(), getHeight());
764 g.setFont(new Font("Arial", Font.BOLD, 12));
765 g.setColor(Color.black);
767 switch (currentStatus)
770 g.drawString(title.concat(" - queuing"), 60, 30);
775 g.drawString(title.concat(" - running"), 60, 30);
779 case STATE_STOPPED_OK:
780 g.drawString(title.concat(" - complete"), 60, 30);
784 case STATE_CANCELLED_OK:
785 g.drawString(title.concat(" - job cancelled!"), 60, 30);
789 case STATE_STOPPED_ERROR:
790 g.drawString(title.concat(" - job error!"), 60, 30);
794 case STATE_STOPPED_SERVERERROR:
795 g.drawString(title.concat(" - Server Error! (try later)"), 60, 30);
802 g.rotate(Math.toRadians(angle), 28, 28);
803 g.drawImage(image, 10, 10, this);
804 g.rotate(-Math.toRadians(angle), 28, 28);
808 public void paintComponent(Graphics g1)
812 g1.drawImage(offscreen, 0, 0, this);
816 boolean renderAsHtml = false;
818 public void setRenderAsHtml(boolean b)
823 public void hyperlinkUpdate(HyperlinkEvent e)
825 if (e.getEventType() == EventType.ACTIVATED)
830 url = e.getURL().toString();
831 Desktop.showUrl(url);
832 } catch (Exception x)
835 Cache.log.error("Couldn't handle string "+url+" as a URL.");
837 // ignore any exceptions due to dud links.