JAL-1355 removed all top level html tags from message bundles
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws.jws2;
22
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.List;
26
27 import javax.swing.*;
28
29 import jalview.datamodel.*;
30 import jalview.gui.*;
31 import compbio.data.msa.MsaWS;
32 import compbio.metadata.Argument;
33 import jalview.util.MessageManager;
34 import jalview.ws.jws2.jabaws2.Jws2Instance;
35 import jalview.ws.params.WsParamSetI;
36
37 /**
38  * DOCUMENT ME!
39  * 
40  * @author $author$
41  * @version $Revision$
42  */
43 public class MsaWSClient extends Jws2Client
44 {
45   /**
46    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
47    */
48   MsaWS server;
49
50   public MsaWSClient(Jws2Instance sh, String altitle,
51           jalview.datamodel.AlignmentView msa, boolean submitGaps,
52           boolean preserveOrder, Alignment seqdataset,
53           AlignFrame _alignFrame)
54   {
55     this(sh, null, null, false, altitle, msa, submitGaps, preserveOrder,
56             seqdataset, _alignFrame);
57     // TODO Auto-generated constructor stub
58   }
59
60   public MsaWSClient(Jws2Instance sh, WsParamSetI preset, String altitle,
61           jalview.datamodel.AlignmentView msa, boolean submitGaps,
62           boolean preserveOrder, Alignment seqdataset,
63           AlignFrame _alignFrame)
64   {
65     this(sh, preset, null, false, altitle, msa, submitGaps, preserveOrder,
66             seqdataset, _alignFrame);
67     // TODO Auto-generated constructor stub
68   }
69
70   /**
71    * Creates a new MsaWSClient object that uses a service given by an externally
72    * retrieved ServiceHandle
73    * 
74    * @param sh
75    *          service handle of type AbstractName(MsaWS)
76    * @param altitle
77    *          DOCUMENT ME!
78    * @param msa
79    *          DOCUMENT ME!
80    * @param submitGaps
81    *          DOCUMENT ME!
82    * @param preserveOrder
83    *          DOCUMENT ME!
84    */
85
86   public MsaWSClient(Jws2Instance sh, WsParamSetI preset,
87           List<Argument> arguments, boolean editParams, String altitle,
88           jalview.datamodel.AlignmentView msa, boolean submitGaps,
89           boolean preserveOrder, Alignment seqdataset,
90           AlignFrame _alignFrame)
91   {
92     super(_alignFrame, preset, arguments);
93     if (!processParams(sh, editParams))
94     {
95       return;
96     }
97
98     if (!(sh.service instanceof MsaWS))
99     {
100       // redundant at mo - but may change
101       JOptionPane
102               .showMessageDialog(
103                       Desktop.desktop,
104                       MessageManager.formatMessage("label.service_called_is_not_msa_service", new String[]{sh.serviceType}),
105                       MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
106
107       return;
108     }
109     server = (MsaWS) sh.service;
110     if ((wsInfo = setWebService(sh, false)) == null)
111     {
112       JOptionPane.showMessageDialog(Desktop.desktop,
113                   MessageManager.formatMessage("label.msa_service_is_unknown", new String[]{sh.serviceType}),
114                   MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
115
116       return;
117     }
118     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
119
120   }
121
122   public MsaWSClient()
123   {
124     super();
125     // add a class reference to the list
126   }
127
128   private void startMsaWSClient(String altitle, AlignmentView msa,
129           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
130   {
131     // if (!locateWebService())
132     // {
133     // return;
134     // }
135
136     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
137             + " of " + altitle + "\nJob details\n");
138     String jobtitle = WebServiceName.toLowerCase();
139     if (jobtitle.endsWith("alignment"))
140     {
141       if (submitGaps
142               && (!jobtitle.endsWith("realignment") || jobtitle
143                       .indexOf("profile") == -1))
144       {
145         int pos = jobtitle.indexOf("alignment");
146         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
147                 + altitle;
148       }
149       else
150       {
151         jobtitle = WebServiceName + " of " + altitle;
152       }
153     }
154     else
155     {
156       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
157               + "alignment of " + altitle;
158     }
159
160     MsaWSThread msathread = new MsaWSThread(server, preset, paramset,
161             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
162             submitGaps, preserveOrder, seqdataset);
163     wsInfo.setthisService(msathread);
164     msathread.start();
165   }
166
167   protected String getServiceActionKey()
168   {
169     return "MsaWS";
170   }
171
172   protected String getServiceActionDescription()
173   {
174     return "Multiple Sequence Alignment";
175   }
176
177   /**
178    * look at ourselves and work out if we are a service that can take a profile
179    * and align to it
180    * 
181    * @return true if we can send gapped sequences to the alignment service
182    */
183   private boolean canSubmitGaps()
184   {
185     // TODO: query service or extract service handle props to check if we can
186     // realign
187     return (WebServiceName.indexOf("lustal") > -1); // cheat!
188   }
189
190   public void attachWSMenuEntry(JMenu rmsawsmenu,
191           final Jws2Instance service, final AlignFrame alignFrame)
192   {
193     if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
194     {
195       // Alignment dependent analysis calculation WS gui
196       return;
197     }
198     setWebService(service, true); // headless
199     boolean finished = true, submitGaps = false;
200     JMenu msawsmenu = rmsawsmenu;
201     String svcname = WebServiceName;
202     if (svcname.endsWith("WS"))
203     {
204       svcname = svcname.substring(0, svcname.length() - 2);
205     }
206     String calcName = svcname + " ";
207     if (canSubmitGaps())
208     {
209       msawsmenu = new JMenu(svcname);
210       rmsawsmenu.add(msawsmenu);
211       calcName = "";
212     }
213     boolean hasparams = service.hasParameters();
214     do
215     {
216       String action = "Align ";
217       if (submitGaps == true)
218       {
219         action = "Realign ";
220         msawsmenu = new JMenu(MessageManager.formatMessage(
221                 "label.realign_with_params", new String[]
222                 { svcname }));
223         msawsmenu.setToolTipText(MessageManager
224                 .getString("label.align_sequences_to_existing_alignment"));
225         rmsawsmenu.add(msawsmenu);
226       }
227       final boolean withGaps = submitGaps;
228
229       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
230               "label.calcname_with_default_settings", new String[]
231               { calcName }));
232       method.setToolTipText(MessageManager.formatMessage(
233               "label.action_with_default_settings", new String[]
234               { action }));
235
236       method.addActionListener(new ActionListener()
237       {
238         public void actionPerformed(ActionEvent e)
239         {
240           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
241           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
242                   true, alignFrame.getViewport().getAlignment()
243                           .getDataset(), alignFrame);
244
245         }
246       });
247       msawsmenu.add(method);
248       if (hasparams)
249       {
250         // only add these menu options if the service has user-modifiable
251         // arguments
252         method = new JMenuItem(
253                 MessageManager.getString("label.edit_settings_and_run"));
254         method.setToolTipText(MessageManager
255                 .getString("label.view_and_change_parameters_before_alignment"));
256
257         method.addActionListener(new ActionListener()
258         {
259           public void actionPerformed(ActionEvent e)
260           {
261             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
262             new MsaWSClient(service, null, null, true, alignFrame
263                     .getTitle(), msa, withGaps, true, alignFrame
264                     .getViewport().getAlignment().getDataset(), alignFrame);
265
266           }
267         });
268         msawsmenu.add(method);
269         List<WsParamSetI> presets = service.getParamStore().getPresets();
270         if (presets != null && presets.size() > 0)
271         {
272           JMenu presetlist = new JMenu(MessageManager.formatMessage(
273                   "label.run_with_preset_params", new String[]
274                   { calcName }));
275
276           for (final WsParamSetI preset : presets)
277           {
278             final JMenuItem methodR = new JMenuItem(preset.getName());
279             methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<p><strong>"
280                             + (preset.isModifiable() ? MessageManager.getString("label.user_preset")
281                                     : MessageManager.getString("label.service_preset")) + "</strong><br/>"
282                             + preset.getDescription() + "</p>"));
283             methodR.addActionListener(new ActionListener()
284             {
285               public void actionPerformed(ActionEvent e)
286               {
287                 AlignmentView msa = alignFrame
288                         .gatherSequencesForAlignment();
289                 new MsaWSClient(service, preset, alignFrame.getTitle(),
290                         msa, false, true, alignFrame.getViewport()
291                                 .getAlignment().getDataset(), alignFrame);
292
293               }
294
295             });
296             presetlist.add(methodR);
297           }
298           msawsmenu.add(presetlist);
299         }
300       }
301       if (!submitGaps && canSubmitGaps())
302       {
303         submitGaps = true;
304         finished = false;
305       }
306       else
307       {
308         finished = true;
309       }
310     } while (!finished);
311   }
312 }