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.Vector;
36 import javax.swing.JComponent;
37 import javax.swing.JEditorPane;
38 import javax.swing.JInternalFrame;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.JTabbedPane;
42 import javax.swing.JTextArea;
43 import javax.swing.event.HyperlinkEvent;
44 import javax.swing.event.HyperlinkListener;
45 import javax.swing.event.InternalFrameAdapter;
46 import javax.swing.event.InternalFrameEvent;
47 import javax.swing.text.html.HTMLEditorKit;
48 import javax.swing.text.html.StyleSheet;
50 import jalview.jbgui.GWebserviceInfo;
51 import jalview.util.ChannelProperties;
52 import jalview.util.MessageManager;
53 import jalview.ws.WSClientI;
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,
263 init(title, info, width, height, makeVisible);
269 * @return DOCUMENT ME!
271 public jalview.ws.WSClientI getthisService()
277 * Update state of GUI based on client capabilities (like whether the job is
278 * cancellable, whether the 'merge results' button is shown.
281 * service client to query for capabilities
283 public void setthisService(jalview.ws.WSClientI newservice)
285 thisService = newservice;
286 serviceIsCancellable = newservice.isCancellable();
287 frame.setClosable(!serviceIsCancellable);
288 serviceCanMergeResults = newservice.canMergeResults();
289 rebuildButtonPanel();
292 private void rebuildButtonPanel()
294 if (buttonPanel != null)
296 buttonPanel.removeAll();
297 if (serviceIsCancellable)
299 buttonPanel.add(cancel);
300 frame.setClosable(false);
304 frame.setClosable(true);
321 void init(String title, String info, int width, int height,
324 frame = new JInternalFrame();
325 frame.setContentPane(this);
326 Desktop.addInternalFrame(frame, title, makeVisible, width, height);
327 frame.setClosable(false);
329 progressBar = new ProgressBar(statusPanel, statusBar);
334 image = ChannelProperties.getImage("rotatable_logo.48");
336 MediaTracker mt = new MediaTracker(this);
337 mt.addImage(image, 0);
342 } catch (Exception ex)
346 AnimatedPanel ap = new AnimatedPanel();
347 ap.setPreferredSize(new Dimension(60, 60));
348 titlePanel.add(ap, BorderLayout.WEST);
349 titlePanel.add(titleText, BorderLayout.CENTER);
350 setStatus(currentStatus);
352 Thread thread = new Thread(ap);
354 final WebserviceInfo thisinfo = this;
355 frame.addInternalFrameListener(new InternalFrameAdapter()
358 public void internalFrameClosed(InternalFrameEvent evt)
360 // System.out.println("Shutting down webservice client");
361 WSClientI service = thisinfo.getthisService();
362 if (service != null && service.isCancellable())
376 * integer status from state constants
378 public void setStatus(int status)
380 currentStatus = status;
382 String message = null;
383 switch (currentStatus)
386 message = MessageManager.getString("label.state_queueing");
390 message = MessageManager.getString("label.state_running");
393 case STATE_STOPPED_OK:
394 message = MessageManager.getString("label.state_completed");
397 case STATE_CANCELLED_OK:
398 message = MessageManager.getString("label.state_job_cancelled");
401 case STATE_STOPPED_ERROR:
402 message = MessageManager.getString("label.state_job_error");
405 case STATE_STOPPED_SERVERERROR:
406 message = MessageManager.getString("label.server_error_try_later");
409 titleText.setText(title + (message == null ? "" : " - " + message));
414 * subjob status indicator
419 public void setStatus(int jobpane, int status)
421 if (jobpane < 0 || jobpane >= jobPanes.size())
423 throw new Error(MessageManager.formatMessage(
424 "error.setstatus_called_non_existent_job_pane", new String[]
425 { Integer.valueOf(jobpane).toString() }));
430 setProgressName(jobpane + " - QUEUED", jobpane);
433 setProgressName(jobpane + " - RUNNING", jobpane);
435 case STATE_STOPPED_OK:
436 setProgressName(jobpane + " - FINISHED", jobpane);
438 case STATE_CANCELLED_OK:
439 setProgressName(jobpane + " - CANCELLED", jobpane);
441 case STATE_STOPPED_ERROR:
442 setProgressName(jobpane + " - BROKEN", jobpane);
444 case STATE_STOPPED_SERVERERROR:
445 setProgressName(jobpane + " - ALERT", jobpane);
448 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
455 * @return DOCUMENT ME!
457 public String getInfoText()
459 return infoText.getText();
468 public void setInfoText(String text)
470 infoText.setText(text);
479 public void appendInfoText(String text)
481 infoText.append(text);
487 * @return DOCUMENT ME!
489 public String getProgressText(int which)
491 if (jobPanes == null)
497 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
498 .getViewport().getComponent(0)).getText();
502 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
503 .getComponent(0)).getText();
513 public void setProgressText(int which, String text)
515 if (jobPanes == null)
521 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
522 .getComponent(0)).setText(ensureHtmlTagged(text));
526 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
527 .getComponent(0)).setText(text);
532 * extract content from <body> content </body>
536 * - set to leave the initial html tag intact
538 * - set to leave the final html tag intact
541 private String getHtmlFragment(String text, boolean leaveFirst,
548 String lowertxt = text.toLowerCase();
549 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
551 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
552 int htmlpose = lowertxt.indexOf(">", htmlpos),
553 htmlende = lowertxt.indexOf(">", htmlend);
554 if (htmlend == -1 && htmlpos == -1)
560 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
562 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
566 * very simple routine for adding/ensuring html tags are present in text.
569 * @return properly html tag enclosed text
571 private String ensureHtmlTagged(String text)
577 String lowertxt = text.toLowerCase();
578 int htmlpos = lowertxt.indexOf("<body");
579 int htmlend = lowertxt.indexOf("</body");
580 int doctype = lowertxt.indexOf("<!doctype");
581 int xmltype = lowertxt.indexOf("<?xml");
584 text = text + "</body></html>";
588 if ((doctype > -1 && htmlpos > doctype)
589 || (xmltype > -1 && htmlpos > xmltype))
591 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
596 text = "<html><head></head><body>\n" + text;
598 if (text.indexOf("<meta") > -1)
601 .println("HTML COntent: \n" + text + "<< END HTML CONTENT\n");
613 public void appendProgressText(int which, String text)
615 if (jobPanes == null)
621 String txt = getHtmlFragment(
622 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
623 .getViewport().getComponent(0)).getText(),
625 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
627 .setText(ensureHtmlTagged(
628 txt + getHtmlFragment(text, false, true)));
632 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
633 .getComponent(0)).append(text);
638 * setProgressText(0, text)
640 public void setProgressText(String text)
642 setProgressText(0, text);
646 * appendProgressText(0, text)
648 public void appendProgressText(String text)
650 appendProgressText(0, text);
656 public String getProgressText()
658 return getProgressText(0);
662 * get the tab title for a subjob
668 public String getProgressName(int which)
670 if (jobPanes == null)
676 return subjobs.getTitleAt(which);
680 return ((JScrollPane) jobPanes.get(which)).getViewport()
681 .getComponent(0).getName();
686 * set the tab title for a subjob
693 public void setProgressName(String name, int which)
697 subjobs.setTitleAt(which, name);
698 subjobs.revalidate();
701 JScrollPane c = (JScrollPane) jobPanes.get(which);
702 c.getViewport().getComponent(0).setName(name);
707 * Gui action for cancelling the current job, if possible.
713 protected void cancel_actionPerformed(ActionEvent e)
715 if (!serviceIsCancellable)
717 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
718 // anyhow - it has to stop threads and clean up
719 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
722 MessageManager.getString(
723 "warn.job_cannot_be_cancelled_close_window"),
724 MessageManager.getString("action.cancel_job"));
728 thisService.cancelJob();
730 frame.setClosable(true);
734 * Spawns a thread that pops up a warning dialog box with the given message
740 public void warnUser(final String message, final String title)
742 javax.swing.SwingUtilities.invokeLater(new Runnable()
747 JvOptionPane.showInternalMessageDialog(Desktop.desktop, message,
748 title, JvOptionPane.WARNING_MESSAGE);
755 * Set up GUI for user to get at results - and possibly automatically display
756 * them if viewResultsImmediatly is set.
758 public void setResultsReady()
760 frame.setClosable(true);
761 buttonPanel.remove(cancel);
762 buttonPanel.add(showResultsNewFrame);
763 if (serviceCanMergeResults)
765 buttonPanel.add(mergeResults);
766 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
768 buttonPanel.validate();
770 if (viewResultsImmediatly)
772 showResultsNewFrame.doClick();
777 * called when job has finished but no result objects can be passed back to
780 public void setFinishedNoResults()
782 frame.setClosable(true);
783 buttonPanel.remove(cancel);
784 buttonPanel.validate();
788 class AnimatedPanel extends JPanel implements Runnable
792 BufferedImage offscreen;
797 startTime = System.currentTimeMillis();
799 float invSpeed = 15f;
801 while (currentStatus < STATE_STOPPED_OK)
803 if (currentStatus == STATE_QUEUING)
808 else if (currentStatus == STATE_RUNNING)
811 factor = (float) (0.5 + 1.5
812 * (0.5 - (0.5 * Math.sin(3.14159 / 180 * (angle + 45)))));
818 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
819 angle += delta * factor;
821 startTime = System.currentTimeMillis();
823 if (currentStatus >= STATE_STOPPED_OK)
830 } catch (Exception ex)
835 cancel.setEnabled(false);
840 startTime = System.currentTimeMillis();
850 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
851 angle += delta * factor;
852 startTime = System.currentTimeMillis();
860 } catch (Exception ex)
869 if (offscreen == null || offscreen.getWidth(this) != getWidth()
870 || offscreen.getHeight(this) != getHeight())
872 offscreen = new BufferedImage(getWidth(), getHeight(),
873 BufferedImage.TYPE_INT_RGB);
876 Graphics2D g = (Graphics2D) offscreen.getGraphics();
878 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
879 RenderingHints.VALUE_ANTIALIAS_ON);
880 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
881 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
882 g.setRenderingHint(RenderingHints.KEY_RENDERING,
883 RenderingHints.VALUE_RENDER_QUALITY);
885 g.setColor(Color.white);
886 g.fillRect(0, 0, getWidth(), getHeight());
890 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
891 g.rotate(3.14159 / 180 * (angle), x, y);
892 g.drawImage(image, 0, 0, this);
893 g.rotate(-3.14159 / 180 * (angle), x, y);
898 public void paintComponent(Graphics g1)
902 g1.drawImage(offscreen, 0, 0, this);
906 boolean renderAsHtml = false;
908 public void setRenderAsHtml(boolean b)
914 public void hyperlinkUpdate(HyperlinkEvent e)
916 Desktop.hyperlinkUpdate(e);
922 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
925 public void setProgressBar(String message, long id)
927 progressBar.setProgressBar(message, id);
931 public void registerHandler(final long id,
932 final IProgressIndicatorHandler handler)
934 progressBar.registerHandler(id, handler);
939 * @return true if any progress bars are still active
942 public boolean operationInProgress()
944 return progressBar.operationInProgress();