1 package jalview.ws.jws2;
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;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.util.List;
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;
22 public class Jws2ClientFactory
24 static boolean registerAAConWSInstance(final JMenu wsmenu,
25 final Jws2Instance service, final AlignFrame alignFrame)
27 final AlignAnalysisUIText aaui = service.getAlignAnalysisUI(); // null
29 // AlignAnalysisUIText.aaConGUI.get(service.serviceType.toString());
32 // not an instantaneous calculation GUI type service
35 // create the instaneous calculation GUI bits and update state if existing
36 // GUI elements already present
38 JCheckBoxMenuItem _aaConEnabled = null;
39 for (int i = 0; i < wsmenu.getItemCount(); i++)
41 JMenuItem item = wsmenu.getItem(i);
42 if (item instanceof JCheckBoxMenuItem
43 && item.getText().equals(aaui.getAAconToggle()))
45 _aaConEnabled = (JCheckBoxMenuItem) item;
48 // is there an aaCon worker already present - if so, set it to use the
49 // given service handle
51 List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
53 .getRegisteredWorkersOfClass(aaui.getClient());
54 if (aaconClient != null && aaconClient.size() > 0)
56 SeqAnnotationServiceCalcWorker worker = (SeqAnnotationServiceCalcWorker) aaconClient
58 if (!worker.service.getHostURL().equals(service.getHostURL()))
60 // javax.swing.SwingUtilities.invokeLater(new Runnable()
65 removeCurrentAAConWorkerFor(aaui, alignFrame);
66 buildCurrentAAConWorkerFor(aaui, alignFrame, service);
73 // is there a service already registered ? there shouldn't be if we are
74 // being called correctly
75 if (_aaConEnabled == null)
77 final JCheckBoxMenuItem aaConEnabled = new JCheckBoxMenuItem(
78 aaui.getAAconToggle());
80 aaConEnabled.setToolTipText(
81 JvSwingUtils.wrapTooltip(true, aaui.getAAconToggleTooltip()));
82 aaConEnabled.addActionListener(new ActionListener()
85 public void actionPerformed(ActionEvent arg0)
87 List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
89 .getRegisteredWorkersOfClass(aaui.getClient());
90 if (aaconClient != null && aaconClient.size() > 0)
92 removeCurrentAAConWorkerFor(aaui, alignFrame);
96 buildCurrentAAConWorkerFor(aaui, alignFrame);
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()
111 public void actionPerformed(ActionEvent arg0)
113 showAAConAnnotationSettingsFor(aaui, alignFrame);
116 wsmenu.add(modifyParams);
117 wsmenu.addMenuListener(new MenuListener()
121 public void menuSelected(MenuEvent arg0)
123 // TODO: refactor to the implementing class.
124 if (alignFrame.getViewport().getAlignment().isNucleotide()
128 aaConEnabled.setEnabled(true);
129 modifyParams.setEnabled(true);
133 aaConEnabled.setEnabled(false);
134 modifyParams.setEnabled(false);
137 List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
138 .getCalcManager().getRegisteredWorkersOfClass(
139 SeqAnnotationServiceCalcWorker.class);
141 boolean serviceEnabled = false;
142 if (aaconClient != null)
144 for (AlignCalcWorkerI _worker : aaconClient)
146 SeqAnnotationServiceCalcWorker worker = (SeqAnnotationServiceCalcWorker) _worker;
147 // this could be cleaner ?
148 if (worker.hasService()
150 .equals(worker.getService().getClass()))
152 serviceEnabled = true;
156 aaConEnabled.setSelected(serviceEnabled);
160 public void menuDeselected(MenuEvent arg0)
162 // TODO Auto-generated method stub
167 public void menuCanceled(MenuEvent arg0)
169 // TODO Auto-generated method stub
178 private static void showAAConAnnotationSettingsFor(
179 final AlignAnalysisUIText aaui, AlignFrame alignFrame)
182 * preferred settings Whether AACon is automatically recalculated Which
183 * AACon server to use What parameters to use
185 // could actually do a class search for this too
186 AAConSettings fave = (AAConSettings) alignFrame.getViewport()
187 .getCalcIdSettingsFor(aaui.getCalcId());
190 fave = createDefaultAAConSettings(aaui);
192 new SequenceAnnotationWSClient(fave, alignFrame, true);
196 private static void buildCurrentAAConWorkerFor(
197 final AlignAnalysisUIText aaui, AlignFrame alignFrame)
199 buildCurrentAAConWorkerFor(aaui, alignFrame, null);
202 private static void buildCurrentAAConWorkerFor(
203 final AlignAnalysisUIText aaui, AlignFrame alignFrame,
204 Jws2Instance service)
207 * preferred settings Whether AACon is automatically recalculated Which
208 * AACon server to use What parameters to use
210 AAConSettings fave = (AAConSettings) alignFrame.getViewport()
211 .getCalcIdSettingsFor(aaui.getCalcId());
214 fave = createDefaultAAConSettings(aaui, service);
218 if (service != null && !fave.getService().getHostURL()
219 .equals(service.getHostURL()))
221 Cache.log.debug("Changing AACon service to " + service.getHostURL()
222 + " from " + fave.getService().getHostURL());
223 fave.setService(service);
226 new SequenceAnnotationWSClient(fave, alignFrame, false);
229 private static AAConSettings createDefaultAAConSettings(
230 AlignAnalysisUIText aaui)
232 return createDefaultAAConSettings(aaui, null);
235 private static AAConSettings createDefaultAAConSettings(
236 AlignAnalysisUIText aaui, Jws2Instance service)
240 if (!service.getServiceType()
241 .equals(compbio.ws.client.Services.AAConWS.toString()))
244 "Ignoring invalid preferred service for AACon calculations (service type was "
245 + service.getServiceType() + ")");
250 // check service is actually in the list of currently avaialable
252 if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
261 // get the default service for AACon
262 service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
263 aaui.getServiceType());
267 // TODO raise dialog box explaining error, and/or open the JABA
270 MessageManager.getString("error.no_aacon_service_found"));
272 return new AAConSettings(true, service, null, null);
275 private static void removeCurrentAAConWorkerFor(AlignAnalysisUIText aaui,
276 AlignFrame alignFrame)
278 alignFrame.getViewport().getCalcManager()
279 .removeRegisteredWorkersOfClass(aaui.getClient());