JAL-3070 standard way of storing/recovering documentation URL for service
[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 Jws2Instance 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().getRegisteredWorkersOfClass(clientClass);
87       AbstractJabaCalcWorker worker;
88       if (clnts == null || clnts.size() == 0)
89       {
90         if (!processParams(sh, editParams))
91         {
92           return;
93         }
94         try
95         {
96           worker = (AbstractJabaCalcWorker) (clientClass
97                   .getConstructor(new Class[]
98                   { Jws2Instance.class, AlignFrame.class, WsParamSetI.class,
99                       List.class })
100                   .newInstance(new Object[]
101                   { sh, alignFrame, this.preset, paramset }));
102         } catch (Exception x)
103         {
104           x.printStackTrace();
105           throw new Error(
106                   MessageManager.getString("error.implementation_error"),
107                   x);
108         }
109         alignFrame.getViewport().getCalcManager().registerWorker(worker);
110         alignFrame.getViewport().getCalcManager().startWorker(worker);
111
112       }
113       else
114       {
115         worker = (AbstractJabaCalcWorker) clnts.get(0);
116         if (editParams)
117         {
118           paramset = worker.getArguments();
119           preset = worker.getPreset();
120         }
121
122         if (!processParams(sh, editParams, true))
123         {
124           return;
125         }
126         // reinstate worker if it was blacklisted (might have happened due to
127         // invalid parameters)
128         alignFrame.getViewport().getCalcManager().enableWorker(worker);
129         worker.updateParameters(this.preset, paramset);
130       }
131     }
132     if (sh.getAction().toLowerCase().contains("disorder"))
133     {
134       // build IUPred style client. take sequences, returns annotation per
135       // sequence.
136       if (!processParams(sh, editParams))
137       {
138         return;
139       }
140
141       alignFrame.getViewport().getCalcManager().startWorker(
142               new AADisorderClient(sh, alignFrame, preset, paramset));
143     }
144   }
145
146   public SequenceAnnotationWSClient(AAConSettings fave,
147           AlignFrame alignFrame, boolean b)
148   {
149     super(alignFrame, fave.getPreset(), fave.getArgumentSet());
150     initSequenceAnnotationWSClient(fave.getService(), alignFrame,
151             fave.getPreset(), b);
152   }
153
154   /*
155    * (non-Javadoc)
156    * 
157    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
158    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
159    */
160   @Override
161   public void attachWSMenuEntry(JMenu wsmenu,
162           final ServiceWithParameters service,
163           final AlignFrame alignFrame)
164   {
165     if (registerAAConWSInstance(wsmenu, service, alignFrame))
166     {
167       // Alignment dependent analysis calculation WS gui
168       return;
169     }
170     boolean hasparams = service.hasParameters();
171     // Assume name ends in WS
172     String calcName = service.getName().substring(0,
173             service.getName().length() - 2);
174
175     JMenuItem annotservice = new JMenuItem(MessageManager.formatMessage(
176             "label.calcname_with_default_settings", new String[]
177             { calcName }));
178     annotservice.addActionListener(new ActionListener()
179     {
180
181       @Override
182       public void actionPerformed(ActionEvent e)
183       {
184         new SequenceAnnotationWSClient((Jws2Instance) service, alignFrame,
185                 null, false);
186       }
187     });
188     wsmenu.add(annotservice);
189     if (hasparams)
190     {
191       // only add these menu options if the service has user-modifiable
192       // arguments
193       annotservice = new JMenuItem(
194               MessageManager.getString("label.edit_settings_and_run"));
195       annotservice.setToolTipText(MessageManager.getString(
196               "label.view_and_change_parameters_before_running_calculation"));
197
198       annotservice.addActionListener(new ActionListener()
199       {
200         @Override
201         public void actionPerformed(ActionEvent e)
202         {
203           new SequenceAnnotationWSClient((Jws2Instance) service, alignFrame,
204                   null, true);
205         }
206       });
207       wsmenu.add(annotservice);
208       List<WsParamSetI> presets = service.getParamStore().getPresets();
209       if (presets != null && presets.size() > 0)
210       {
211         JMenu presetlist = new JMenu(MessageManager
212                 .formatMessage("label.run_with_preset", new String[]
213                 { calcName }));
214
215         for (final WsParamSetI preset : presets)
216         {
217           final JMenuItem methodR = new JMenuItem(preset.getName());
218           methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<strong>"
219                   + (preset.isModifiable()
220                           ? MessageManager.getString("label.user_preset")
221                           : MessageManager
222                                   .getString("label.service_preset"))
223                   + "</strong><br/>" + preset.getDescription()));
224           methodR.addActionListener(new ActionListener()
225           {
226             @Override
227             public void actionPerformed(ActionEvent e)
228             {
229               new SequenceAnnotationWSClient((Jws2Instance) service,
230                       alignFrame, preset,
231                       false);
232             }
233
234           });
235           presetlist.add(methodR);
236         }
237         wsmenu.add(presetlist);
238       }
239
240     }
241     else
242     {
243       annotservice = new JMenuItem(
244               MessageManager.getString("label.view_documentation"));
245       if (service != null && service.hasDocumentationUrl())
246       {
247         annotservice.addActionListener(new ActionListener()
248         {
249
250           @Override
251           public void actionPerformed(ActionEvent arg0)
252           {
253             Desktop.instance.showUrl(service.getDocumentationUrl());
254           }
255         });
256         annotservice.setToolTipText(
257                 JvSwingUtils.wrapTooltip(true, MessageManager.formatMessage(
258                         "label.view_service_doc_url", new String[]
259                         { service.getDocumentationUrl(),
260                             service.getDocumentationUrl() })));
261         wsmenu.add(annotservice);
262       }
263     }
264   }
265 }