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