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.
24 import java.awt.BorderLayout;
25 import java.awt.Color;
26 import java.awt.Component;
27 import java.awt.Dimension;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Vector;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.JTable;
38 import javax.swing.JTextField;
39 import javax.swing.table.AbstractTableModel;
40 import javax.swing.table.TableCellRenderer;
41 import jalview.bin.Cache;
42 import jalview.jbgui.GWsPreferences;
43 import jalview.util.MessageManager;
44 import jalview.ws.WSDiscovererI;
45 import jalview.ws.jws2.Jws2Discoverer;
46 import jalview.ws.rest.RestServiceDescription;
48 public class WsPreferences extends GWsPreferences
51 public WsPreferences()
54 initFromPreferences();
59 Vector<String> oldUrls, rsbsUrls, oldRsbsUrls;
61 private boolean needWsMenuUpdate;
63 private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
64 oldEnfin, oldWsWarning;
66 private void initFromPreferences()
69 wsUrls = Jws2Discoverer.getInstance().getServiceUrls();
70 if (!wsUrls.isEmpty())
72 oldUrls = new Vector<String>(wsUrls);
77 wsUrls = new Vector<String>();
79 wsList.setDefaultRenderer(Integer.class, new JabaWSStatusRenderer());
80 wsList.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
82 rsbsUrls = jalview.ws.rest.RestClient.getRsbsDescriptions();
85 oldRsbsUrls = new Vector<String>(rsbsUrls);
90 rsbsUrls = new Vector<String>();
93 enableEnfinServices.setSelected(
94 oldEnfin = Cache.getDefault("SHOW_ENFIN_SERVICES", true));
95 enableEnfinServices.addActionListener(updateAction);
96 enableJws2Services.setSelected(
97 oldJws2 = Cache.getDefault("SHOW_JWS2_SERVICES", true));
98 enableJws2Services.addActionListener(updateAction);
99 indexByHost.setSelected(
100 oldIndexByHost = Cache.getDefault("WSMENU_BYHOST", false));
101 indexByHost.addActionListener(updateAction);
102 indexByType.setSelected(
103 oldIndexByType = Cache.getDefault("WSMENU_BYTYPE", false));
104 indexByType.addActionListener(updateAction);
105 displayWsWarning.setSelected(oldWsWarning = Cache
106 .getDefault("SHOW_WSDISCOVERY_ERRORS", true));
109 ActionListener updateAction = new ActionListener()
113 public void actionPerformed(ActionEvent e)
120 private void updateList()
122 Object tdat[][] = new Object[wsUrls.size()][2];
124 for (String url : wsUrls)
126 int status = Jws2Discoverer.getInstance().getServerStatusFor(url);
127 tdat[r][1] = Integer.valueOf(status);
131 wsList.setModel(new WsUrlTableModel(tdat));
132 wsList.getColumn(MessageManager.getString("label.status"))
136 private class JabaWSStatusRenderer extends JPanel
137 implements TableCellRenderer
139 public JabaWSStatusRenderer()
142 setMinimumSize(new Dimension(10, 10));
148 * render an Integer reflecting service status as a colour and symbol
152 public Component getTableCellRendererComponent(JTable arg0,
153 Object status, boolean isSelected, boolean hasFocus, int row,
157 String t = new String("");
158 switch (((Integer) status).intValue())
160 case WSDiscovererI.STATUS_OK:
161 // cb.setSelected(true);
165 case WSDiscovererI.STATUS_NO_SERVICES:
166 // cb.setSelected(true);
170 case WSDiscovererI.STATUS_INVALID:
171 // cb.setSelected(false);
175 case WSDiscovererI.STATUS_UNKNOWN:
177 // cb.setSelected(false);
189 private class WsUrlTableModel extends AbstractTableModel
192 private Object[][] data;
194 private String[] columnNames = new String[] {
195 MessageManager.getString("label.service_url"),
196 MessageManager.getString("label.status") };
198 public WsUrlTableModel(Object[][] tdat)
204 public int getColumnCount()
210 public String getColumnName(int column)
212 return columnNames[column];
216 public int getRowCount()
226 public java.lang.Class<?> getColumnClass(int columnIndex)
228 return getValueAt(0, columnIndex).getClass();
232 public Object getValueAt(int rowIndex, int columnIndex)
234 return data[rowIndex][columnIndex];
239 private void updateRsbsList()
241 sbrsList.setListData(rsbsUrls);
244 private void updateServiceList()
246 Jws2Discoverer.getInstance().setServiceUrls(wsUrls);
249 private void updateRsbsServiceList()
251 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
258 * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
262 protected void deleteWsUrl_actionPerformed(ActionEvent e)
264 int sel = wsList.getSelectedRow();
276 * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
280 protected void editWsUrl_actionPerformed(ActionEvent e)
282 int sel = wsList.getSelectedRow();
285 String url = editUrl(wsUrls.get(sel),
286 MessageManager.getString("label.edit_jabaws_url"));
289 int present = wsUrls.indexOf(url);
293 wsUrls.set(sel, url);
309 protected void newSbrsUrl_actionPerformed(ActionEvent e)
311 RestServiceEditorPane rse = new RestServiceEditorPane();
312 rse.showDialog(MessageManager.getString("label.add_new_sbrs_service"));
313 String rservice = rse.getEditedRestService();
314 if (rservice != null && !rsbsUrls.contains(rservice))
316 rsbsUrls.add(rservice);
323 protected void editSbrsUrl_actionPerformed(ActionEvent e)
325 int sel = sbrsList.getSelectedIndex();
328 RestServiceEditorPane rse = new RestServiceEditorPane(
329 new RestServiceDescription(rsbsUrls.elementAt(sel)));
330 rse.showDialog(MessageManager.getString("label.edit_sbrs_entry"));
331 String rservice = rse.getEditedRestService();
332 if (rservice != null)
334 int present = rsbsUrls.indexOf(rservice);
338 rsbsUrls.setElementAt(rservice, sel);
345 rsbsUrls.removeElementAt(sel);
354 void updateWsMenuConfig(boolean old)
358 if (oldUrls != wsUrls || (wsUrls != null && oldUrls != null
359 && !wsUrls.equals(oldUrls)))
363 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
364 if (oldRsbsUrls != rsbsUrls || (rsbsUrls != null
365 && oldRsbsUrls != null && !oldRsbsUrls.equals(rsbsUrls)))
369 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
375 Cache.setProperty("SHOW_ENFIN_SERVICES",
377 old ? oldEnfin : enableEnfinServices.isSelected())
379 Cache.setProperty("SHOW_JWS2_SERVICES",
380 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
382 Cache.setProperty("WSMENU_BYHOST",
383 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
385 Cache.setProperty("WSMENU_BYTYPE",
386 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
389 Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
391 old ? oldWsWarning : displayWsWarning.isSelected())
394 updateRsbsServiceList();
401 * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
405 protected void moveWsUrlDown_actionPerformed(ActionEvent e)
407 int p = wsList.getSelectedRow();
408 if (p > -1 && p < wsUrls.size() - 1)
410 String t = wsUrls.get(p + 1);
411 wsUrls.set(p + 1, wsUrls.get(p));
414 wsList.getSelectionModel().setSelectionInterval(p + 1, p + 1);
423 * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
427 protected void moveWsUrlUp_actionPerformed(ActionEvent e)
429 int p = wsList.getSelectedRow();
432 String t = wsUrls.get(p - 1);
433 wsUrls.set(p - 1, wsUrls.get(p));
436 wsList.getSelectionModel().setSelectionInterval(p - 1, p - 1);
441 private String editUrl(String initUrl, String title)
443 String url = initUrl;
449 JTextField urltf = new JTextField(url, 40);
450 JPanel panel = new JPanel(new BorderLayout());
451 JPanel pane12 = new JPanel(new BorderLayout());
452 pane12.add(new JLabel(MessageManager.getString("label.url:")),
453 BorderLayout.CENTER);
454 pane12.add(urltf, BorderLayout.EAST);
455 panel.add(pane12, BorderLayout.NORTH);
456 boolean valid = false;
457 int resp = JvOptionPane.CANCEL_OPTION;
458 while (!valid && (resp = JvOptionPane.showInternalConfirmDialog(
459 Desktop.getDesktopPane(), panel, title,
460 JvOptionPane.OK_CANCEL_OPTION)) == JvOptionPane.OK_OPTION)
464 // TODO: do a better job of checking that the url is a valid discovery
465 // URL for web services.
466 String tx = urltf.getText().trim();
467 while (tx.length() > 0 && tx.lastIndexOf('/') == tx.length() - 1)
469 tx = tx.substring(0, tx.length() - 1);
474 } catch (Exception e)
477 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
478 MessageManager.getString("label.invalid_url"));
481 if (valid && resp == JvOptionPane.OK_OPTION)
483 int validate = JvOptionPane.showInternalConfirmDialog(Desktop.getDesktopPane(),
484 MessageManager.getString("info.validate_jabaws_server"),
485 MessageManager.getString("label.test_server"),
486 JvOptionPane.YES_NO_OPTION);
488 if (validate == JvOptionPane.OK_OPTION)
490 if (Jws2Discoverer.getInstance().testServiceUrl(foo))
492 return foo.toString();
496 int opt = JvOptionPane.showInternalOptionDialog(Desktop.getDesktopPane(),
497 "The Server '" + foo.toString()
498 + "' failed validation,\ndo you want to add it anyway? ",
499 "Server Validation Failed", JvOptionPane.YES_NO_OPTION,
500 JvOptionPane.INFORMATION_MESSAGE, null, null, null);
501 if (opt == JvOptionPane.YES_OPTION)
503 return foo.toString();
507 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
508 MessageManager.getString(
509 "warn.server_didnt_pass_validation"));
516 // just return the URL anyway
517 return foo.toString();
526 * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
530 protected void newWsUrl_actionPerformed(ActionEvent e)
532 String url = editUrl(null,
533 MessageManager.getString("label.add_jabaws_url"));
536 if (!wsUrls.contains(url))
538 int selind = wsList.getSelectedRow();
541 wsUrls.add(selind, url);
556 * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
560 protected void refreshWs_actionPerformed(ActionEvent e)
562 new Thread(new Runnable()
569 lastrefresh = update - 1;
570 updateWsMenuConfig(false);
578 * Refresh the web services menus - but only if there has been a change in the
579 * configuration (indicated by update!=lastrefresh)
581 * @param showProgress
582 * show progress in dialog or on desktop
584 protected void refreshWsMenu(boolean showProgress)
588 new Thread(new Runnable()
594 progressBar.setVisible(true);
596 progressBar.setIndeterminate(true);
597 if (lastrefresh != update)
599 lastrefresh = update;
600 Desktop.getInstance().startServiceDiscovery(true); // wait around for all
601 // threads to complete
605 progressBar.setIndeterminate(false);
606 progressBar.setVisible(false);
614 new Thread(new Runnable()
620 long ct = System.currentTimeMillis();
621 Desktop.getInstance().setProgressBar(MessageManager
622 .getString("status.refreshing_web_service_menus"), ct);
623 if (lastrefresh != update)
625 lastrefresh = update;
626 Desktop.getInstance().startServiceDiscovery(true);
629 Desktop.getInstance().setProgressBar(null, ct);
637 * state counters for ensuring that updates only happen if config has changed.
639 private long update = 0, lastrefresh = 0;
645 * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
649 protected void resetWs_actionPerformed(ActionEvent e)
651 Jws2Discoverer.getInstance().setServiceUrls(null);
652 List<String> nwsUrls = Jws2Discoverer.getInstance().getServiceUrls();
653 if (!wsUrls.equals(nwsUrls))
660 updateAndRefreshWsMenuConfig(true);
663 protected void ok_ActionPerformed(ActionEvent e)
665 // update everything regardless.
666 updateAndRefreshWsMenuConfig(false);
669 public void updateAndRefreshWsMenuConfig(
670 final boolean showProgressInDialog)
672 new Thread(new Runnable()
678 updateWsMenuConfig(false);
679 refreshWsMenu(showProgressInDialog);