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