2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 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 enableJws2Services.setSelected(oldJws2 = Cache.getDefault(
81 "SHOW_JWS2_SERVICES", true));
82 enableJws2Services.addActionListener(updateAction);
83 indexByHost.setSelected(oldIndexByHost = Cache.getDefault(
84 "WSMENU_BYHOST", false));
85 indexByHost.addActionListener(updateAction);
86 indexByType.setSelected(oldIndexByType = Cache.getDefault(
87 "WSMENU_BYTYPE", false));
88 indexByType.addActionListener(updateAction);
89 displayWsWarning.setSelected(oldWsWarning = Cache.getDefault(
90 "SHOW_WSDISCOVERY_ERRORS", true));
93 ActionListener updateAction = new ActionListener()
97 public void actionPerformed(ActionEvent e)
104 private void updateList()
106 wsList.setListData(wsUrls);
109 private void updateRsbsList()
111 sbrsList.setListData(rsbsUrls);
114 private void updateServiceList()
116 jalview.ws.jws2.Jws2Discoverer.setServiceUrls(wsUrls);
119 private void updateRsbsServiceList()
121 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
128 * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
132 protected void deleteWsUrl_actionPerformed(ActionEvent e)
134 int sel = wsList.getSelectedIndex();
137 wsUrls.removeElementAt(sel);
146 * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
150 protected void editWsUrl_actionPerformed(ActionEvent e)
152 int sel = wsList.getSelectedIndex();
155 String url = editUrl(wsUrls.elementAt(sel), "Edit JABAWS URL");
158 int present = wsUrls.indexOf(url);
162 wsUrls.setElementAt(url, sel);
169 wsUrls.removeElementAt(sel);
177 protected void newSbrsUrl_actionPerformed(ActionEvent e)
179 RestServiceEditorPane rse = new RestServiceEditorPane();
180 rse.showDialog("Add a new Simple Bioinformatics Rest Service");
181 String rservice = rse.getEditedRestService();
182 if (rservice!=null && !rsbsUrls.contains(rservice))
184 rsbsUrls.add(rservice);
190 protected void editSbrsUrl_actionPerformed(ActionEvent e)
192 int sel = sbrsList.getSelectedIndex();
195 RestServiceEditorPane rse = new RestServiceEditorPane(new RestServiceDescription(rsbsUrls.elementAt(sel)));
196 rse.showDialog("Edit Simple Bioinformatics Rest Service entry");
197 String rservice = rse.getEditedRestService();
200 int present = rsbsUrls.indexOf(rservice);
203 rsbsUrls.setElementAt(rservice,sel);
207 rsbsUrls.removeElementAt(sel);
216 void updateWsMenuConfig(boolean old)
220 if (oldUrls!=wsUrls || (wsUrls!=null && oldUrls!=null && !wsUrls.equals(oldUrls)))
224 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
225 if (oldRsbsUrls!=rsbsUrls || (rsbsUrls!=null && oldRsbsUrls!=null && !oldRsbsUrls.equals(rsbsUrls)))
229 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
236 "SHOW_ENFIN_SERVICES",
238 old ? oldEnfin : enableEnfinServices.isSelected())
241 "SHOW_JWS2_SERVICES",
242 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
246 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
250 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
253 Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
254 Boolean.valueOf(old ? oldWsWarning : displayWsWarning.isSelected()).toString());
256 updateRsbsServiceList();
263 * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
267 protected void moveWsUrlDown_actionPerformed(ActionEvent e)
269 int p = wsList.getSelectedIndex();
270 if (p > -1 && p < wsUrls.size() - 1)
272 String t = wsUrls.get(p + 1);
273 wsUrls.setElementAt(wsUrls.elementAt(p), p + 1);
274 wsUrls.setElementAt(t, p);
276 wsList.setSelectedIndex(p + 1);
285 * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
289 protected void moveWsUrlUp_actionPerformed(ActionEvent e)
291 int p = wsList.getSelectedIndex();
294 String t = wsUrls.get(p - 1);
295 wsUrls.setElementAt(wsUrls.elementAt(p), p - 1);
296 wsUrls.setElementAt(t, p);
298 wsList.setSelectedIndex(p - 1);
303 private String editUrl(String initUrl, String title)
305 String url = initUrl;
311 JTextField urltf = new JTextField(url, 40);
312 JPanel panel = new JPanel(new BorderLayout());
313 JPanel pane12 = new JPanel(new BorderLayout());
314 pane12.add(new JLabel("URL: "), BorderLayout.CENTER);
315 pane12.add(urltf, BorderLayout.EAST);
316 panel.add(pane12, BorderLayout.NORTH);
317 boolean valid = false;
318 int resp = JOptionPane.CANCEL_OPTION;
320 && (resp = JOptionPane.showInternalConfirmDialog(
321 Desktop.desktop, panel, title,
322 JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
326 // TODO: do a better job of checking that the url is a valid discovery
327 // URL for web services.
328 String tx = urltf.getText().trim();
329 while (tx.length()>0 && tx.lastIndexOf('/')==tx.length()-1)
331 tx = tx.substring(0, tx.length()-1);
336 } catch (Exception e)
339 JOptionPane.showInternalMessageDialog(Desktop.desktop,
343 if (valid && resp == JOptionPane.OK_OPTION)
345 int validate = JOptionPane
346 .showInternalConfirmDialog(
348 "Validate JabaWS Server ?\n(Look in console output for results)",
349 "Test Server?", JOptionPane.YES_NO_OPTION);
350 if (validate == JOptionPane.OK_OPTION)
352 if (jalview.ws.jws2.Jws2Discoverer.testServiceUrl(foo))
354 return foo.toString();
359 .showInternalMessageDialog(
361 "Service did not pass validation.\nCheck the Jalview Console for more details.");
366 // just return the URL anyway
367 return foo.toString();
376 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
380 protected void newWsUrl_actionPerformed(ActionEvent e)
382 String url = editUrl(null, "Add new JABAWS URL");
385 if (!wsUrls.contains(url))
387 int selind = wsList.getSelectedIndex();
390 wsUrls.insertElementAt(url, selind);
394 wsUrls.addElement(url);
405 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
409 protected void refreshWs_actionPerformed(ActionEvent e)
411 new Thread(new Runnable()
417 lastrefresh = update - 1;
418 updateWsMenuConfig(false);
426 * Refresh the web services menus - but only if there has been a change in the
427 * configuration (indicated by update!=lastrefresh)
429 * @param showProgress
430 * show progress in dialog or on desktop
432 protected void refreshWsMenu(boolean showProgress)
436 new Thread(new Runnable()
441 progressBar.setVisible(true);
443 progressBar.setIndeterminate(true);
444 if (lastrefresh != update)
446 lastrefresh = update;
447 Desktop.instance.startServiceDiscovery(true); // wait around for all
448 // threads to complete
450 progressBar.setIndeterminate(false);
451 progressBar.setVisible(false);
459 new Thread(new Runnable()
464 long ct = System.currentTimeMillis();
465 Desktop.instance.setProgressBar("Refreshing Web Service Menus",
467 if (lastrefresh != update)
469 lastrefresh = update;
470 Desktop.instance.startServiceDiscovery(true);
472 Desktop.instance.setProgressBar(null, ct);
480 * state counters for ensuring that updates only happen if config has changed.
482 private long update = 0, lastrefresh = 0;
488 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
492 protected void resetWs_actionPerformed(ActionEvent e)
494 jalview.ws.jws2.Jws2Discoverer.setServiceUrls(null);
495 Vector nwsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
496 if (!wsUrls.equals(nwsUrls)) {
502 updateAndRefreshWsMenuConfig(true);
505 protected void ok_ActionPerformed(ActionEvent e)
507 // update everything regardless.
508 updateAndRefreshWsMenuConfig(false);
511 public void updateAndRefreshWsMenuConfig(
512 final boolean showProgressInDialog)
514 new Thread(new Runnable()
519 updateWsMenuConfig(false);
520 refreshWsMenu(showProgressInDialog);