Merge branch 'JAL-3927_ws_preferences_dialog_fix' into alpha/JAL-3066_Jalview_212_sli...
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.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.api.AlignCalcWorkerI;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.Desktop;
26 import jalview.gui.JvSwingUtils;
27 import jalview.util.MessageManager;
28 import jalview.ws.api.ServiceWithParameters;
29 import jalview.ws.params.AutoCalcSetting;
30 import jalview.ws.params.WsParamSetI;
31 import jalview.ws.uimodel.AlignAnalysisUIText;
32
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.util.List;
36
37 import javax.swing.JMenu;
38 import javax.swing.JMenuItem;
39
40 /**
41  * @author jprocter
42  * 
43  */
44 public class SequenceAnnotationWSClient extends Jws2Client
45 {
46   /**
47    * initialise a client so its attachWSMenuEntry method can be called.
48    */
49   public SequenceAnnotationWSClient()
50   {
51     // TODO Auto-generated constructor stub
52   }
53
54   public SequenceAnnotationWSClient(final ServiceWithParameters sh,
55           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
56   {
57     super(alignFrame, preset, null);
58     initSequenceAnnotationWSClient(sh, alignFrame, preset, editParams);
59   }
60
61   // dan think. Do I need to change this method to run RNAalifold through the
62   // GUI
63
64   private void initSequenceAnnotationWSClient(final ServiceWithParameters sh,
65       AlignFrame alignFrame, final WsParamSetI preset, boolean editParams)
66   {
67     // dan changed! dan test. comment out if conditional
68     // if (alignFrame.getViewport().getAlignment().isNucleotide())
69     // {
70     // JvOptionPane.showMessageDialog(Desktop.desktop, sh.serviceType
71     // + " can only be used\nfor amino acid alignments.",
72     // "Wrong type of sequences!", JvOptionPane.WARNING_MESSAGE);
73     // return;
74     //
75     // }
76     AlignAnalysisUIText aaui = sh.getAlignAnalysisUI();
77     if (aaui != null)
78     {
79       Class clientClass = aaui.getClient();
80
81       // Build an AACon style client - take alignment, return annotation for
82       // columns
83
84       List<AlignCalcWorkerI> clnts = alignFrame.getViewport()
85           .getCalcManager()
86           .getWorkersOfClass(SeqAnnotationServiceCalcWorker.class);
87
88       SeqAnnotationServiceCalcWorker tmpworker = null;
89       if (clnts != null)
90       {
91         for (AlignCalcWorkerI _worker : clnts)
92         {
93           tmpworker = (SeqAnnotationServiceCalcWorker) _worker;
94           if (tmpworker.hasService()
95               && tmpworker.getService().getClass().equals(clientClass))
96           {
97             break;
98           }
99           tmpworker = null;
100         }
101       }
102       final var worker = tmpworker;
103       if (worker == null)
104       {
105         processParams(sh, editParams).thenAccept((startJob) -> {
106           if (startJob)
107           {
108             final SeqAnnotationServiceCalcWorker worker_;
109             try
110             {
111               worker_ = new SeqAnnotationServiceCalcWorker(sh, alignFrame, this.preset,
112                   paramset);
113             } catch (Exception x)
114             {
115               x.printStackTrace();
116               throw new Error(
117                   MessageManager.getString("error.implementation_error"),
118                   x);
119             }
120             alignFrame.getViewport().getCalcManager().registerWorker(worker_);
121                 // also starts the worker
122             startSeqAnnotationWorker(sh, alignFrame, preset, editParams);
123           }
124         });
125       }
126       else
127       {
128         WsParamSetI preset_;
129         if (editParams)
130         {
131           paramset = worker.getArguments();
132           preset_ = worker.getPreset();
133         }
134         else
135         {
136           preset_ = preset;
137         }
138         processParams(sh, editParams, true).thenAccept((startJob) -> {
139           if (startJob)
140           {
141             // reinstate worker if it was blacklisted (might have happened due
142             // to
143             // invalid parameters)
144             alignFrame.getViewport().getCalcManager().enableWorker(worker);
145             worker.updateParameters(this.preset, paramset);
146             startSeqAnnotationWorker(sh, alignFrame, preset_, editParams);
147           }
148         });
149       }
150     }
151     else
152     {
153       startSeqAnnotationWorker(sh, alignFrame, preset, editParams);
154     }
155   }
156
157   private void startSeqAnnotationWorker(ServiceWithParameters sh,
158       AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
159   {
160     if (!sh.isInteractiveUpdate())
161     {
162       // build IUPred style client. take sequences, returns annotation per
163       // sequence.
164       processParams(sh, editParams).thenAccept((startJob) -> {
165         if (startJob)
166         {
167           alignFrame.getViewport().getCalcManager().startWorker(
168               new SeqAnnotationServiceCalcWorker(sh, alignFrame, preset, paramset));
169         }
170       });
171     }
172   }
173
174   public SequenceAnnotationWSClient(AutoCalcSetting fave,
175           AlignFrame alignFrame, boolean b)
176   {
177     super(alignFrame, fave.getPreset(), fave.getArgumentSet());
178     initSequenceAnnotationWSClient(fave.getService(), alignFrame,
179             fave.getPreset(), b);
180   }
181
182   /*
183    * (non-Javadoc)
184    * 
185    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
186    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
187    */
188   @Override
189   public void attachWSMenuEntry(JMenu wsmenu,
190           final ServiceWithParameters service,
191           final AlignFrame alignFrame)
192   {
193     if (Jws2ClientFactory.registerAAConWSInstance(wsmenu,
194             service, alignFrame))
195     {
196       // Alignment dependent analysis calculation WS gui
197       return;
198     }
199     boolean hasparams = service.hasParameters();
200     String calcName = service.getName();
201     if (calcName.endsWith("WS"))
202     {
203       // Remove "WS" suffix
204       calcName = calcName.substring(0, calcName.length() - 2);
205     }
206
207     JMenuItem annotservice = new JMenuItem(MessageManager.formatMessage(
208             "label.calcname_with_default_settings", new String[]
209             { calcName }));
210     annotservice.addActionListener(new ActionListener()
211     {
212
213       @Override
214       public void actionPerformed(ActionEvent e)
215       {
216         new SequenceAnnotationWSClient(service, alignFrame,
217                 null, false);
218       }
219     });
220     wsmenu.add(annotservice);
221     if (hasparams)
222     {
223       // only add these menu options if the service has user-modifiable
224       // arguments
225       annotservice = new JMenuItem(
226               MessageManager.getString("label.edit_settings_and_run"));
227       annotservice.setToolTipText(MessageManager.getString(
228               "label.view_and_change_parameters_before_running_calculation"));
229
230       annotservice.addActionListener(new ActionListener()
231       {
232         @Override
233         public void actionPerformed(ActionEvent e)
234         {
235           new SequenceAnnotationWSClient(service, alignFrame,
236                   null, true);
237         }
238       });
239       wsmenu.add(annotservice);
240       List<WsParamSetI> presets = service.getParamStore().getPresets();
241       if (presets != null && presets.size() > 0)
242       {
243         JMenu presetlist = new JMenu(MessageManager
244                 .formatMessage("label.run_with_preset", new String[]
245                 { calcName }));
246
247         for (final WsParamSetI preset : presets)
248         {
249           final JMenuItem methodR = new JMenuItem(preset.getName());
250           methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<strong>"
251                   + (preset.isModifiable()
252                           ? MessageManager.getString("label.user_preset")
253                           : MessageManager
254                                   .getString("label.service_preset"))
255                   + "</strong><br/>" + preset.getDescription()));
256           methodR.addActionListener(new ActionListener()
257           {
258             @Override
259             public void actionPerformed(ActionEvent e)
260             {
261               new SequenceAnnotationWSClient(service,
262                       alignFrame, preset,
263                       false);
264             }
265
266           });
267           presetlist.add(methodR);
268         }
269         wsmenu.add(presetlist);
270       }
271
272     }
273     else
274     {
275       annotservice = new JMenuItem(
276               MessageManager.getString("label.view_documentation"));
277       if (service != null && service.hasDocumentationUrl())
278       {
279         annotservice.addActionListener(new ActionListener()
280         {
281
282           @Override
283           public void actionPerformed(ActionEvent arg0)
284           {
285             Desktop.getInstance().showUrl(service.getDocumentationUrl());
286           }
287         });
288         annotservice.setToolTipText(
289                 JvSwingUtils.wrapTooltip(true, MessageManager.formatMessage(
290                         "label.view_service_doc_url", new String[]
291                         { service.getDocumentationUrl(),
292                             service.getDocumentationUrl() })));
293         wsmenu.add(annotservice);
294       }
295     }
296   }
297 }