JAL-676 JAL-1001 - simple menu item to access documentation for services without...
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.java
1 /**
2  * 
3  */
4 package jalview.ws.jws2;
5
6 import jalview.api.AlignCalcWorkerI;
7 import jalview.bin.Cache;
8 import jalview.gui.AlignFrame;
9 import jalview.gui.Desktop;
10 import jalview.gui.JvSwingUtils;
11 import jalview.ws.jws2.dm.AAConsSettings;
12 import jalview.ws.jws2.jabaws2.Jws2Instance;
13 import jalview.ws.params.WsParamSetI;
14
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.util.List;
18
19 import javax.swing.JCheckBoxMenuItem;
20 import javax.swing.JMenu;
21 import javax.swing.JMenuItem;
22 import javax.swing.JOptionPane;
23 import javax.swing.event.MenuEvent;
24 import javax.swing.event.MenuListener;
25
26 /**
27  * @author jprocter 
28  * 
29  */
30 public class SequenceAnnotationWSClient extends Jws2Client 
31 {
32
33   public static final String AAConsCalcId = "jabaws2.AACons";
34
35   /**
36    * initialise a client so its attachWSMenuEntry method can be called.
37    */
38   public SequenceAnnotationWSClient()
39   {
40     // TODO Auto-generated constructor stub
41   }
42
43   public SequenceAnnotationWSClient(final Jws2Instance sh,
44           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
45   {
46     super(alignFrame, preset, null);
47     initSequenceAnnotationWSClient(sh, alignFrame, preset, editParams);
48   }
49
50   public void initSequenceAnnotationWSClient(final Jws2Instance sh,
51           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
52   {
53     if (alignFrame.getViewport().getAlignment().isNucleotide())
54     {
55       JOptionPane.showMessageDialog(Desktop.desktop, sh.serviceType
56               + " can only be used\nfor amino acid alignments.",
57               "Wrong type of sequences!", JOptionPane.WARNING_MESSAGE);
58       return;
59
60     }
61     if (sh.action.toLowerCase().contains("conservation"))
62     {
63       // Build an AACons style client - take alignment, return annotation for
64       // columns
65
66       List<AlignCalcWorkerI> clnts = alignFrame.getViewport()
67               .getCalcManager()
68               .getRegisteredWorkersOfClass(AAConsClient.class);
69       if (clnts == null || clnts.size() == 0)
70       {
71         if (!processParams(sh, editParams))
72         {
73           return;
74         }
75         AAConsClient worker;
76         alignFrame
77                 .getViewport()
78                 .getCalcManager()
79                 .registerWorker(
80                         worker = new AAConsClient(sh, alignFrame,
81                                 this.preset, paramset));
82         alignFrame.getViewport().getCalcManager().startWorker(worker);
83
84       }
85       else
86       {
87         AAConsClient worker = (AAConsClient) clnts.get(0);
88         if (editParams)
89         {
90           paramset = worker.getArguments();
91           preset = worker.getPreset();
92         }
93
94         if (!processParams(sh, editParams, true))
95         {
96           return;
97         }
98         // reinstate worker if it was blacklisted (might have happened due to
99         // invalid parameters)
100         alignFrame.getViewport().getCalcManager().workerMayRun(worker);
101         worker.updateParameters(this.preset, paramset);
102
103       }
104     }
105     if (sh.action.toLowerCase().contains("disorder"))
106     {
107       // build IUPred style client. take sequences, returns annotation per
108       // sequence.
109       if (!processParams(sh, editParams))
110       {
111         return;
112       }
113
114       alignFrame
115               .getViewport()
116               .getCalcManager()
117               .startWorker(
118                       new AADisorderClient(sh, alignFrame, preset, paramset));
119     }
120
121   }
122
123   public SequenceAnnotationWSClient(AAConsSettings fave,
124           AlignFrame alignFrame, boolean b)
125   {
126     super(alignFrame, fave.getPreset(), fave.getJobArgset());
127     initSequenceAnnotationWSClient(fave.getService(), alignFrame,
128             fave.getPreset(), b);
129   }
130
131   /*
132    * (non-Javadoc)
133    * 
134    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
135    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
136    */
137   public void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
138           final AlignFrame alignFrame)
139   {
140     if (service.serviceType.equals(compbio.ws.client.Services.AAConWS
141             .toString()))
142     {
143       registerAAConsWSInstance(wsmenu, service, alignFrame);
144       return;
145     }
146     boolean hasparams = service.hasParameters();
147     // Assume name ends in WS
148     String calcName = service.serviceType.substring(0,
149             service.serviceType.length() - 2);
150
151     JMenuItem aacons = new JMenuItem(calcName + " Defaults");
152     aacons.addActionListener(new ActionListener()
153     {
154
155       @Override
156       public void actionPerformed(ActionEvent e)
157       {
158         new SequenceAnnotationWSClient(service, alignFrame, null, false);
159       }
160     });
161     wsmenu.add(aacons);
162     if (hasparams)
163     {
164       // only add these menu options if the service has user-modifiable
165       // arguments
166       aacons = new JMenuItem("Edit settings and run ...");
167       aacons.setToolTipText("View and change parameters before running calculation");
168
169       aacons.addActionListener(new ActionListener()
170       {
171         public void actionPerformed(ActionEvent e)
172         {
173           new SequenceAnnotationWSClient(service, alignFrame, null, true);
174         }
175       });
176       wsmenu.add(aacons);
177       List<WsParamSetI> presets = service.getParamStore().getPresets();
178       if (presets != null && presets.size() > 0)
179       {
180         JMenu presetlist = new JMenu("Run " + calcName + "with preset");
181
182         for (final WsParamSetI preset : presets)
183         {
184           final JMenuItem methodR = new JMenuItem(preset.getName());
185           methodR.setToolTipText("<html><p>"
186                   + JvSwingUtils.wrapTooltip("<strong>"
187                           + (preset.isModifiable() ? "User Preset"
188                                   : "Service Preset") + "</strong><br/>"
189                           + preset.getDescription() + "</p>") + "</html>");
190           methodR.addActionListener(new ActionListener()
191           {
192             public void actionPerformed(ActionEvent e)
193             {
194               new SequenceAnnotationWSClient(service, alignFrame, preset,
195                       false);
196             }
197
198           });
199           presetlist.add(methodR);
200         }
201         wsmenu.add(presetlist);
202       }
203
204     } else {
205       aacons = new JMenuItem("View documentation");
206       if (service.docUrl!=null)
207       {
208         aacons.addActionListener(new ActionListener()
209         {
210           
211           @Override
212           public void actionPerformed(ActionEvent arg0)
213           {
214             Desktop.instance.showUrl(service.docUrl);
215           }
216         });
217         aacons.setToolTipText("<html>"+JvSwingUtils.wrapTooltip("View <a href=\""+service.docUrl+"\">"+service.docUrl+"</a>")+"</html>");
218         wsmenu.add(aacons);
219       }
220     }
221   }
222
223   private final String AAconsToggle = "AACons Calculations",
224           AAconsToggleTooltip = "When checked, AACons calculations are updated automatically.",
225           AAeditSettings = "Change AACons Settings...",
226           AAeditSettingsTooltip = "Modify settings for AACons calculations.";
227
228   // private final enableAAConsCalculation(final AlignFrame alignFrame, )
229   private void registerAAConsWSInstance(final JMenu wsmenu,
230           final Jws2Instance service, final AlignFrame alignFrame)
231   {
232     // register this in the AACons settings set
233     JCheckBoxMenuItem _aaConsEnabled = null;
234     for (int i = 0; i < wsmenu.getItemCount(); i++)
235     {
236       JMenuItem item = wsmenu.getItem(i);
237       if (item instanceof JCheckBoxMenuItem
238               && item.getText().equals(AAconsToggle))
239       {
240         _aaConsEnabled = (JCheckBoxMenuItem) item;
241       }
242     }
243     // is there an aaCons worker already present - if so, set it to use the
244     // given service handle
245     {
246       List<AlignCalcWorkerI> aaconsClient = alignFrame.getViewport()
247               .getCalcManager()
248               .getRegisteredWorkersOfClass(AAConsClient.class);
249       if (aaconsClient != null && aaconsClient.size() > 0)
250       {
251         AAConsClient worker = (AAConsClient) aaconsClient.get(0);
252         if (!worker.service.hosturl.equals(service.hosturl))
253         {
254          // javax.swing.SwingUtilities.invokeLater(new Runnable()
255           {
256          //   @Override
257          //   public void run()
258             {
259               removeCurrentAAConsWorkerFor(alignFrame);
260               buildCurrentAAConsWorkerFor(alignFrame, service);
261             }
262           }//);
263         }
264       }
265     }
266
267     // is there a service already registered ? there shouldn't be if we are
268     // being called correctly
269     if (_aaConsEnabled == null)
270     {
271       final JCheckBoxMenuItem aaConsEnabled = new JCheckBoxMenuItem(
272               AAconsToggle);
273       wsmenu.addMenuListener(new MenuListener()
274       {
275
276         @Override
277         public void menuSelected(MenuEvent arg0)
278         {
279           wsmenu.setEnabled(!alignFrame.getViewport().getAlignment()
280                   .isNucleotide());
281           List<AlignCalcWorkerI> aaconsClient = alignFrame.getViewport()
282                   .getCalcManager()
283                   .getRegisteredWorkersOfClass(AAConsClient.class);
284           if (aaconsClient != null && aaconsClient.size() > 0)
285           {
286             aaConsEnabled.setSelected(true);
287           }
288           else
289           {
290             aaConsEnabled.setSelected(false);
291           }
292         }
293
294         @Override
295         public void menuDeselected(MenuEvent arg0)
296         {
297           // TODO Auto-generated method stub
298
299         }
300
301         @Override
302         public void menuCanceled(MenuEvent arg0)
303         {
304           // TODO Auto-generated method stub
305
306         }
307       });
308       aaConsEnabled.setToolTipText("<html><p>"
309               + JvSwingUtils.wrapTooltip(AAconsToggleTooltip + "</p>")
310               + "</html>");
311       aaConsEnabled.addActionListener(new ActionListener()
312       {
313         @Override
314         public void actionPerformed(ActionEvent arg0)
315         {
316           // aaConsEnabled.setSelected(!aaConsEnabled.isSelected());
317           List<AlignCalcWorkerI> aaconsClient = alignFrame.getViewport()
318                   .getCalcManager()
319                   .getRegisteredWorkersOfClass(AAConsClient.class);
320           if (aaconsClient != null && aaconsClient.size() > 0)
321           {
322             removeCurrentAAConsWorkerFor(alignFrame);
323           }
324           else
325           {
326             buildCurrentAAConsWorkerFor(alignFrame);
327
328           }
329         }
330
331       });
332       wsmenu.add(aaConsEnabled);
333       JMenuItem modifyParams = new JMenuItem(AAeditSettings);
334       modifyParams.setToolTipText("<html><p>"
335               + JvSwingUtils.wrapTooltip(AAeditSettingsTooltip + "</p>")
336               + "</html>");
337       modifyParams.addActionListener(new ActionListener()
338       {
339
340         @Override
341         public void actionPerformed(ActionEvent arg0)
342         {
343           showAAConsAnnotationSettingsFor(alignFrame);
344         }
345       });
346       wsmenu.add(modifyParams);
347
348     }
349   }
350
351   private static void showAAConsAnnotationSettingsFor(AlignFrame alignFrame)
352   {
353     /*
354      * preferred settings Whether AACons is automatically recalculated Which
355      * AACons server to use What parameters to use
356      */
357     // could actually do a class search for this too
358     AAConsSettings fave = (AAConsSettings) alignFrame.getViewport()
359             .getCalcIdSettingsFor(AAConsCalcId);
360     if (fave == null)
361     {
362       fave = createDefaultAAConsSettings();
363     }
364     new SequenceAnnotationWSClient(fave, alignFrame, true);
365
366   }
367
368   private static void buildCurrentAAConsWorkerFor(AlignFrame alignFrame)
369   {
370     buildCurrentAAConsWorkerFor(alignFrame, null);
371   }
372
373   private static void buildCurrentAAConsWorkerFor(AlignFrame alignFrame,
374           Jws2Instance service)
375   {
376     /*
377      * preferred settings Whether AACons is automatically recalculated Which
378      * AACons server to use What parameters to use
379      */
380     AAConsSettings fave = (AAConsSettings) alignFrame.getViewport()
381             .getCalcIdSettingsFor(AAConsCalcId);
382     if (fave == null)
383     {
384       fave = createDefaultAAConsSettings(service);
385     }
386     else
387     {
388       if (service != null
389               && !fave.getService().hosturl.equals(service.hosturl))
390       {
391         Cache.log.debug("Changing AACons service to " + service.hosturl
392                 + " from " + fave.getService().hosturl);
393         fave.setService(service);
394       }
395     }
396     new SequenceAnnotationWSClient(fave, alignFrame, false);
397   }
398
399   private static AAConsSettings createDefaultAAConsSettings()
400   {
401     return createDefaultAAConsSettings(null);
402   }
403
404   private static AAConsSettings createDefaultAAConsSettings(
405           Jws2Instance service)
406   {
407     if (service != null)
408     {
409       if (!service.serviceType.toString().equals(
410               compbio.ws.client.Services.AAConWS.toString()))
411       {
412         Cache.log
413                 .warn("Ignoring invalid preferred service for AACons calculations (service type was "
414                         + service.serviceType + ")");
415         service = null;
416       }
417       else
418       {
419         // check service is actually in the list of currently avaialable
420         // services
421         if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
422         {
423           // it isn't ..
424           service = null;
425         }
426       }
427     }
428     if (service == null)
429     {
430       // get the default service for AACons
431       service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
432               compbio.ws.client.Services.AAConWS.toString());
433       /*
434        * for (Jws2Instance sv : Jws2Discoverer.getDiscoverer().getServices()) {
435        * if (sv.serviceType.toString().equals(
436        * compbio.ws.client.Services.AAConWS.toString())) { service = sv; break;
437        * } }
438        */
439     }
440     if (service == null)
441     {
442       // TODO raise dialog box explaining error, and/or open the JABA
443       // preferences menu.
444       throw new Error("No AACons service found.");
445     }
446     return new AAConsSettings(true, service, null, null);
447   }
448
449   private static void removeCurrentAAConsWorkerFor(AlignFrame alignFrame)
450   {
451     alignFrame.getViewport().getCalcManager()
452             .removeRegisteredWorkersOfClass(AAConsClient.class);
453     // AAConsClient.removeAAConsAnnotation(alignFrame.alignPanel);
454
455   }
456 }