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.
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;
100 JTabbedPane subjobs = null;
102 java.util.Vector jobPanes = null;
104 private boolean serviceCanMergeResults = false;
106 private boolean viewResultsImmediatly = true;
112 * to indicate if results will be shown in a new window as soon as
113 * they are available.
115 public boolean isViewResultsImmediatly()
117 return viewResultsImmediatly;
124 * to indicate if results will be shown in a new window as soon as
125 * they are available.
127 public void setViewResultsImmediatly(boolean viewResultsImmediatly)
129 this.viewResultsImmediatly = viewResultsImmediatly;
132 private StyleSheet getStyleSheet(HTMLEditorKit editorKit)
135 // Copied blatantly from
136 // http://www.velocityreviews.com/forums/t132265-string-into-htmldocument.html
137 StyleSheet myStyleSheet = new StyleSheet();
139 myStyleSheet.addStyleSheet(editorKit.getStyleSheet());
141 editorKit.setStyleSheet(myStyleSheet);
144 * Set the style sheet rules here by reading them from the constants
148 * for (int ix=0; ix<CSS_RULES.length; ix++) {
150 * myStyleSheet.addRule(CSS_RULES[ix]);
159 public synchronized int addJobPane()
161 JScrollPane jobpane = new JScrollPane();
162 JComponent _progressText;
165 JEditorPane progressText = new JEditorPane("text/html", "");
166 progressText.addHyperlinkListener(this);
167 _progressText = progressText;
168 // progressText.setFont(new java.awt.Font("Verdana", 0, 10));
169 // progressText.setBorder(null);
170 progressText.setEditable(false);
172 * HTMLEditorKit myEditorKit = new HTMLEditorKit();
174 * StyleSheet myStyleSheet = getStyleSheet(myEditorKit);
176 * HTMLDocument tipDocument = (HTMLDocument)
177 * (myEditorKit.createDefaultDocument());
179 * progressText.setDocument(tipDocument);
180 */progressText.setText("<html><h1>WS Job</h1></html>");
184 JTextArea progressText = new JTextArea();
185 _progressText = progressText;
187 progressText.setFont(new java.awt.Font("Verdana", 0, 10));
188 progressText.setBorder(null);
189 progressText.setEditable(false);
190 progressText.setText("WS Job");
191 progressText.setLineWrap(true);
192 progressText.setWrapStyleWord(true);
194 jobpane.setName("JobPane");
195 jobpane.getViewport().add(_progressText, null);
196 jobpane.setBorder(null);
197 if (jobPanes == null)
199 jobPanes = new Vector();
201 int newpane = jobPanes.size();
202 jobPanes.add(jobpane);
206 this.add(jobpane, BorderLayout.CENTER);
212 // revert to a tabbed pane.
213 JScrollPane firstpane;
214 this.remove(firstpane = (JScrollPane) jobPanes.get(0));
215 subjobs = new JTabbedPane();
216 this.add(subjobs, BorderLayout.CENTER);
217 subjobs.add(firstpane);
218 subjobs.setTitleAt(0, firstpane.getName());
220 subjobs.add(jobpane);
222 return newpane; // index for accessor methods below
226 * Creates a new WebserviceInfo object.
229 * short name and job type
231 * reference or other human readable description
233 public WebserviceInfo(String title, String info)
235 init(title, info, 520, 500);
239 * Creates a new WebserviceInfo object.
250 public WebserviceInfo(String title, String info, int width, int height)
252 init(title, info, width, height);
258 * @return DOCUMENT ME!
260 public jalview.ws.WSClientI getthisService()
266 * Update state of GUI based on client capabilities (like whether the job is
267 * cancellable, whether the 'merge results' button is shown.
270 * service client to query for capabilities
272 public void setthisService(jalview.ws.WSClientI newservice)
274 thisService = newservice;
275 serviceIsCancellable = newservice.isCancellable();
276 frame.setClosable(!serviceIsCancellable);
277 serviceCanMergeResults = newservice.canMergeResults();
278 rebuildButtonPanel();
281 private void rebuildButtonPanel()
283 if (buttonPanel != null)
285 buttonPanel.removeAll();
286 if (serviceIsCancellable)
288 buttonPanel.add(cancel);
289 frame.setClosable(false);
293 frame.setClosable(true);
310 void init(String title, String info, int width, int height)
312 frame = new JInternalFrame();
313 frame.setContentPane(this);
314 Desktop.addInternalFrame(frame, title, width, height);
315 frame.setClosable(false);
320 java.net.URL url = getClass().getResource(
321 "/images/Jalview_Logo_small.png");
322 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
324 MediaTracker mt = new MediaTracker(this);
325 mt.addImage(image, 0);
330 } catch (Exception ex)
334 AnimatedPanel ap = new AnimatedPanel();
335 titlePanel.add(ap, BorderLayout.CENTER);
337 Thread thread = new Thread(ap);
339 final WebserviceInfo thisinfo = this;
340 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
342 public void internalFrameClosed(
343 javax.swing.event.InternalFrameEvent evt)
345 // System.out.println("Shutting down webservice client");
346 WSClientI service = thisinfo.getthisService();
347 if (service != null && service.isCancellable())
361 * integer status from state constants
363 public void setStatus(int status)
365 currentStatus = status;
369 * subjob status indicator
374 public void setStatus(int jobpane, int status)
376 if (jobpane < 0 || jobpane >= jobPanes.size())
378 throw new Error(MessageManager.formatMessage("error.setstatus_called_non_existent_job_pane", new String[]{Integer.valueOf(jobpane).toString()}));
383 setProgressName(jobpane + " - QUEUED", jobpane);
386 setProgressName(jobpane + " - RUNNING", jobpane);
388 case STATE_STOPPED_OK:
389 setProgressName(jobpane + " - FINISHED", jobpane);
391 case STATE_CANCELLED_OK:
392 setProgressName(jobpane + " - CANCELLED", jobpane);
394 case STATE_STOPPED_ERROR:
395 setProgressName(jobpane + " - BROKEN", jobpane);
397 case STATE_STOPPED_SERVERERROR:
398 setProgressName(jobpane + " - ALERT", jobpane);
401 setProgressName(jobpane + " - UNKNOWN STATE", jobpane);
408 * @return DOCUMENT ME!
410 public String getInfoText()
412 return infoText.getText();
421 public void setInfoText(String text)
423 infoText.setText(text);
432 public void appendInfoText(String text)
434 infoText.append(text);
440 * @return DOCUMENT ME!
442 public String getProgressText(int which)
444 if (jobPanes == null)
450 return ((JEditorPane) ((JScrollPane) jobPanes.get(which))
451 .getViewport().getComponent(0)).getText();
455 return ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
456 .getComponent(0)).getText();
466 public void setProgressText(int which, String text)
468 if (jobPanes == null)
474 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
475 .getComponent(0)).setText(ensureHtmlTagged(text));
479 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
480 .getComponent(0)).setText(text);
485 * extract content from <body> content </body>
489 * - set to leave the initial html tag intact
491 * - set to leave the final html tag intact
494 private String getHtmlFragment(String text, boolean leaveFirst,
501 String lowertxt = text.toLowerCase();
502 int htmlpos = leaveFirst ? -1 : lowertxt.indexOf("<body");
504 int htmlend = leaveLast ? -1 : lowertxt.indexOf("</body");
505 int htmlpose = lowertxt.indexOf(">", htmlpos), htmlende = lowertxt
506 .indexOf(">", htmlend);
507 if (htmlend == -1 && htmlpos == -1)
513 return text.substring((htmlpos == -1 ? 0 : htmlpose + 1), htmlend);
515 return text.substring(htmlpos == -1 ? 0 : htmlpose + 1);
519 * very simple routine for adding/ensuring html tags are present in text.
522 * @return properly html tag enclosed text
524 private String ensureHtmlTagged(String text)
530 String lowertxt = text.toLowerCase();
531 int htmlpos = lowertxt.indexOf("<body");
532 int htmlend = lowertxt.indexOf("</body");
533 int doctype = lowertxt.indexOf("<!doctype");
534 int xmltype = lowertxt.indexOf("<?xml");
537 text = text + "</body></html>";
541 if ((doctype > -1 && htmlpos > doctype)
542 || (xmltype > -1 && htmlpos > xmltype))
544 text = "<html><head></head><body>\n" + text.substring(htmlpos - 1);
549 text = "<html><head></head><body>\n" + text;
551 if (text.indexOf("<meta") > -1)
553 System.err.println("HTML COntent: \n" + text
554 + "<< END HTML CONTENT\n");
566 public void appendProgressText(int which, String text)
568 if (jobPanes == null)
574 String txt = getHtmlFragment(
575 ((JEditorPane) ((JScrollPane) jobPanes.get(which))
576 .getViewport().getComponent(0)).getText(), true,
578 ((JEditorPane) ((JScrollPane) jobPanes.get(which)).getViewport()
579 .getComponent(0)).setText(ensureHtmlTagged(txt
580 + getHtmlFragment(text, false, true)));
584 ((JTextArea) ((JScrollPane) jobPanes.get(which)).getViewport()
585 .getComponent(0)).append(text);
590 * setProgressText(0, text)
592 public void setProgressText(String text)
594 setProgressText(0, text);
598 * appendProgressText(0, text)
600 public void appendProgressText(String text)
602 appendProgressText(0, text);
608 public String getProgressText()
610 return getProgressText(0);
614 * get the tab title for a subjob
620 public String getProgressName(int which)
622 if (jobPanes == null)
628 return subjobs.getTitleAt(which);
632 return ((JScrollPane) jobPanes.get(which)).getViewport()
633 .getComponent(0).getName();
638 * set the tab title for a subjob
645 public void setProgressName(String name, int which)
649 subjobs.setTitleAt(which, name);
650 subjobs.revalidate();
653 JScrollPane c = (JScrollPane) jobPanes.get(which);
654 c.getViewport().getComponent(0).setName(name);
659 * Gui action for cancelling the current job, if possible.
664 protected void cancel_actionPerformed(ActionEvent e)
666 if (!serviceIsCancellable)
668 // JBPNote : TODO: We should REALLY just tell the WSClientI to cancel
669 // anyhow - it has to stop threads and clean up
670 // JBPNote : TODO: Instead of a warning, we should have an optional 'Are
672 warnUser(MessageManager.getString("warn.job_cannot_be_cancelled_close_window"),
673 MessageManager.getString("action.cancel_job"));
677 thisService.cancelJob();
679 frame.setClosable(true);
683 * Spawns a thread that pops up a warning dialog box with the given message
689 public void warnUser(final String message, final String title)
691 javax.swing.SwingUtilities.invokeLater(new Runnable()
695 JOptionPane.showInternalMessageDialog(Desktop.desktop, message,
696 title, JOptionPane.WARNING_MESSAGE);
703 * Set up GUI for user to get at results - and possibly automatically display
704 * them if viewResultsImmediatly is set.
706 public void setResultsReady()
708 frame.setClosable(true);
709 buttonPanel.remove(cancel);
710 buttonPanel.add(showResultsNewFrame);
711 if (serviceCanMergeResults)
713 buttonPanel.add(mergeResults);
714 buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
716 buttonPanel.validate();
718 if (viewResultsImmediatly)
720 showResultsNewFrame.doClick();
725 * called when job has finished but no result objects can be passed back to
728 public void setFinishedNoResults()
730 frame.setClosable(true);
731 buttonPanel.remove(cancel);
732 buttonPanel.validate();
736 class AnimatedPanel extends JPanel implements Runnable
740 BufferedImage offscreen;
744 startTime = System.currentTimeMillis();
746 while (currentStatus < STATE_STOPPED_OK)
752 int units = (int) ((System.currentTimeMillis() - startTime) / 10f);
755 startTime = System.currentTimeMillis();
757 if (currentStatus >= STATE_STOPPED_OK)
763 } catch (Exception ex)
768 cancel.setEnabled(false);
773 if (offscreen == null || offscreen.getWidth(this) != getWidth()
774 || offscreen.getHeight(this) != getHeight())
776 offscreen = new BufferedImage(getWidth(), getHeight(),
777 BufferedImage.TYPE_INT_ARGB);
780 Graphics2D g = (Graphics2D) offscreen.getGraphics();
782 g.setColor(Color.white);
783 g.fillRect(0, 0, getWidth(), getHeight());
785 g.setFont(new Font("Arial", Font.BOLD, 12));
786 g.setColor(Color.black);
788 switch (currentStatus)
792 title.concat(" - ").concat(
793 MessageManager.getString("label.state_queueing")),
800 title.concat(" - ").concat(
801 MessageManager.getString("label.state_running")),
806 case STATE_STOPPED_OK:
808 title.concat(" - ").concat(
809 MessageManager.getString("label.state_completed")),
814 case STATE_CANCELLED_OK:
816 title.concat(" - ").concat(
818 .getString("label.state_job_cancelled")),
823 case STATE_STOPPED_ERROR:
825 title.concat(" - ").concat(
826 MessageManager.getString("label.state_job_error")),
831 case STATE_STOPPED_SERVERERROR:
833 title.concat(" - ").concat(
835 .getString("label.server_error_try_later")),
843 int x = image.getWidth(this) / 2, y = image.getHeight(this) / 2;
844 g.rotate(Math.toRadians(angle), 10 + x, 10 + y);
845 g.drawImage(image, 10, 10, this);
846 g.rotate(-Math.toRadians(angle), 10 + x, 10 + y);
850 public void paintComponent(Graphics g1)
854 g1.drawImage(offscreen, 0, 0, this);
858 boolean renderAsHtml = false;
860 public void setRenderAsHtml(boolean b)
865 public void hyperlinkUpdate(HyperlinkEvent e)
867 Desktop.hyperlinkUpdate(e);
870 // methods for implementing IProgressIndicator
871 // need to refactor to a reusable stub class
872 Hashtable progressBars, progressBarHandlers;
877 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
880 public void setProgressBar(String message, long id)
882 if (progressBars == null)
884 progressBars = new Hashtable();
885 progressBarHandlers = new Hashtable();
888 JPanel progressPanel;
889 Long lId = new Long(id);
890 GridLayout layout = (GridLayout) statusPanel.getLayout();
891 if (progressBars.get(lId) != null)
893 progressPanel = (JPanel) progressBars.get(new Long(id));
894 statusPanel.remove(progressPanel);
895 progressBars.remove(lId);
896 progressPanel = null;
899 statusBar.setText(message);
901 if (progressBarHandlers.contains(lId))
903 progressBarHandlers.remove(lId);
905 layout.setRows(layout.getRows() - 1);
909 progressPanel = new JPanel(new BorderLayout(10, 5));
911 JProgressBar progressBar = new JProgressBar();
912 progressBar.setIndeterminate(true);
914 progressPanel.add(new JLabel(message), BorderLayout.WEST);
915 progressPanel.add(progressBar, BorderLayout.CENTER);
917 layout.setRows(layout.getRows() + 1);
918 statusPanel.add(progressPanel);
920 progressBars.put(lId, progressPanel);
923 // setMenusForViewport();
928 public void registerHandler(final long id,
929 final IProgressIndicatorHandler handler)
931 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
933 throw new Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
935 progressBarHandlers.put(new Long(id), handler);
936 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
937 if (handler.canCancel())
939 JButton cancel = new JButton(
940 MessageManager.getString("action.cancel"));
941 final IProgressIndicator us = this;
942 cancel.addActionListener(new ActionListener()
946 public void actionPerformed(ActionEvent e)
948 handler.cancelActivity(id);
949 us.setProgressBar(MessageManager.formatMessage("label.cancelled_params", new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
952 progressPanel.add(cancel, BorderLayout.EAST);
958 * @return true if any progress bars are still active
961 public boolean operationInProgress()
963 if (progressBars != null && progressBars.size() > 0)