2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
24 import java.awt.image.*;
26 import javax.swing.event.HyperlinkEvent;
27 import javax.swing.event.HyperlinkListener;
28 import javax.swing.text.html.HTMLEditorKit;
29 import javax.swing.text.html.StyleSheet;
31 import jalview.jbgui.*;
32 import jalview.ws.WSClientI;
35 * Base class for web service client thread and gui TODO: create StAX parser to
36 * extract html body content reliably when preparing html formatted job statuses
41 public class WebserviceInfo extends GWebserviceInfo implements
42 HyperlinkListener, IProgressIndicator
46 public static final int STATE_QUEUING = 0;
49 public static final int STATE_RUNNING = 1;
51 /** Job has finished with no errors */
52 public static final int STATE_STOPPED_OK = 2;
54 /** Job has been cancelled with no errors */
55 public static final int STATE_CANCELLED_OK = 3;
57 /** job has stopped because of some error */
58 public static final int STATE_STOPPED_ERROR = 4;
60 /** job has failed because of some unavoidable service interruption */
61 public static final int STATE_STOPPED_SERVERERROR = 5;
63 int currentStatus = STATE_QUEUING;
71 jalview.ws.WSClientI thisService;
73 boolean serviceIsCancellable;
77 JTabbedPane subjobs = null;
79 java.util.Vector jobPanes = null;
81 private boolean serviceCanMergeResults = false;
83 private boolean viewResultsImmediatly = true;
89 * to indicate if results will be shown in a new window as soon as
92 public boolean isViewResultsImmediatly()
94 return viewResultsImmediatly;
101 * to indicate if results will be shown in a new window as soon as
102 * they are available.
104 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
106 this.viewResultsImmediatly = viewResultsImmediatly;
109 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
112 // Copied blatantly from
113 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
114 StyleSheet myStyleSheet = new StyleSheet();
116 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
118 editorKit.setStyleSheet(myStyleSheet);
121 * Set the style sheet rules here by reading them from the constants
125 * for (int ix=0; ix<CSS_RULES.length; ix++) {
127 * myStyleSheet.addRule(CSS_RULES[ix]);
136 public synchronized int addJobPane()
138 JScrollPane jobpane = new JScrollPane();
139 JComponent _progressText;
142 JEditorPane progressText = new JEditorPane("text/html", "");
143 progressText.addHyperlinkListener(this);
144 _progressText = progressText;
145 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
146 // progressText.setBorder(null);
147 progressText.setEditable(false);
149 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
151 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
153 * HTMLDocument tipDocument = (HTMLDocument)
154 * (myEditorKit.createDefaultDocument());
156 * progressText.setDocument(tipDocument);
157 */progressText.setText("<html><h1>WS Job</h1></html>");
161 JTextArea progressText = new JTextArea();
162 _progressText = progressText;
164 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
165 progressText.setBorder(null);
166 progressText.setEditable(false);
167 progressText.setText("WS Job");
168 progressText.setLineWrap(true);
169 progressText.setWrapStyleWord(true);
171 jobpane.setName("JobPane");
172 jobpane.getViewport().add(_progressText, null);
173 jobpane.setBorder(null);
174 if (jobPanes == null)
176 jobPanes = new Vector();
178 int newpane = jobPanes.size();
179 jobPanes.add(jobpane);
183 this.add(jobpane, BorderLayout.CENTER);
189 // revert to a tabbed pane.
190 JScrollPane firstpane;
191 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
192 subjobs = new JTabbedPane();
193 this.add(subjobs, BorderLayout.CENTER);
194 subjobs.add(firstpane);
195 subjobs.setTitleAt(0, firstpane.getName());
197 subjobs.add(jobpane);
199 return newpane; // index for accessor methods below
203 * Creates a new WebserviceInfo object.
206 * short name and job type
208 * reference or other human readable description
210 public WebserviceInfo(String title, String info)
212 init(title, info, 520, 500);
216 * Creates a new WebserviceInfo object.
227 public WebserviceInfo(String title, String info, int width, int height)
229 init(title, info, width, height);
235 * @return DOCUMENT ME!
237 public jalview.ws.WSClientI getthisService()
243 * Update state of GUI based on client capabilities (like whether the job is
244 * cancellable, whether the 'merge results' button is shown.
247 * service client to query for capabilities
249 public void setthisService(jalview.ws.WSClientI newservice)
251 thisService = newservice;
252 serviceIsCancellable = newservice.isCancellable();
253 frame.setClosable(!serviceIsCancellable);
254 serviceCanMergeResults = newservice.canMergeResults();
255 rebuildButtonPanel();
258 private void rebuildButtonPanel()
260 if (buttonPanel != null)
262 buttonPanel.removeAll();
263 if (serviceIsCancellable)
265 buttonPanel.add(cancel);
266 frame.setClosable(false);
270 frame.setClosable(true);
287 void init(String title, String info, int width, int height)
289 frame = new JInternalFrame();
290 frame.setContentPane(this);
291 Desktop.addInternalFrame(frame, title, width, height);
292 frame.setClosable(false);
297 java.net.URL url = getClass().getResource(
298 "/images/Jalview_Logo_small.png");
299 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
301 MediaTracker mt = new MediaTracker(this);
302 mt.addImage(image, 0);
307 } catch (Exception ex)
311 AnimatedPanel ap = new AnimatedPanel();
312 titlePanel.add(ap, BorderLayout.CENTER);
314 Thread thread = new Thread(ap);
316 final WebserviceInfo thisinfo = this;
317 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
319 public void internalFrameClosed(
320 javax.swing.event.InternalFrameEvent evt)
322 // System.out.println("Shutting down webservice client");
323 WSClientI service = thisinfo.getthisService();
324 if (service != null && service.isCancellable())
338 * integer status from state constants
340 public void setStatus(int status)
342 currentStatus = status;
346 * subjob status indicator
351 public void setStatus(int jobpane, int status)
353 if (jobpane < 0 || jobpane >= jobPanes.size())
355 throw new Error("setStatus called for non-existent job pane."
361 setProgressName(jobpane + " - QUEUED", jobpane);
364 setProgressName(jobpane + " - RUNNING", jobpane);
366 case STATE_STOPPED_OK:
367 setProgressName(jobpane + " - FINISHED", jobpane);
369 case STATE_CANCELLED_OK:
370 setProgressName(jobpane + " - CANCELLED", jobpane);
372 case STATE_STOPPED_ERROR:
373 setProgressName(jobpane + " - BROKEN", jobpane);
375 case STATE_STOPPED_SERVERERROR:
376 setProgressName(jobpane + " - ALERT", jobpane);
379 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
386 * @return DOCUMENT ME!
388 public String getInfoText()
390 return infoText.getText();
399 public void setInfoText(String text)
401 infoText.setText(text);
410 public void appendInfoText(String text)
412 infoText.append(text);
418 * @return DOCUMENT ME!
420 public String getProgressText(int which)
422 if (jobPanes == null)
428 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
429 .getViewport().getComponent(0)).getText();
433 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
434 .getComponent(0)).getText();
444 public void setProgressText(int which, String text)
446 if (jobPanes == null)
452 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
453 .getComponent(0)).setText(ensureHtmlTagged(text));
457 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
458 .getComponent(0)).setText(text);
463 * extract content from <body> content </body>
467 * - set to leave the initial html tag intact
469 * - set to leave the final html tag intact
472 private String getHtmlFragment(String text, boolean leaveFirst,
479 String lowertxt = text.toLowerCase();
480 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
482 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
483 int htmlpose = lowertxt.indexOf(">", htmlpos), htmlende = lowertxt
484 .indexOf(">", htmlend);
485 if (htmlend == -1 && htmlpos == -1)
491 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
493 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
497 * very simple routine for adding/ensuring html tags are present in text.
500 * @return properly html tag enclosed text
502 private String ensureHtmlTagged(String text)
508 String lowertxt = text.toLowerCase();
509 int htmlpos = lowertxt.indexOf("<body");
510 int htmlend = lowertxt.indexOf("</body");
511 int doctype = lowertxt.indexOf("<!doctype");
512 int xmltype = lowertxt.indexOf("<?xml");
515 text = text + "</body></html>";
519 if ((doctype > -1 && htmlpos > doctype)
520 || (xmltype > -1 && htmlpos > xmltype))
522 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
527 text = "<html><head></head><body>\n" + text;
529 if (text.indexOf("<meta") > -1)
531 System.err.println("HTML COntent: \n" + text
532 + "<< END HTML CONTENT\n");
544 public void appendProgressText(int which, String text)
546 if (jobPanes == null)
552 String txt = getHtmlFragment(
553 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
554 .getViewport().getComponent(0)).getText(), true,
556 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
557 .getComponent(0)).setText(ensureHtmlTagged(txt
558 + getHtmlFragment(text, false, true)));
562 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
563 .getComponent(0)).append(text);
568 * setProgressText(0, text)
570 public void setProgressText(String text)
572 setProgressText(0, text);
576 * appendProgressText(0, text)
578 public void appendProgressText(String text)
580 appendProgressText(0, text);
586 public String getProgressText()
588 return getProgressText(0);
592 * get the tab title for a subjob
598 public String getProgressName(int which)
600 if (jobPanes == null)
606 return subjobs.getTitleAt(which);
610 return ((JScrollPane) jobPanes.get(which)).getViewport()
611 .getComponent(0).getName();
616 * set the tab title for a subjob
623 public void setProgressName(String name, int which)
627 subjobs.setTitleAt(which, name);
628 subjobs.revalidate();
631 JScrollPane c = (JScrollPane) jobPanes.get(which);
632 c.getViewport().getComponent(0).setName(name);
637 * Gui action for cancelling the current job, if possible.
642 protected void cancel_actionPerformed(ActionEvent e)
644 if (!serviceIsCancellable)
646 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
647 // anyhow - it has to stop threads and clean up
648 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
650 warnUser("This job cannot be cancelled.\nJust close the window.",
655 thisService.cancelJob();
657 frame.setClosable(true);
661 * Spawns a thread that pops up a warning dialog box with the given message
667 public void warnUser(final String message, final String title)
669 javax.swing.SwingUtilities.invokeLater(new Runnable()
673 JOptionPane.showInternalMessageDialog(Desktop.desktop, message,
674 title, JOptionPane.WARNING_MESSAGE);
681 * Set up GUI for user to get at results - and possibly automatically display
682 * them if viewResultsImmediatly is set.
684 public void setResultsReady()
686 frame.setClosable(true);
687 buttonPanel.remove(cancel);
688 buttonPanel.add(showResultsNewFrame);
689 if (serviceCanMergeResults)
691 buttonPanel.add(mergeResults);
692 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
694 buttonPanel.validate();
696 if (viewResultsImmediatly)
698 showResultsNewFrame.doClick();
703 * called when job has finished but no result objects can be passed back to
706 public void setFinishedNoResults()
708 frame.setClosable(true);
709 buttonPanel.remove(cancel);
710 buttonPanel.validate();
714 class AnimatedPanel extends JPanel implements Runnable
718 BufferedImage offscreen;
722 startTime = System.currentTimeMillis();
724 while (currentStatus < STATE_STOPPED_OK)
730 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
733 startTime = System.currentTimeMillis();
735 if (currentStatus >= STATE_STOPPED_OK)
741 } catch (Exception ex)
746 cancel.setEnabled(false);
751 if (offscreen == null || offscreen.getWidth(this) != getWidth()
752 || offscreen.getHeight(this) != getHeight())
754 offscreen = new BufferedImage(getWidth(), getHeight(),
755 BufferedImage.TYPE_INT_ARGB);
758 Graphics2D g = (Graphics2D) offscreen.getGraphics();
760 g.setColor(Color.white);
761 g.fillRect(0, 0, getWidth(), getHeight());
763 g.setFont(new Font("Arial", Font.BOLD, 12));
764 g.setColor(Color.black);
766 switch (currentStatus)
769 g.drawString(title.concat(" - queuing"), 60, 30);
774 g.drawString(title.concat(" - running"), 60, 30);
778 case STATE_STOPPED_OK:
779 g.drawString(title.concat(" - complete"), 60, 30);
783 case STATE_CANCELLED_OK:
784 g.drawString(title.concat(" - job cancelled!"), 60, 30);
788 case STATE_STOPPED_ERROR:
789 g.drawString(title.concat(" - job error!"), 60, 30);
793 case STATE_STOPPED_SERVERERROR:
794 g.drawString(title.concat(" - Server Error! (try later)"), 60, 30);
801 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
802 g.rotate(Math.toRadians(angle), 10 + x, 10 + y);
803 g.drawImage(image, 10, 10, this);
804 g.rotate(-Math.toRadians(angle), 10 + x, 10 + y);
808 public void paintComponent(Graphics g1)
812 g1.drawImage(offscreen, 0, 0, this);
816 boolean renderAsHtml = false;
818 public void setRenderAsHtml(boolean b)
823 public void hyperlinkUpdate(HyperlinkEvent e)
825 Desktop.hyperlinkUpdate(e);
828 // methods for implementing IProgressIndicator
829 // need to refactor to a reusable stub class
830 Hashtable progressBars, progressBarHandlers;
835 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
838 public void setProgressBar(String message, long id)
840 if (progressBars == null)
842 progressBars = new Hashtable();
843 progressBarHandlers = new Hashtable();
846 JPanel progressPanel;
847 Long lId = new Long(id);
848 GridLayout layout = (GridLayout) statusPanel.getLayout();
849 if (progressBars.get(lId) != null)
851 progressPanel = (JPanel) progressBars.get(new Long(id));
852 statusPanel.remove(progressPanel);
853 progressBars.remove(lId);
854 progressPanel = null;
857 statusBar.setText(message);
859 if (progressBarHandlers.contains(lId))
861 progressBarHandlers.remove(lId);
863 layout.setRows(layout.getRows() - 1);
867 progressPanel = new JPanel(new BorderLayout(10, 5));
869 JProgressBar progressBar = new JProgressBar();
870 progressBar.setIndeterminate(true);
872 progressPanel.add(new JLabel(message), BorderLayout.WEST);
873 progressPanel.add(progressBar, BorderLayout.CENTER);
875 layout.setRows(layout.getRows() + 1);
876 statusPanel.add(progressPanel);
878 progressBars.put(lId, progressPanel);
881 // setMenusForViewport();
886 public void registerHandler(final long id,
887 final IProgressIndicatorHandler handler)
889 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
892 "call setProgressBar before registering the progress bar's handler.");
894 progressBarHandlers.put(new Long(id), handler);
895 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
896 if (handler.canCancel())
898 JButton cancel = new JButton("Cancel");
899 final IProgressIndicator us = this;
900 cancel.addActionListener(new ActionListener()
904 public void actionPerformed(ActionEvent e)
906 handler.cancelActivity(id);
909 + ((JLabel) progressPanel.getComponent(0))
913 progressPanel.add(cancel, BorderLayout.EAST);
919 * @return true if any progress bars are still active
922 public boolean operationInProgress()
924 if (progressBars != null && progressBars.size() > 0)