Merge branch 'Jalview-JS/develop' into merge_js_develop
[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   public void initSequenceAnnotationWSClient(final ServiceWithParameters sh,
65           AlignFrame alignFrame, 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 worker = null;
89       if (clnts != null)
90       {
91         for (AlignCalcWorkerI _worker : clnts)
92         {
93           worker = (SeqAnnotationServiceCalcWorker) _worker;
94           if (worker.hasService()
95                   && worker.getService().getClass().equals(clientClass))
96           {
97             break;
98           }
99           worker = null;
100         }
101       }
102       if (worker == null)
103       {
104         if (!processParams(sh, editParams))
105         {
106           return;
107         }
108         try
109         {
110           worker = new SeqAnnotationServiceCalcWorker(sh, alignFrame, this.preset,
111                   paramset);
112         } catch (Exception x)
113         {
114           x.printStackTrace();
115           throw new Error(
116                   MessageManager.getString("error.implementation_error"),
117                   x);
118         }
119         alignFrame.getViewport().getCalcManager().registerWorker(worker); // also
120                                                                           // starts
121                                                                           // the
122                                                                           // worker
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.isInteractiveUpdate())
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(AutoCalcSetting 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,
176             service, alignFrame))
177     {
178       // Alignment dependent analysis calculation WS gui
179       return;
180     }
181     boolean hasparams = service.hasParameters();
182     String calcName = service.getName();
183     if (calcName.endsWith("WS"))
184     {
185       // Remove "WS" suffix
186       calcName = calcName.substring(0, calcName.length() - 2);
187     }
188
189     JMenuItem annotservice = new JMenuItem(MessageManager.formatMessage(
190             "label.calcname_with_default_settings", new String[]
191             { calcName }));
192     annotservice.addActionListener(new ActionListener()
193     {
194
195       @Override
196       public void actionPerformed(ActionEvent e)
197       {
198         new SequenceAnnotationWSClient(service, alignFrame,
199                 null, false);
200       }
201     });
202     wsmenu.add(annotservice);
203     if (hasparams)
204     {
205       // only add these menu options if the service has user-modifiable
206       // arguments
207       annotservice = new JMenuItem(
208               MessageManager.getString("label.edit_settings_and_run"));
209       annotservice.setToolTipText(MessageManager.getString(
210               "label.view_and_change_parameters_before_running_calculation"));
211
212       annotservice.addActionListener(new ActionListener()
213       {
214         @Override
215         public void actionPerformed(ActionEvent e)
216         {
217           new SequenceAnnotationWSClient(service, alignFrame,
218                   null, true);
219         }
220       });
221       wsmenu.add(annotservice);
222       List<WsParamSetI> presets = service.getParamStore().getPresets();
223       if (presets != null && presets.size() > 0)
224       {
225         JMenu presetlist = new JMenu(MessageManager
226                 .formatMessage("label.run_with_preset", new String[]
227                 { calcName }));
228
229         for (final WsParamSetI preset : presets)
230         {
231           final JMenuItem methodR = new JMenuItem(preset.getName());
232           methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<strong>"
233                   + (preset.isModifiable()
234                           ? MessageManager.getString("label.user_preset")
235                           : MessageManager
236                                   .getString("label.service_preset"))
237                   + "</strong><br/>" + preset.getDescription()));
238           methodR.addActionListener(new ActionListener()
239           {
240             @Override
241             public void actionPerformed(ActionEvent e)
242             {
243               new SequenceAnnotationWSClient(service,
244                       alignFrame, preset,
245                       false);
246             }
247
248           });
249           presetlist.add(methodR);
250         }
251         wsmenu.add(presetlist);
252       }
253
254     }
255     else
256     {
257       annotservice = new JMenuItem(
258               MessageManager.getString("label.view_documentation"));
259       if (service != null && service.hasDocumentationUrl())
260       {
261         annotservice.addActionListener(new ActionListener()
262         {
263
264           @Override
265           public void actionPerformed(ActionEvent arg0)
266           {
267             Desktop.getInstance().showUrl(service.getDocumentationUrl());
268           }
269         });
270         annotservice.setToolTipText(
271                 JvSwingUtils.wrapTooltip(true, MessageManager.formatMessage(
272                         "label.view_service_doc_url", new String[]
273                         { service.getDocumentationUrl(),
274                             service.getDocumentationUrl() })));
275         wsmenu.add(annotservice);
276       }
277     }
278   }
279 }