2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.List;
37 import java.util.Vector;
39 import javax.swing.JLabel;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JTable;
43 import javax.swing.JTextField;
44 import javax.swing.table.AbstractTableModel;
45 import javax.swing.table.TableCellRenderer;
47 public class WsPreferences extends GWsPreferences
50 public WsPreferences()
53 initFromPreferences();
58 Vector<String> oldUrls, rsbsUrls, oldRsbsUrls;
60 private boolean needWsMenuUpdate;
62 private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
63 oldEnfin, oldWsWarning;
65 private void initFromPreferences()
68 wsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
69 if (!wsUrls.isEmpty())
71 oldUrls = new Vector<String>(wsUrls);
76 wsUrls = new Vector<String>();
78 wsList.setDefaultRenderer(Integer.class, new JabaWSStatusRenderer());
79 wsList.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
81 rsbsUrls = jalview.ws.rest.RestClient.getRsbsDescriptions();
84 oldRsbsUrls = new Vector<String>(rsbsUrls);
89 rsbsUrls = new Vector<String>();
92 enableEnfinServices.setSelected(
93 oldEnfin = Cache.getDefault("SHOW_ENFIN_SERVICES", true));
94 enableEnfinServices.addActionListener(updateAction);
95 enableJws2Services.setSelected(
96 oldJws2 = Cache.getDefault("SHOW_JWS2_SERVICES", true));
97 enableJws2Services.addActionListener(updateAction);
98 indexByHost.setSelected(
99 oldIndexByHost = Cache.getDefault("WSMENU_BYHOST", false));
100 indexByHost.addActionListener(updateAction);
101 indexByType.setSelected(
102 oldIndexByType = Cache.getDefault("WSMENU_BYTYPE", false));
103 indexByType.addActionListener(updateAction);
104 displayWsWarning.setSelected(oldWsWarning = Cache
105 .getDefault("SHOW_WSDISCOVERY_ERRORS", true));
108 ActionListener updateAction = new ActionListener()
112 public void actionPerformed(ActionEvent e)
119 private void updateList()
121 Object tdat[][] = new Object[wsUrls.size()][2];
123 for (String url : wsUrls)
125 int status = Jws2Discoverer.getDiscoverer().getServerStatusFor(url);
126 tdat[r][1] = new Integer(status);
130 wsList.setModel(new WsUrlTableModel(tdat));
131 wsList.getColumn(MessageManager.getString("label.status"))
135 private class JabaWSStatusRenderer extends JPanel
136 implements TableCellRenderer
138 public JabaWSStatusRenderer()
141 setMinimumSize(new Dimension(10, 10));
147 * render an Integer reflecting service status as a colour and symbol
151 public Component getTableCellRendererComponent(JTable arg0,
152 Object status, boolean isSelected, boolean hasFocus, int row,
156 String t = new String("");
157 switch (((Integer) status).intValue())
160 // cb.setSelected(true);
165 // cb.setSelected(true);
170 // cb.setSelected(false);
175 // cb.setSelected(false);
187 private class WsUrlTableModel extends AbstractTableModel
190 private Object[][] data;
192 private String[] columnNames = new String[] {
193 MessageManager.getString("label.service_url"),
194 MessageManager.getString("label.status") };
196 public WsUrlTableModel(Object[][] tdat)
202 public int getColumnCount()
208 public String getColumnName(int column)
210 return columnNames[column];
214 public int getRowCount()
224 public java.lang.Class<?> getColumnClass(int columnIndex)
226 return getValueAt(0, columnIndex).getClass();
230 public Object getValueAt(int rowIndex, int columnIndex)
232 return data[rowIndex][columnIndex];
237 private void updateRsbsList()
239 sbrsList.setListData(rsbsUrls);
242 private void updateServiceList()
244 Jws2Discoverer.getDiscoverer().setServiceUrls(wsUrls);
247 private void updateRsbsServiceList()
249 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
256 * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
260 protected void deleteWsUrl_actionPerformed(ActionEvent e)
262 int sel = wsList.getSelectedRow();
274 * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
278 protected void editWsUrl_actionPerformed(ActionEvent e)
280 int sel = wsList.getSelectedRow();
283 String url = editUrl(wsUrls.get(sel),
284 MessageManager.getString("label.edit_jabaws_url"));
287 int present = wsUrls.indexOf(url);
291 wsUrls.set(sel, url);
307 protected void newSbrsUrl_actionPerformed(ActionEvent e)
309 RestServiceEditorPane rse = new RestServiceEditorPane();
310 rse.showDialog(MessageManager.getString("label.add_new_sbrs_service"));
311 String rservice = rse.getEditedRestService();
312 if (rservice != null && !rsbsUrls.contains(rservice))
314 rsbsUrls.add(rservice);
321 protected void editSbrsUrl_actionPerformed(ActionEvent e)
323 int sel = sbrsList.getSelectedIndex();
326 RestServiceEditorPane rse = new RestServiceEditorPane(
327 new RestServiceDescription(rsbsUrls.elementAt(sel)));
328 rse.showDialog(MessageManager.getString("label.edit_sbrs_entry"));
329 String rservice = rse.getEditedRestService();
330 if (rservice != null)
332 int present = rsbsUrls.indexOf(rservice);
336 rsbsUrls.setElementAt(rservice, sel);
343 rsbsUrls.removeElementAt(sel);
352 void updateWsMenuConfig(boolean old)
356 if (oldUrls != wsUrls || (wsUrls != null && oldUrls != null
357 && !wsUrls.equals(oldUrls)))
361 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
362 if (oldRsbsUrls != rsbsUrls || (rsbsUrls != null
363 && oldRsbsUrls != null && !oldRsbsUrls.equals(rsbsUrls)))
367 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
373 Cache.setProperty("SHOW_ENFIN_SERVICES",
375 old ? oldEnfin : enableEnfinServices.isSelected())
377 Cache.setProperty("SHOW_JWS2_SERVICES",
378 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
380 Cache.setProperty("WSMENU_BYHOST",
381 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
383 Cache.setProperty("WSMENU_BYTYPE",
384 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
387 Cache.setProperty("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.set(p + 1, wsUrls.get(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.set(p - 1, wsUrls.get(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 = JvOptionPane.CANCEL_OPTION;
456 while (!valid && (resp = JvOptionPane.showInternalConfirmDialog(
457 Desktop.desktop, panel, title,
458 JvOptionPane.OK_CANCEL_OPTION)) == JvOptionPane.OK_OPTION)
462 // TODO: do a better job of checking that the url is a valid discovery
463 // URL for web services.
464 String tx = urltf.getText().trim();
465 while (tx.length() > 0 && tx.lastIndexOf('/') == tx.length() - 1)
467 tx = tx.substring(0, tx.length() - 1);
472 } catch (Exception e)
475 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
476 MessageManager.getString("label.invalid_url"));
479 if (valid && resp == JvOptionPane.OK_OPTION)
481 int validate = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
482 MessageManager.getString("info.validate_jabaws_server"),
483 MessageManager.getString("label.test_server"),
484 JvOptionPane.YES_NO_OPTION);
486 if (validate == JvOptionPane.OK_OPTION)
488 if (Jws2Discoverer.testServiceUrl(foo))
490 return foo.toString();
494 int opt = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
495 "The Server '" + foo.toString()
496 + "' failed validation,\ndo you want to add it anyway? ",
497 "Server Validation Failed", JvOptionPane.YES_NO_OPTION,
498 JvOptionPane.INFORMATION_MESSAGE, null, null, null);
499 if (opt == JvOptionPane.YES_OPTION)
501 return foo.toString();
505 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
506 MessageManager.getString(
507 "warn.server_didnt_pass_validation"));
514 // just return the URL anyway
515 return foo.toString();
524 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
528 protected void newWsUrl_actionPerformed(ActionEvent e)
530 String url = editUrl(null,
531 MessageManager.getString("label.add_jabaws_url"));
534 if (!wsUrls.contains(url))
536 int selind = wsList.getSelectedRow();
539 wsUrls.add(selind, url);
554 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
558 protected void refreshWs_actionPerformed(ActionEvent e)
560 new Thread(new Runnable()
567 lastrefresh = update - 1;
568 updateWsMenuConfig(false);
576 * Refresh the web services menus - but only if there has been a change in the
577 * configuration (indicated by update!=lastrefresh)
579 * @param showProgress
580 * show progress in dialog or on desktop
582 protected void refreshWsMenu(boolean showProgress)
586 new Thread(new Runnable()
592 progressBar.setVisible(true);
594 progressBar.setIndeterminate(true);
595 if (lastrefresh != update)
597 lastrefresh = update;
598 Desktop.instance.startServiceDiscovery(true); // wait around for all
599 // threads to complete
603 progressBar.setIndeterminate(false);
604 progressBar.setVisible(false);
612 new Thread(new Runnable()
618 long ct = System.currentTimeMillis();
619 Desktop.instance.setProgressBar(MessageManager
620 .getString("status.refreshing_web_service_menus"), ct);
621 if (lastrefresh != update)
623 lastrefresh = update;
624 Desktop.instance.startServiceDiscovery(true);
627 Desktop.instance.setProgressBar(null, ct);
635 * state counters for ensuring that updates only happen if config has changed.
637 private long update = 0, lastrefresh = 0;
643 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
647 protected void resetWs_actionPerformed(ActionEvent e)
649 Jws2Discoverer.getDiscoverer().setServiceUrls(null);
650 List<String> nwsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
651 if (!wsUrls.equals(nwsUrls))
658 updateAndRefreshWsMenuConfig(true);
661 protected void ok_ActionPerformed(ActionEvent e)
663 // update everything regardless.
664 updateAndRefreshWsMenuConfig(false);
667 public void updateAndRefreshWsMenuConfig(
668 final boolean showProgressInDialog)
670 new Thread(new Runnable()
676 updateWsMenuConfig(false);
677 refreshWsMenu(showProgressInDialog);