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