JAL-1807 explicit imports (jalview.ws.*)
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentView;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.Desktop;
27 import jalview.gui.JvSwingUtils;
28 import jalview.util.MessageManager;
29 import jalview.ws.jws2.jabaws2.Jws2Instance;
30 import jalview.ws.params.WsParamSetI;
31
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.MouseAdapter;
35 import java.awt.event.MouseEvent;
36 import java.util.List;
37
38 import javax.swing.JMenu;
39 import javax.swing.JMenuItem;
40 import javax.swing.JOptionPane;
41 import javax.swing.ToolTipManager;
42
43 import compbio.data.msa.MsaWS;
44 import compbio.metadata.Argument;
45
46 /**
47  * DOCUMENT ME!
48  * 
49  * @author $author$
50  * @version $Revision$
51  */
52 public class MsaWSClient extends Jws2Client
53 {
54   /**
55    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
56    */
57   MsaWS server;
58
59   public MsaWSClient(Jws2Instance sh, String altitle,
60  AlignmentView msa,
61           boolean submitGaps,
62           boolean preserveOrder, Alignment seqdataset,
63           AlignFrame _alignFrame)
64   {
65     this(sh, null, null, false, altitle, msa, submitGaps, preserveOrder,
66             seqdataset, _alignFrame);
67     // TODO Auto-generated constructor stub
68   }
69
70   public MsaWSClient(Jws2Instance sh, WsParamSetI preset, String altitle,
71           AlignmentView msa, boolean submitGaps,
72           boolean preserveOrder, Alignment seqdataset,
73           AlignFrame _alignFrame)
74   {
75     this(sh, preset, null, false, altitle, msa, submitGaps, preserveOrder,
76             seqdataset, _alignFrame);
77     // TODO Auto-generated constructor stub
78   }
79
80   /**
81    * Creates a new MsaWSClient object that uses a service given by an externally
82    * retrieved ServiceHandle
83    * 
84    * @param sh
85    *          service handle of type AbstractName(MsaWS)
86    * @param altitle
87    *          DOCUMENT ME!
88    * @param msa
89    *          DOCUMENT ME!
90    * @param submitGaps
91    *          DOCUMENT ME!
92    * @param preserveOrder
93    *          DOCUMENT ME!
94    */
95
96   public MsaWSClient(Jws2Instance sh, WsParamSetI preset,
97           List<Argument> arguments, boolean editParams, String altitle,
98           AlignmentView msa, boolean submitGaps,
99           boolean preserveOrder, Alignment seqdataset,
100           AlignFrame _alignFrame)
101   {
102     super(_alignFrame, preset, arguments);
103     if (!processParams(sh, editParams))
104     {
105       return;
106     }
107
108     if (!(sh.service instanceof MsaWS))
109     {
110       // redundant at mo - but may change
111       JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
112               .formatMessage("label.service_called_is_not_msa_service",
113                       new String[]
114                       { sh.serviceType }), MessageManager
115               .getString("label.internal_jalview_error"),
116               JOptionPane.WARNING_MESSAGE);
117
118       return;
119     }
120     server = (MsaWS) sh.service;
121     if ((wsInfo = setWebService(sh, false)) == null)
122     {
123       JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
124               .formatMessage("label.msa_service_is_unknown", new String[]
125               { sh.serviceType }), MessageManager
126               .getString("label.internal_jalview_error"),
127               JOptionPane.WARNING_MESSAGE);
128
129       return;
130     }
131
132     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
133
134
135   }
136
137   public MsaWSClient()
138   {
139     super();
140     // add a class reference to the list
141   }
142
143   private void startMsaWSClient(String altitle, AlignmentView msa,
144           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
145   {
146     // if (!locateWebService())
147     // {
148     // return;
149     // }
150
151     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
152             + " of " + altitle + "\nJob details\n");
153     String jobtitle = WebServiceName.toLowerCase();
154     if (jobtitle.endsWith("alignment"))
155     {
156       if (submitGaps
157               && (!jobtitle.endsWith("realignment") || jobtitle
158                       .indexOf("profile") == -1))
159       {
160         int pos = jobtitle.indexOf("alignment");
161         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
162                 + altitle;
163       }
164       else
165       {
166         jobtitle = WebServiceName + " of " + altitle;
167       }
168     }
169     else
170     {
171       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
172               + "alignment of " + altitle;
173     }
174
175     MsaWSThread msathread = new MsaWSThread(server, preset, paramset,
176             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
177             submitGaps, preserveOrder, seqdataset);
178     if (msathread.hasValidInput())
179     {
180       wsInfo.setthisService(msathread);
181       wsInfo.setVisible(true);
182       msathread.start();
183     }
184     else
185     {
186       JOptionPane.showMessageDialog(alignFrame,
187               MessageManager.getString("info.invalid_msa_input_mininfo"),
188               MessageManager.getString("info.invalid_msa_notenough"),
189               JOptionPane.INFORMATION_MESSAGE);
190       wsInfo.setVisible(false);
191     }
192   }
193
194   public static void main(String[] args)
195   {
196     System.out.println("A".matches("(-*[a-zA-Z]-*){1}[a-zA-Z-]*"));
197   }
198
199
200
201   protected String getServiceActionKey()
202   {
203     return "MsaWS";
204   }
205
206   protected String getServiceActionDescription()
207   {
208     return "Multiple Sequence Alignment";
209   }
210
211   /**
212    * look at ourselves and work out if we are a service that can take a profile
213    * and align to it
214    * 
215    * @return true if we can send gapped sequences to the alignment service
216    */
217   private boolean canSubmitGaps()
218   {
219     // TODO: query service or extract service handle props to check if we can
220     // realign
221     return (WebServiceName.indexOf("lustal") > -1); // cheat!
222   }
223
224   public void attachWSMenuEntry(JMenu rmsawsmenu,
225           final Jws2Instance service, final AlignFrame alignFrame)
226   {
227     if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
228     {
229       // Alignment dependent analysis calculation WS gui
230       return;
231     }
232     setWebService(service, true); // headless
233     boolean finished = true, submitGaps = false;
234     JMenu msawsmenu = rmsawsmenu;
235     String svcname = WebServiceName;
236     if (svcname.endsWith("WS"))
237     {
238       svcname = svcname.substring(0, svcname.length() - 2);
239     }
240     String calcName = svcname + " ";
241     if (canSubmitGaps())
242     {
243       msawsmenu = new JMenu(svcname);
244       rmsawsmenu.add(msawsmenu);
245       calcName = "";
246     }
247     boolean hasparams = service.hasParameters();
248     do
249     {
250       String action = "Align ";
251       if (submitGaps == true)
252       {
253         action = "Realign ";
254         msawsmenu = new JMenu(MessageManager.formatMessage(
255                 "label.realign_with_params", new String[]
256                 { svcname }));
257         msawsmenu.setToolTipText(MessageManager
258                 .getString("label.align_sequences_to_existing_alignment"));
259         rmsawsmenu.add(msawsmenu);
260       }
261       final boolean withGaps = submitGaps;
262
263       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
264               "label.calcname_with_default_settings", new String[]
265               { calcName }));
266       method.setToolTipText(MessageManager.formatMessage(
267               "label.action_with_default_settings", new String[]
268               { action }));
269
270       method.addActionListener(new ActionListener()
271       {
272         public void actionPerformed(ActionEvent e)
273         {
274           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
275
276           if (msa != null)
277           {
278           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
279                   true, alignFrame.getViewport().getAlignment()
280                           .getDataset(), alignFrame);
281           }
282
283         }
284       });
285       msawsmenu.add(method);
286       if (hasparams)
287       {
288         // only add these menu options if the service has user-modifiable
289         // arguments
290         method = new JMenuItem(
291                 MessageManager.getString("label.edit_settings_and_run"));
292         method.setToolTipText(MessageManager
293                 .getString("label.view_and_change_parameters_before_alignment"));
294
295         method.addActionListener(new ActionListener()
296         {
297           public void actionPerformed(ActionEvent e)
298           {
299             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
300             if (msa != null)
301             {
302               new MsaWSClient(service, null, null, true, alignFrame
303                     .getTitle(), msa, withGaps, true, alignFrame
304                     .getViewport().getAlignment().getDataset(), alignFrame);
305             }
306
307           }
308         });
309         msawsmenu.add(method);
310         List<WsParamSetI> presets = service.getParamStore().getPresets();
311         if (presets != null && presets.size() > 0)
312         {
313           JMenu presetlist = new JMenu(MessageManager.formatMessage(
314                   "label.run_with_preset_params", new String[]
315                   { calcName }));
316
317           final int showToolTipFor = ToolTipManager.sharedInstance()
318                   .getDismissDelay();
319           for (final WsParamSetI preset : presets)
320           {
321             final JMenuItem methodR = new JMenuItem(preset.getName());
322             final int QUICK_TOOLTIP = 1500;
323             // JAL-1582 shorten tooltip display time in these menu items as
324             // they can obscure other options
325             methodR.addMouseListener(new MouseAdapter()
326             {
327               @Override
328               public void mouseEntered(MouseEvent e)
329               {
330                 ToolTipManager.sharedInstance().setDismissDelay(
331                         QUICK_TOOLTIP);
332               }
333
334               @Override
335               public void mouseExited(MouseEvent e)
336               {
337                 ToolTipManager.sharedInstance().setDismissDelay(showToolTipFor);
338               }
339
340             });
341             methodR.setToolTipText(JvSwingUtils.wrapTooltip(
342                     true,
343                     "<p><strong>"
344                             + (preset.isModifiable() ? MessageManager
345                                     .getString("label.user_preset")
346                                     : MessageManager
347                                             .getString("label.service_preset"))
348                             + "</strong><br/>" + preset.getDescription()
349                             + "</p>"));
350             methodR.addActionListener(new ActionListener()
351             {
352               public void actionPerformed(ActionEvent e)
353               {
354                 AlignmentView msa = alignFrame
355                         .gatherSequencesForAlignment();
356
357                 if (msa != null)
358                 {
359                   MsaWSClient msac = new MsaWSClient(service, preset,
360                           alignFrame.getTitle(),
361                         msa, false, true, alignFrame.getViewport()
362                                 .getAlignment().getDataset(), alignFrame);
363                 }
364
365               }
366
367             });
368             presetlist.add(methodR);
369           }
370           msawsmenu.add(presetlist);
371         }
372       }
373       if (!submitGaps && canSubmitGaps())
374       {
375         submitGaps = true;
376         finished = false;
377       }
378       else
379       {
380         finished = true;
381       }
382     } while (!finished);
383   }
384 }