2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
26 import java.awt.event.*;
27 import java.awt.image.*;
29 import javax.swing.event.HyperlinkEvent;
30 import javax.swing.event.HyperlinkListener;
31 import javax.swing.text.html.HTMLEditorKit;
32 import javax.swing.text.html.StyleSheet;
34 import jalview.jbgui.*;
35 import jalview.util.MessageManager;
36 import jalview.ws.WSClientI;
39 * Base class for web service client thread and gui TODO: create StAX parser to
40 * extract html body content reliably when preparing html formatted job statuses
45 public class WebserviceInfo extends GWebserviceInfo implements
46 HyperlinkListener, IProgressIndicator
50 public static final int STATE_QUEUING = 0;
53 public static final int STATE_RUNNING = 1;
55 /** Job has finished with no errors */
56 public static final int STATE_STOPPED_OK = 2;
58 /** Job has been cancelled with no errors */
59 public static final int STATE_CANCELLED_OK = 3;
61 /** job has stopped because of some error */
62 public static final int STATE_STOPPED_ERROR = 4;
64 /** job has failed because of some unavoidable service interruption */
65 public static final int STATE_STOPPED_SERVERERROR = 5;
67 int currentStatus = STATE_QUEUING;
75 jalview.ws.WSClientI thisService;
77 boolean serviceIsCancellable;
81 JTabbedPane subjobs = null;
83 java.util.Vector jobPanes = null;
85 private boolean serviceCanMergeResults = false;
87 private boolean viewResultsImmediatly = true;
93 * to indicate if results will be shown in a new window as soon as
96 public boolean isViewResultsImmediatly()
98 return viewResultsImmediatly;
105 * to indicate if results will be shown in a new window as soon as
106 * they are available.
108 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
110 this.viewResultsImmediatly = viewResultsImmediatly;
113 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
116 // Copied blatantly from
117 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
118 StyleSheet myStyleSheet = new StyleSheet();
120 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
122 editorKit.setStyleSheet(myStyleSheet);
125 * Set the style sheet rules here by reading them from the constants
129 * for (int ix=0; ix<CSS_RULES.length; ix++) {
131 * myStyleSheet.addRule(CSS_RULES[ix]);
140 public synchronized int addJobPane()
142 JScrollPane jobpane = new JScrollPane();
143 JComponent _progressText;
146 JEditorPane progressText = new JEditorPane("text/html", "");
147 progressText.addHyperlinkListener(this);
148 _progressText = progressText;
149 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
150 // progressText.setBorder(null);
151 progressText.setEditable(false);
153 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
155 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
157 * HTMLDocument tipDocument = (HTMLDocument)
158 * (myEditorKit.createDefaultDocument());
160 * progressText.setDocument(tipDocument);
161 */progressText.setText("<html><h1>WS Job</h1></html>");
165 JTextArea progressText = new JTextArea();
166 _progressText = progressText;
168 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
169 progressText.setBorder(null);
170 progressText.setEditable(false);
171 progressText.setText("WS Job");
172 progressText.setLineWrap(true);
173 progressText.setWrapStyleWord(true);
175 jobpane.setName("JobPane");
176 jobpane.getViewport().add(_progressText, null);
177 jobpane.setBorder(null);
178 if (jobPanes == null)
180 jobPanes = new Vector();
182 int newpane = jobPanes.size();
183 jobPanes.add(jobpane);
187 this.add(jobpane, BorderLayout.CENTER);
193 // revert to a tabbed pane.
194 JScrollPane firstpane;
195 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
196 subjobs = new JTabbedPane();
197 this.add(subjobs, BorderLayout.CENTER);
198 subjobs.add(firstpane);
199 subjobs.setTitleAt(0, firstpane.getName());
201 subjobs.add(jobpane);
203 return newpane; // index for accessor methods below
207 * Creates a new WebserviceInfo object.
210 * short name and job type
212 * reference or other human readable description
214 public WebserviceInfo(String title, String info)
216 init(title, info, 520, 500);
220 * Creates a new WebserviceInfo object.
231 public WebserviceInfo(String title, String info, int width, int height)
233 init(title, info, width, height);
239 * @return DOCUMENT ME!
241 public jalview.ws.WSClientI getthisService()
247 * Update state of GUI based on client capabilities (like whether the job is
248 * cancellable, whether the 'merge results' button is shown.
251 * service client to query for capabilities
253 public void setthisService(jalview.ws.WSClientI newservice)
255 thisService = newservice;
256 serviceIsCancellable = newservice.isCancellable();
257 frame.setClosable(!serviceIsCancellable);
258 serviceCanMergeResults = newservice.canMergeResults();
259 rebuildButtonPanel();
262 private void rebuildButtonPanel()
264 if (buttonPanel != null)
266 buttonPanel.removeAll();
267 if (serviceIsCancellable)
269 buttonPanel.add(cancel);
270 frame.setClosable(false);
274 frame.setClosable(true);
291 void init(String title, String info, int width, int height)
293 frame = new JInternalFrame();
294 frame.setContentPane(this);
295 Desktop.addInternalFrame(frame, title, width, height);
296 frame.setClosable(false);
301 java.net.URL url = getClass().getResource(
302 "/images/Jalview_Logo_small.png");
303 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
305 MediaTracker mt = new MediaTracker(this);
306 mt.addImage(image, 0);
311 } catch (Exception ex)
315 AnimatedPanel ap = new AnimatedPanel();
316 titlePanel.add(ap, BorderLayout.CENTER);
318 Thread thread = new Thread(ap);
320 final WebserviceInfo thisinfo = this;
321 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
323 public void internalFrameClosed(
324 javax.swing.event.InternalFrameEvent evt)
326 // System.out.println("Shutting down webservice client");
327 WSClientI service = thisinfo.getthisService();
328 if (service != null && service.isCancellable())
342 * integer status from state constants
344 public void setStatus(int status)
346 currentStatus = status;
350 * subjob status indicator
355 public void setStatus(int jobpane, int status)
357 if (jobpane < 0 || jobpane >= jobPanes.size())
359 throw new Error("setStatus called for non-existent job pane."
365 setProgressName(jobpane + " - QUEUED", jobpane);
368 setProgressName(jobpane + " - RUNNING", jobpane);
370 case STATE_STOPPED_OK:
371 setProgressName(jobpane + " - FINISHED", jobpane);
373 case STATE_CANCELLED_OK:
374 setProgressName(jobpane + " - CANCELLED", jobpane);
376 case STATE_STOPPED_ERROR:
377 setProgressName(jobpane + " - BROKEN", jobpane);
379 case STATE_STOPPED_SERVERERROR:
380 setProgressName(jobpane + " - ALERT", jobpane);
383 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
390 * @return DOCUMENT ME!
392 public String getInfoText()
394 return infoText.getText();
403 public void setInfoText(String text)
405 infoText.setText(text);
414 public void appendInfoText(String text)
416 infoText.append(text);
422 * @return DOCUMENT ME!
424 public String getProgressText(int which)
426 if (jobPanes == null)
432 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
433 .getViewport().getComponent(0)).getText();
437 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
438 .getComponent(0)).getText();
448 public void setProgressText(int which, String text)
450 if (jobPanes == null)
456 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
457 .getComponent(0)).setText(ensureHtmlTagged(text));
461 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
462 .getComponent(0)).setText(text);
467 * extract content from <body> content </body>
471 * - set to leave the initial html tag intact
473 * - set to leave the final html tag intact
476 private String getHtmlFragment(String text, boolean leaveFirst,
483 String lowertxt = text.toLowerCase();
484 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
486 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
487 int htmlpose = lowertxt.indexOf(">", htmlpos), htmlende = lowertxt
488 .indexOf(">", htmlend);
489 if (htmlend == -1 && htmlpos == -1)
495 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
497 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
501 * very simple routine for adding/ensuring html tags are present in text.
504 * @return properly html tag enclosed text
506 private String ensureHtmlTagged(String text)
512 String lowertxt = text.toLowerCase();
513 int htmlpos = lowertxt.indexOf("<body");
514 int htmlend = lowertxt.indexOf("</body");
515 int doctype = lowertxt.indexOf("<!doctype");
516 int xmltype = lowertxt.indexOf("<?xml");
519 text = text + "</body></html>";
523 if ((doctype > -1 && htmlpos > doctype)
524 || (xmltype > -1 && htmlpos > xmltype))
526 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
531 text = "<html><head></head><body>\n" + text;
533 if (text.indexOf("<meta") > -1)
535 System.err.println("HTML COntent: \n" + text
536 + "<< END HTML CONTENT\n");
548 public void appendProgressText(int which, String text)
550 if (jobPanes == null)
556 String txt = getHtmlFragment(
557 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
558 .getViewport().getComponent(0)).getText(), true,
560 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
561 .getComponent(0)).setText(ensureHtmlTagged(txt
562 + getHtmlFragment(text, false, true)));
566 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
567 .getComponent(0)).append(text);
572 * setProgressText(0, text)
574 public void setProgressText(String text)
576 setProgressText(0, text);
580 * appendProgressText(0, text)
582 public void appendProgressText(String text)
584 appendProgressText(0, text);
590 public String getProgressText()
592 return getProgressText(0);
596 * get the tab title for a subjob
602 public String getProgressName(int which)
604 if (jobPanes == null)
610 return subjobs.getTitleAt(which);
614 return ((JScrollPane) jobPanes.get(which)).getViewport()
615 .getComponent(0).getName();
620 * set the tab title for a subjob
627 public void setProgressName(String name, int which)
631 subjobs.setTitleAt(which, name);
632 subjobs.revalidate();
635 JScrollPane c = (JScrollPane) jobPanes.get(which);
636 c.getViewport().getComponent(0).setName(name);
641 * Gui action for cancelling the current job, if possible.
646 protected void cancel_actionPerformed(ActionEvent e)
648 if (!serviceIsCancellable)
650 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
651 // anyhow - it has to stop threads and clean up
652 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
654 warnUser("This job cannot be cancelled.\nJust close the window.",
659 thisService.cancelJob();
661 frame.setClosable(true);
665 * Spawns a thread that pops up a warning dialog box with the given message
671 public void warnUser(final String message, final String title)
673 javax.swing.SwingUtilities.invokeLater(new Runnable()
677 JOptionPane.showInternalMessageDialog(Desktop.desktop, message,
678 title, JOptionPane.WARNING_MESSAGE);
685 * Set up GUI for user to get at results - and possibly automatically display
686 * them if viewResultsImmediatly is set.
688 public void setResultsReady()
690 frame.setClosable(true);
691 buttonPanel.remove(cancel);
692 buttonPanel.add(showResultsNewFrame);
693 if (serviceCanMergeResults)
695 buttonPanel.add(mergeResults);
696 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
698 buttonPanel.validate();
700 if (viewResultsImmediatly)
702 showResultsNewFrame.doClick();
707 * called when job has finished but no result objects can be passed back to
710 public void setFinishedNoResults()
712 frame.setClosable(true);
713 buttonPanel.remove(cancel);
714 buttonPanel.validate();
718 class AnimatedPanel extends JPanel implements Runnable
722 BufferedImage offscreen;
726 startTime = System.currentTimeMillis();
728 while (currentStatus < STATE_STOPPED_OK)
734 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
737 startTime = System.currentTimeMillis();
739 if (currentStatus >= STATE_STOPPED_OK)
745 } catch (Exception ex)
750 cancel.setEnabled(false);
755 if (offscreen == null || offscreen.getWidth(this) != getWidth()
756 || offscreen.getHeight(this) != getHeight())
758 offscreen = new BufferedImage(getWidth(), getHeight(),
759 BufferedImage.TYPE_INT_ARGB);
762 Graphics2D g = (Graphics2D) offscreen.getGraphics();
764 g.setColor(Color.white);
765 g.fillRect(0, 0, getWidth(), getHeight());
767 g.setFont(new Font("Arial", Font.BOLD, 12));
768 g.setColor(Color.black);
770 switch (currentStatus)
774 title.concat(" - ").concat(
775 MessageManager.getString("label.state_queueing")),
782 title.concat(" - ").concat(
783 MessageManager.getString("label.state_running")),
788 case STATE_STOPPED_OK:
790 title.concat(" - ").concat(
791 MessageManager.getString("label.state_completed")),
796 case STATE_CANCELLED_OK:
798 title.concat(" - ").concat(
800 .getString("label.state_job_cancelled")),
805 case STATE_STOPPED_ERROR:
807 title.concat(" - ").concat(
808 MessageManager.getString("label.state_job_error")),
813 case STATE_STOPPED_SERVERERROR:
815 title.concat(" - ").concat(
817 .getString("label.server_error_try_later")),
825 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
826 g.rotate(Math.toRadians(angle), 10 + x, 10 + y);
827 g.drawImage(image, 10, 10, this);
828 g.rotate(-Math.toRadians(angle), 10 + x, 10 + y);
832 public void paintComponent(Graphics g1)
836 g1.drawImage(offscreen, 0, 0, this);
840 boolean renderAsHtml = false;
842 public void setRenderAsHtml(boolean b)
847 public void hyperlinkUpdate(HyperlinkEvent e)
849 Desktop.hyperlinkUpdate(e);
852 // methods for implementing IProgressIndicator
853 // need to refactor to a reusable stub class
854 Hashtable progressBars, progressBarHandlers;
859 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
862 public void setProgressBar(String message, long id)
864 if (progressBars == null)
866 progressBars = new Hashtable();
867 progressBarHandlers = new Hashtable();
870 JPanel progressPanel;
871 Long lId = new Long(id);
872 GridLayout layout = (GridLayout) statusPanel.getLayout();
873 if (progressBars.get(lId) != null)
875 progressPanel = (JPanel) progressBars.get(new Long(id));
876 statusPanel.remove(progressPanel);
877 progressBars.remove(lId);
878 progressPanel = null;
881 statusBar.setText(message);
883 if (progressBarHandlers.contains(lId))
885 progressBarHandlers.remove(lId);
887 layout.setRows(layout.getRows() - 1);
891 progressPanel = new JPanel(new BorderLayout(10, 5));
893 JProgressBar progressBar = new JProgressBar();
894 progressBar.setIndeterminate(true);
896 progressPanel.add(new JLabel(message), BorderLayout.WEST);
897 progressPanel.add(progressBar, BorderLayout.CENTER);
899 layout.setRows(layout.getRows() + 1);
900 statusPanel.add(progressPanel);
902 progressBars.put(lId, progressPanel);
905 // setMenusForViewport();
910 public void registerHandler(final long id,
911 final IProgressIndicatorHandler handler)
913 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
916 "call setProgressBar before registering the progress bar's handler.");
918 progressBarHandlers.put(new Long(id), handler);
919 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
920 if (handler.canCancel())
922 JButton cancel = new JButton(
923 MessageManager.getString("action.cancel"));
924 final IProgressIndicator us = this;
925 cancel.addActionListener(new ActionListener()
929 public void actionPerformed(ActionEvent e)
931 handler.cancelActivity(id);
934 + ((JLabel) progressPanel.getComponent(0))
938 progressPanel.add(cancel, BorderLayout.EAST);
944 * @return true if any progress bars are still active
947 public boolean operationInProgress()
949 if (progressBars != null && progressBars.size() > 0)