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(oldEnfin = Cache.getDefault(
93 "SHOW_ENFIN_SERVICES", true));
94 enableEnfinServices.addActionListener(updateAction);
95 enableJws2Services.setSelected(oldJws2 = Cache.getDefault(
96 "SHOW_JWS2_SERVICES", true));
97 enableJws2Services.addActionListener(updateAction);
98 indexByHost.setSelected(oldIndexByHost = Cache.getDefault(
99 "WSMENU_BYHOST", false));
100 indexByHost.addActionListener(updateAction);
101 indexByType.setSelected(oldIndexByType = Cache.getDefault(
102 "WSMENU_BYTYPE", false));
103 indexByType.addActionListener(updateAction);
104 displayWsWarning.setSelected(oldWsWarning = Cache.getDefault(
105 "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")).setMinWidth(
135 private class JabaWSStatusRenderer extends JPanel implements
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
357 || (wsUrls != null && oldUrls != null && !wsUrls
362 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
363 if (oldRsbsUrls != rsbsUrls
364 || (rsbsUrls != null && oldRsbsUrls != null && !oldRsbsUrls
369 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
376 "SHOW_ENFIN_SERVICES",
378 old ? oldEnfin : enableEnfinServices.isSelected())
381 "SHOW_JWS2_SERVICES",
382 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
386 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
390 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
394 "SHOW_WSDISCOVERY_ERRORS",
396 old ? oldWsWarning : displayWsWarning.isSelected())
399 updateRsbsServiceList();
406 * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
410 protected void moveWsUrlDown_actionPerformed(ActionEvent e)
412 int p = wsList.getSelectedRow();
413 if (p > -1 && p < wsUrls.size() - 1)
415 String t = wsUrls.get(p + 1);
416 wsUrls.set(p + 1, wsUrls.get(p));
419 wsList.getSelectionModel().setSelectionInterval(p + 1, p + 1);
428 * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
432 protected void moveWsUrlUp_actionPerformed(ActionEvent e)
434 int p = wsList.getSelectedRow();
437 String t = wsUrls.get(p - 1);
438 wsUrls.set(p - 1, wsUrls.get(p));
441 wsList.getSelectionModel().setSelectionInterval(p - 1, p - 1);
446 private String editUrl(String initUrl, String title)
448 String url = initUrl;
454 JTextField urltf = new JTextField(url, 40);
455 JPanel panel = new JPanel(new BorderLayout());
456 JPanel pane12 = new JPanel(new BorderLayout());
457 pane12.add(new JLabel(MessageManager.getString("label.url")),
458 BorderLayout.CENTER);
459 pane12.add(urltf, BorderLayout.EAST);
460 panel.add(pane12, BorderLayout.NORTH);
461 boolean valid = false;
462 int resp = JOptionPane.CANCEL_OPTION;
464 && (resp = JOptionPane.showInternalConfirmDialog(
465 Desktop.desktop, panel, title,
466 JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
470 // TODO: do a better job of checking that the url is a valid discovery
471 // URL for web services.
472 String tx = urltf.getText().trim();
473 while (tx.length() > 0 && tx.lastIndexOf('/') == tx.length() - 1)
475 tx = tx.substring(0, tx.length() - 1);
480 } catch (Exception e)
483 JOptionPane.showInternalMessageDialog(Desktop.desktop,
484 MessageManager.getString("label.invalid_url"));
487 if (valid && resp == JOptionPane.OK_OPTION)
489 int validate = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
490 MessageManager.getString("info.validate_jabaws_server"),
491 MessageManager.getString("label.test_server"),
492 JOptionPane.YES_NO_OPTION);
494 if (validate == JOptionPane.OK_OPTION)
496 if (Jws2Discoverer.testServiceUrl(foo))
498 return foo.toString();
502 int opt = JOptionPane
503 .showInternalOptionDialog(
507 + "' failed validation,\ndo you want to add it anyway? ",
508 "Server Validation Failed",
509 JOptionPane.YES_NO_OPTION,
510 JOptionPane.INFORMATION_MESSAGE, null, null, null);
511 if (opt == JOptionPane.YES_OPTION)
513 return foo.toString();
518 .showInternalMessageDialog(
521 .getString("warn.server_didnt_pass_validation"));
528 // just return the URL anyway
529 return foo.toString();
538 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
542 protected void newWsUrl_actionPerformed(ActionEvent e)
544 String url = editUrl(null,
545 MessageManager.getString("label.add_jabaws_url"));
548 if (!wsUrls.contains(url))
550 int selind = wsList.getSelectedRow();
553 wsUrls.add(selind, url);
568 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
572 protected void refreshWs_actionPerformed(ActionEvent e)
574 new Thread(new Runnable()
580 lastrefresh = update - 1;
581 updateWsMenuConfig(false);
589 * Refresh the web services menus - but only if there has been a change in the
590 * configuration (indicated by update!=lastrefresh)
592 * @param showProgress
593 * show progress in dialog or on desktop
595 protected void refreshWsMenu(boolean showProgress)
599 new Thread(new Runnable()
604 progressBar.setVisible(true);
606 progressBar.setIndeterminate(true);
607 if (lastrefresh != update)
609 lastrefresh = update;
610 Desktop.instance.startServiceDiscovery(true); // wait around for all
611 // threads to complete
615 progressBar.setIndeterminate(false);
616 progressBar.setVisible(false);
624 new Thread(new Runnable()
629 long ct = System.currentTimeMillis();
630 Desktop.instance.setProgressBar(MessageManager
631 .getString("status.refreshing_web_service_menus"), ct);
632 if (lastrefresh != update)
634 lastrefresh = update;
635 Desktop.instance.startServiceDiscovery(true);
638 Desktop.instance.setProgressBar(null, ct);
646 * state counters for ensuring that updates only happen if config has changed.
648 private long update = 0, lastrefresh = 0;
654 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
658 protected void resetWs_actionPerformed(ActionEvent e)
660 Jws2Discoverer.getDiscoverer().setServiceUrls(null);
661 List<String> nwsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
662 if (!wsUrls.equals(nwsUrls))
669 updateAndRefreshWsMenuConfig(true);
672 protected void ok_ActionPerformed(ActionEvent e)
674 // update everything regardless.
675 updateAndRefreshWsMenuConfig(false);
678 public void updateAndRefreshWsMenuConfig(
679 final boolean showProgressInDialog)
681 new Thread(new Runnable()
686 updateWsMenuConfig(false);
687 refreshWsMenu(showProgressInDialog);