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;
29 import java.awt.Dimension;
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 ap.setPreferredSize(new Dimension(60, 0));
346 titlePanel.add(ap, BorderLayout.LINE_START);
347 titlePanel.add(titleText, BorderLayout.CENTER);
349 Thread thread = new Thread(ap);
351 final WebserviceInfo thisinfo = this;
352 frame.addInternalFrameListener(
353 new javax.swing.event.InternalFrameAdapter()
356 public void internalFrameClosed(
357 javax.swing.event.InternalFrameEvent evt)
359 // System.out.println("Shutting down webservice client");
360 WSClientI service = thisinfo.getthisService();
361 if (service != null && service.isCancellable())
375 * integer status from state constants
377 public void setStatus(int status)
379 currentStatus = status;
381 String message = null;
382 switch (currentStatus)
385 message = MessageManager.getString("label.state_queueing");
389 message = MessageManager.getString("label.state_running");
392 case STATE_STOPPED_OK:
393 message = MessageManager.getString("label.state_completed");
396 case STATE_CANCELLED_OK:
397 message = MessageManager.getString("label.state_job_cancelled");
400 case STATE_STOPPED_ERROR:
401 message = MessageManager.getString("label.state_job_error");
404 case STATE_STOPPED_SERVERERROR:
405 message = MessageManager.getString("label.server_error_try_later");
408 titleText.setText(title + (message == null ? "" : " - " + message));
413 * subjob status indicator
418 public void setStatus(int jobpane, int status)
420 if (jobpane < 0 || jobpane >= jobPanes.size())
422 throw new Error(MessageManager.formatMessage(
423 "error.setstatus_called_non_existent_job_pane", new String[]
424 { Integer.valueOf(jobpane).toString() }));
429 setProgressName(jobpane + " - QUEUED", jobpane);
432 setProgressName(jobpane + " - RUNNING", jobpane);
434 case STATE_STOPPED_OK:
435 setProgressName(jobpane + " - FINISHED", jobpane);
437 case STATE_CANCELLED_OK:
438 setProgressName(jobpane + " - CANCELLED", jobpane);
440 case STATE_STOPPED_ERROR:
441 setProgressName(jobpane + " - BROKEN", jobpane);
443 case STATE_STOPPED_SERVERERROR:
444 setProgressName(jobpane + " - ALERT", jobpane);
447 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
454 * @return DOCUMENT ME!
456 public String getInfoText()
458 return infoText.getText();
467 public void setInfoText(String text)
469 infoText.setText(text);
478 public void appendInfoText(String text)
480 infoText.append(text);
486 * @return DOCUMENT ME!
488 public String getProgressText(int which)
490 if (jobPanes == null)
496 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
497 .getViewport().getComponent(0)).getText();
501 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
502 .getComponent(0)).getText();
512 public void setProgressText(int which, String text)
514 if (jobPanes == null)
520 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
521 .getComponent(0)).setText(ensureHtmlTagged(text));
525 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
526 .getComponent(0)).setText(text);
531 * extract content from <body> content </body>
535 * - set to leave the initial html tag intact
537 * - set to leave the final html tag intact
540 private String getHtmlFragment(String text, boolean leaveFirst,
547 String lowertxt = text.toLowerCase();
548 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
550 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
551 int htmlpose = lowertxt.indexOf(">", htmlpos),
552 htmlende = lowertxt.indexOf(">", htmlend);
553 if (htmlend == -1 && htmlpos == -1)
559 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
561 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
565 * very simple routine for adding/ensuring html tags are present in text.
568 * @return properly html tag enclosed text
570 private String ensureHtmlTagged(String text)
576 String lowertxt = text.toLowerCase();
577 int htmlpos = lowertxt.indexOf("<body");
578 int htmlend = lowertxt.indexOf("</body");
579 int doctype = lowertxt.indexOf("<!doctype");
580 int xmltype = lowertxt.indexOf("<?xml");
583 text = text + "</body></html>";
587 if ((doctype > -1 && htmlpos > doctype)
588 || (xmltype > -1 && htmlpos > xmltype))
590 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
595 text = "<html><head></head><body>\n" + text;
597 if (text.indexOf("<meta") > -1)
600 .println("HTML COntent: \n" + text + "<< END HTML CONTENT\n");
612 public void appendProgressText(int which, String text)
614 if (jobPanes == null)
620 String txt = getHtmlFragment(
621 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
622 .getViewport().getComponent(0)).getText(),
624 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
626 .setText(ensureHtmlTagged(
627 txt + getHtmlFragment(text, false, true)));
631 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
632 .getComponent(0)).append(text);
637 * setProgressText(0, text)
639 public void setProgressText(String text)
641 setProgressText(0, text);
645 * appendProgressText(0, text)
647 public void appendProgressText(String text)
649 appendProgressText(0, text);
655 public String getProgressText()
657 return getProgressText(0);
661 * get the tab title for a subjob
667 public String getProgressName(int which)
669 if (jobPanes == null)
675 return subjobs.getTitleAt(which);
679 return ((JScrollPane) jobPanes.get(which)).getViewport()
680 .getComponent(0).getName();
685 * set the tab title for a subjob
692 public void setProgressName(String name, int which)
696 subjobs.setTitleAt(which, name);
697 subjobs.revalidate();
700 JScrollPane c = (JScrollPane) jobPanes.get(which);
701 c.getViewport().getComponent(0).setName(name);
706 * Gui action for cancelling the current job, if possible.
712 protected void cancel_actionPerformed(ActionEvent e)
714 if (!serviceIsCancellable)
716 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
717 // anyhow - it has to stop threads and clean up
718 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
721 MessageManager.getString(
722 "warn.job_cannot_be_cancelled_close_window"),
723 MessageManager.getString("action.cancel_job"));
727 thisService.cancelJob();
729 frame.setClosable(true);
733 * Spawns a thread that pops up a warning dialog box with the given message
739 public void warnUser(final String message, final String title)
741 javax.swing.SwingUtilities.invokeLater(new Runnable()
746 JvOptionPane.showInternalMessageDialog(Desktop.desktop, message,
747 title, JvOptionPane.WARNING_MESSAGE);
754 * Set up GUI for user to get at results - and possibly automatically display
755 * them if viewResultsImmediatly is set.
757 public void setResultsReady()
759 frame.setClosable(true);
760 buttonPanel.remove(cancel);
761 buttonPanel.add(showResultsNewFrame);
762 if (serviceCanMergeResults)
764 buttonPanel.add(mergeResults);
765 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
767 buttonPanel.validate();
769 if (viewResultsImmediatly)
771 showResultsNewFrame.doClick();
776 * called when job has finished but no result objects can be passed back to
779 public void setFinishedNoResults()
781 frame.setClosable(true);
782 buttonPanel.remove(cancel);
783 buttonPanel.validate();
787 class AnimatedPanel extends JPanel implements Runnable
791 BufferedImage offscreen;
796 startTime = System.currentTimeMillis();
798 while (currentStatus < STATE_STOPPED_OK)
804 int units = (int) ((System.currentTimeMillis() - startTime)
808 startTime = System.currentTimeMillis();
810 if (currentStatus >= STATE_STOPPED_OK)
816 } catch (Exception ex)
821 cancel.setEnabled(false);
826 if (offscreen == null || offscreen.getWidth(this) != getWidth()
827 || offscreen.getHeight(this) != getHeight())
829 offscreen = new BufferedImage(getWidth(), getHeight(),
830 BufferedImage.TYPE_INT_ARGB);
833 Graphics2D g = (Graphics2D) offscreen.getGraphics();
835 g.setColor(Color.white);
836 g.fillRect(0, 0, getWidth(), getHeight());
840 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
841 g.rotate(Math.toRadians(angle), 10 + x, 10 + y);
842 g.drawImage(image, 10, 10, this);
843 g.rotate(-Math.toRadians(angle), 10 + x, 10 + y);
848 public void paintComponent(Graphics g1)
852 g1.drawImage(offscreen, 0, 0, this);
856 boolean renderAsHtml = false;
858 public void setRenderAsHtml(boolean b)
864 public void hyperlinkUpdate(HyperlinkEvent e)
866 Desktop.hyperlinkUpdate(e);
872 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
875 public void setProgressBar(String message, long id)
877 progressBar.setProgressBar(message, id);
881 public void registerHandler(final long id,
882 final IProgressIndicatorHandler handler)
884 progressBar.registerHandler(id, handler);
889 * @return true if any progress bars are still active
892 public boolean operationInProgress()
894 return progressBar.operationInProgress();