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.util.Platform;
26 import jalview.ws.WSClientI;
28 import java.awt.BorderLayout;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics;
32 import java.awt.Graphics2D;
33 import java.awt.GridLayout;
34 import java.awt.Image;
35 import java.awt.MediaTracker;
36 import java.awt.RenderingHints;
37 import java.awt.event.ActionEvent;
38 import java.awt.image.BufferedImage;
39 import java.util.Vector;
41 import javax.swing.JComponent;
42 import javax.swing.JEditorPane;
43 import javax.swing.JInternalFrame;
44 import javax.swing.JPanel;
45 import javax.swing.JScrollPane;
46 import javax.swing.JTabbedPane;
47 import javax.swing.JTextArea;
48 import javax.swing.event.HyperlinkEvent;
49 import javax.swing.event.HyperlinkListener;
50 import javax.swing.event.InternalFrameAdapter;
51 import javax.swing.event.InternalFrameEvent;
52 import javax.swing.text.html.HTMLEditorKit;
53 import javax.swing.text.html.StyleSheet;
56 * Base class for web service client thread and gui TODO: create StAX parser to
57 * extract html body content reliably when preparing html formatted job statuses
62 public class WebserviceInfo extends GWebserviceInfo
63 implements HyperlinkListener, IProgressIndicator
67 public static final int STATE_QUEUING = 0;
70 public static final int STATE_RUNNING = 1;
72 /** Job has finished with no errors */
73 public static final int STATE_STOPPED_OK = 2;
75 /** Job has been cancelled with no errors */
76 public static final int STATE_CANCELLED_OK = 3;
78 /** job has stopped because of some error */
79 public static final int STATE_STOPPED_ERROR = 4;
81 /** job has failed because of some unavoidable service interruption */
82 public static final int STATE_STOPPED_SERVERERROR = 5;
84 int currentStatus = STATE_QUEUING;
92 jalview.ws.WSClientI thisService;
94 boolean serviceIsCancellable;
98 private IProgressIndicator progressBar;
101 public void setVisible(boolean aFlag)
103 super.setVisible(aFlag);
104 frame.setVisible(aFlag);
107 JTabbedPane subjobs = null;
109 java.util.Vector jobPanes = null;
111 private boolean serviceCanMergeResults = false;
113 private boolean viewResultsImmediatly = true;
119 * to indicate if results will be shown in a new window as soon as
120 * they are available.
122 public boolean isViewResultsImmediatly()
124 return viewResultsImmediatly;
131 * to indicate if results will be shown in a new window as soon as
132 * they are available.
134 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
136 this.viewResultsImmediatly = viewResultsImmediatly;
139 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
142 // Copied blatantly from
143 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
144 StyleSheet myStyleSheet = new StyleSheet();
146 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
148 editorKit.setStyleSheet(myStyleSheet);
151 * Set the style sheet rules here by reading them from the constants
155 * for (int ix=0; ix<CSS_RULES.length; ix++) {
157 * myStyleSheet.addRule(CSS_RULES[ix]);
166 public synchronized int addJobPane()
168 JScrollPane jobpane = new JScrollPane();
169 JComponent _progressText;
172 JEditorPane progressText = new JEditorPane("text/html", "");
173 progressText.addHyperlinkListener(this);
174 _progressText = progressText;
175 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
176 // progressText.setBorder(null);
177 progressText.setEditable(false);
179 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
181 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
183 * HTMLDocument tipDocument = (HTMLDocument)
184 * (myEditorKit.createDefaultDocument());
186 * progressText.setDocument(tipDocument);
187 */progressText.setText("<html><h1>WS Job</h1></html>");
191 JTextArea progressText = new JTextArea();
192 _progressText = progressText;
194 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
195 progressText.setBorder(null);
196 progressText.setEditable(false);
197 progressText.setText("WS Job");
198 progressText.setLineWrap(true);
199 progressText.setWrapStyleWord(true);
201 jobpane.setName("JobPane");
202 jobpane.getViewport().add(_progressText, null);
203 jobpane.setBorder(null);
204 if (jobPanes == null)
206 jobPanes = new Vector();
208 int newpane = jobPanes.size();
209 jobPanes.add(jobpane);
213 this.add(jobpane, BorderLayout.CENTER);
219 // revert to a tabbed pane.
220 JScrollPane firstpane;
221 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
222 subjobs = new JTabbedPane();
223 this.add(subjobs, BorderLayout.CENTER);
224 subjobs.add(firstpane);
225 subjobs.setTitleAt(0, firstpane.getName());
227 subjobs.add(jobpane);
229 return newpane; // index for accessor methods below
233 * Creates a new WebserviceInfo object.
236 * short name and job type
238 * reference or other human readable description
240 * true to display the webservices window immediatly (otherwise need
241 * to call setVisible(true))
243 public WebserviceInfo(String title, String info, boolean makeVisible)
245 init(title, info, 520, 500, makeVisible);
249 * Creates a new WebserviceInfo object.
260 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.setContentPane(this);
327 Desktop.addInternalFrame(frame, title, makeVisible, width, height, Desktop.FRAME_ALLOW_RESIZE, Desktop.FRAME_SET_MIN_SIZE_300);
328 frame.setClosable(false);
330 progressBar = new ProgressBar(statusPanel, statusBar);
335 java.net.URL url = getClass()
336 .getResource("/images/Jalview_Logo_small_with_border.png");
337 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
339 MediaTracker mt = new MediaTracker(this);
340 mt.addImage(image, 0);
345 } catch (Exception ex)
349 AnimatedPanel ap = new AnimatedPanel();
350 ap.setPreferredSize(new Dimension(60, 60));
351 titlePanel.add(ap, BorderLayout.WEST);
352 titlePanel.add(titleText, BorderLayout.CENTER);
353 setStatus(currentStatus);
354 if (!Platform.isJS())
356 // No animation for the moment//
357 Thread thread = new Thread(ap);
360 final WebserviceInfo thisinfo = this;
361 frame.addInternalFrameListener(
362 new InternalFrameAdapter()
365 public void internalFrameClosed(InternalFrameEvent evt)
367 // System.out.println("Shutting down webservice client");
368 WSClientI service = thisinfo.getthisService();
369 if (service != null && service.isCancellable())
383 * integer status from state constants
385 public void setStatus(int status)
387 currentStatus = status;
389 String message = null;
390 switch (currentStatus)
393 message = MessageManager.getString("label.state_queueing");
397 message = MessageManager.getString("label.state_running");
400 case STATE_STOPPED_OK:
401 message = MessageManager.getString("label.state_completed");
404 case STATE_CANCELLED_OK:
405 message = MessageManager.getString("label.state_job_cancelled");
408 case STATE_STOPPED_ERROR:
409 message = MessageManager.getString("label.state_job_error");
412 case STATE_STOPPED_SERVERERROR:
413 message = MessageManager.getString("label.server_error_try_later");
416 titleText.setText(title + (message == null ? "" : " - " + message));
421 * subjob status indicator
426 public void setStatus(int jobpane, int status)
428 if (jobpane < 0 || jobpane >= jobPanes.size())
430 throw new Error(MessageManager.formatMessage(
431 "error.setstatus_called_non_existent_job_pane", new String[]
432 { Integer.valueOf(jobpane).toString() }));
437 setProgressName(jobpane + " - QUEUED", jobpane);
440 setProgressName(jobpane + " - RUNNING", jobpane);
442 case STATE_STOPPED_OK:
443 setProgressName(jobpane + " - FINISHED", jobpane);
445 case STATE_CANCELLED_OK:
446 setProgressName(jobpane + " - CANCELLED", jobpane);
448 case STATE_STOPPED_ERROR:
449 setProgressName(jobpane + " - BROKEN", jobpane);
451 case STATE_STOPPED_SERVERERROR:
452 setProgressName(jobpane + " - ALERT", jobpane);
455 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
462 * @return DOCUMENT ME!
464 public String getInfoText()
466 return infoText.getText();
475 public void setInfoText(String text)
477 infoText.setText(text);
486 public void appendInfoText(String text)
488 infoText.append(text);
494 * @return DOCUMENT ME!
496 public String getProgressText(int which)
498 if (jobPanes == null)
504 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
505 .getViewport().getComponent(0)).getText();
509 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
510 .getComponent(0)).getText();
520 public void setProgressText(int which, String text)
522 if (jobPanes == null)
528 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
529 .getComponent(0)).setText(ensureHtmlTagged(text));
533 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
534 .getComponent(0)).setText(text);
539 * extract content from <body> content </body>
543 * - set to leave the initial html tag intact
545 * - set to leave the final html tag intact
548 private String getHtmlFragment(String text, boolean leaveFirst,
555 String lowertxt = text.toLowerCase();
556 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
558 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
559 int htmlpose = lowertxt.indexOf(">", htmlpos),
560 htmlende = lowertxt.indexOf(">", htmlend);
561 if (htmlend == -1 && htmlpos == -1)
567 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
569 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
573 * very simple routine for adding/ensuring html tags are present in text.
576 * @return properly html tag enclosed text
578 private String ensureHtmlTagged(String text)
584 String lowertxt = text.toLowerCase();
585 int htmlpos = lowertxt.indexOf("<body");
586 int htmlend = lowertxt.indexOf("</body");
587 int doctype = lowertxt.indexOf("<!doctype");
588 int xmltype = lowertxt.indexOf("<?xml");
591 text = text + "</body></html>";
595 if ((doctype > -1 && htmlpos > doctype)
596 || (xmltype > -1 && htmlpos > xmltype))
598 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
603 text = "<html><head></head><body>\n" + text;
605 if (text.indexOf("<meta") > -1)
608 .println("HTML COntent: \n" + text + "<< END HTML CONTENT\n");
620 public void appendProgressText(int which, String text)
622 if (jobPanes == null)
628 String txt = getHtmlFragment(
629 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
630 .getViewport().getComponent(0)).getText(),
632 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
634 .setText(ensureHtmlTagged(
635 txt + getHtmlFragment(text, false, true)));
639 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
640 .getComponent(0)).append(text);
645 * setProgressText(0, text)
647 public void setProgressText(String text)
649 setProgressText(0, text);
653 * appendProgressText(0, text)
655 public void appendProgressText(String text)
657 appendProgressText(0, text);
663 public String getProgressText()
665 return getProgressText(0);
669 * get the tab title for a subjob
675 public String getProgressName(int which)
677 if (jobPanes == null)
683 return subjobs.getTitleAt(which);
687 return ((JScrollPane) jobPanes.get(which)).getViewport()
688 .getComponent(0).getName();
693 * set the tab title for a subjob
700 public void setProgressName(String name, int which)
704 subjobs.setTitleAt(which, name);
705 subjobs.revalidate();
708 JScrollPane c = (JScrollPane) jobPanes.get(which);
709 c.getViewport().getComponent(0).setName(name);
714 * Gui action for cancelling the current job, if possible.
720 protected void cancel_actionPerformed(ActionEvent e)
722 if (!serviceIsCancellable)
724 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
725 // anyhow - it has to stop threads and clean up
726 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
729 MessageManager.getString(
730 "warn.job_cannot_be_cancelled_close_window"),
731 MessageManager.getString("action.cancel_job"));
735 thisService.cancelJob();
737 frame.setClosable(true);
741 * Spawns a thread that pops up a warning dialog box with the given message
747 public void warnUser(final String message, final String title)
749 javax.swing.SwingUtilities.invokeLater(new Runnable()
754 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(), message,
755 title, JvOptionPane.WARNING_MESSAGE);
762 * Set up GUI for user to get at results - and possibly automatically display
763 * them if viewResultsImmediatly is set.
765 public void setResultsReady()
767 frame.setClosable(true);
768 buttonPanel.remove(cancel);
769 buttonPanel.add(showResultsNewFrame);
770 if (serviceCanMergeResults)
772 buttonPanel.add(mergeResults);
773 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
775 buttonPanel.validate();
777 if (viewResultsImmediatly)
779 showResultsNewFrame.doClick();
784 * called when job has finished but no result objects can be passed back to
787 public void setFinishedNoResults()
789 frame.setClosable(true);
790 buttonPanel.remove(cancel);
791 buttonPanel.validate();
795 class AnimatedPanel extends JPanel implements Runnable
799 BufferedImage offscreen;
804 startTime = System.currentTimeMillis();
806 float invSpeed = 15f;
808 while (currentStatus < STATE_STOPPED_OK)
810 if (currentStatus == STATE_QUEUING)
815 else if (currentStatus == STATE_RUNNING)
818 factor = (float) (0.5 + 1.5
819 * (0.5 - (0.5 * Math.sin(3.14159 / 180 * (angle + 45)))));
825 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
826 angle += delta * factor;
828 startTime = System.currentTimeMillis();
830 if (currentStatus >= STATE_STOPPED_OK)
837 } catch (Exception ex)
842 cancel.setEnabled(false);
847 startTime = System.currentTimeMillis();
857 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
858 angle += delta * factor;
859 startTime = System.currentTimeMillis();
867 } catch (Exception ex)
876 if (offscreen == null || offscreen.getWidth(this) != getWidth()
877 || offscreen.getHeight(this) != getHeight())
879 offscreen = new BufferedImage(getWidth(), getHeight(),
880 BufferedImage.TYPE_INT_RGB);
883 Graphics2D g = (Graphics2D) offscreen.getGraphics();
885 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
886 RenderingHints.VALUE_ANTIALIAS_ON);
887 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
888 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
889 g.setRenderingHint(RenderingHints.KEY_RENDERING,
890 RenderingHints.VALUE_RENDER_QUALITY);
892 g.setColor(Color.white);
893 g.fillRect(0, 0, getWidth(), getHeight());
897 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
898 g.rotate(3.14159 / 180 * (angle), x, y);
899 g.drawImage(image, 0, 0, this);
900 g.rotate(-3.14159 / 180 * (angle), x, y);
905 public void paintComponent(Graphics g1)
909 g1.drawImage(offscreen, 0, 0, this);
913 boolean renderAsHtml = false;
915 public void setRenderAsHtml(boolean b)
921 public void hyperlinkUpdate(HyperlinkEvent e)
923 Desktop.hyperlinkUpdate(e);
929 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
932 public void setProgressBar(String message, long id)
934 progressBar.setProgressBar(message, id);
938 public void removeProgressBar(long id)
940 progressBar.removeProgressBar(id);
944 public void registerHandler(final long id,
945 final IProgressIndicatorHandler handler)
947 progressBar.registerHandler(id, handler);
952 * @return true if any progress bars are still active
955 public boolean operationInProgress()
957 return progressBar.operationInProgress();