JAL-3070 rough-and-ready refactor of JABA SequenceAnnotation style services - needs...
[jalview.git] / src / jalview / ws / jws2 / Jws2ClientFactory.java
1 package jalview.ws.jws2;
2
3 import jalview.api.AlignCalcWorkerI;
4 import jalview.bin.Cache;
5 import jalview.gui.AlignFrame;
6 import jalview.gui.JvSwingUtils;
7 import jalview.util.MessageManager;
8 import jalview.ws.api.ServiceWithParameters;
9 import jalview.ws.jws2.dm.AAConSettings;
10 import jalview.ws.jws2.jabaws2.Jws2Instance;
11 import jalview.ws.uimodel.AlignAnalysisUIText;
12
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.util.List;
16
17 import javax.swing.JCheckBoxMenuItem;
18 import javax.swing.JMenu;
19 import javax.swing.JMenuItem;
20 import javax.swing.event.MenuEvent;
21 import javax.swing.event.MenuListener;
22
23 public class Jws2ClientFactory
24 {
25   static boolean registerAAConWSInstance(final JMenu wsmenu,
26           final ServiceWithParameters service, final AlignFrame alignFrame)
27   {
28     Jws2Instance jaba_service = (Jws2Instance) service;
29     final AlignAnalysisUIText aaui = jaba_service.getAlignAnalysisUI(); // null
30                                                                         // ; //
31     // AlignAnalysisUIText.aaConGUI.get(service.serviceType.toString());
32     if (aaui == null)
33     {
34       // not an instantaneous calculation GUI type service
35       return false;
36     }
37     // create the instaneous calculation GUI bits and update state if existing
38     // GUI elements already present
39
40     JCheckBoxMenuItem _aaConEnabled = null;
41     for (int i = 0; i < wsmenu.getItemCount(); i++)
42     {
43       JMenuItem item = wsmenu.getItem(i);
44       if (item instanceof JCheckBoxMenuItem
45               && item.getText().equals(aaui.getAAconToggle()))
46       {
47         _aaConEnabled = (JCheckBoxMenuItem) item;
48       }
49     }
50     // is there an aaCon worker already present - if so, set it to use the
51     // given service handle
52     {
53       List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
54               .getCalcManager()
55               .getRegisteredWorkersOfClass(aaui.getClient());
56       if (aaconClient != null && aaconClient.size() > 0)
57       {
58         AbstractJabaCalcWorker worker = (AbstractJabaCalcWorker) aaconClient
59                 .get(0);
60         if (!worker.service.getHostURL().equals(service.getHostURL()))
61         {
62           // javax.swing.SwingUtilities.invokeLater(new Runnable()
63           {
64             // @Override
65             // public void run()
66             {
67               removeCurrentAAConWorkerFor(aaui, alignFrame);
68               buildCurrentAAConWorkerFor(aaui, alignFrame, jaba_service);
69             }
70           } // );
71         }
72       }
73     }
74
75     // is there a service already registered ? there shouldn't be if we are
76     // being called correctly
77     if (_aaConEnabled == null)
78     {
79       final JCheckBoxMenuItem aaConEnabled = new JCheckBoxMenuItem(
80               aaui.getAAconToggle());
81
82       aaConEnabled.setToolTipText(
83               JvSwingUtils.wrapTooltip(true, aaui.getAAconToggleTooltip()));
84       aaConEnabled.addActionListener(new ActionListener()
85       {
86         @Override
87         public void actionPerformed(ActionEvent arg0)
88         {
89           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
90                   .getCalcManager()
91                   .getRegisteredWorkersOfClass(aaui.getClient());
92           if (aaconClient != null && aaconClient.size() > 0)
93           {
94             removeCurrentAAConWorkerFor(aaui, alignFrame);
95           }
96           else
97           {
98             buildCurrentAAConWorkerFor(aaui, alignFrame);
99
100           }
101         }
102
103       });
104       wsmenu.add(aaConEnabled);
105       final JMenuItem modifyParams = new JMenuItem(
106               aaui.getAAeditSettings());
107       modifyParams.setToolTipText(JvSwingUtils.wrapTooltip(true,
108               aaui.getAAeditSettingsTooltip()));
109       modifyParams.addActionListener(new ActionListener()
110       {
111
112         @Override
113         public void actionPerformed(ActionEvent arg0)
114         {
115           showAAConAnnotationSettingsFor(aaui, alignFrame);
116         }
117       });
118       wsmenu.add(modifyParams);
119       wsmenu.addMenuListener(new MenuListener()
120       {
121
122         @Override
123         public void menuSelected(MenuEvent arg0)
124         {
125           // TODO: refactor to the implementing class.
126           if (alignFrame.getViewport().getAlignment().isNucleotide()
127                   ? aaui.isNa()
128                   : aaui.isPr())
129           {
130             aaConEnabled.setEnabled(true);
131             modifyParams.setEnabled(true);
132           }
133           else
134           {
135             aaConEnabled.setEnabled(false);
136             modifyParams.setEnabled(false);
137           }
138           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
139                   .getCalcManager()
140                   .getRegisteredWorkersOfClass(aaui.getClient());
141           if (aaconClient != null && aaconClient.size() > 0)
142           {
143             aaConEnabled.setSelected(true);
144           }
145           else
146           {
147             aaConEnabled.setSelected(false);
148           }
149         }
150
151         @Override
152         public void menuDeselected(MenuEvent arg0)
153         {
154           // TODO Auto-generated method stub
155
156         }
157
158         @Override
159         public void menuCanceled(MenuEvent arg0)
160         {
161           // TODO Auto-generated method stub
162
163         }
164       });
165
166     }
167     return true;
168   }
169
170   private static void showAAConAnnotationSettingsFor(
171           final AlignAnalysisUIText aaui, AlignFrame alignFrame)
172   {
173     /*
174      * preferred settings Whether AACon is automatically recalculated Which
175      * AACon server to use What parameters to use
176      */
177     // could actually do a class search for this too
178     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
179             .getCalcIdSettingsFor(aaui.getCalcId());
180     if (fave == null)
181     {
182       fave = createDefaultAAConSettings(aaui);
183     }
184     new SequenceAnnotationWSClient(fave, alignFrame, true);
185
186   }
187
188   private static void buildCurrentAAConWorkerFor(
189           final AlignAnalysisUIText aaui, AlignFrame alignFrame)
190   {
191     buildCurrentAAConWorkerFor(aaui, alignFrame, null);
192   }
193
194   private static void buildCurrentAAConWorkerFor(
195           final AlignAnalysisUIText aaui, AlignFrame alignFrame,
196           Jws2Instance service)
197   {
198     /*
199      * preferred settings Whether AACon is automatically recalculated Which
200      * AACon server to use What parameters to use
201      */
202     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
203             .getCalcIdSettingsFor(aaui.getCalcId());
204     if (fave == null)
205     {
206       fave = createDefaultAAConSettings(aaui, service);
207     }
208     else
209     {
210       if (service != null && !fave.getService().getHostURL()
211               .equals(service.getHostURL()))
212       {
213         Cache.log.debug("Changing AACon service to " + service.getHostURL()
214                 + " from " + fave.getService().getHostURL());
215         fave.setService(service);
216       }
217     }
218     new SequenceAnnotationWSClient(fave, alignFrame, false);
219   }
220
221   private static AAConSettings createDefaultAAConSettings(
222           AlignAnalysisUIText aaui)
223   {
224     return createDefaultAAConSettings(aaui, null);
225   }
226
227   private static AAConSettings createDefaultAAConSettings(
228           AlignAnalysisUIText aaui, Jws2Instance service)
229   {
230     if (service != null)
231     {
232       if (!service.getServiceType()
233               .equals(compbio.ws.client.Services.AAConWS.toString()))
234       {
235         Cache.log.warn(
236                 "Ignoring invalid preferred service for AACon calculations (service type was "
237                         + service.getServiceType() + ")");
238         service = null;
239       }
240       else
241       {
242         // check service is actually in the list of currently avaialable
243         // services
244         if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
245         {
246           // it isn't ..
247           service = null;
248         }
249       }
250     }
251     if (service == null)
252     {
253       // get the default service for AACon
254       service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
255               aaui.getServiceType());
256     }
257     if (service == null)
258     {
259       // TODO raise dialog box explaining error, and/or open the JABA
260       // preferences menu.
261       throw new Error(
262               MessageManager.getString("error.no_aacon_service_found"));
263     }
264     return new AAConSettings(true, service, null, null);
265   }
266
267   private static void removeCurrentAAConWorkerFor(AlignAnalysisUIText aaui,
268           AlignFrame alignFrame)
269   {
270     alignFrame.getViewport().getCalcManager()
271             .removeRegisteredWorkersOfClass(aaui.getClient());
272   }
273 }