Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 10:11:34 GMT
  2. Package jalview.gui

File WsPreferences.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

78
209
33
3
683
523
87
0.42
6.33
11
2.64

Classes

Class Line # Actions
WsPreferences 46 183 75
0.00%
WsPreferences.JabaWSStatusRenderer 134 18 5
0.00%
WsPreferences.WsUrlTableModel 186 8 7
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.gui;
22   
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;
29    import java.net.URL;
30    import java.util.List;
31    import java.util.Vector;
32   
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;
39   
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;
45   
 
46    public class WsPreferences extends GWsPreferences
47    {
48   
 
49  0 toggle public WsPreferences()
50    {
51  0 super();
52  0 initFromPreferences();
53    }
54   
55    List<String> wsUrls;
56   
57    Vector<String> oldUrls, rsbsUrls, oldRsbsUrls;
58   
59    private boolean needWsMenuUpdate;
60   
61    private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
62    oldEnfin, oldWsWarning;
63   
 
64  0 toggle private void initFromPreferences()
65    {
66   
67  0 wsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
68  0 if (!wsUrls.isEmpty())
69    {
70  0 oldUrls = new Vector<String>(wsUrls);
71    }
72    else
73    {
74  0 oldUrls = null;
75  0 wsUrls = new Vector<String>();
76    }
77  0 wsList.setDefaultRenderer(Integer.class, new JabaWSStatusRenderer());
78  0 wsList.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
79  0 updateList();
80  0 rsbsUrls = jalview.ws.rest.RestClient.getRsbsDescriptions();
81  0 if (rsbsUrls != null)
82    {
83  0 oldRsbsUrls = new Vector<String>(rsbsUrls);
84    }
85    else
86    {
87  0 oldRsbsUrls = null;
88  0 rsbsUrls = new Vector<String>();
89    }
90  0 updateRsbsList();
91  0 enableEnfinServices.setSelected(
92    oldEnfin = Cache.getDefault("SHOW_ENFIN_SERVICES", true));
93  0 enableEnfinServices.addActionListener(updateAction);
94  0 enableJws2Services.setSelected(
95    oldJws2 = Cache.getDefault("SHOW_JWS2_SERVICES", true));
96  0 enableJws2Services.addActionListener(updateAction);
97  0 indexByHost.setSelected(
98    oldIndexByHost = Cache.getDefault("WSMENU_BYHOST", false));
99  0 indexByHost.addActionListener(updateAction);
100  0 indexByType.setSelected(
101    oldIndexByType = Cache.getDefault("WSMENU_BYTYPE", false));
102  0 indexByType.addActionListener(updateAction);
103  0 displayWsWarning.setSelected(oldWsWarning = Cache
104    .getDefault("SHOW_WSDISCOVERY_ERRORS", true));
105    }
106   
107    ActionListener updateAction = new ActionListener()
108    {
109   
 
110  0 toggle @Override
111    public void actionPerformed(ActionEvent e)
112    {
113  0 update++;
114    }
115   
116    };
117   
 
118  0 toggle private void updateList()
119    {
120  0 Object tdat[][] = new Object[wsUrls.size()][2];
121  0 int r = 0;
122  0 for (String url : wsUrls)
123    {
124  0 int status = Jws2Discoverer.getDiscoverer().getServerStatusFor(url);
125  0 tdat[r][1] = Integer.valueOf(status);
126  0 tdat[r++][0] = url;
127    }
128   
129  0 wsList.setModel(new WsUrlTableModel(tdat));
130  0 wsList.getColumn(MessageManager.getString("label.status"))
131    .setMinWidth(10);
132    }
133   
 
134    private class JabaWSStatusRenderer extends JPanel
135    implements TableCellRenderer
136    {
 
137  0 toggle public JabaWSStatusRenderer()
138    {
139  0 setOpaque(true);
140  0 setMinimumSize(new Dimension(10, 10));
141    // setText(" ");
142   
143    }
144   
145    /**
146    * render an Integer reflecting service status as a colour and symbol
147    */
148   
 
149  0 toggle @Override
150    public Component getTableCellRendererComponent(JTable arg0,
151    Object status, boolean isSelected, boolean hasFocus, int row,
152    int column)
153    {
154  0 Color c;
155  0 String t = new String("");
156  0 switch (((Integer) status).intValue())
157    {
158  0 case 1:
159    // cb.setSelected(true);
160    // cb.setBackground(
161  0 c = Color.green;
162  0 break;
163  0 case 0:
164    // cb.setSelected(true);
165    // cb.setBackground(
166  0 c = Color.lightGray;
167  0 break;
168  0 case -1:
169    // cb.setSelected(false);
170    // cb.setBackground(
171  0 c = Color.red;
172  0 break;
173  0 default:
174    // cb.setSelected(false);
175    // cb.setBackground(
176  0 c = Color.orange;
177    }
178  0 setBackground(c);
179    // setText(t);
180  0 return this;
181   
182    }
183   
184    }
185   
 
186    private class WsUrlTableModel extends AbstractTableModel
187    {
188   
189    private Object[][] data;
190   
191    private String[] columnNames = new String[] {
192    MessageManager.getString("label.service_url"),
193    MessageManager.getString("label.status") };
194   
 
195  0 toggle public WsUrlTableModel(Object[][] tdat)
196    {
197  0 this.data = tdat;
198    }
199   
 
200  0 toggle @Override
201    public int getColumnCount()
202    {
203  0 return 2;
204    }
205   
 
206  0 toggle @Override
207    public String getColumnName(int column)
208    {
209  0 return columnNames[column];
210    }
211   
 
212  0 toggle @Override
213    public int getRowCount()
214    {
215  0 if (data == null)
216    {
217  0 return 0;
218    }
219  0 return data.length;
220    }
221   
 
222  0 toggle @Override
223    public java.lang.Class<?> getColumnClass(int columnIndex)
224    {
225  0 return getValueAt(0, columnIndex).getClass();
226    };
227   
 
228  0 toggle @Override
229    public Object getValueAt(int rowIndex, int columnIndex)
230    {
231  0 return data[rowIndex][columnIndex];
232    }
233   
234    }
235   
 
236  0 toggle private void updateRsbsList()
237    {
238  0 sbrsList.setListData(rsbsUrls);
239    }
240   
 
241  0 toggle private void updateServiceList()
242    {
243  0 Jws2Discoverer.getDiscoverer().setServiceUrls(wsUrls);
244    }
245   
 
246  0 toggle private void updateRsbsServiceList()
247    {
248  0 jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
249    }
250   
251    /*
252    * (non-Javadoc)
253    *
254    * @see
255    * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
256    * .ActionEvent)
257    */
 
258  0 toggle @Override
259    protected void deleteWsUrl_actionPerformed(ActionEvent e)
260    {
261  0 int sel = wsList.getSelectedRow();
262  0 if (sel > -1)
263    {
264  0 wsUrls.remove(sel);
265  0 update++;
266  0 updateList();
267    }
268    }
269   
270    /*
271    * (non-Javadoc)
272    *
273    * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
274    * ActionEvent)
275    */
 
276  0 toggle @Override
277    protected void editWsUrl_actionPerformed(ActionEvent e)
278    {
279  0 int sel = wsList.getSelectedRow();
280  0 if (sel > -1)
281    {
282  0 String url = editUrl(wsUrls.get(sel),
283    MessageManager.getString("label.edit_jabaws_url"));
284  0 if (url != null)
285    {
286  0 int present = wsUrls.indexOf(url);
287  0 if (present == -1)
288    {
289  0 update++;
290  0 wsUrls.set(sel, url);
291  0 updateList();
292    }
293    else
294    {
295  0 if (present != sel)
296    {
297  0 wsUrls.remove(sel);
298  0 updateList();
299    }
300    }
301    }
302    }
303    }
304   
 
305  0 toggle @Override
306    protected void newSbrsUrl_actionPerformed(ActionEvent e)
307    {
308  0 RestServiceEditorPane rse = new RestServiceEditorPane();
309  0 rse.showDialog(MessageManager.getString("label.add_new_sbrs_service"));
310  0 String rservice = rse.getEditedRestService();
311  0 if (rservice != null && !rsbsUrls.contains(rservice))
312    {
313  0 rsbsUrls.add(rservice);
314  0 update++;
315  0 updateRsbsList();
316    }
317    }
318   
 
319  0 toggle @Override
320    protected void editSbrsUrl_actionPerformed(ActionEvent e)
321    {
322  0 int sel = sbrsList.getSelectedIndex();
323  0 if (sel > -1)
324    {
325  0 RestServiceEditorPane rse = new RestServiceEditorPane(
326    new RestServiceDescription(rsbsUrls.elementAt(sel)));
327  0 rse.showDialog(MessageManager.getString("label.edit_sbrs_entry"));
328  0 String rservice = rse.getEditedRestService();
329  0 if (rservice != null)
330    {
331  0 int present = rsbsUrls.indexOf(rservice);
332  0 if (present == -1)
333    {
334  0 update++;
335  0 rsbsUrls.setElementAt(rservice, sel);
336  0 updateRsbsList();
337    }
338    else
339    {
340  0 if (present != sel)
341    {
342  0 rsbsUrls.removeElementAt(sel);
343  0 update++;
344  0 updateRsbsList();
345    }
346    }
347    }
348    }
349    }
350   
 
351  0 toggle void updateWsMenuConfig(boolean old)
352    {
353  0 if (old)
354    {
355  0 if (oldUrls != wsUrls || (wsUrls != null && oldUrls != null
356    && !wsUrls.equals(oldUrls)))
357    {
358  0 update++;
359    }
360  0 wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
361  0 if (oldRsbsUrls != rsbsUrls || (rsbsUrls != null
362    && oldRsbsUrls != null && !oldRsbsUrls.equals(rsbsUrls)))
363    {
364  0 update++;
365    }
366  0 oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
367    }
368    else
369    {
370   
371    }
372  0 Cache.setProperty("SHOW_ENFIN_SERVICES",
373    Boolean.valueOf(
374  0 old ? oldEnfin : enableEnfinServices.isSelected())
375    .toString());
376  0 Cache.setProperty("SHOW_JWS2_SERVICES",
377  0 Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
378    .toString());
379  0 Cache.setProperty("WSMENU_BYHOST",
380  0 Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
381    .toString());
382  0 Cache.setProperty("WSMENU_BYTYPE",
383  0 Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
384    .toString());
385   
386  0 Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
387    Boolean.valueOf(
388  0 old ? oldWsWarning : displayWsWarning.isSelected())
389    .toString());
390  0 updateServiceList();
391  0 updateRsbsServiceList();
392    }
393   
394    /*
395    * (non-Javadoc)
396    *
397    * @see
398    * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
399    * .ActionEvent)
400    */
 
401  0 toggle @Override
402    protected void moveWsUrlDown_actionPerformed(ActionEvent e)
403    {
404  0 int p = wsList.getSelectedRow();
405  0 if (p > -1 && p < wsUrls.size() - 1)
406    {
407  0 String t = wsUrls.get(p + 1);
408  0 wsUrls.set(p + 1, wsUrls.get(p));
409  0 wsUrls.set(p, t);
410  0 updateList();
411  0 wsList.getSelectionModel().setSelectionInterval(p + 1, p + 1);
412  0 update++;
413    }
414    }
415   
416    /*
417    * (non-Javadoc)
418    *
419    * @see
420    * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
421    * .ActionEvent)
422    */
 
423  0 toggle @Override
424    protected void moveWsUrlUp_actionPerformed(ActionEvent e)
425    {
426  0 int p = wsList.getSelectedRow();
427  0 if (p > 0)
428    {
429  0 String t = wsUrls.get(p - 1);
430  0 wsUrls.set(p - 1, wsUrls.get(p));
431  0 wsUrls.set(p, t);
432  0 updateList();
433  0 wsList.getSelectionModel().setSelectionInterval(p - 1, p - 1);
434  0 update++;
435    }
436    }
437   
 
438  0 toggle private String editUrl(String initUrl, String title)
439    {
440  0 String url = initUrl;
441  0 URL foo = null;
442  0 if (url == null)
443    {
444  0 url = "";
445    }
446  0 JTextField urltf = new JTextField(url, 40);
447  0 JPanel panel = new JPanel(new BorderLayout());
448  0 JPanel pane12 = new JPanel(new BorderLayout());
449  0 pane12.add(new JLabel(MessageManager.getString("label.url:")),
450    BorderLayout.CENTER);
451  0 pane12.add(urltf, BorderLayout.EAST);
452  0 panel.add(pane12, BorderLayout.NORTH);
453  0 boolean valid = false;
454  0 int resp = JvOptionPane.CANCEL_OPTION;
455  0 while (!valid && (resp = JvOptionPane.showInternalConfirmDialog(
456    Desktop.desktop, panel, title,
457    JvOptionPane.OK_CANCEL_OPTION)) == JvOptionPane.OK_OPTION)
458    {
459  0 try
460    {
461    // TODO: do a better job of checking that the url is a valid discovery
462    // URL for web services.
463  0 String tx = urltf.getText().trim();
464  0 while (tx.length() > 0 && tx.lastIndexOf('/') == tx.length() - 1)
465    {
466  0 tx = tx.substring(0, tx.length() - 1);
467    }
468  0 foo = new URL(tx);
469  0 valid = true;
470  0 urltf.setText(tx);
471    } catch (Exception e)
472    {
473  0 valid = false;
474  0 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
475    MessageManager.getString("label.invalid_url"));
476    }
477    }
478  0 if (valid && resp == JvOptionPane.OK_OPTION)
479    {
480  0 int validate = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
481    MessageManager.getString("info.validate_jabaws_server"),
482    MessageManager.getString("label.test_server"),
483    JvOptionPane.YES_NO_OPTION);
484   
485  0 if (validate == JvOptionPane.OK_OPTION)
486    {
487  0 if (Jws2Discoverer.testServiceUrl(foo))
488    {
489  0 return foo.toString();
490    }
491    else
492    {
493  0 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  0 if (opt == JvOptionPane.YES_OPTION)
499    {
500  0 return foo.toString();
501    }
502    else
503    {
504  0 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
505    MessageManager.getString(
506    "warn.server_didnt_pass_validation"));
507    }
508   
509    }
510    }
511    else
512    {
513    // just return the URL anyway
514  0 return foo.toString();
515    }
516    }
517  0 return initUrl;
518    }
519   
520    /*
521    * (non-Javadoc)
522    *
523    * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
524    * ActionEvent)
525    */
 
526  0 toggle @Override
527    protected void newWsUrl_actionPerformed(ActionEvent e)
528    {
529  0 String url = editUrl(null,
530    MessageManager.getString("label.add_jabaws_url"));
531  0 if (url != null)
532    {
533  0 if (!wsUrls.contains(url))
534    {
535  0 int selind = wsList.getSelectedRow();
536  0 if (selind > -1)
537    {
538  0 wsUrls.add(selind, url);
539    }
540    else
541    {
542  0 wsUrls.add(url);
543    }
544  0 update++;
545  0 updateList();
546    }
547    }
548    }
549   
550    /*
551    * (non-Javadoc)
552    *
553    * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
554    * ActionEvent)
555    */
 
556  0 toggle @Override
557    protected void refreshWs_actionPerformed(ActionEvent e)
558    {
559  0 new Thread(new Runnable()
560    {
561   
 
562  0 toggle @Override
563    public void run()
564    {
565    // force a refresh.
566  0 lastrefresh = update - 1;
567  0 updateWsMenuConfig(false);
568  0 refreshWsMenu(true);
569    }
570    }).start();
571   
572    }
573   
574    /**
575    * Refresh the web services menus - but only if there has been a change in the
576    * configuration (indicated by update!=lastrefresh)
577    *
578    * @param showProgress
579    * show progress in dialog or on desktop
580    */
 
581  0 toggle protected void refreshWsMenu(boolean showProgress)
582    {
583  0 if (showProgress)
584    {
585  0 new Thread(new Runnable()
586    {
587   
 
588  0 toggle @Override
589    public void run()
590    {
591  0 progressBar.setVisible(true);
592  0 validate();
593  0 progressBar.setIndeterminate(true);
594  0 if (lastrefresh != update)
595    {
596  0 lastrefresh = update;
597  0 Desktop.instance.startServiceDiscovery(true); // wait around for all
598    // threads to complete
599  0 updateList();
600   
601    }
602  0 progressBar.setIndeterminate(false);
603  0 progressBar.setVisible(false);
604  0 validate();
605    }
606    }).start();
607   
608    }
609    else
610    {
611  0 new Thread(new Runnable()
612    {
613   
 
614  0 toggle @Override
615    public void run()
616    {
617  0 long ct = System.currentTimeMillis();
618  0 Desktop.instance.setProgressBar(MessageManager
619    .getString("status.refreshing_web_service_menus"), ct);
620  0 if (lastrefresh != update)
621    {
622  0 lastrefresh = update;
623  0 Desktop.instance.startServiceDiscovery(true);
624  0 updateList();
625    }
626  0 Desktop.instance.setProgressBar(null, ct);
627    }
628   
629    }).start();
630    }
631    }
632   
633    /**
634    * state counters for ensuring that updates only happen if config has changed.
635    */
636    protected long update = 0;
637   
638    private long lastrefresh = 0;
639   
640    /*
641    * (non-Javadoc)
642    *
643    * @see
644    * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
645    * )
646    */
 
647  0 toggle @Override
648    protected void resetWs_actionPerformed(ActionEvent e)
649    {
650  0 Jws2Discoverer.getDiscoverer().setServiceUrls(null);
651  0 List<String> nwsUrls = Jws2Discoverer.getDiscoverer().getServiceUrls();
652  0 if (!wsUrls.equals(nwsUrls))
653    {
654  0 update++;
655    }
656  0 wsUrls = nwsUrls;
657  0 updateList();
658   
659  0 updateAndRefreshWsMenuConfig(true);
660    }
661   
 
662  0 toggle protected void ok_ActionPerformed(ActionEvent e)
663    {
664    // update everything regardless.
665  0 updateAndRefreshWsMenuConfig(false);
666    }
667   
 
668  0 toggle public void updateAndRefreshWsMenuConfig(
669    final boolean showProgressInDialog)
670    {
671  0 new Thread(new Runnable()
672    {
673   
 
674  0 toggle @Override
675    public void run()
676    {
677  0 updateWsMenuConfig(false);
678  0 refreshWsMenu(showProgressInDialog);
679    }
680    }).start();
681   
682    }
683    }