751d3305264934abd5949ce59571f5e179474d28
[jalview.git] / src / jalview / ws / jws2 / Jws2Client.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 jalview.api.AlignCalcWorkerI;
24 import jalview.bin.Cache;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.Desktop;
27 import jalview.gui.JvSwingUtils;
28 import jalview.gui.WebserviceInfo;
29 import jalview.gui.WsJobParameters;
30 import jalview.util.MessageManager;
31 import jalview.ws.jws2.dm.AAConSettings;
32 import jalview.ws.jws2.dm.JabaWsParamSet;
33 import jalview.ws.jws2.jabaws2.Jws2Instance;
34 import jalview.ws.params.WsParamSetI;
35 import jalview.ws.uimodel.AlignAnalysisUIText;
36
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.util.List;
40
41 import javax.swing.JCheckBoxMenuItem;
42 import javax.swing.JMenu;
43 import javax.swing.JMenuItem;
44 import javax.swing.event.MenuEvent;
45 import javax.swing.event.MenuListener;
46
47 import compbio.metadata.Argument;
48
49 /**
50  * provides metadata for a jabaws2 service instance - resolves names, etc.
51  * 
52  * @author JimP
53  * 
54  */
55 public abstract class Jws2Client extends jalview.ws.WSClient
56 {
57   protected AlignFrame alignFrame;
58
59   protected WsParamSetI preset;
60
61   protected List<Argument> paramset;
62
63   public Jws2Client(AlignFrame _alignFrame, WsParamSetI preset,
64           List<Argument> arguments)
65   {
66     alignFrame = _alignFrame;
67     this.preset = preset;
68     if (preset != null)
69     {
70       if (!((preset instanceof JabaPreset) || preset instanceof JabaWsParamSet))
71       {
72         /*
73          * { this.preset = ((JabaPreset) preset).p; } else if (preset instanceof
74          * JabaWsParamSet) { List<Argument> newargs = new ArrayList<Argument>();
75          * JabaWsParamSet pset = ((JabaWsParamSet) preset); for (Option opt :
76          * pset.getjabaArguments()) { newargs.add(opt); } if (arguments != null
77          * && arguments.size() > 0) { // merge arguments with preset's own
78          * arguments. for (Argument opt : arguments) { newargs.add(opt); } }
79          * paramset = newargs; } else {
80          */
81         throw new Error(MessageManager.getString("error.implementation_error_can_only_instantiate_jaba_param_sets"));
82       }
83     }
84     else
85     {
86       // just provided with a bunch of arguments
87       this.paramset = arguments;
88     }
89   }
90
91   boolean processParams(Jws2Instance sh, boolean editParams)
92   {
93     return processParams(sh, editParams, false);
94   }
95
96   protected boolean processParams(Jws2Instance sh, boolean editParams,
97           boolean adjustingExisting)
98   {
99
100     if (editParams)
101     {
102       if (sh.paramStore == null)
103       {
104         sh.paramStore = new JabaParamStore(sh,
105                 Desktop.getUserParameterStore());
106       }
107       WsJobParameters jobParams = (preset == null && paramset != null && paramset
108               .size() > 0) ? new WsJobParameters(null, sh, null, paramset)
109               : new WsJobParameters(sh, preset);
110       if (adjustingExisting)
111       {
112         jobParams.setName(MessageManager
113                 .getString("label.adjusting_parameters_for_calculation"));
114       }
115       if (!jobParams.showRunDialog())
116       {
117         return false;
118       }
119       WsParamSetI prset = jobParams.getPreset();
120       if (prset == null)
121       {
122         paramset = jobParams.isServiceDefaults() ? null : JabaParamStore
123                 .getJabafromJwsArgs(jobParams.getJobParams());
124         this.preset = null;
125       }
126       else
127       {
128         this.preset = prset; // ((JabaPreset) prset).p;
129         paramset = null; // no user supplied parameters.
130       }
131     }
132     return true;
133
134   }
135
136   public Jws2Client()
137   {
138     // anonymous constructor - used for headless method calls only
139   }
140
141   protected WebserviceInfo setWebService(Jws2Instance serv, boolean b)
142   {
143     // serviceHandle = serv;
144     String serviceInstance = serv.action; // serv.service.getClass().getName();
145     WebServiceName = serv.serviceType;
146     WebServiceJobTitle = serv.getActionText();
147     WsURL = serv.hosturl;
148     if (!b)
149     {
150       return new WebserviceInfo(WebServiceJobTitle, WebServiceJobTitle
151               + " using service hosted at " + serv.hosturl + "\n"
152               + (serv.description != null ? serv.description : ""), false);
153     }
154     return null;
155   }
156
157   /*
158    * Jws2Instance serviceHandle; (non-Javadoc)
159    * 
160    * @see jalview.ws.WSMenuEntryProviderI#attachWSMenuEntry(javax.swing.JMenu,
161    * jalview.gui.AlignFrame)
162    * 
163    * @Override public void attachWSMenuEntry(JMenu wsmenu, AlignFrame
164    * alignFrame) { if (serviceHandle==null) { throw new
165    * Error("Implementation error: No service handle for this Jws2 service."); }
166    * attachWSMenuEntry(wsmenu, serviceHandle, alignFrame); }
167    */
168   /**
169    * add the menu item for a particular jws2 service instance
170    * 
171    * @param wsmenu
172    * @param service
173    * @param alignFrame
174    */
175   abstract void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
176           final AlignFrame alignFrame);
177   
178
179   protected boolean registerAAConWSInstance(final JMenu wsmenu,
180           final Jws2Instance service, final AlignFrame alignFrame)
181   {
182     final AlignAnalysisUIText aaui = service.getAlignAnalysisUI(); // null ; //
183                                                                    // AlignAnalysisUIText.aaConGUI.get(service.serviceType.toString());
184     if (aaui == null)
185     {
186       // not an instantaneous calculation GUI type service
187       return false;
188     }
189     // create the instaneous calculation GUI bits and update state if existing
190     // GUI elements already present
191
192     JCheckBoxMenuItem _aaConEnabled = null;
193     for (int i = 0; i < wsmenu.getItemCount(); i++)
194     {
195       JMenuItem item = wsmenu.getItem(i);
196       if (item instanceof JCheckBoxMenuItem
197               && item.getText().equals(aaui.getAAconToggle()))
198       {
199         _aaConEnabled = (JCheckBoxMenuItem) item;
200       }
201     }
202     // is there an aaCon worker already present - if so, set it to use the
203     // given service handle
204     {
205       List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
206               .getCalcManager()
207               .getRegisteredWorkersOfClass(aaui.getClient());
208       if (aaconClient != null && aaconClient.size() > 0)
209       {
210         AbstractJabaCalcWorker worker = (AbstractJabaCalcWorker) aaconClient
211                 .get(0);
212         if (!worker.service.hosturl.equals(service.hosturl))
213         {
214           // javax.swing.SwingUtilities.invokeLater(new Runnable()
215           {
216             // @Override
217             // public void run()
218             {
219               removeCurrentAAConWorkerFor(aaui, alignFrame);
220               buildCurrentAAConWorkerFor(aaui, alignFrame, service);
221             }
222           }// );
223         }
224       }
225     }
226
227     // is there a service already registered ? there shouldn't be if we are
228     // being called correctly
229     if (_aaConEnabled == null)
230     {
231       final JCheckBoxMenuItem aaConEnabled = new JCheckBoxMenuItem(
232               aaui.getAAconToggle());
233
234       aaConEnabled.setToolTipText(JvSwingUtils.wrapTooltip(true, aaui.getAAconToggleTooltip()));
235       aaConEnabled.addActionListener(new ActionListener()
236       {
237         @Override
238         public void actionPerformed(ActionEvent arg0)
239         {
240           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
241                   .getCalcManager()
242                   .getRegisteredWorkersOfClass(aaui.getClient());
243           if (aaconClient != null && aaconClient.size() > 0)
244           {
245             removeCurrentAAConWorkerFor(aaui, alignFrame);
246           }
247           else
248           {
249             buildCurrentAAConWorkerFor(aaui, alignFrame);
250
251           }
252         }
253
254       });
255       wsmenu.add(aaConEnabled);
256       final JMenuItem modifyParams = new JMenuItem(aaui.getAAeditSettings());
257       modifyParams.setToolTipText("<html><p>"
258               + JvSwingUtils.wrapTooltip(false, aaui.getAAeditSettingsTooltip()
259                       + "</p>") + "</html>");
260       modifyParams.addActionListener(new ActionListener()
261       {
262
263         @Override
264         public void actionPerformed(ActionEvent arg0)
265         {
266           showAAConAnnotationSettingsFor(aaui, alignFrame);
267         }
268       });
269       wsmenu.add(modifyParams);
270       wsmenu.addMenuListener(new MenuListener()
271       {
272
273         @Override
274         public void menuSelected(MenuEvent arg0)
275         {
276           // TODO: refactor to the implementing class.
277           if (alignFrame.getViewport().getAlignment().isNucleotide() ? aaui
278                   .isNa() : aaui.isPr())
279           {
280             aaConEnabled.setEnabled(true);
281             modifyParams.setEnabled(true);
282           }
283           else
284           {
285             aaConEnabled.setEnabled(false);
286             modifyParams.setEnabled(false);
287           }
288           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
289                   .getCalcManager()
290                   .getRegisteredWorkersOfClass(aaui.getClient());
291           if (aaconClient != null && aaconClient.size() > 0)
292           {
293             aaConEnabled.setSelected(true);
294           }
295           else
296           {
297             aaConEnabled.setSelected(false);
298           }
299         }
300
301         @Override
302         public void menuDeselected(MenuEvent arg0)
303         {
304           // TODO Auto-generated method stub
305
306         }
307
308         @Override
309         public void menuCanceled(MenuEvent arg0)
310         {
311           // TODO Auto-generated method stub
312
313         }
314       });
315
316     }
317     return true;
318   }
319
320   private static void showAAConAnnotationSettingsFor(
321           final AlignAnalysisUIText aaui, AlignFrame alignFrame)
322   {
323     /*
324      * preferred settings Whether AACon is automatically recalculated Which
325      * AACon server to use What parameters to use
326      */
327     // could actually do a class search for this too
328     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
329             .getCalcIdSettingsFor(aaui.getCalcId());
330     if (fave == null)
331     {
332       fave = createDefaultAAConSettings(aaui);
333     }
334     new SequenceAnnotationWSClient(fave, alignFrame, true);
335
336   }
337
338   private static void buildCurrentAAConWorkerFor(
339           final AlignAnalysisUIText aaui, AlignFrame alignFrame)
340   {
341     buildCurrentAAConWorkerFor(aaui, alignFrame, null);
342   }
343
344   private static void buildCurrentAAConWorkerFor(
345           final AlignAnalysisUIText aaui, AlignFrame alignFrame,
346           Jws2Instance service)
347   {
348     /*
349      * preferred settings Whether AACon is automatically recalculated Which
350      * AACon server to use What parameters to use
351      */
352     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
353             .getCalcIdSettingsFor(aaui.getCalcId());
354     if (fave == null)
355     {
356       fave = createDefaultAAConSettings(aaui, service);
357     }
358     else
359     {
360       if (service != null
361               && !fave.getService().hosturl.equals(service.hosturl))
362       {
363         Cache.log.debug("Changing AACon service to " + service.hosturl
364                 + " from " + fave.getService().hosturl);
365         fave.setService(service);
366       }
367     }
368     new SequenceAnnotationWSClient(fave, alignFrame, false);
369   }
370
371   private static AAConSettings createDefaultAAConSettings(
372           AlignAnalysisUIText aaui)
373   {
374     return createDefaultAAConSettings(aaui, null);
375   }
376
377   private static AAConSettings createDefaultAAConSettings(
378           AlignAnalysisUIText aaui, Jws2Instance service)
379   {
380     if (service != null)
381     {
382       if (!service.serviceType.toString().equals(
383               compbio.ws.client.Services.AAConWS.toString()))
384       {
385         Cache.log
386                 .warn("Ignoring invalid preferred service for AACon calculations (service type was "
387                         + service.serviceType + ")");
388         service = null;
389       }
390       else
391       {
392         // check service is actually in the list of currently avaialable
393         // services
394         if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
395         {
396           // it isn't ..
397           service = null;
398         }
399       }
400     }
401     if (service == null)
402     {
403       // get the default service for AACon
404       service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
405               aaui.getServiceType());
406     }
407     if (service == null)
408     {
409       // TODO raise dialog box explaining error, and/or open the JABA
410       // preferences menu.
411       throw new Error(MessageManager.getString("error.no_aacon_service_found"));
412     }
413     return new AAConSettings(true, service, null, null);
414   }
415
416   private static void removeCurrentAAConWorkerFor(AlignAnalysisUIText aaui,
417           AlignFrame alignFrame)
418   {
419     alignFrame.getViewport().getCalcManager()
420             .removeRegisteredWorkersOfClass(aaui.getClient());
421   }
422
423 }