JAL-3070 remove final reference to Jws2Instance
[jalview.git] / src / jalview / ws / jws2 / Jws2Client.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.bin.Cache;
25 import jalview.gui.AlignFrame;
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.dm.JabaWsParamSet;
31 import jalview.ws.jws2.jabaws2.Jws2Instance;
32 import jalview.ws.params.ArgumentI;
33 import jalview.ws.params.WsParamSetI;
34 import jalview.ws.uimodel.AlignAnalysisUIText;
35
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.util.List;
39
40 import javax.swing.JCheckBoxMenuItem;
41 import javax.swing.JMenu;
42 import javax.swing.JMenuItem;
43 import javax.swing.event.MenuEvent;
44 import javax.swing.event.MenuListener;
45
46 /**
47  * provides metadata for a jabaws2 service instance - resolves names, etc.
48  * 
49  * @author JimP
50  * 
51  */
52 public abstract class Jws2Client extends jalview.ws.WSClient
53 {
54   public Jws2Client(AlignFrame _alignFrame, WsParamSetI preset,
55           List<ArgumentI> arguments)
56   {
57     super(_alignFrame, preset, arguments);
58     if (preset != null)
59     {
60       if (!((preset instanceof JabaPreset)
61               || preset instanceof JabaWsParamSet))
62       {
63         /*
64          * { this.preset = ((JabaPreset) preset).p; } else if (preset instanceof
65          * JabaWsParamSet) { List<Argument> newargs = new ArrayList<Argument>();
66          * JabaWsParamSet pset = ((JabaWsParamSet) preset); for (Option opt :
67          * pset.getjabaArguments()) { newargs.add(opt); } if (arguments != null
68          * && arguments.size() > 0) { // merge arguments with preset's own
69          * arguments. for (Argument opt : arguments) { newargs.add(opt); } }
70          * paramset = newargs; } else {
71          */
72         throw new Error(MessageManager.getString(
73                 "error.implementation_error_can_only_instantiate_jaba_param_sets"));
74       }
75     }
76     else
77     {
78       // TODO: verify that paramset is compatible with the service being
79       // contacted
80     }
81   }
82
83   public Jws2Client()
84   {
85     // anonymous constructor - used for headless method calls only
86   }
87
88   /*
89    * Jws2Instance serviceHandle; (non-Javadoc)
90    * 
91    * @see jalview.ws.WSMenuEntryProviderI#attachWSMenuEntry(javax.swing.JMenu,
92    * jalview.gui.AlignFrame)
93    * 
94    * @Override public void attachWSMenuEntry(JMenu wsmenu, AlignFrame
95    * alignFrame) { if (serviceHandle==null) { throw new
96    * Error("Implementation error: No service handle for this Jws2 service."); }
97    * attachWSMenuEntry(wsmenu, serviceHandle, alignFrame); }
98    */
99   /**
100    * add the menu item for a particular jws2 service instance
101    * 
102    * @param wsmenu
103    * @param service
104    * @param alignFrame
105    */
106   abstract void attachWSMenuEntry(JMenu wsmenu,
107           final ServiceWithParameters service,
108           final AlignFrame alignFrame);
109
110   static boolean registerAAConWSInstance(final JMenu wsmenu,
111           final ServiceWithParameters service, final AlignFrame alignFrame)
112   {
113     Jws2Instance jaba_service = (Jws2Instance) service;
114     final AlignAnalysisUIText aaui = jaba_service.getAlignAnalysisUI(); // null ; //
115                                                                    // AlignAnalysisUIText.aaConGUI.get(service.serviceType.toString());
116     if (aaui == null)
117     {
118       // not an instantaneous calculation GUI type service
119       return false;
120     }
121     // create the instaneous calculation GUI bits and update state if existing
122     // GUI elements already present
123
124     JCheckBoxMenuItem _aaConEnabled = null;
125     for (int i = 0; i < wsmenu.getItemCount(); i++)
126     {
127       JMenuItem item = wsmenu.getItem(i);
128       if (item instanceof JCheckBoxMenuItem
129               && item.getText().equals(aaui.getAAconToggle()))
130       {
131         _aaConEnabled = (JCheckBoxMenuItem) item;
132       }
133     }
134     // is there an aaCon worker already present - if so, set it to use the
135     // given service handle
136     {
137       List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
138               .getCalcManager()
139               .getRegisteredWorkersOfClass(aaui.getClient());
140       if (aaconClient != null && aaconClient.size() > 0)
141       {
142         AbstractJabaCalcWorker worker = (AbstractJabaCalcWorker) aaconClient
143                 .get(0);
144         if (!worker.service.getHostURL().equals(service.getHostURL()))
145         {
146           // javax.swing.SwingUtilities.invokeLater(new Runnable()
147           {
148             // @Override
149             // public void run()
150             {
151               removeCurrentAAConWorkerFor(aaui, alignFrame);
152               buildCurrentAAConWorkerFor(aaui, alignFrame, jaba_service);
153             }
154           } // );
155         }
156       }
157     }
158
159     // is there a service already registered ? there shouldn't be if we are
160     // being called correctly
161     if (_aaConEnabled == null)
162     {
163       final JCheckBoxMenuItem aaConEnabled = new JCheckBoxMenuItem(
164               aaui.getAAconToggle());
165
166       aaConEnabled.setToolTipText(
167               JvSwingUtils.wrapTooltip(true, aaui.getAAconToggleTooltip()));
168       aaConEnabled.addActionListener(new ActionListener()
169       {
170         @Override
171         public void actionPerformed(ActionEvent arg0)
172         {
173           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
174                   .getCalcManager()
175                   .getRegisteredWorkersOfClass(aaui.getClient());
176           if (aaconClient != null && aaconClient.size() > 0)
177           {
178             removeCurrentAAConWorkerFor(aaui, alignFrame);
179           }
180           else
181           {
182             buildCurrentAAConWorkerFor(aaui, alignFrame);
183
184           }
185         }
186
187       });
188       wsmenu.add(aaConEnabled);
189       final JMenuItem modifyParams = new JMenuItem(
190               aaui.getAAeditSettings());
191       modifyParams.setToolTipText(JvSwingUtils.wrapTooltip(true,
192               aaui.getAAeditSettingsTooltip()));
193       modifyParams.addActionListener(new ActionListener()
194       {
195
196         @Override
197         public void actionPerformed(ActionEvent arg0)
198         {
199           showAAConAnnotationSettingsFor(aaui, alignFrame);
200         }
201       });
202       wsmenu.add(modifyParams);
203       wsmenu.addMenuListener(new MenuListener()
204       {
205
206         @Override
207         public void menuSelected(MenuEvent arg0)
208         {
209           // TODO: refactor to the implementing class.
210           if (alignFrame.getViewport().getAlignment().isNucleotide()
211                   ? aaui.isNa()
212                   : aaui.isPr())
213           {
214             aaConEnabled.setEnabled(true);
215             modifyParams.setEnabled(true);
216           }
217           else
218           {
219             aaConEnabled.setEnabled(false);
220             modifyParams.setEnabled(false);
221           }
222           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
223                   .getCalcManager()
224                   .getRegisteredWorkersOfClass(aaui.getClient());
225           if (aaconClient != null && aaconClient.size() > 0)
226           {
227             aaConEnabled.setSelected(true);
228           }
229           else
230           {
231             aaConEnabled.setSelected(false);
232           }
233         }
234
235         @Override
236         public void menuDeselected(MenuEvent arg0)
237         {
238           // TODO Auto-generated method stub
239
240         }
241
242         @Override
243         public void menuCanceled(MenuEvent arg0)
244         {
245           // TODO Auto-generated method stub
246
247         }
248       });
249
250     }
251     return true;
252   }
253
254   private static void showAAConAnnotationSettingsFor(
255           final AlignAnalysisUIText aaui, AlignFrame alignFrame)
256   {
257     /*
258      * preferred settings Whether AACon is automatically recalculated Which
259      * AACon server to use What parameters to use
260      */
261     // could actually do a class search for this too
262     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
263             .getCalcIdSettingsFor(aaui.getCalcId());
264     if (fave == null)
265     {
266       fave = createDefaultAAConSettings(aaui);
267     }
268     new SequenceAnnotationWSClient(fave, alignFrame, true);
269
270   }
271
272   private static void buildCurrentAAConWorkerFor(
273           final AlignAnalysisUIText aaui, AlignFrame alignFrame)
274   {
275     buildCurrentAAConWorkerFor(aaui, alignFrame, null);
276   }
277
278   private static void buildCurrentAAConWorkerFor(
279           final AlignAnalysisUIText aaui, AlignFrame alignFrame,
280           Jws2Instance service)
281   {
282     /*
283      * preferred settings Whether AACon is automatically recalculated Which
284      * AACon server to use What parameters to use
285      */
286     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
287             .getCalcIdSettingsFor(aaui.getCalcId());
288     if (fave == null)
289     {
290       fave = createDefaultAAConSettings(aaui, service);
291     }
292     else
293     {
294       if (service != null
295               && !fave.getService().getHostURL()
296                       .equals(service.getHostURL()))
297       {
298         Cache.log.debug("Changing AACon service to " + service.getHostURL()
299                 + " from " + fave.getService().getHostURL());
300         fave.setService(service);
301       }
302     }
303     new SequenceAnnotationWSClient(fave, alignFrame, false);
304   }
305
306   private static AAConSettings createDefaultAAConSettings(
307           AlignAnalysisUIText aaui)
308   {
309     return createDefaultAAConSettings(aaui, null);
310   }
311
312   private static AAConSettings createDefaultAAConSettings(
313           AlignAnalysisUIText aaui, Jws2Instance service)
314   {
315     if (service != null)
316     {
317       if (!service.getServiceType()
318               .equals(compbio.ws.client.Services.AAConWS.toString()))
319       {
320         Cache.log.warn(
321                 "Ignoring invalid preferred service for AACon calculations (service type was "
322                         + service.getServiceType() + ")");
323         service = null;
324       }
325       else
326       {
327         // check service is actually in the list of currently avaialable
328         // services
329         if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
330         {
331           // it isn't ..
332           service = null;
333         }
334       }
335     }
336     if (service == null)
337     {
338       // get the default service for AACon
339       service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
340               aaui.getServiceType());
341     }
342     if (service == null)
343     {
344       // TODO raise dialog box explaining error, and/or open the JABA
345       // preferences menu.
346       throw new Error(
347               MessageManager.getString("error.no_aacon_service_found"));
348     }
349     return new AAConSettings(true, service, null, null);
350   }
351
352   private static void removeCurrentAAConWorkerFor(AlignAnalysisUIText aaui,
353           AlignFrame alignFrame)
354   {
355     alignFrame.getViewport().getCalcManager()
356             .removeRegisteredWorkersOfClass(aaui.getClient());
357   }
358
359 }