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.util.ChannelProperties;
27 import jalview.ws.WSClientI;
29 import java.awt.BorderLayout;
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.GridLayout;
35 import java.awt.Image;
36 import java.awt.MediaTracker;
37 import java.awt.RenderingHints;
38 import java.awt.event.ActionEvent;
39 import java.awt.image.BufferedImage;
40 import java.util.Locale;
41 import java.util.Vector;
43 import javax.swing.JComponent;
44 import javax.swing.JEditorPane;
45 import javax.swing.JInternalFrame;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JTabbedPane;
49 import javax.swing.JTextArea;
50 import javax.swing.event.HyperlinkEvent;
51 import javax.swing.event.HyperlinkListener;
52 import javax.swing.event.InternalFrameAdapter;
53 import javax.swing.event.InternalFrameEvent;
54 import javax.swing.text.html.HTMLEditorKit;
55 import javax.swing.text.html.StyleSheet;
58 * Base class for web service client thread and gui TODO: create StAX parser to
59 * extract html body content reliably when preparing html formatted job statuses
64 public class WebserviceInfo extends GWebserviceInfo
65 implements HyperlinkListener, IProgressIndicator
69 public static final int STATE_QUEUING = 0;
72 public static final int STATE_RUNNING = 1;
74 /** Job has finished with no errors */
75 public static final int STATE_STOPPED_OK = 2;
77 /** Job has been cancelled with no errors */
78 public static final int STATE_CANCELLED_OK = 3;
80 /** job has stopped because of some error */
81 public static final int STATE_STOPPED_ERROR = 4;
83 /** job has failed because of some unavoidable service interruption */
84 public static final int STATE_STOPPED_SERVERERROR = 5;
86 int currentStatus = STATE_QUEUING;
94 jalview.ws.WSClientI thisService;
96 boolean serviceIsCancellable;
100 private IProgressIndicator progressBar;
103 public void setVisible(boolean aFlag)
105 super.setVisible(aFlag);
106 frame.setVisible(aFlag);
109 JTabbedPane subjobs = null;
111 java.util.Vector jobPanes = null;
113 private boolean serviceCanMergeResults = false;
115 private boolean viewResultsImmediatly = true;
121 * to indicate if results will be shown in a new window as soon as
122 * they are available.
124 public boolean isViewResultsImmediatly()
126 return viewResultsImmediatly;
133 * to indicate if results will be shown in a new window as soon as
134 * they are available.
136 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
138 this.viewResultsImmediatly = viewResultsImmediatly;
141 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
144 // Copied blatantly from
145 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
146 StyleSheet myStyleSheet = new StyleSheet();
148 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
150 editorKit.setStyleSheet(myStyleSheet);
153 * Set the style sheet rules here by reading them from the constants
157 * for (int ix=0; ix<CSS_RULES.length; ix++) {
159 * myStyleSheet.addRule(CSS_RULES[ix]);
168 public synchronized int addJobPane()
170 JScrollPane jobpane = new JScrollPane();
171 JComponent _progressText;
174 JEditorPane progressText = new JEditorPane("text/html", "");
175 progressText.addHyperlinkListener(this);
176 _progressText = progressText;
177 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
178 // progressText.setBorder(null);
179 progressText.setEditable(false);
181 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
183 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
185 * HTMLDocument tipDocument = (HTMLDocument)
186 * (myEditorKit.createDefaultDocument());
188 * progressText.setDocument(tipDocument);
189 */progressText.setText("<html><h1>WS Job</h1></html>");
193 JTextArea progressText = new JTextArea();
194 _progressText = progressText;
196 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
197 progressText.setBorder(null);
198 progressText.setEditable(false);
199 progressText.setText("WS Job");
200 progressText.setLineWrap(true);
201 progressText.setWrapStyleWord(true);
203 jobpane.setName("JobPane");
204 jobpane.getViewport().add(_progressText, null);
205 jobpane.setBorder(null);
206 if (jobPanes == null)
208 jobPanes = new Vector();
210 int newpane = jobPanes.size();
211 jobPanes.add(jobpane);
215 this.add(jobpane, BorderLayout.CENTER);
221 // revert to a tabbed pane.
222 JScrollPane firstpane;
223 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
224 subjobs = new JTabbedPane();
225 this.add(subjobs, BorderLayout.CENTER);
226 subjobs.add(firstpane);
227 subjobs.setTitleAt(0, firstpane.getName());
229 subjobs.add(jobpane);
231 return newpane; // index for accessor methods below
235 * Creates a new WebserviceInfo object.
238 * short name and job type
240 * reference or other human readable description
242 * true to display the webservices window immediatly (otherwise need
243 * to call setVisible(true))
245 public WebserviceInfo(String title, String info, boolean makeVisible)
247 init(title, info, 520, 500, makeVisible);
251 * Creates a new WebserviceInfo object.
262 public WebserviceInfo(String title, String info, int width, int height,
266 init(title, info, width, height, makeVisible);
272 * @return DOCUMENT ME!
274 public jalview.ws.WSClientI getthisService()
280 * Update state of GUI based on client capabilities (like whether the job is
281 * cancellable, whether the 'merge results' button is shown.
284 * service client to query for capabilities
286 public void setthisService(jalview.ws.WSClientI newservice)
288 thisService = newservice;
289 serviceIsCancellable = newservice.isCancellable();
290 frame.setClosable(!serviceIsCancellable);
291 serviceCanMergeResults = newservice.canMergeResults();
292 rebuildButtonPanel();
295 private void rebuildButtonPanel()
297 if (buttonPanel != null)
299 buttonPanel.removeAll();
300 if (serviceIsCancellable)
302 buttonPanel.add(cancel);
303 frame.setClosable(false);
307 frame.setClosable(true);
324 void init(String title, String info, int width, int height,
327 frame = new JInternalFrame();
328 frame.setContentPane(this);
329 Desktop.addInternalFrame(frame, title, makeVisible, width, height, Desktop.FRAME_ALLOW_RESIZE, Desktop.FRAME_SET_MIN_SIZE_300);
330 frame.setClosable(false);
332 progressBar = new ProgressBar(statusPanel, statusBar);
337 image = ChannelProperties.getImage("rotatable_logo.48");
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);
355 if (!Platform.isJS())
357 // No animation for the moment//
358 Thread thread = new Thread(ap);
361 final WebserviceInfo thisinfo = this;
362 frame.addInternalFrameListener(
363 new InternalFrameAdapter()
366 public void internalFrameClosed(InternalFrameEvent evt)
368 // System.out.println("Shutting down webservice client");
369 WSClientI service = thisinfo.getthisService();
370 if (service != null && service.isCancellable())
384 * integer status from state constants
386 public void setStatus(int status)
388 currentStatus = status;
390 String message = null;
391 switch (currentStatus)
394 message = MessageManager.getString("label.state_queueing");
398 message = MessageManager.getString("label.state_running");
401 case STATE_STOPPED_OK:
402 message = MessageManager.getString("label.state_completed");
405 case STATE_CANCELLED_OK:
406 message = MessageManager.getString("label.state_job_cancelled");
409 case STATE_STOPPED_ERROR:
410 message = MessageManager.getString("label.state_job_error");
413 case STATE_STOPPED_SERVERERROR:
414 message = MessageManager.getString("label.server_error_try_later");
417 titleText.setText(title + (message == null ? "" : " - " + message));
422 * subjob status indicator
427 public void setStatus(int jobpane, int status)
429 if (jobpane < 0 || jobpane >= jobPanes.size())
431 throw new Error(MessageManager.formatMessage(
432 "error.setstatus_called_non_existent_job_pane", new String[]
433 { Integer.valueOf(jobpane).toString() }));
438 setProgressName(jobpane + " - QUEUED", jobpane);
441 setProgressName(jobpane + " - RUNNING", jobpane);
443 case STATE_STOPPED_OK:
444 setProgressName(jobpane + " - FINISHED", jobpane);
446 case STATE_CANCELLED_OK:
447 setProgressName(jobpane + " - CANCELLED", jobpane);
449 case STATE_STOPPED_ERROR:
450 setProgressName(jobpane + " - BROKEN", jobpane);
452 case STATE_STOPPED_SERVERERROR:
453 setProgressName(jobpane + " - ALERT", jobpane);
456 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
463 * @return DOCUMENT ME!
465 public String getInfoText()
467 return infoText.getText();
476 public void setInfoText(String text)
478 infoText.setText(text);
487 public void appendInfoText(String text)
489 infoText.append(text);
495 * @return DOCUMENT ME!
497 public String getProgressText(int which)
499 if (jobPanes == null)
505 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
506 .getViewport().getComponent(0)).getText();
510 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
511 .getComponent(0)).getText();
521 public void setProgressText(int which, String text)
523 if (jobPanes == null)
529 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
530 .getComponent(0)).setText(ensureHtmlTagged(text));
534 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
535 .getComponent(0)).setText(text);
540 * extract content from <body> content </body>
544 * - set to leave the initial html tag intact
546 * - set to leave the final html tag intact
549 private String getHtmlFragment(String text, boolean leaveFirst,
556 String lowertxt = text.toLowerCase(Locale.ROOT);
557 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
559 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
560 int htmlpose = lowertxt.indexOf(">", htmlpos),
561 htmlende = lowertxt.indexOf(">", htmlend);
562 if (htmlend == -1 && htmlpos == -1)
568 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
570 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
574 * very simple routine for adding/ensuring html tags are present in text.
577 * @return properly html tag enclosed text
579 private String ensureHtmlTagged(String text)
585 String lowertxt = text.toLowerCase(Locale.ROOT);
586 int htmlpos = lowertxt.indexOf("<body");
587 int htmlend = lowertxt.indexOf("</body");
588 int doctype = lowertxt.indexOf("<!doctype");
589 int xmltype = lowertxt.indexOf("<?xml");
592 text = text + "</body></html>";
596 if ((doctype > -1 && htmlpos > doctype)
597 || (xmltype > -1 && htmlpos > xmltype))
599 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
604 text = "<html><head></head><body>\n" + text;
606 if (text.indexOf("<meta") > -1)
609 .println("HTML COntent: \n" + text + "<< END HTML CONTENT\n");
621 public void appendProgressText(int which, String text)
623 if (jobPanes == null)
629 String txt = getHtmlFragment(
630 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
631 .getViewport().getComponent(0)).getText(),
633 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
635 .setText(ensureHtmlTagged(
636 txt + getHtmlFragment(text, false, true)));
640 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
641 .getComponent(0)).append(text);
646 * setProgressText(0, text)
648 public void setProgressText(String text)
650 setProgressText(0, text);
654 * appendProgressText(0, text)
656 public void appendProgressText(String text)
658 appendProgressText(0, text);
664 public String getProgressText()
666 return getProgressText(0);
670 * get the tab title for a subjob
676 public String getProgressName(int which)
678 if (jobPanes == null)
684 return subjobs.getTitleAt(which);
688 return ((JScrollPane) jobPanes.get(which)).getViewport()
689 .getComponent(0).getName();
694 * set the tab title for a subjob
701 public void setProgressName(String name, int which)
705 subjobs.setTitleAt(which, name);
706 subjobs.revalidate();
709 JScrollPane c = (JScrollPane) jobPanes.get(which);
710 c.getViewport().getComponent(0).setName(name);
715 * Gui action for cancelling the current job, if possible.
721 protected void cancel_actionPerformed(ActionEvent e)
723 if (!serviceIsCancellable)
725 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
726 // anyhow - it has to stop threads and clean up
727 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
730 MessageManager.getString(
731 "warn.job_cannot_be_cancelled_close_window"),
732 MessageManager.getString("action.cancel_job"));
736 thisService.cancelJob();
738 frame.setClosable(true);
742 * Spawns a thread that pops up a warning dialog box with the given message
748 public void warnUser(final String message, final String title)
750 javax.swing.SwingUtilities.invokeLater(new Runnable()
755 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(), message,
756 title, JvOptionPane.WARNING_MESSAGE);
763 * Set up GUI for user to get at results - and possibly automatically display
764 * them if viewResultsImmediatly is set.
766 public void setResultsReady()
768 frame.setClosable(true);
769 buttonPanel.remove(cancel);
770 buttonPanel.add(showResultsNewFrame);
771 if (serviceCanMergeResults)
773 buttonPanel.add(mergeResults);
774 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
776 buttonPanel.validate();
778 if (viewResultsImmediatly)
780 showResultsNewFrame.doClick();
785 * called when job has finished but no result objects can be passed back to
788 public void setFinishedNoResults()
790 frame.setClosable(true);
791 buttonPanel.remove(cancel);
792 buttonPanel.validate();
796 class AnimatedPanel extends JPanel implements Runnable
800 BufferedImage offscreen;
805 startTime = System.currentTimeMillis();
807 float invSpeed = 15f;
809 while (currentStatus < STATE_STOPPED_OK)
811 if (currentStatus == STATE_QUEUING)
816 else if (currentStatus == STATE_RUNNING)
819 factor = (float) (0.5 + 1.5
820 * (0.5 - (0.5 * Math.sin(3.14159 / 180 * (angle + 45)))));
826 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
827 angle += delta * factor;
829 startTime = System.currentTimeMillis();
831 if (currentStatus >= STATE_STOPPED_OK)
838 } catch (Exception ex)
843 cancel.setEnabled(false);
848 startTime = System.currentTimeMillis();
858 float delta = (System.currentTimeMillis() - startTime) / invSpeed;
859 angle += delta * factor;
860 startTime = System.currentTimeMillis();
868 } catch (Exception ex)
877 if (offscreen == null || offscreen.getWidth(this) != getWidth()
878 || offscreen.getHeight(this) != getHeight())
880 offscreen = new BufferedImage(getWidth(), getHeight(),
881 BufferedImage.TYPE_INT_RGB);
884 Graphics2D g = (Graphics2D) offscreen.getGraphics();
886 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
887 RenderingHints.VALUE_ANTIALIAS_ON);
888 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
889 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
890 g.setRenderingHint(RenderingHints.KEY_RENDERING,
891 RenderingHints.VALUE_RENDER_QUALITY);
893 g.setColor(Color.white);
894 g.fillRect(0, 0, getWidth(), getHeight());
898 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
899 g.rotate(3.14159 / 180 * (angle), x, y);
900 g.drawImage(image, 0, 0, this);
901 g.rotate(-3.14159 / 180 * (angle), x, y);
906 public void paintComponent(Graphics g1)
910 g1.drawImage(offscreen, 0, 0, this);
914 boolean renderAsHtml = false;
916 public void setRenderAsHtml(boolean b)
922 public void hyperlinkUpdate(HyperlinkEvent e)
924 Desktop.hyperlinkUpdate(e);
930 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
933 public void setProgressBar(String message, long id)
935 progressBar.setProgressBar(message, id);
939 public void addProgressBar(long id, String message)
941 progressBar.addProgressBar(id, message);
945 public void removeProgressBar(long id)
947 progressBar.removeProgressBar(id);
951 public void registerHandler(final long id,
952 final IProgressIndicatorHandler handler)
954 progressBar.registerHandler(id, handler);
959 * @return true if any progress bars are still active
962 public boolean operationInProgress()
964 return progressBar.operationInProgress();