2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Cache;
24 import jalview.jbgui.GWsPreferences;
25 import jalview.util.MessageManager;
26 import jalview.ws.jws2.Jws2Discoverer;
27 import jalview.ws.rest.RestServiceDescription;
29 import java.awt.BorderLayout;
30 import java.awt.Color;
31 import java.awt.Component;
32 import java.awt.Dimension;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
36 import java.util.Vector;
38 import javax.swing.JLabel;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JTable;
42 import javax.swing.JTextField;
43 import javax.swing.table.AbstractTableModel;
44 import javax.swing.table.TableCellRenderer;
46 public class WsPreferences extends GWsPreferences
49 public WsPreferences()
52 initFromPreferences();
55 Vector<String> wsUrls, oldUrls, rsbsUrls, oldRsbsUrls;
57 private boolean needWsMenuUpdate;
59 private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
60 oldEnfin, oldWsWarning;
62 private void initFromPreferences()
65 wsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
68 oldUrls = new Vector<String>(wsUrls);
73 wsUrls = new Vector<String>();
75 wsList.setDefaultRenderer(Integer.class, new JabaWSStatusRenderer());
76 wsList.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
78 rsbsUrls = jalview.ws.rest.RestClient.getRsbsDescriptions();
81 oldRsbsUrls = new Vector<String>(rsbsUrls);
86 rsbsUrls = new Vector<String>();
89 enableEnfinServices.setSelected(oldEnfin = Cache.getDefault(
90 "SHOW_ENFIN_SERVICES", true));
91 enableEnfinServices.addActionListener(updateAction);
92 enableJws2Services.setSelected(oldJws2 = Cache.getDefault(
93 "SHOW_JWS2_SERVICES", true));
94 enableJws2Services.addActionListener(updateAction);
95 indexByHost.setSelected(oldIndexByHost = Cache.getDefault(
96 "WSMENU_BYHOST", false));
97 indexByHost.addActionListener(updateAction);
98 indexByType.setSelected(oldIndexByType = Cache.getDefault(
99 "WSMENU_BYTYPE", false));
100 indexByType.addActionListener(updateAction);
101 displayWsWarning.setSelected(oldWsWarning = Cache.getDefault(
102 "SHOW_WSDISCOVERY_ERRORS", true));
105 ActionListener updateAction = new ActionListener()
109 public void actionPerformed(ActionEvent e)
116 private void updateList()
118 Object tdat[][] = new Object[wsUrls.size()][2];
120 for (String url : wsUrls)
122 int status = Jws2Discoverer.getDiscoverer().getServerStatusFor(url);
123 tdat[r][1] = new Integer(status);
127 wsList.setModel(new WsUrlTableModel(tdat));
128 wsList.getColumn(MessageManager.getString("label.status")).setMinWidth(10);
131 private class JabaWSStatusRenderer extends JPanel implements
134 public JabaWSStatusRenderer()
137 setMinimumSize(new Dimension(10, 10));
143 * render an Integer reflecting service status as a colour and symbol
147 public Component getTableCellRendererComponent(JTable arg0,
148 Object status, boolean isSelected, boolean hasFocus, int row,
152 String t = new String("");
153 switch (((Integer) status).intValue())
156 // cb.setSelected(true);
161 // cb.setSelected(true);
166 // cb.setSelected(false);
171 // cb.setSelected(false);
183 private class WsUrlTableModel extends AbstractTableModel
186 private Object[][] data;
187 private String[] columnNames = new String[]
188 { MessageManager.getString("label.service_url"), MessageManager.getString("label.status") };
190 public WsUrlTableModel(Object[][] tdat)
196 public int getColumnCount()
202 public String getColumnName(int column)
204 return columnNames[column];
208 public int getRowCount()
218 public java.lang.Class<?> getColumnClass(int columnIndex)
220 return getValueAt(0, columnIndex).getClass();
224 public Object getValueAt(int rowIndex, int columnIndex)
226 return data[rowIndex][columnIndex];
231 private void updateRsbsList()
233 sbrsList.setListData(rsbsUrls);
236 private void updateServiceList()
238 jalview.ws.jws2.Jws2Discoverer.setServiceUrls(wsUrls);
241 private void updateRsbsServiceList()
243 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
250 * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
254 protected void deleteWsUrl_actionPerformed(ActionEvent e)
256 int sel = wsList.getSelectedRow();
259 wsUrls.removeElementAt(sel);
268 * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
272 protected void editWsUrl_actionPerformed(ActionEvent e)
274 int sel = wsList.getSelectedRow();
277 String url = editUrl(wsUrls.elementAt(sel), MessageManager.getString("label.edit_jabaws_url"));
280 int present = wsUrls.indexOf(url);
284 wsUrls.setElementAt(url, sel);
291 wsUrls.removeElementAt(sel);
300 protected void newSbrsUrl_actionPerformed(ActionEvent e)
302 RestServiceEditorPane rse = new RestServiceEditorPane();
303 rse.showDialog(MessageManager.getString("label.add_new_sbrs_service"));
304 String rservice = rse.getEditedRestService();
305 if (rservice != null && !rsbsUrls.contains(rservice))
307 rsbsUrls.add(rservice);
314 protected void editSbrsUrl_actionPerformed(ActionEvent e)
316 int sel = sbrsList.getSelectedIndex();
319 RestServiceEditorPane rse = new RestServiceEditorPane(
320 new RestServiceDescription(rsbsUrls.elementAt(sel)));
321 rse.showDialog(MessageManager.getString("label.edit_sbrs_entry"));
322 String rservice = rse.getEditedRestService();
323 if (rservice != null)
325 int present = rsbsUrls.indexOf(rservice);
329 rsbsUrls.setElementAt(rservice, sel);
336 rsbsUrls.removeElementAt(sel);
345 void updateWsMenuConfig(boolean old)
349 if (oldUrls != wsUrls
350 || (wsUrls != null && oldUrls != null && !wsUrls
355 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
356 if (oldRsbsUrls != rsbsUrls
357 || (rsbsUrls != null && oldRsbsUrls != null && !oldRsbsUrls
362 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
369 "SHOW_ENFIN_SERVICES",
371 old ? oldEnfin : enableEnfinServices.isSelected())
374 "SHOW_JWS2_SERVICES",
375 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
379 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
383 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
387 "SHOW_WSDISCOVERY_ERRORS",
389 old ? oldWsWarning : displayWsWarning.isSelected())
392 updateRsbsServiceList();
399 * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
403 protected void moveWsUrlDown_actionPerformed(ActionEvent e)
405 int p = wsList.getSelectedRow();
406 if (p > -1 && p < wsUrls.size() - 1)
408 String t = wsUrls.get(p + 1);
409 wsUrls.setElementAt(wsUrls.elementAt(p), p + 1);
410 wsUrls.setElementAt(t, p);
412 wsList.getSelectionModel().setSelectionInterval(p + 1, p + 1);
421 * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
425 protected void moveWsUrlUp_actionPerformed(ActionEvent e)
427 int p = wsList.getSelectedRow();
430 String t = wsUrls.get(p - 1);
431 wsUrls.setElementAt(wsUrls.elementAt(p), p - 1);
432 wsUrls.setElementAt(t, p);
434 wsList.getSelectionModel().setSelectionInterval(p - 1, p - 1);
439 private String editUrl(String initUrl, String title)
441 String url = initUrl;
447 JTextField urltf = new JTextField(url, 40);
448 JPanel panel = new JPanel(new BorderLayout());
449 JPanel pane12 = new JPanel(new BorderLayout());
450 pane12.add(new JLabel(MessageManager.getString("label.url")),
451 BorderLayout.CENTER);
452 pane12.add(urltf, BorderLayout.EAST);
453 panel.add(pane12, BorderLayout.NORTH);
454 boolean valid = false;
455 int resp = JOptionPane.CANCEL_OPTION;
457 && (resp = JOptionPane.showInternalConfirmDialog(
458 Desktop.desktop, panel, title,
459 JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
463 // TODO: do a better job of checking that the url is a valid discovery
464 // URL for web services.
465 String tx = urltf.getText().trim();
466 while (tx.length() > 0 && tx.lastIndexOf('/') == tx.length() - 1)
468 tx = tx.substring(0, tx.length() - 1);
473 } catch (Exception e)
476 JOptionPane.showInternalMessageDialog(Desktop.desktop,
477 MessageManager.getString("label.invalid_url"));
480 if (valid && resp == JOptionPane.OK_OPTION)
482 int validate = JOptionPane
483 .showInternalConfirmDialog(
485 MessageManager.getString("info.validate_jabaws_server"),
486 MessageManager.getString("label.test_server"), JOptionPane.YES_NO_OPTION);
487 if (validate == JOptionPane.OK_OPTION)
489 if (jalview.ws.jws2.Jws2Discoverer.testServiceUrl(foo))
491 return foo.toString();
496 .showInternalMessageDialog(
498 MessageManager.getString("warn.server_didnt_pass_validation"));
503 // just return the URL anyway
504 return foo.toString();
513 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
517 protected void newWsUrl_actionPerformed(ActionEvent e)
519 String url = editUrl(null, MessageManager.getString("label.add_jabaws_url"));
522 if (!wsUrls.contains(url))
524 int selind = wsList.getSelectedRow();
527 wsUrls.insertElementAt(url, selind);
531 wsUrls.addElement(url);
542 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
546 protected void refreshWs_actionPerformed(ActionEvent e)
548 new Thread(new Runnable()
554 lastrefresh = update - 1;
555 updateWsMenuConfig(false);
563 * Refresh the web services menus - but only if there has been a change in the
564 * configuration (indicated by update!=lastrefresh)
566 * @param showProgress
567 * show progress in dialog or on desktop
569 protected void refreshWsMenu(boolean showProgress)
573 new Thread(new Runnable()
578 progressBar.setVisible(true);
580 progressBar.setIndeterminate(true);
581 if (lastrefresh != update)
583 lastrefresh = update;
584 Desktop.instance.startServiceDiscovery(true); // wait around for all
585 // threads to complete
589 progressBar.setIndeterminate(false);
590 progressBar.setVisible(false);
598 new Thread(new Runnable()
603 long ct = System.currentTimeMillis();
604 Desktop.instance.setProgressBar(MessageManager.getString("status.refreshing_web_service_menus"),
606 if (lastrefresh != update)
608 lastrefresh = update;
609 Desktop.instance.startServiceDiscovery(true);
612 Desktop.instance.setProgressBar(null, ct);
620 * state counters for ensuring that updates only happen if config has changed.
622 private long update = 0, lastrefresh = 0;
628 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
632 protected void resetWs_actionPerformed(ActionEvent e)
634 jalview.ws.jws2.Jws2Discoverer.setServiceUrls(null);
635 Vector nwsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
636 if (!wsUrls.equals(nwsUrls))
643 updateAndRefreshWsMenuConfig(true);
646 protected void ok_ActionPerformed(ActionEvent e)
648 // update everything regardless.
649 updateAndRefreshWsMenuConfig(false);
652 public void updateAndRefreshWsMenuConfig(
653 final boolean showProgressInDialog)
655 new Thread(new Runnable()
660 updateWsMenuConfig(false);
661 refreshWsMenu(showProgressInDialog);