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.JOptionPane;
43 import javax.swing.JPanel;
44 import javax.swing.JScrollPane;
45 import javax.swing.JTabbedPane;
46 import javax.swing.JTextArea;
47 import javax.swing.event.HyperlinkEvent;
48 import javax.swing.event.HyperlinkListener;
49 import javax.swing.text.html.HTMLEditorKit;
50 import javax.swing.text.html.StyleSheet;
53 * Base class for web service client thread and gui TODO: create StAX parser to
54 * extract html body content reliably when preparing html formatted job statuses
59 public class WebserviceInfo extends GWebserviceInfo
60 implements HyperlinkListener, IProgressIndicator
64 public static final int STATE_QUEUING = 0;
67 public static final int STATE_RUNNING = 1;
69 /** Job has finished with no errors */
70 public static final int STATE_STOPPED_OK = 2;
72 /** Job has been cancelled with no errors */
73 public static final int STATE_CANCELLED_OK = 3;
75 /** job has stopped because of some error */
76 public static final int STATE_STOPPED_ERROR = 4;
78 /** job has failed because of some unavoidable service interruption */
79 public static final int STATE_STOPPED_SERVERERROR = 5;
81 int currentStatus = STATE_QUEUING;
89 jalview.ws.WSClientI thisService;
91 boolean serviceIsCancellable;
95 private IProgressIndicator progressBar;
98 public void setVisible(boolean aFlag)
100 super.setVisible(aFlag);
101 frame.setVisible(aFlag);
104 JTabbedPane subjobs = null;
106 java.util.Vector jobPanes = null;
108 private boolean serviceCanMergeResults = false;
110 private boolean viewResultsImmediatly = true;
116 * to indicate if results will be shown in a new window as soon as
117 * they are available.
119 public boolean isViewResultsImmediatly()
121 return viewResultsImmediatly;
128 * to indicate if results will be shown in a new window as soon as
129 * they are available.
131 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
133 this.viewResultsImmediatly = viewResultsImmediatly;
136 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
139 // Copied blatantly from
140 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
141 StyleSheet myStyleSheet = new StyleSheet();
143 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
145 editorKit.setStyleSheet(myStyleSheet);
148 * Set the style sheet rules here by reading them from the constants
152 * for (int ix=0; ix<CSS_RULES.length; ix++) {
154 * myStyleSheet.addRule(CSS_RULES[ix]);
163 public synchronized int addJobPane()
165 JScrollPane jobpane = new JScrollPane();
166 JComponent _progressText;
169 JEditorPane progressText = new JEditorPane("text/html", "");
170 progressText.addHyperlinkListener(this);
171 _progressText = progressText;
172 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
173 // progressText.setBorder(null);
174 progressText.setEditable(false);
176 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
178 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
180 * HTMLDocument tipDocument = (HTMLDocument)
181 * (myEditorKit.createDefaultDocument());
183 * progressText.setDocument(tipDocument);
184 */progressText.setText("<html><h1>WS Job</h1></html>");
188 JTextArea progressText = new JTextArea();
189 _progressText = progressText;
191 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
192 progressText.setBorder(null);
193 progressText.setEditable(false);
194 progressText.setText("WS Job");
195 progressText.setLineWrap(true);
196 progressText.setWrapStyleWord(true);
198 jobpane.setName("JobPane");
199 jobpane.getViewport().add(_progressText, null);
200 jobpane.setBorder(null);
201 if (jobPanes == null)
203 jobPanes = new Vector();
205 int newpane = jobPanes.size();
206 jobPanes.add(jobpane);
210 this.add(jobpane, BorderLayout.CENTER);
216 // revert to a tabbed pane.
217 JScrollPane firstpane;
218 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
219 subjobs = new JTabbedPane();
220 this.add(subjobs, BorderLayout.CENTER);
221 subjobs.add(firstpane);
222 subjobs.setTitleAt(0, firstpane.getName());
224 subjobs.add(jobpane);
226 return newpane; // index for accessor methods below
230 * Creates a new WebserviceInfo object.
233 * short name and job type
235 * reference or other human readable description
237 * true to display the webservices window immediatly (otherwise need
238 * to call setVisible(true))
240 public WebserviceInfo(String title, String info, boolean makeVisible)
242 init(title, info, 520, 500, makeVisible);
246 * Creates a new WebserviceInfo object.
257 public WebserviceInfo(String title, String info, int width, int height,
260 init(title, info, width, height, makeVisible);
266 * @return DOCUMENT ME!
268 public jalview.ws.WSClientI getthisService()
274 * Update state of GUI based on client capabilities (like whether the job is
275 * cancellable, whether the 'merge results' button is shown.
278 * service client to query for capabilities
280 public void setthisService(jalview.ws.WSClientI newservice)
282 thisService = newservice;
283 serviceIsCancellable = newservice.isCancellable();
284 frame.setClosable(!serviceIsCancellable);
285 serviceCanMergeResults = newservice.canMergeResults();
286 rebuildButtonPanel();
289 private void rebuildButtonPanel()
291 if (buttonPanel != null)
293 buttonPanel.removeAll();
294 if (serviceIsCancellable)
296 buttonPanel.add(cancel);
297 frame.setClosable(false);
301 frame.setClosable(true);
318 void init(String title, String info, int width, int height,
321 frame = new JInternalFrame();
322 frame.setContentPane(this);
323 Desktop.addInternalFrame(frame, title, makeVisible, width, height);
324 frame.setClosable(false);
326 progressBar = new ProgressBar(statusPanel, statusBar);
331 java.net.URL url = getClass()
332 .getResource("/images/Jalview_Logo_small.png");
333 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
335 MediaTracker mt = new MediaTracker(this);
336 mt.addImage(image, 0);
341 } catch (Exception ex)
345 AnimatedPanel ap = new AnimatedPanel();
346 titlePanel.add(ap, BorderLayout.CENTER);
348 Thread thread = new Thread(ap);
350 final WebserviceInfo thisinfo = this;
351 frame.addInternalFrameListener(
352 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.
679 protected void cancel_actionPerformed(ActionEvent e)
681 if (!serviceIsCancellable)
683 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
684 // anyhow - it has to stop threads and clean up
685 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
688 MessageManager.getString(
689 "warn.job_cannot_be_cancelled_close_window"),
690 MessageManager.getString("action.cancel_job"));
694 thisService.cancelJob();
696 frame.setClosable(true);
700 * Spawns a thread that pops up a warning dialog box with the given message
706 public void warnUser(final String message, final String title)
708 javax.swing.SwingUtilities.invokeLater(new Runnable()
712 JvOptionPane.showInternalMessageDialog(Desktop.desktop, message,
713 title, JvOptionPane.WARNING_MESSAGE);
720 * Set up GUI for user to get at results - and possibly automatically display
721 * them if viewResultsImmediatly is set.
723 public void setResultsReady()
725 frame.setClosable(true);
726 buttonPanel.remove(cancel);
727 buttonPanel.add(showResultsNewFrame);
728 if (serviceCanMergeResults)
730 buttonPanel.add(mergeResults);
731 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
733 buttonPanel.validate();
735 if (viewResultsImmediatly)
737 showResultsNewFrame.doClick();
742 * called when job has finished but no result objects can be passed back to
745 public void setFinishedNoResults()
747 frame.setClosable(true);
748 buttonPanel.remove(cancel);
749 buttonPanel.validate();
753 class AnimatedPanel extends JPanel implements Runnable
757 BufferedImage offscreen;
761 startTime = System.currentTimeMillis();
763 while (currentStatus < STATE_STOPPED_OK)
769 int units = (int) ((System.currentTimeMillis() - startTime)
773 startTime = System.currentTimeMillis();
775 if (currentStatus >= STATE_STOPPED_OK)
781 } catch (Exception ex)
786 cancel.setEnabled(false);
791 if (offscreen == null || offscreen.getWidth(this) != getWidth()
792 || offscreen.getHeight(this) != getHeight())
794 offscreen = new BufferedImage(getWidth(), getHeight(),
795 BufferedImage.TYPE_INT_ARGB);
798 Graphics2D g = (Graphics2D) offscreen.getGraphics();
800 g.setColor(Color.white);
801 g.fillRect(0, 0, getWidth(), getHeight());
803 g.setFont(new Font("Arial", Font.BOLD, 12));
804 g.setColor(Color.black);
806 switch (currentStatus)
810 title.concat(" - ").concat(
811 MessageManager.getString("label.state_queueing")),
818 title.concat(" - ").concat(
819 MessageManager.getString("label.state_running")),
824 case STATE_STOPPED_OK:
826 title.concat(" - ").concat(
827 MessageManager.getString("label.state_completed")),
832 case STATE_CANCELLED_OK:
835 .concat(MessageManager
836 .getString("label.state_job_cancelled")),
841 case STATE_STOPPED_ERROR:
843 title.concat(" - ").concat(
844 MessageManager.getString("label.state_job_error")),
849 case STATE_STOPPED_SERVERERROR:
852 .concat(MessageManager
853 .getString("label.server_error_try_later")),
861 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
862 g.rotate(Math.toRadians(angle), 10 + x, 10 + y);
863 g.drawImage(image, 10, 10, this);
864 g.rotate(-Math.toRadians(angle), 10 + x, 10 + y);
868 public void paintComponent(Graphics g1)
872 g1.drawImage(offscreen, 0, 0, this);
876 boolean renderAsHtml = false;
878 public void setRenderAsHtml(boolean b)
883 public void hyperlinkUpdate(HyperlinkEvent e)
885 Desktop.hyperlinkUpdate(e);
891 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
894 public void setProgressBar(String message, long id)
896 progressBar.setProgressBar(message, id);
900 public void registerHandler(final long id,
901 final IProgressIndicatorHandler handler)
903 progressBar.registerHandler(id, handler);
908 * @return true if any progress bars are still active
911 public boolean operationInProgress()
913 return progressBar.operationInProgress();