2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.awt.BorderLayout;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
24 import java.util.Vector;
26 import javax.swing.JCheckBox;
27 import javax.swing.JLabel;
28 import javax.swing.JOptionPane;
29 import javax.swing.JPanel;
30 import javax.swing.JTextField;
32 import jalview.bin.Cache;
33 import jalview.jbgui.GWsPreferences;
34 import jalview.ws.rest.RestServiceDescription;
36 public class WsPreferences extends GWsPreferences
39 public WsPreferences()
42 initFromPreferences();
45 Vector<String> wsUrls, oldUrls,rsbsUrls,oldRsbsUrls;
47 private boolean needWsMenuUpdate;
49 private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
50 oldEnfin, oldWsWarning;
52 private void initFromPreferences()
55 wsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
58 oldUrls = new Vector<String>(wsUrls);
63 wsUrls = new Vector<String>();
66 rsbsUrls = jalview.ws.rest.RestClient.getRsbsDescriptions();
69 oldRsbsUrls = new Vector<String>(rsbsUrls);
74 rsbsUrls = new Vector<String>();
77 enableEnfinServices.setSelected(oldEnfin = Cache.getDefault(
78 "SHOW_ENFIN_SERVICES", true));
79 enableEnfinServices.addActionListener(updateAction);
80 enableJws1Services.setSelected(oldJws1 = Cache.getDefault(
81 "SHOW_JWS1_SERVICES", true));
82 enableJws1Services.addActionListener(updateAction);
83 enableJws2Services.setSelected(oldJws2 = Cache.getDefault(
84 "SHOW_JWS2_SERVICES", true));
85 enableJws2Services.addActionListener(updateAction);
86 indexByHost.setSelected(oldIndexByHost = Cache.getDefault(
87 "WSMENU_BYHOST", true));
88 indexByHost.addActionListener(updateAction);
89 indexByType.setSelected(oldIndexByType = Cache.getDefault(
90 "WSMENU_BYTYPE", true));
91 indexByType.addActionListener(updateAction);
92 displayWsWarning.setSelected(oldWsWarning = Cache.getDefault(
93 "SHOW_WSDISCOVERY_ERRORS", true));
96 ActionListener updateAction = new ActionListener()
100 public void actionPerformed(ActionEvent e)
107 private void updateList()
109 wsList.setListData(wsUrls);
112 private void updateRsbsList()
114 sbrsList.setListData(rsbsUrls);
117 private void updateServiceList()
119 jalview.ws.jws2.Jws2Discoverer.setServiceUrls(wsUrls);
122 private void updateRsbsServiceList()
124 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
131 * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
135 protected void deleteWsUrl_actionPerformed(ActionEvent e)
137 int sel = wsList.getSelectedIndex();
140 wsUrls.removeElementAt(sel);
149 * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
153 protected void editWsUrl_actionPerformed(ActionEvent e)
155 int sel = wsList.getSelectedIndex();
158 String url = editUrl(wsUrls.elementAt(sel), "Edit JABAWS URL");
161 int present = wsUrls.indexOf(url);
165 wsUrls.setElementAt(url, sel);
172 wsUrls.removeElementAt(sel);
180 protected void newSbrsUrl_actionPerformed(ActionEvent e)
182 RestServiceEditorPane rse = new RestServiceEditorPane();
183 rse.showDialog("Add a new Simple Bioinformatics Rest Service");
184 String rservice = rse.getEditedRestService();
185 if (rservice!=null && !rsbsUrls.contains(rservice))
187 rsbsUrls.add(rservice);
193 protected void editSbrsUrl_actionPerformed(ActionEvent e)
195 int sel = sbrsList.getSelectedIndex();
198 RestServiceEditorPane rse = new RestServiceEditorPane(new RestServiceDescription(rsbsUrls.elementAt(sel)));
199 rse.showDialog("Edit Simple Bioinformatics Rest Service entry");
200 String rservice = rse.getEditedRestService();
203 int present = rsbsUrls.indexOf(rservice);
206 rsbsUrls.setElementAt(rservice,sel);
210 rsbsUrls.removeElementAt(sel);
219 void updateWsMenuConfig(boolean old)
223 if (oldUrls!=wsUrls || (wsUrls!=null && oldUrls!=null && !wsUrls.equals(oldUrls)))
227 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
228 if (oldRsbsUrls!=rsbsUrls || (rsbsUrls!=null && oldRsbsUrls!=null && !oldRsbsUrls.equals(rsbsUrls)))
232 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
239 "SHOW_ENFIN_SERVICES",
241 old ? oldEnfin : enableEnfinServices.isSelected())
244 "SHOW_JWS1_SERVICES",
245 Boolean.valueOf(old ? oldJws1 : enableJws1Services.isSelected())
248 "SHOW_JWS2_SERVICES",
249 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
253 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
257 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
260 Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
261 Boolean.valueOf(old ? oldWsWarning : displayWsWarning.isSelected()).toString());
263 updateRsbsServiceList();
270 * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
274 protected void moveWsUrlDown_actionPerformed(ActionEvent e)
276 int p = wsList.getSelectedIndex();
277 if (p > -1 && p < wsUrls.size() - 1)
279 String t = wsUrls.get(p + 1);
280 wsUrls.setElementAt(wsUrls.elementAt(p), p + 1);
281 wsUrls.setElementAt(t, p);
283 wsList.setSelectedIndex(p + 1);
292 * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
296 protected void moveWsUrlUp_actionPerformed(ActionEvent e)
298 int p = wsList.getSelectedIndex();
301 String t = wsUrls.get(p - 1);
302 wsUrls.setElementAt(wsUrls.elementAt(p), p - 1);
303 wsUrls.setElementAt(t, p);
305 wsList.setSelectedIndex(p - 1);
310 private String editUrl(String initUrl, String title)
312 String url = initUrl;
318 JTextField urltf = new JTextField(url, 40);
319 JPanel panel = new JPanel(new BorderLayout());
320 JPanel pane12 = new JPanel(new BorderLayout());
321 pane12.add(new JLabel("URL: "), BorderLayout.CENTER);
322 pane12.add(urltf, BorderLayout.EAST);
323 panel.add(pane12, BorderLayout.NORTH);
324 boolean valid = false;
325 int resp = JOptionPane.CANCEL_OPTION;
327 && (resp = JOptionPane.showInternalConfirmDialog(
328 Desktop.desktop, panel, title,
329 JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
333 // TODO: do a better job of checking that the url is a valid discovery
334 // URL for web services.
335 String tx = urltf.getText().trim();
336 while (tx.length()>0 && tx.lastIndexOf('/')==tx.length()-1)
338 tx = tx.substring(0, tx.length()-1);
343 } catch (Exception e)
346 JOptionPane.showInternalMessageDialog(Desktop.desktop,
350 if (valid && resp == JOptionPane.OK_OPTION)
352 int validate = JOptionPane
353 .showInternalConfirmDialog(
355 "Validate JabaWS Server ?\n(Look in console output for results)",
356 "Test Server?", JOptionPane.YES_NO_OPTION);
357 if (validate == JOptionPane.OK_OPTION)
359 if (jalview.ws.jws2.Jws2Discoverer.testServiceUrl(foo))
361 return foo.toString();
366 .showInternalMessageDialog(
368 "Service did not pass validation.\nCheck the Jalview Console for more details.");
373 // just return the URL anyway
374 return foo.toString();
383 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
387 protected void newWsUrl_actionPerformed(ActionEvent e)
389 String url = editUrl(null, "Add new JABAWS URL");
392 if (!wsUrls.contains(url))
394 int selind = wsList.getSelectedIndex();
397 wsUrls.insertElementAt(url, selind);
401 wsUrls.addElement(url);
412 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
416 protected void refreshWs_actionPerformed(ActionEvent e)
418 new Thread(new Runnable()
424 lastrefresh = update - 1;
425 updateWsMenuConfig(false);
433 * Refresh the web services menus - but only if there has been a change in the
434 * configuration (indicated by update!=lastrefresh)
436 * @param showProgress
437 * show progress in dialog or on desktop
439 protected void refreshWsMenu(boolean showProgress)
443 new Thread(new Runnable()
448 progressBar.setVisible(true);
450 progressBar.setIndeterminate(true);
451 if (lastrefresh != update)
453 lastrefresh = update;
454 Desktop.instance.startServiceDiscovery(true); // wait around for all
455 // threads to complete
457 progressBar.setIndeterminate(false);
458 progressBar.setVisible(false);
466 new Thread(new Runnable()
471 long ct = System.currentTimeMillis();
472 Desktop.instance.setProgressBar("Refreshing Web Service Menus",
474 if (lastrefresh != update)
476 lastrefresh = update;
477 Desktop.instance.startServiceDiscovery(true);
479 Desktop.instance.setProgressBar(null, ct);
487 * state counters for ensuring that updates only happen if config has changed.
489 private long update = 0, lastrefresh = 0;
495 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
499 protected void resetWs_actionPerformed(ActionEvent e)
501 jalview.ws.jws2.Jws2Discoverer.setServiceUrls(null);
502 Vector nwsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
503 if (!wsUrls.equals(nwsUrls)) {
509 updateAndRefreshWsMenuConfig(true);
512 protected void ok_ActionPerformed(ActionEvent e)
514 // update everything regardless.
515 updateAndRefreshWsMenuConfig(false);
518 public void updateAndRefreshWsMenuConfig(
519 final boolean showProgressInDialog)
521 new Thread(new Runnable()
526 updateWsMenuConfig(false);
527 refreshWsMenu(showProgressInDialog);