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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Component;
26 import java.awt.Dimension;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
30 import java.util.List;
31 import java.util.Vector;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JTable;
36 import javax.swing.JTextField;
37 import javax.swing.table.AbstractTableModel;
38 import javax.swing.table.TableCellRenderer;
40 import jalview.bin.Cache;
41 import jalview.jbgui.GWsPreferences;
42 import jalview.util.MessageManager;
43 import jalview.ws.jws2.Jws2Discoverer;
44 import jalview.ws.rest.RestServiceDescription;
46 public class WsPreferences extends GWsPreferences
49 public WsPreferences()
52 initFromPreferences();
57 Vector<String> oldUrls, rsbsUrls, oldRsbsUrls;
59 private boolean needWsMenuUpdate;
61 private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
62 oldEnfin, oldWsWarning;
64 private void initFromPreferences()
67 wsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
68 if (!wsUrls.isEmpty())
70 oldUrls = new Vector<String>(wsUrls);
75 wsUrls = new Vector<String>();
77 wsList.setDefaultRenderer(Integer.class, new JabaWSStatusRenderer());
78 wsList.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
80 rsbsUrls = jalview.ws.rest.RestClient.getRsbsDescriptions();
83 oldRsbsUrls = new Vector<String>(rsbsUrls);
88 rsbsUrls = new Vector<String>();
91 enableEnfinServices.setSelected(
92 oldEnfin = Cache.getDefault("SHOW_ENFIN_SERVICES", true));
93 enableEnfinServices.addActionListener(updateAction);
94 enableJws2Services.setSelected(
95 oldJws2 = Cache.getDefault("SHOW_JWS2_SERVICES", true));
96 enableJws2Services.addActionListener(updateAction);
97 indexByHost.setSelected(
98 oldIndexByHost = Cache.getDefault("WSMENU_BYHOST", false));
99 indexByHost.addActionListener(updateAction);
100 indexByType.setSelected(
101 oldIndexByType = Cache.getDefault("WSMENU_BYTYPE", false));
102 indexByType.addActionListener(updateAction);
103 displayWsWarning.setSelected(oldWsWarning = Cache
104 .getDefault("SHOW_WSDISCOVERY_ERRORS", true));
107 ActionListener updateAction = new ActionListener()
111 public void actionPerformed(ActionEvent e)
118 private void updateList()
120 Object tdat[][] = new Object[wsUrls.size()][2];
122 for (String url : wsUrls)
124 int status = Jws2Discoverer.getDiscoverer().getServerStatusFor(url);
125 tdat[r][1] = Integer.valueOf(status);
129 wsList.setModel(new WsUrlTableModel(tdat));
130 wsList.getColumn(MessageManager.getString("label.status"))
134 private class JabaWSStatusRenderer extends JPanel
135 implements TableCellRenderer
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;
191 private String[] columnNames = new String[] {
192 MessageManager.getString("label.service_url"),
193 MessageManager.getString("label.status") };
195 public WsUrlTableModel(Object[][] tdat)
201 public int getColumnCount()
207 public String getColumnName(int column)
209 return columnNames[column];
213 public int getRowCount()
223 public java.lang.Class<?> getColumnClass(int columnIndex)
225 return getValueAt(0, columnIndex).getClass();
229 public Object getValueAt(int rowIndex, int columnIndex)
231 return data[rowIndex][columnIndex];
236 private void updateRsbsList()
238 sbrsList.setListData(rsbsUrls);
241 private void updateServiceList()
243 Jws2Discoverer.getDiscoverer().setServiceUrls(wsUrls);
246 private void updateRsbsServiceList()
248 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
255 * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
259 protected void deleteWsUrl_actionPerformed(ActionEvent e)
261 int sel = wsList.getSelectedRow();
273 * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
277 protected void editWsUrl_actionPerformed(ActionEvent e)
279 int sel = wsList.getSelectedRow();
282 String url = editUrl(wsUrls.get(sel),
283 MessageManager.getString("label.edit_jabaws_url"));
286 int present = wsUrls.indexOf(url);
290 wsUrls.set(sel, url);
306 protected void newSbrsUrl_actionPerformed(ActionEvent e)
308 RestServiceEditorPane rse = new RestServiceEditorPane();
309 rse.showDialog(MessageManager.getString("label.add_new_sbrs_service"));
310 String rservice = rse.getEditedRestService();
311 if (rservice != null && !rsbsUrls.contains(rservice))
313 rsbsUrls.add(rservice);
320 protected void editSbrsUrl_actionPerformed(ActionEvent e)
322 int sel = sbrsList.getSelectedIndex();
325 RestServiceEditorPane rse = new RestServiceEditorPane(
326 new RestServiceDescription(rsbsUrls.elementAt(sel)));
327 rse.showDialog(MessageManager.getString("label.edit_sbrs_entry"));
328 String rservice = rse.getEditedRestService();
329 if (rservice != null)
331 int present = rsbsUrls.indexOf(rservice);
335 rsbsUrls.setElementAt(rservice, sel);
342 rsbsUrls.removeElementAt(sel);
351 void updateWsMenuConfig(boolean old)
355 if (oldUrls != wsUrls || (wsUrls != null && oldUrls != null
356 && !wsUrls.equals(oldUrls)))
360 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
361 if (oldRsbsUrls != rsbsUrls || (rsbsUrls != null
362 && oldRsbsUrls != null && !oldRsbsUrls.equals(rsbsUrls)))
366 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
372 Cache.setProperty("SHOW_ENFIN_SERVICES",
374 old ? oldEnfin : enableEnfinServices.isSelected())
376 Cache.setProperty("SHOW_JWS2_SERVICES",
377 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
379 Cache.setProperty("WSMENU_BYHOST",
380 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
382 Cache.setProperty("WSMENU_BYTYPE",
383 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
386 Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
388 old ? oldWsWarning : displayWsWarning.isSelected())
391 updateRsbsServiceList();
398 * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
402 protected void moveWsUrlDown_actionPerformed(ActionEvent e)
404 int p = wsList.getSelectedRow();
405 if (p > -1 && p < wsUrls.size() - 1)
407 String t = wsUrls.get(p + 1);
408 wsUrls.set(p + 1, wsUrls.get(p));
411 wsList.getSelectionModel().setSelectionInterval(p + 1, p + 1);
420 * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
424 protected void moveWsUrlUp_actionPerformed(ActionEvent e)
426 int p = wsList.getSelectedRow();
429 String t = wsUrls.get(p - 1);
430 wsUrls.set(p - 1, wsUrls.get(p));
433 wsList.getSelectionModel().setSelectionInterval(p - 1, p - 1);
438 private String editUrl(String initUrl, String title)
440 String url = initUrl;
446 JTextField urltf = new JTextField(url, 40);
447 JPanel panel = new JPanel(new BorderLayout());
448 JPanel pane12 = new JPanel(new BorderLayout());
449 pane12.add(new JLabel(MessageManager.getString("label.url:")),
450 BorderLayout.CENTER);
451 pane12.add(urltf, BorderLayout.EAST);
452 panel.add(pane12, BorderLayout.NORTH);
453 boolean valid = false;
454 int resp = JvOptionPane.CANCEL_OPTION;
455 while (!valid && (resp = JvOptionPane.showInternalConfirmDialog(
456 Desktop.desktop, panel, title,
457 JvOptionPane.OK_CANCEL_OPTION)) == JvOptionPane.OK_OPTION)
461 // TODO: do a better job of checking that the url is a valid discovery
462 // URL for web services.
463 String tx = urltf.getText().trim();
464 while (tx.length() > 0 && tx.lastIndexOf('/') == tx.length() - 1)
466 tx = tx.substring(0, tx.length() - 1);
471 } catch (Exception e)
474 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
475 MessageManager.getString("label.invalid_url"));
478 if (valid && resp == JvOptionPane.OK_OPTION)
480 int validate = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
481 MessageManager.getString("info.validate_jabaws_server"),
482 MessageManager.getString("label.test_server"),
483 JvOptionPane.YES_NO_OPTION);
485 if (validate == JvOptionPane.OK_OPTION)
487 if (Jws2Discoverer.testServiceUrl(foo))
489 return foo.toString();
493 int opt = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
494 "The Server '" + foo.toString()
495 + "' failed validation,\ndo you want to add it anyway? ",
496 "Server Validation Failed", JvOptionPane.YES_NO_OPTION,
497 JvOptionPane.INFORMATION_MESSAGE, null, null, null);
498 if (opt == JvOptionPane.YES_OPTION)
500 return foo.toString();
504 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
505 MessageManager.getString(
506 "warn.server_didnt_pass_validation"));
513 // just return the URL anyway
514 return foo.toString();
523 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
527 protected void newWsUrl_actionPerformed(ActionEvent e)
529 String url = editUrl(null,
530 MessageManager.getString("label.add_jabaws_url"));
533 if (!wsUrls.contains(url))
535 int selind = wsList.getSelectedRow();
538 wsUrls.add(selind, url);
553 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
557 protected void refreshWs_actionPerformed(ActionEvent e)
559 new Thread(new Runnable()
566 lastrefresh = update - 1;
567 updateWsMenuConfig(false);
575 * Refresh the web services menus - but only if there has been a change in the
576 * configuration (indicated by update!=lastrefresh)
578 * @param showProgress
579 * show progress in dialog or on desktop
581 protected void refreshWsMenu(boolean showProgress)
585 new Thread(new Runnable()
591 progressBar.setVisible(true);
593 progressBar.setIndeterminate(true);
594 if (lastrefresh != update)
596 lastrefresh = update;
597 Desktop.instance.startServiceDiscovery(true); // wait around for all
598 // threads to complete
602 progressBar.setIndeterminate(false);
603 progressBar.setVisible(false);
611 new Thread(new Runnable()
617 long ct = System.currentTimeMillis();
618 Desktop.instance.setProgressBar(MessageManager
619 .getString("status.refreshing_web_service_menus"), ct);
620 if (lastrefresh != update)
622 lastrefresh = update;
623 Desktop.instance.startServiceDiscovery(true);
626 Desktop.instance.setProgressBar(null, ct);
634 * state counters for ensuring that updates only happen if config has changed.
636 protected long update = 0;
638 private long lastrefresh = 0;
644 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
648 protected void resetWs_actionPerformed(ActionEvent e)
650 Jws2Discoverer.getDiscoverer().setServiceUrls(null);
651 List<String> nwsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
652 if (!wsUrls.equals(nwsUrls))
659 updateAndRefreshWsMenuConfig(true);
662 protected void ok_ActionPerformed(ActionEvent e)
664 // update everything regardless.
665 updateAndRefreshWsMenuConfig(false);
668 public void updateAndRefreshWsMenuConfig(
669 final boolean showProgressInDialog)
671 new Thread(new Runnable()
677 updateWsMenuConfig(false);
678 refreshWsMenu(showProgressInDialog);