2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.jbgui.GWebserviceInfo;
24 import jalview.util.MessageManager;
25 import jalview.ws.WSClientI;
27 import java.awt.BorderLayout;
28 import java.awt.Color;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.GridLayout;
33 import java.awt.Image;
34 import java.awt.MediaTracker;
35 import java.awt.event.ActionEvent;
36 import java.awt.image.BufferedImage;
37 import java.util.Vector;
39 import javax.swing.JComponent;
40 import javax.swing.JEditorPane;
41 import javax.swing.JInternalFrame;
42 import javax.swing.JPanel;
43 import javax.swing.JScrollPane;
44 import javax.swing.JTabbedPane;
45 import javax.swing.JTextArea;
46 import javax.swing.event.HyperlinkEvent;
47 import javax.swing.event.HyperlinkListener;
48 import javax.swing.text.html.HTMLEditorKit;
49 import javax.swing.text.html.StyleSheet;
52 * Base class for web service client thread and gui TODO: create StAX parser to
53 * extract html body content reliably when preparing html formatted job statuses
58 public class WebserviceInfo extends GWebserviceInfo
59 implements HyperlinkListener, IProgressIndicator
63 public static final int STATE_QUEUING = 0;
66 public static final int STATE_RUNNING = 1;
68 /** Job has finished with no errors */
69 public static final int STATE_STOPPED_OK = 2;
71 /** Job has been cancelled with no errors */
72 public static final int STATE_CANCELLED_OK = 3;
74 /** job has stopped because of some error */
75 public static final int STATE_STOPPED_ERROR = 4;
77 /** job has failed because of some unavoidable service interruption */
78 public static final int STATE_STOPPED_SERVERERROR = 5;
80 int currentStatus = STATE_QUEUING;
88 jalview.ws.WSClientI thisService;
90 boolean serviceIsCancellable;
94 private IProgressIndicator progressBar;
97 public void setVisible(boolean aFlag)
99 super.setVisible(aFlag);
100 frame.setVisible(aFlag);
103 JTabbedPane subjobs = null;
105 java.util.Vector jobPanes = null;
107 private boolean serviceCanMergeResults = false;
109 private boolean viewResultsImmediatly = true;
115 * to indicate if results will be shown in a new window as soon as
116 * they are available.
118 public boolean isViewResultsImmediatly()
120 return viewResultsImmediatly;
127 * to indicate if results will be shown in a new window as soon as
128 * they are available.
130 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
132 this.viewResultsImmediatly = viewResultsImmediatly;
135 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
138 // Copied blatantly from
139 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
140 StyleSheet myStyleSheet = new StyleSheet();
142 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
144 editorKit.setStyleSheet(myStyleSheet);
147 * Set the style sheet rules here by reading them from the constants
151 * for (int ix=0; ix<CSS_RULES.length; ix++) {
153 * myStyleSheet.addRule(CSS_RULES[ix]);
162 public synchronized int addJobPane()
164 JScrollPane jobpane = new JScrollPane();
165 JComponent _progressText;
168 JEditorPane progressText = new JEditorPane("text/html", "");
169 progressText.addHyperlinkListener(this);
170 _progressText = progressText;
171 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
172 // progressText.setBorder(null);
173 progressText.setEditable(false);
175 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
177 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
179 * HTMLDocument tipDocument = (HTMLDocument)
180 * (myEditorKit.createDefaultDocument());
182 * progressText.setDocument(tipDocument);
183 */progressText.setText("<html><h1>WS Job</h1></html>");
187 JTextArea progressText = new JTextArea();
188 _progressText = progressText;
190 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
191 progressText.setBorder(null);
192 progressText.setEditable(false);
193 progressText.setText("WS Job");
194 progressText.setLineWrap(true);
195 progressText.setWrapStyleWord(true);
197 jobpane.setName("JobPane");
198 jobpane.getViewport().add(_progressText, null);
199 jobpane.setBorder(null);
200 if (jobPanes == null)
202 jobPanes = new Vector();
204 int newpane = jobPanes.size();
205 jobPanes.add(jobpane);
209 this.add(jobpane, BorderLayout.CENTER);
215 // revert to a tabbed pane.
216 JScrollPane firstpane;
217 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
218 subjobs = new JTabbedPane();
219 this.add(subjobs, BorderLayout.CENTER);
220 subjobs.add(firstpane);
221 subjobs.setTitleAt(0, firstpane.getName());
223 subjobs.add(jobpane);
225 return newpane; // index for accessor methods below
229 * Creates a new WebserviceInfo object.
232 * short name and job type
234 * reference or other human readable description
236 * true to display the webservices window immediatly (otherwise need
237 * to call setVisible(true))
239 public WebserviceInfo(String title, String info, boolean makeVisible)
241 init(title, info, 520, 500, makeVisible);
245 * Creates a new WebserviceInfo object.
256 public WebserviceInfo(String title, String info, int width, int height,
259 init(title, info, width, height, makeVisible);
265 * @return DOCUMENT ME!
267 public jalview.ws.WSClientI getthisService()
273 * Update state of GUI based on client capabilities (like whether the job is
274 * cancellable, whether the 'merge results' button is shown.
277 * service client to query for capabilities
279 public void setthisService(jalview.ws.WSClientI newservice)
281 thisService = newservice;
282 serviceIsCancellable = newservice.isCancellable();
283 frame.setClosable(!serviceIsCancellable);
284 serviceCanMergeResults = newservice.canMergeResults();
285 rebuildButtonPanel();
288 private void rebuildButtonPanel()
290 if (buttonPanel != null)
292 buttonPanel.removeAll();
293 if (serviceIsCancellable)
295 buttonPanel.add(cancel);
296 frame.setClosable(false);
300 frame.setClosable(true);
317 void init(String title, String info, int width, int height,
320 frame = new JInternalFrame();
321 frame.setContentPane(this);
322 Desktop.addInternalFrame(frame, title, makeVisible, width, height);
323 frame.setClosable(false);
325 progressBar = new ProgressBar(statusPanel, statusBar);
330 java.net.URL url = getClass()
331 .getResource("/images/Jalview_Logo_small.png");
332 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
334 MediaTracker mt = new MediaTracker(this);
335 mt.addImage(image, 0);
340 } catch (Exception ex)
344 AnimatedPanel ap = new AnimatedPanel();
345 titlePanel.add(ap, BorderLayout.CENTER);
347 Thread thread = new Thread(ap);
349 final WebserviceInfo thisinfo = this;
350 frame.addInternalFrameListener(
351 new javax.swing.event.InternalFrameAdapter()
354 public void internalFrameClosed(
355 javax.swing.event.InternalFrameEvent evt)
357 // System.out.println("Shutting down webservice client");
358 WSClientI service = thisinfo.getthisService();
359 if (service != null && service.isCancellable())
373 * integer status from state constants
375 public void setStatus(int status)
377 currentStatus = status;
381 * subjob status indicator
386 public void setStatus(int jobpane, int status)
388 if (jobpane < 0 || jobpane >= jobPanes.size())
390 throw new Error(MessageManager.formatMessage(
391 "error.setstatus_called_non_existent_job_pane", new String[]
392 { Integer.valueOf(jobpane).toString() }));
397 setProgressName(jobpane + " - QUEUED", jobpane);
400 setProgressName(jobpane + " - RUNNING", jobpane);
402 case STATE_STOPPED_OK:
403 setProgressName(jobpane + " - FINISHED", jobpane);
405 case STATE_CANCELLED_OK:
406 setProgressName(jobpane + " - CANCELLED", jobpane);
408 case STATE_STOPPED_ERROR:
409 setProgressName(jobpane + " - BROKEN", jobpane);
411 case STATE_STOPPED_SERVERERROR:
412 setProgressName(jobpane + " - ALERT", jobpane);
415 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
422 * @return DOCUMENT ME!
424 public String getInfoText()
426 return infoText.getText();
435 public void setInfoText(String text)
437 infoText.setText(text);
446 public void appendInfoText(String text)
448 infoText.append(text);
454 * @return DOCUMENT ME!
456 public String getProgressText(int which)
458 if (jobPanes == null)
464 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
465 .getViewport().getComponent(0)).getText();
469 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
470 .getComponent(0)).getText();
480 public void setProgressText(int which, String text)
482 if (jobPanes == null)
488 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
489 .getComponent(0)).setText(ensureHtmlTagged(text));
493 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
494 .getComponent(0)).setText(text);
499 * extract content from <body> content </body>
503 * - set to leave the initial html tag intact
505 * - set to leave the final html tag intact
508 private String getHtmlFragment(String text, boolean leaveFirst,
515 String lowertxt = text.toLowerCase();
516 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
518 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
519 int htmlpose = lowertxt.indexOf(">", htmlpos),
520 htmlende = lowertxt.indexOf(">", htmlend);
521 if (htmlend == -1 && htmlpos == -1)
527 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
529 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
533 * very simple routine for adding/ensuring html tags are present in text.
536 * @return properly html tag enclosed text
538 private String ensureHtmlTagged(String text)
544 String lowertxt = text.toLowerCase();
545 int htmlpos = lowertxt.indexOf("<body");
546 int htmlend = lowertxt.indexOf("</body");
547 int doctype = lowertxt.indexOf("<!doctype");
548 int xmltype = lowertxt.indexOf("<?xml");
551 text = text + "</body></html>";
555 if ((doctype > -1 && htmlpos > doctype)
556 || (xmltype > -1 && htmlpos > xmltype))
558 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
563 text = "<html><head></head><body>\n" + text;
565 if (text.indexOf("<meta") > -1)
568 .println("HTML COntent: \n" + text + "<< END HTML CONTENT\n");
580 public void appendProgressText(int which, String text)
582 if (jobPanes == null)
588 String txt = getHtmlFragment(
589 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
590 .getViewport().getComponent(0)).getText(),
592 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
594 .setText(ensureHtmlTagged(
595 txt + getHtmlFragment(text, false, true)));
599 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
600 .getComponent(0)).append(text);
605 * setProgressText(0, text)
607 public void setProgressText(String text)
609 setProgressText(0, text);
613 * appendProgressText(0, text)
615 public void appendProgressText(String text)
617 appendProgressText(0, text);
623 public String getProgressText()
625 return getProgressText(0);
629 * get the tab title for a subjob
635 public String getProgressName(int which)
637 if (jobPanes == null)
643 return subjobs.getTitleAt(which);
647 return ((JScrollPane) jobPanes.get(which)).getViewport()
648 .getComponent(0).getName();
653 * set the tab title for a subjob
660 public void setProgressName(String name, int which)
664 subjobs.setTitleAt(which, name);
665 subjobs.revalidate();
668 JScrollPane c = (JScrollPane) jobPanes.get(which);
669 c.getViewport().getComponent(0).setName(name);
674 * Gui action for cancelling the current job, if possible.
680 protected void cancel_actionPerformed(ActionEvent e)
682 if (!serviceIsCancellable)
684 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
685 // anyhow - it has to stop threads and clean up
686 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
689 MessageManager.getString(
690 "warn.job_cannot_be_cancelled_close_window"),
691 MessageManager.getString("action.cancel_job"));
695 thisService.cancelJob();
697 frame.setClosable(true);
701 * Spawns a thread that pops up a warning dialog box with the given message
707 public void warnUser(final String message, final String title)
709 javax.swing.SwingUtilities.invokeLater(new Runnable()
714 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(), message,
715 title, JvOptionPane.WARNING_MESSAGE);
722 * Set up GUI for user to get at results - and possibly automatically display
723 * them if viewResultsImmediatly is set.
725 public void setResultsReady()
727 frame.setClosable(true);
728 buttonPanel.remove(cancel);
729 buttonPanel.add(showResultsNewFrame);
730 if (serviceCanMergeResults)
732 buttonPanel.add(mergeResults);
733 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
735 buttonPanel.validate();
737 if (viewResultsImmediatly)
739 showResultsNewFrame.doClick();
744 * called when job has finished but no result objects can be passed back to
747 public void setFinishedNoResults()
749 frame.setClosable(true);
750 buttonPanel.remove(cancel);
751 buttonPanel.validate();
755 class AnimatedPanel extends JPanel implements Runnable
759 BufferedImage offscreen;
764 startTime = System.currentTimeMillis();
766 while (currentStatus < STATE_STOPPED_OK)
772 int units = (int) ((System.currentTimeMillis() - startTime)
776 startTime = System.currentTimeMillis();
778 if (currentStatus >= STATE_STOPPED_OK)
784 } catch (Exception ex)
789 cancel.setEnabled(false);
794 if (offscreen == null || offscreen.getWidth(this) != getWidth()
795 || offscreen.getHeight(this) != getHeight())
797 offscreen = new BufferedImage(getWidth(), getHeight(),
798 BufferedImage.TYPE_INT_ARGB);
801 Graphics2D g = (Graphics2D) offscreen.getGraphics();
803 g.setColor(Color.white);
804 g.fillRect(0, 0, getWidth(), getHeight());
806 g.setFont(new Font("Arial", Font.BOLD, 12));
807 g.setColor(Color.black);
809 switch (currentStatus)
813 title.concat(" - ").concat(
814 MessageManager.getString("label.state_queueing")),
821 title.concat(" - ").concat(
822 MessageManager.getString("label.state_running")),
827 case STATE_STOPPED_OK:
829 title.concat(" - ").concat(
830 MessageManager.getString("label.state_completed")),
835 case STATE_CANCELLED_OK:
838 .concat(MessageManager
839 .getString("label.state_job_cancelled")),
844 case STATE_STOPPED_ERROR:
846 title.concat(" - ").concat(
847 MessageManager.getString("label.state_job_error")),
852 case STATE_STOPPED_SERVERERROR:
855 .concat(MessageManager
856 .getString("label.server_error_try_later")),
864 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
865 g.rotate(Math.toRadians(angle), 10 + x, 10 + y);
866 g.drawImage(image, 10, 10, this);
867 g.rotate(-Math.toRadians(angle), 10 + x, 10 + y);
872 public void paintComponent(Graphics g1)
876 g1.drawImage(offscreen, 0, 0, this);
880 boolean renderAsHtml = false;
882 public void setRenderAsHtml(boolean b)
888 public void hyperlinkUpdate(HyperlinkEvent e)
890 Desktop.hyperlinkUpdate(e);
896 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
899 public void setProgressBar(String message, long id)
901 progressBar.setProgressBar(message, id);
905 public void registerHandler(final long id,
906 final IProgressIndicatorHandler handler)
908 progressBar.registerHandler(id, handler);
913 * @return true if any progress bars are still active
916 public boolean operationInProgress()
918 return progressBar.operationInProgress();