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