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.ws.WSClientI;
27 import java.awt.BorderLayout;
28 import java.awt.Color;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.GridLayout;
33 import java.awt.Image;
34 import java.awt.MediaTracker;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.image.BufferedImage;
38 import java.util.Hashtable;
39 import java.util.Vector;
41 import javax.swing.JButton;
42 import javax.swing.JComponent;
43 import javax.swing.JEditorPane;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JOptionPane;
47 import javax.swing.JPanel;
48 import javax.swing.JProgressBar;
49 import javax.swing.JScrollPane;
50 import javax.swing.JTabbedPane;
51 import javax.swing.JTextArea;
52 import javax.swing.event.HyperlinkEvent;
53 import javax.swing.event.HyperlinkListener;
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 implements
65 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;
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);
332 java.net.URL url = getClass().getResource(
333 "/images/Jalview_Logo_small.png");
334 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
336 MediaTracker mt = new MediaTracker(this);
337 mt.addImage(image, 0);
342 } catch (Exception ex)
346 AnimatedPanel ap = new AnimatedPanel();
347 titlePanel.add(ap, BorderLayout.CENTER);
349 Thread thread = new Thread(ap);
351 final WebserviceInfo thisinfo = this;
352 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
354 public void internalFrameClosed(
355 javax.swing.event.InternalFrameEvent evt)
357 // System.out.println("Shutting down webservice client");
358 WSClientI service = thisinfo.getthisService();
359 if (service != null && service.isCancellable())
373 * integer status from state constants
375 public void setStatus(int status)
377 currentStatus = status;
381 * subjob status indicator
386 public void setStatus(int jobpane, int status)
388 if (jobpane < 0 || jobpane >= jobPanes.size())
390 throw new Error(MessageManager.formatMessage("error.setstatus_called_non_existent_job_pane", new String[]{Integer.valueOf(jobpane).toString()}));
395 setProgressName(jobpane + " - QUEUED", jobpane);
398 setProgressName(jobpane + " - RUNNING", jobpane);
400 case STATE_STOPPED_OK:
401 setProgressName(jobpane + " - FINISHED", jobpane);
403 case STATE_CANCELLED_OK:
404 setProgressName(jobpane + " - CANCELLED", jobpane);
406 case STATE_STOPPED_ERROR:
407 setProgressName(jobpane + " - BROKEN", jobpane);
409 case STATE_STOPPED_SERVERERROR:
410 setProgressName(jobpane + " - ALERT", jobpane);
413 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
420 * @return DOCUMENT ME!
422 public String getInfoText()
424 return infoText.getText();
433 public void setInfoText(String text)
435 infoText.setText(text);
444 public void appendInfoText(String text)
446 infoText.append(text);
452 * @return DOCUMENT ME!
454 public String getProgressText(int which)
456 if (jobPanes == null)
462 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
463 .getViewport().getComponent(0)).getText();
467 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
468 .getComponent(0)).getText();
478 public void setProgressText(int which, String text)
480 if (jobPanes == null)
486 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
487 .getComponent(0)).setText(ensureHtmlTagged(text));
491 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
492 .getComponent(0)).setText(text);
497 * extract content from <body> content </body>
501 * - set to leave the initial html tag intact
503 * - set to leave the final html tag intact
506 private String getHtmlFragment(String text, boolean leaveFirst,
513 String lowertxt = text.toLowerCase();
514 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
516 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
517 int htmlpose = lowertxt.indexOf(">", htmlpos), htmlende = lowertxt
518 .indexOf(">", htmlend);
519 if (htmlend == -1 && htmlpos == -1)
525 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
527 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
531 * very simple routine for adding/ensuring html tags are present in text.
534 * @return properly html tag enclosed text
536 private String ensureHtmlTagged(String text)
542 String lowertxt = text.toLowerCase();
543 int htmlpos = lowertxt.indexOf("<body");
544 int htmlend = lowertxt.indexOf("</body");
545 int doctype = lowertxt.indexOf("<!doctype");
546 int xmltype = lowertxt.indexOf("<?xml");
549 text = text + "</body></html>";
553 if ((doctype > -1 && htmlpos > doctype)
554 || (xmltype > -1 && htmlpos > xmltype))
556 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
561 text = "<html><head></head><body>\n" + text;
563 if (text.indexOf("<meta") > -1)
565 System.err.println("HTML COntent: \n" + text
566 + "<< END HTML CONTENT\n");
578 public void appendProgressText(int which, String text)
580 if (jobPanes == null)
586 String txt = getHtmlFragment(
587 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
588 .getViewport().getComponent(0)).getText(), true,
590 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
591 .getComponent(0)).setText(ensureHtmlTagged(txt
592 + getHtmlFragment(text, false, true)));
596 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
597 .getComponent(0)).append(text);
602 * setProgressText(0, text)
604 public void setProgressText(String text)
606 setProgressText(0, text);
610 * appendProgressText(0, text)
612 public void appendProgressText(String text)
614 appendProgressText(0, text);
620 public String getProgressText()
622 return getProgressText(0);
626 * get the tab title for a subjob
632 public String getProgressName(int which)
634 if (jobPanes == null)
640 return subjobs.getTitleAt(which);
644 return ((JScrollPane) jobPanes.get(which)).getViewport()
645 .getComponent(0).getName();
650 * set the tab title for a subjob
657 public void setProgressName(String name, int which)
661 subjobs.setTitleAt(which, name);
662 subjobs.revalidate();
665 JScrollPane c = (JScrollPane) jobPanes.get(which);
666 c.getViewport().getComponent(0).setName(name);
671 * Gui action for cancelling the current job, if possible.
676 protected void cancel_actionPerformed(ActionEvent e)
678 if (!serviceIsCancellable)
680 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
681 // anyhow - it has to stop threads and clean up
682 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
684 warnUser(MessageManager.getString("warn.job_cannot_be_cancelled_close_window"),
685 MessageManager.getString("action.cancel_job"));
689 thisService.cancelJob();
691 frame.setClosable(true);
695 * Spawns a thread that pops up a warning dialog box with the given message
701 public void warnUser(final String message, final String title)
703 javax.swing.SwingUtilities.invokeLater(new Runnable()
707 JOptionPane.showInternalMessageDialog(Desktop.desktop, message,
708 title, JOptionPane.WARNING_MESSAGE);
715 * Set up GUI for user to get at results - and possibly automatically display
716 * them if viewResultsImmediatly is set.
718 public void setResultsReady()
720 frame.setClosable(true);
721 buttonPanel.remove(cancel);
722 buttonPanel.add(showResultsNewFrame);
723 if (serviceCanMergeResults)
725 buttonPanel.add(mergeResults);
726 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
728 buttonPanel.validate();
730 if (viewResultsImmediatly)
732 showResultsNewFrame.doClick();
737 * called when job has finished but no result objects can be passed back to
740 public void setFinishedNoResults()
742 frame.setClosable(true);
743 buttonPanel.remove(cancel);
744 buttonPanel.validate();
748 class AnimatedPanel extends JPanel implements Runnable
752 BufferedImage offscreen;
756 startTime = System.currentTimeMillis();
758 while (currentStatus < STATE_STOPPED_OK)
764 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
767 startTime = System.currentTimeMillis();
769 if (currentStatus >= STATE_STOPPED_OK)
775 } catch (Exception ex)
780 cancel.setEnabled(false);
785 if (offscreen == null || offscreen.getWidth(this) != getWidth()
786 || offscreen.getHeight(this) != getHeight())
788 offscreen = new BufferedImage(getWidth(), getHeight(),
789 BufferedImage.TYPE_INT_ARGB);
792 Graphics2D g = (Graphics2D) offscreen.getGraphics();
794 g.setColor(Color.white);
795 g.fillRect(0, 0, getWidth(), getHeight());
797 g.setFont(new Font("Arial", Font.BOLD, 12));
798 g.setColor(Color.black);
800 switch (currentStatus)
804 title.concat(" - ").concat(
805 MessageManager.getString("label.state_queueing")),
812 title.concat(" - ").concat(
813 MessageManager.getString("label.state_running")),
818 case STATE_STOPPED_OK:
820 title.concat(" - ").concat(
821 MessageManager.getString("label.state_completed")),
826 case STATE_CANCELLED_OK:
828 title.concat(" - ").concat(
830 .getString("label.state_job_cancelled")),
835 case STATE_STOPPED_ERROR:
837 title.concat(" - ").concat(
838 MessageManager.getString("label.state_job_error")),
843 case STATE_STOPPED_SERVERERROR:
845 title.concat(" - ").concat(
847 .getString("label.server_error_try_later")),
855 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
856 g.rotate(Math.toRadians(angle), 10 + x, 10 + y);
857 g.drawImage(image, 10, 10, this);
858 g.rotate(-Math.toRadians(angle), 10 + x, 10 + y);
862 public void paintComponent(Graphics g1)
866 g1.drawImage(offscreen, 0, 0, this);
870 boolean renderAsHtml = false;
872 public void setRenderAsHtml(boolean b)
877 public void hyperlinkUpdate(HyperlinkEvent e)
879 Desktop.hyperlinkUpdate(e);
882 // methods for implementing IProgressIndicator
883 // need to refactor to a reusable stub class
884 Hashtable progressBars, progressBarHandlers;
889 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
892 public void setProgressBar(String message, long id)
894 if (progressBars == null)
896 progressBars = new Hashtable();
897 progressBarHandlers = new Hashtable();
900 JPanel progressPanel;
901 Long lId = new Long(id);
902 GridLayout layout = (GridLayout) statusPanel.getLayout();
903 if (progressBars.get(lId) != null)
905 progressPanel = (JPanel) progressBars.get(new Long(id));
906 statusPanel.remove(progressPanel);
907 progressBars.remove(lId);
908 progressPanel = null;
911 statusBar.setText(message);
913 if (progressBarHandlers.contains(lId))
915 progressBarHandlers.remove(lId);
917 layout.setRows(layout.getRows() - 1);
921 progressPanel = new JPanel(new BorderLayout(10, 5));
923 JProgressBar progressBar = new JProgressBar();
924 progressBar.setIndeterminate(true);
926 progressPanel.add(new JLabel(message), BorderLayout.WEST);
927 progressPanel.add(progressBar, BorderLayout.CENTER);
929 layout.setRows(layout.getRows() + 1);
930 statusPanel.add(progressPanel);
932 progressBars.put(lId, progressPanel);
935 // setMenusForViewport();
940 public void registerHandler(final long id,
941 final IProgressIndicatorHandler handler)
943 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
945 throw new Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
947 progressBarHandlers.put(new Long(id), handler);
948 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
949 if (handler.canCancel())
951 JButton cancel = new JButton(
952 MessageManager.getString("action.cancel"));
953 final IProgressIndicator us = this;
954 cancel.addActionListener(new ActionListener()
958 public void actionPerformed(ActionEvent e)
960 handler.cancelActivity(id);
961 us.setProgressBar(MessageManager.formatMessage("label.cancelled_params", new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
964 progressPanel.add(cancel, BorderLayout.EAST);
970 * @return true if any progress bars are still active
973 public boolean operationInProgress()
975 if (progressBars != null && progressBars.size() > 0)