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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.Graphics;
27 import java.awt.Graphics2D;
28 import java.awt.GridLayout;
29 import java.awt.Image;
30 import java.awt.MediaTracker;
31 import java.awt.RenderingHints;
32 import java.awt.event.ActionEvent;
33 import java.awt.image.BufferedImage;
34 import java.util.Locale;
35 import java.util.Vector;
37 import javax.swing.JComponent;
38 import javax.swing.JEditorPane;
39 import javax.swing.JInternalFrame;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTabbedPane;
43 import javax.swing.JTextArea;
44 import javax.swing.event.HyperlinkEvent;
45 import javax.swing.event.HyperlinkListener;
46 import javax.swing.event.InternalFrameAdapter;
47 import javax.swing.event.InternalFrameEvent;
48 import javax.swing.text.html.HTMLEditorKit;
49 import javax.swing.text.html.StyleSheet;
51 import jalview.jbgui.GWebserviceInfo;
52 import jalview.util.ChannelProperties;
53 import jalview.util.MessageManager;
54 import jalview.ws.WSClientI;
57 * Base class for web service client thread and gui TODO: create StAX parser to
58 * extract html body content reliably when preparing html formatted job statuses
63 public class WebserviceInfo extends GWebserviceInfo
64 implements HyperlinkListener, IProgressIndicator
68 public static final int STATE_QUEUING = 0;
71 public static final int STATE_RUNNING = 1;
73 /** Job has finished with no errors */
74 public static final int STATE_STOPPED_OK = 2;
76 /** Job has been cancelled with no errors */
77 public static final int STATE_CANCELLED_OK = 3;
79 /** job has stopped because of some error */
80 public static final int STATE_STOPPED_ERROR = 4;
82 /** job has failed because of some unavoidable service interruption */
83 public static final int STATE_STOPPED_SERVERERROR = 5;
85 int currentStatus = STATE_QUEUING;
93 jalview.ws.WSClientI thisService;
95 boolean serviceIsCancellable;
99 private IProgressIndicator progressBar;
102 public void setVisible(boolean aFlag)
104 super.setVisible(aFlag);
105 frame.setVisible(aFlag);
108 JTabbedPane subjobs = null;
110 java.util.Vector jobPanes = null;
112 private boolean serviceCanMergeResults = false;
114 private boolean viewResultsImmediatly = true;
120 * to indicate if results will be shown in a new window as soon as
121 * they are available.
123 public boolean isViewResultsImmediatly()
125 return viewResultsImmediatly;
132 * to indicate if results will be shown in a new window as soon as
133 * they are available.
135 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
137 this.viewResultsImmediatly = viewResultsImmediatly;
140 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
143 // Copied blatantly from
144 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
145 StyleSheet myStyleSheet = new StyleSheet();
147 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
149 editorKit.setStyleSheet(myStyleSheet);
152 * Set the style sheet rules here by reading them from the constants
156 * for (int ix=0; ix<CSS_RULES.length; ix++) {
158 * myStyleSheet.addRule(CSS_RULES[ix]);
167 public synchronized int addJobPane()
169 JScrollPane jobpane = new JScrollPane();
170 JComponent _progressText;
173 JEditorPane progressText = new JEditorPane("text/html", "");
174 progressText.addHyperlinkListener(this);
175 _progressText = progressText;
176 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
177 // progressText.setBorder(null);
178 progressText.setEditable(false);
180 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
182 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
184 * HTMLDocument tipDocument = (HTMLDocument)
185 * (myEditorKit.createDefaultDocument());
187 * progressText.setDocument(tipDocument);
188 */progressText.setText("<html><h1>WS Job</h1></html>");
192 JTextArea progressText = new JTextArea();
193 _progressText = progressText;
195 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
196 progressText.setBorder(null);
197 progressText.setEditable(false);
198 progressText.setText("WS Job");
199 progressText.setLineWrap(true);
200 progressText.setWrapStyleWord(true);
202 jobpane.setName("JobPane");
203 jobpane.getViewport().add(_progressText, null);
204 jobpane.setBorder(null);
205 if (jobPanes == null)
207 jobPanes = new Vector();
209 int newpane = jobPanes.size();
210 jobPanes.add(jobpane);
214 this.add(jobpane, BorderLayout.CENTER);
220 // revert to a tabbed pane.
221 JScrollPane firstpane;
222 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
223 subjobs = new JTabbedPane();
224 this.add(subjobs, BorderLayout.CENTER);
225 subjobs.add(firstpane);
226 subjobs.setTitleAt(0, firstpane.getName());
228 subjobs.add(jobpane);
230 return newpane; // index for accessor methods below
234 * Creates a new WebserviceInfo object.
237 * short name and job type
239 * reference or other human readable description
241 * true to display the webservices window immediatly (otherwise need
242 * to call setVisible(true))
244 public WebserviceInfo(String title, String info, boolean makeVisible)
246 init(title, info, 520, 500, makeVisible);
250 * Creates a new WebserviceInfo object.
261 public WebserviceInfo(String title, String info, int width, int height,
264 init(title, info, width, height, makeVisible);
270 * @return DOCUMENT ME!
272 public jalview.ws.WSClientI getthisService()
278 * Update state of GUI based on client capabilities (like whether the job is
279 * cancellable, whether the 'merge results' button is shown.
282 * service client to query for capabilities
284 public void setthisService(jalview.ws.WSClientI newservice)
286 thisService = newservice;
287 serviceIsCancellable = newservice.isCancellable();
288 frame.setClosable(!serviceIsCancellable);
289 serviceCanMergeResults = newservice.canMergeResults();
290 rebuildButtonPanel();
293 private void rebuildButtonPanel()
295 if (buttonPanel != null)
297 buttonPanel.removeAll();
298 if (serviceIsCancellable)
300 buttonPanel.add(cancel);
301 frame.setClosable(false);
305 frame.setClosable(true);
322 void init(String title, String info, int width, int height,
325 frame = new JInternalFrame();
326 frame.setFrameIcon(null);
327 frame.setContentPane(this);
328 Desktop.addInternalFrame(frame, title, makeVisible, width, height);
329 frame.setClosable(false);
331 progressBar = new ProgressBar(statusPanel, statusBar);
336 image = ChannelProperties.getImage("rotatable_logo.48");
338 MediaTracker mt = new MediaTracker(this);
339 mt.addImage(image, 0);
344 } catch (Exception ex)
348 AnimatedPanel ap = new AnimatedPanel();
349 ap.setPreferredSize(new Dimension(60, 60));
350 titlePanel.add(ap, BorderLayout.WEST);
351 titlePanel.add(titleText, BorderLayout.CENTER);
352 setStatus(currentStatus);
354 Thread thread = new Thread(ap);
356 final WebserviceInfo thisinfo = this;
357 frame.addInternalFrameListener(new InternalFrameAdapter()
360 public void internalFrameClosed(InternalFrameEvent evt)
362 // jalview.bin.Console.outPrintln("Shutting down webservice client");
363 WSClientI service = thisinfo.getthisService();
364 if (service != null && service.isCancellable())
378 * integer status from state constants
380 public void setStatus(int status)
382 currentStatus = status;
384 String message = null;
385 switch (currentStatus)
388 message = MessageManager.getString("label.state_queueing");
392 message = MessageManager.getString("label.state_running");
395 case STATE_STOPPED_OK:
396 message = MessageManager.getString("label.state_completed");
399 case STATE_CANCELLED_OK:
400 message = MessageManager.getString("label.state_job_cancelled");
403 case STATE_STOPPED_ERROR:
404 message = MessageManager.getString("label.state_job_error");
407 case STATE_STOPPED_SERVERERROR:
408 message = MessageManager.getString("label.server_error_try_later");
411 titleText.setText(title + (message == null ? "" : " - " + message));
416 * subjob status indicator
421 public void setStatus(int jobpane, int status)
423 if (jobpane < 0 || jobpane >= jobPanes.size())
425 throw new Error(MessageManager.formatMessage(
426 "error.setstatus_called_non_existent_job_pane", new String[]
427 { Integer.valueOf(jobpane).toString() }));
432 setProgressName(jobpane + " - QUEUED", jobpane);
435 setProgressName(jobpane + " - RUNNING", jobpane);
437 case STATE_STOPPED_OK:
438 setProgressName(jobpane + " - FINISHED", jobpane);
440 case STATE_CANCELLED_OK:
441 setProgressName(jobpane + " - CANCELLED", jobpane);
443 case STATE_STOPPED_ERROR:
444 setProgressName(jobpane + " - BROKEN", jobpane);
446 case STATE_STOPPED_SERVERERROR:
447 setProgressName(jobpane + " - ALERT", jobpane);
450 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
457 * @return DOCUMENT ME!
459 public String getInfoText()
461 return infoText.getText();
470 public void setInfoText(String text)
472 infoText.setText(text);
481 public void appendInfoText(String text)
483 infoText.append(text);
489 * @return DOCUMENT ME!
491 public String getProgressText(int which)
493 if (jobPanes == null)
499 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
500 .getViewport().getComponent(0)).getText();
504 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
505 .getComponent(0)).getText();
515 public void setProgressText(int which, String text)
517 if (jobPanes == null)
523 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
524 .getComponent(0)).setText(ensureHtmlTagged(text));
528 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
529 .getComponent(0)).setText(text);
534 * extract content from <body> content </body>
538 * - set to leave the initial html tag intact
540 * - set to leave the final html tag intact
543 private String getHtmlFragment(String text, boolean leaveFirst,
550 String lowertxt = text.toLowerCase(Locale.ROOT);
551 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
553 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
554 int htmlpose = lowertxt.indexOf(">", htmlpos),
555 htmlende = lowertxt.indexOf(">", htmlend);
556 if (htmlend == -1 && htmlpos == -1)
562 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
564 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
568 * very simple routine for adding/ensuring html tags are present in text.
571 * @return properly html tag enclosed text
573 private String ensureHtmlTagged(String text)
579 String lowertxt = text.toLowerCase(Locale.ROOT);
580 int htmlpos = lowertxt.indexOf("<body");
581 int htmlend = lowertxt.indexOf("</body");
582 int doctype = lowertxt.indexOf("<!doctype");
583 int xmltype = lowertxt.indexOf("<?xml");
586 text = text + "</body></html>";
590 if ((doctype > -1 && htmlpos > doctype)
591 || (xmltype > -1 && htmlpos > xmltype))
593 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
598 text = "<html><head></head><body>\n" + text;
600 if (text.indexOf("<meta") > -1)
603 .println("HTML COntent: \n" + text + "<< END HTML CONTENT\n");
615 public void appendProgressText(int which, String text)
617 if (jobPanes == null)
623 String txt = getHtmlFragment(
624 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
625 .getViewport().getComponent(0)).getText(),
627 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
629 .setText(ensureHtmlTagged(
630 txt + getHtmlFragment(text, false, true)));
634 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
635 .getComponent(0)).append(text);
640 * setProgressText(0, text)
642 public void setProgressText(String text)
644 setProgressText(0, text);
648 * appendProgressText(0, text)
650 public void appendProgressText(String text)
652 appendProgressText(0, text);
658 public String getProgressText()
660 return getProgressText(0);
664 * get the tab title for a subjob
670 public String getProgressName(int which)
672 if (jobPanes == null)
678 return subjobs.getTitleAt(which);
682 return ((JScrollPane) jobPanes.get(which)).getViewport()
683 .getComponent(0).getName();
688 * set the tab title for a subjob
695 public void setProgressName(String name, int which)
699 subjobs.setTitleAt(which, name);
700 subjobs.revalidate();
703 JScrollPane c = (JScrollPane) jobPanes.get(which);
704 c.getViewport().getComponent(0).setName(name);
709 * Gui action for cancelling the current job, if possible.
715 protected void cancel_actionPerformed(ActionEvent e)
717 if (!serviceIsCancellable)
719 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
720 // anyhow - it has to stop threads and clean up
721 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
724 MessageManager.getString(
725 "warn.job_cannot_be_cancelled_close_window"),
726 MessageManager.getString("action.cancel_job"));
730 thisService.cancelJob();
732 frame.setClosable(true);
736 * Spawns a thread that pops up a warning dialog box with the given message
742 public void warnUser(final String message, final String title)
744 javax.swing.SwingUtilities.invokeLater(new Runnable()
749 JvOptionPane.showInternalMessageDialog(Desktop.desktop, message,
750 title, JvOptionPane.WARNING_MESSAGE);
757 * Set up GUI for user to get at results - and possibly automatically display
758 * them if viewResultsImmediatly is set.
760 public void setResultsReady()
762 frame.setClosable(true);
763 buttonPanel.remove(cancel);
764 buttonPanel.add(showResultsNewFrame);
765 if (serviceCanMergeResults)
767 buttonPanel.add(mergeResults);
768 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
770 buttonPanel.validate();
772 if (viewResultsImmediatly)
774 showResultsNewFrame.doClick();
779 * called when job has finished but no result objects can be passed back to
782 public void setFinishedNoResults()
784 frame.setClosable(true);
785 buttonPanel.remove(cancel);
786 buttonPanel.validate();
790 class AnimatedPanel extends JPanel implements Runnable
794 BufferedImage offscreen;
799 startTime = System.currentTimeMillis();
801 float invSpeed = 15f;
803 while (currentStatus < STATE_STOPPED_OK)
805 if (currentStatus == STATE_QUEUING)
810 else if (currentStatus == STATE_RUNNING)
813 factor = (float) (0.5 + 1.5
814 * (0.5 - (0.5 * Math.sin(3.14159 / 180 * (angle + 45)))));
820 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
821 angle += delta * factor;
823 startTime = System.currentTimeMillis();
825 if (currentStatus >= STATE_STOPPED_OK)
832 } catch (Exception ex)
837 cancel.setEnabled(false);
842 startTime = System.currentTimeMillis();
852 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
853 angle += delta * factor;
854 startTime = System.currentTimeMillis();
862 } catch (Exception ex)
871 if (offscreen == null || offscreen.getWidth(this) != getWidth()
872 || offscreen.getHeight(this) != getHeight())
874 offscreen = new BufferedImage(getWidth(), getHeight(),
875 BufferedImage.TYPE_INT_RGB);
878 Graphics2D g = (Graphics2D) offscreen.getGraphics();
880 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
881 RenderingHints.VALUE_ANTIALIAS_ON);
882 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
883 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
884 g.setRenderingHint(RenderingHints.KEY_RENDERING,
885 RenderingHints.VALUE_RENDER_QUALITY);
887 g.setColor(Color.white);
888 g.fillRect(0, 0, getWidth(), getHeight());
892 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
893 g.rotate(3.14159 / 180 * (angle), x, y);
894 g.drawImage(image, 0, 0, this);
895 g.rotate(-3.14159 / 180 * (angle), x, y);
900 public void paintComponent(Graphics g1)
904 g1.drawImage(offscreen, 0, 0, this);
908 boolean renderAsHtml = false;
910 public void setRenderAsHtml(boolean b)
916 public void hyperlinkUpdate(HyperlinkEvent e)
918 Desktop.hyperlinkUpdate(e);
924 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
927 public void setProgressBar(String message, long id)
929 progressBar.setProgressBar(message, id);
933 public void registerHandler(final long id,
934 final IProgressIndicatorHandler handler)
936 progressBar.registerHandler(id, handler);
941 * @return true if any progress bars are still active
944 public boolean operationInProgress()
946 return progressBar.operationInProgress();