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(10);
134 private class JabaWSStatusRenderer extends JPanel implements
137 public JabaWSStatusRenderer()
140 setMinimumSize(new Dimension(10, 10));
146 * render an Integer reflecting service status as a colour and symbol
150 public Component getTableCellRendererComponent(JTable arg0,
151 Object status, boolean isSelected, boolean hasFocus, int row,
155 String t = new String("");
156 switch (((Integer) status).intValue())
159 // cb.setSelected(true);
164 // cb.setSelected(true);
169 // cb.setSelected(false);
174 // cb.setSelected(false);
186 private class WsUrlTableModel extends AbstractTableModel
189 private Object[][] data;
190 private String[] columnNames = new String[]
191 { MessageManager.getString("label.service_url"), MessageManager.getString("label.status") };
193 public WsUrlTableModel(Object[][] tdat)
199 public int getColumnCount()
205 public String getColumnName(int column)
207 return columnNames[column];
211 public int getRowCount()
221 public java.lang.Class<?> getColumnClass(int columnIndex)
223 return getValueAt(0, columnIndex).getClass();
227 public Object getValueAt(int rowIndex, int columnIndex)
229 return data[rowIndex][columnIndex];
234 private void updateRsbsList()
236 sbrsList.setListData(rsbsUrls);
239 private void updateServiceList()
241 Jws2Discoverer.getDiscoverer().setServiceUrls(wsUrls);
244 private void updateRsbsServiceList()
246 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
253 * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
257 protected void deleteWsUrl_actionPerformed(ActionEvent e)
259 int sel = wsList.getSelectedRow();
271 * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
275 protected void editWsUrl_actionPerformed(ActionEvent e)
277 int sel = wsList.getSelectedRow();
280 String url = editUrl(wsUrls.get(sel),
281 MessageManager.getString("label.edit_jabaws_url"));
284 int present = wsUrls.indexOf(url);
288 wsUrls.set(sel, url);
304 protected void newSbrsUrl_actionPerformed(ActionEvent e)
306 RestServiceEditorPane rse = new RestServiceEditorPane();
307 rse.showDialog(MessageManager.getString("label.add_new_sbrs_service"));
308 String rservice = rse.getEditedRestService();
309 if (rservice != null && !rsbsUrls.contains(rservice))
311 rsbsUrls.add(rservice);
318 protected void editSbrsUrl_actionPerformed(ActionEvent e)
320 int sel = sbrsList.getSelectedIndex();
323 RestServiceEditorPane rse = new RestServiceEditorPane(
324 new RestServiceDescription(rsbsUrls.elementAt(sel)));
325 rse.showDialog(MessageManager.getString("label.edit_sbrs_entry"));
326 String rservice = rse.getEditedRestService();
327 if (rservice != null)
329 int present = rsbsUrls.indexOf(rservice);
333 rsbsUrls.setElementAt(rservice, sel);
340 rsbsUrls.removeElementAt(sel);
349 void updateWsMenuConfig(boolean old)
353 if (oldUrls != wsUrls
354 || (wsUrls != null && oldUrls != null && !wsUrls
359 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
360 if (oldRsbsUrls != rsbsUrls
361 || (rsbsUrls != null && oldRsbsUrls != null && !oldRsbsUrls
366 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
373 "SHOW_ENFIN_SERVICES",
375 old ? oldEnfin : enableEnfinServices.isSelected())
378 "SHOW_JWS2_SERVICES",
379 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
383 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
387 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
391 "SHOW_WSDISCOVERY_ERRORS",
393 old ? oldWsWarning : displayWsWarning.isSelected())
396 updateRsbsServiceList();
403 * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
407 protected void moveWsUrlDown_actionPerformed(ActionEvent e)
409 int p = wsList.getSelectedRow();
410 if (p > -1 && p < wsUrls.size() - 1)
412 String t = wsUrls.get(p + 1);
413 wsUrls.set(p + 1, wsUrls.get(p));
416 wsList.getSelectionModel().setSelectionInterval(p + 1, p + 1);
425 * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
429 protected void moveWsUrlUp_actionPerformed(ActionEvent e)
431 int p = wsList.getSelectedRow();
434 String t = wsUrls.get(p - 1);
435 wsUrls.set(p - 1, wsUrls.get(p));
438 wsList.getSelectionModel().setSelectionInterval(p - 1, p - 1);
443 private String editUrl(String initUrl, String title)
445 String url = initUrl;
451 JTextField urltf = new JTextField(url, 40);
452 JPanel panel = new JPanel(new BorderLayout());
453 JPanel pane12 = new JPanel(new BorderLayout());
454 pane12.add(new JLabel(MessageManager.getString("label.url")),
455 BorderLayout.CENTER);
456 pane12.add(urltf, BorderLayout.EAST);
457 panel.add(pane12, BorderLayout.NORTH);
458 boolean valid = false;
459 int resp = JOptionPane.CANCEL_OPTION;
461 && (resp = JOptionPane.showInternalConfirmDialog(
462 Desktop.desktop, panel, title,
463 JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
467 // TODO: do a better job of checking that the url is a valid discovery
468 // URL for web services.
469 String tx = urltf.getText().trim();
470 while (tx.length() > 0 && tx.lastIndexOf('/') == tx.length() - 1)
472 tx = tx.substring(0, tx.length() - 1);
477 } catch (Exception e)
480 JOptionPane.showInternalMessageDialog(Desktop.desktop,
481 MessageManager.getString("label.invalid_url"));
484 if (valid && resp == JOptionPane.OK_OPTION)
486 int validate = JOptionPane
487 .showInternalConfirmDialog(
489 MessageManager.getString("info.validate_jabaws_server"),
490 MessageManager.getString("label.test_server"), JOptionPane.YES_NO_OPTION);
492 if (validate == JOptionPane.OK_OPTION)
494 if (Jws2Discoverer.testServiceUrl(foo))
496 return foo.toString();
500 int opt = JOptionPane
501 .showInternalOptionDialog(
505 + "' failed validation,\ndo you want to add it anyway? ",
506 "Server Validation Failed",
507 JOptionPane.YES_NO_OPTION,
508 JOptionPane.INFORMATION_MESSAGE, null, null, null);
509 if (opt == JOptionPane.YES_OPTION)
511 return foo.toString();
516 .showInternalMessageDialog(
519 .getString("warn.server_didnt_pass_validation"));
527 // just return the URL anyway
528 return foo.toString();
537 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
541 protected void newWsUrl_actionPerformed(ActionEvent e)
543 String url = editUrl(null, MessageManager.getString("label.add_jabaws_url"));
546 if (!wsUrls.contains(url))
548 int selind = wsList.getSelectedRow();
551 wsUrls.add(selind, url);
566 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
570 protected void refreshWs_actionPerformed(ActionEvent e)
572 new Thread(new Runnable()
578 lastrefresh = update - 1;
579 updateWsMenuConfig(false);
587 * Refresh the web services menus - but only if there has been a change in the
588 * configuration (indicated by update!=lastrefresh)
590 * @param showProgress
591 * show progress in dialog or on desktop
593 protected void refreshWsMenu(boolean showProgress)
597 new Thread(new Runnable()
602 progressBar.setVisible(true);
604 progressBar.setIndeterminate(true);
605 if (lastrefresh != update)
607 lastrefresh = update;
608 Desktop.instance.startServiceDiscovery(true); // wait around for all
609 // threads to complete
613 progressBar.setIndeterminate(false);
614 progressBar.setVisible(false);
622 new Thread(new Runnable()
627 long ct = System.currentTimeMillis();
628 Desktop.instance.setProgressBar(MessageManager.getString("status.refreshing_web_service_menus"),
630 if (lastrefresh != update)
632 lastrefresh = update;
633 Desktop.instance.startServiceDiscovery(true);
636 Desktop.instance.setProgressBar(null, ct);
644 * state counters for ensuring that updates only happen if config has changed.
646 private long update = 0, lastrefresh = 0;
652 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
656 protected void resetWs_actionPerformed(ActionEvent e)
658 Jws2Discoverer.getDiscoverer().setServiceUrls(null);
659 List<String> nwsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
660 if (!wsUrls.equals(nwsUrls))
667 updateAndRefreshWsMenuConfig(true);
670 protected void ok_ActionPerformed(ActionEvent e)
672 // update everything regardless.
673 updateAndRefreshWsMenuConfig(false);
676 public void updateAndRefreshWsMenuConfig(
677 final boolean showProgressInDialog)
679 new Thread(new Runnable()
684 updateWsMenuConfig(false);
685 refreshWsMenu(showProgressInDialog);