update author list in license for (JAL-826)
[jalview.git] / src / jalview / gui / WsPreferences.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.awt.BorderLayout;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.net.URL;
24 import java.util.Vector;
25
26 import javax.swing.JCheckBox;
27 import javax.swing.JLabel;
28 import javax.swing.JOptionPane;
29 import javax.swing.JPanel;
30 import javax.swing.JTextField;
31
32 import jalview.bin.Cache;
33 import jalview.jbgui.GWsPreferences;
34 import jalview.ws.rest.RestServiceDescription;
35
36 public class WsPreferences extends GWsPreferences
37 {
38
39   public WsPreferences()
40   {
41     super();
42     initFromPreferences();
43   }
44
45   Vector<String> wsUrls, oldUrls,rsbsUrls,oldRsbsUrls;
46
47   private boolean needWsMenuUpdate;
48
49   private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
50           oldEnfin, oldWsWarning;
51
52   private void initFromPreferences()
53   {
54
55     wsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
56     if (wsUrls != null)
57     {
58       oldUrls = new Vector<String>(wsUrls);
59     }
60     else
61     {
62       oldUrls = null;
63       wsUrls = new Vector<String>();
64     }
65     updateList();
66     rsbsUrls = jalview.ws.rest.RestClient.getRsbsDescriptions();
67     if (rsbsUrls != null)
68     {
69       oldRsbsUrls = new Vector<String>(rsbsUrls);
70     }
71     else
72     {
73       oldRsbsUrls = null;
74       rsbsUrls = new Vector<String>();
75     }
76     updateRsbsList();
77     enableEnfinServices.setSelected(oldEnfin = Cache.getDefault(
78             "SHOW_ENFIN_SERVICES", true));
79     enableEnfinServices.addActionListener(updateAction);
80     enableJws2Services.setSelected(oldJws2 = Cache.getDefault(
81             "SHOW_JWS2_SERVICES", true));
82     enableJws2Services.addActionListener(updateAction);
83     indexByHost.setSelected(oldIndexByHost = Cache.getDefault(
84             "WSMENU_BYHOST", false));
85     indexByHost.addActionListener(updateAction);
86     indexByType.setSelected(oldIndexByType = Cache.getDefault(
87             "WSMENU_BYTYPE", false));
88     indexByType.addActionListener(updateAction);
89     displayWsWarning.setSelected(oldWsWarning = Cache.getDefault(
90             "SHOW_WSDISCOVERY_ERRORS", true));
91   }
92
93   ActionListener updateAction = new ActionListener()
94   {
95
96     @Override
97     public void actionPerformed(ActionEvent e)
98     {
99       update++;
100     }
101
102   };
103
104   private void updateList()
105   {
106     wsList.setListData(wsUrls);
107   }
108
109   private void updateRsbsList()
110   {
111     sbrsList.setListData(rsbsUrls);
112   }
113
114   private void updateServiceList()
115   {
116     jalview.ws.jws2.Jws2Discoverer.setServiceUrls(wsUrls);
117   }
118
119   private void updateRsbsServiceList()
120   {
121     jalview.ws.rest.RestClient.setRsbsServices(rsbsUrls);
122   }
123
124   /*
125    * (non-Javadoc)
126    * 
127    * @see
128    * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
129    * .ActionEvent)
130    */
131   @Override
132   protected void deleteWsUrl_actionPerformed(ActionEvent e)
133   {
134     int sel = wsList.getSelectedIndex();
135     if (sel > -1)
136     {
137       wsUrls.removeElementAt(sel);
138       update++;
139       updateList();
140     }
141   }
142
143   /*
144    * (non-Javadoc)
145    * 
146    * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
147    * ActionEvent)
148    */
149   @Override
150   protected void editWsUrl_actionPerformed(ActionEvent e)
151   {
152     int sel = wsList.getSelectedIndex();
153     if (sel > -1)
154     {
155       String url = editUrl(wsUrls.elementAt(sel), "Edit JABAWS URL");
156       if (url != null)
157       {
158         int present = wsUrls.indexOf(url);
159         if (present == -1)
160         {
161           update++;
162           wsUrls.setElementAt(url, sel);
163           updateList();
164         }
165         else
166         {
167           if (present != sel)
168           {
169             wsUrls.removeElementAt(sel);
170             updateList();
171           }
172         }
173       }
174     }
175   }
176   @Override
177   protected void newSbrsUrl_actionPerformed(ActionEvent e)
178   {
179     RestServiceEditorPane rse = new RestServiceEditorPane();
180     rse.showDialog("Add a new Simple Bioinformatics Rest Service");
181     String rservice = rse.getEditedRestService();
182     if (rservice!=null && !rsbsUrls.contains(rservice))
183     {
184       rsbsUrls.add(rservice);
185       update++;
186       updateRsbsList();
187     }
188   }
189   @Override
190   protected void editSbrsUrl_actionPerformed(ActionEvent e)
191   {
192     int sel = sbrsList.getSelectedIndex();
193     if (sel > -1)
194     {
195       RestServiceEditorPane rse = new RestServiceEditorPane(new RestServiceDescription(rsbsUrls.elementAt(sel)));
196       rse.showDialog("Edit Simple Bioinformatics Rest Service entry");
197       String rservice = rse.getEditedRestService();
198       if (rservice!=null)
199       {
200         int present = rsbsUrls.indexOf(rservice);
201         if (present==-1) {
202           update++;
203           rsbsUrls.setElementAt(rservice,sel);
204           updateRsbsList();
205         } else {
206           if (present!=sel) {
207             rsbsUrls.removeElementAt(sel);
208             update++;
209             updateRsbsList();
210           }
211         }
212       }
213     }
214   }
215   
216   void updateWsMenuConfig(boolean old)
217   {
218     if (old)
219     {
220       if (oldUrls!=wsUrls || (wsUrls!=null && oldUrls!=null && !wsUrls.equals(oldUrls)))
221       {
222         update++;
223       }
224       wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
225       if (oldRsbsUrls!=rsbsUrls || (rsbsUrls!=null && oldRsbsUrls!=null && !oldRsbsUrls.equals(rsbsUrls)))
226       {
227         update++;
228       }
229       oldRsbsUrls = (oldRsbsUrls == null) ? null : new Vector(oldRsbsUrls);
230     }
231     else
232     {
233
234     }
235     Cache.setProperty(
236             "SHOW_ENFIN_SERVICES",
237             Boolean.valueOf(
238                     old ? oldEnfin : enableEnfinServices.isSelected())
239                     .toString());
240     Cache.setProperty(
241             "SHOW_JWS2_SERVICES",
242             Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
243                     .toString());
244     Cache.setProperty(
245             "WSMENU_BYHOST",
246             Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
247                     .toString());
248     Cache.setProperty(
249             "WSMENU_BYTYPE",
250             Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
251                     .toString());
252     
253     Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
254             Boolean.valueOf(old ? oldWsWarning : displayWsWarning.isSelected()).toString());
255     updateServiceList();
256     updateRsbsServiceList();
257   }
258
259   /*
260    * (non-Javadoc)
261    * 
262    * @see
263    * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
264    * .ActionEvent)
265    */
266   @Override
267   protected void moveWsUrlDown_actionPerformed(ActionEvent e)
268   {
269     int p = wsList.getSelectedIndex();
270     if (p > -1 && p < wsUrls.size() - 1)
271     {
272       String t = wsUrls.get(p + 1);
273       wsUrls.setElementAt(wsUrls.elementAt(p), p + 1);
274       wsUrls.setElementAt(t, p);
275       updateList();
276       wsList.setSelectedIndex(p + 1);
277       update++;
278     }
279   }
280
281   /*
282    * (non-Javadoc)
283    * 
284    * @see
285    * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
286    * .ActionEvent)
287    */
288   @Override
289   protected void moveWsUrlUp_actionPerformed(ActionEvent e)
290   {
291     int p = wsList.getSelectedIndex();
292     if (p > 0)
293     {
294       String t = wsUrls.get(p - 1);
295       wsUrls.setElementAt(wsUrls.elementAt(p), p - 1);
296       wsUrls.setElementAt(t, p);
297       updateList();
298       wsList.setSelectedIndex(p - 1);
299       update++;
300     }
301   }
302
303   private String editUrl(String initUrl, String title)
304   {
305     String url = initUrl;
306     URL foo = null;
307     if (url == null)
308     {
309       url = "";
310     }
311     JTextField urltf = new JTextField(url, 40);
312     JPanel panel = new JPanel(new BorderLayout());
313     JPanel pane12 = new JPanel(new BorderLayout());
314     pane12.add(new JLabel("URL: "), BorderLayout.CENTER);
315     pane12.add(urltf, BorderLayout.EAST);
316     panel.add(pane12, BorderLayout.NORTH);
317     boolean valid = false;
318     int resp = JOptionPane.CANCEL_OPTION;
319     while (!valid
320             && (resp = JOptionPane.showInternalConfirmDialog(
321                     Desktop.desktop, panel, title,
322                     JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
323     {
324       try
325       {
326         // TODO: do a better job of checking that the url is a valid discovery
327         // URL for web services.
328         String tx = urltf.getText().trim();
329         while (tx.length()>0 && tx.lastIndexOf('/')==tx.length()-1)
330         {
331           tx = tx.substring(0, tx.length()-1);
332         }
333         foo = new URL(tx);
334         valid = true;
335         urltf.setText(tx);
336       } catch (Exception e)
337       {
338         valid = false;
339         JOptionPane.showInternalMessageDialog(Desktop.desktop,
340                 "Invalid URL !");
341       }
342     }
343     if (valid && resp == JOptionPane.OK_OPTION)
344     {
345       int validate = JOptionPane
346               .showInternalConfirmDialog(
347                       Desktop.desktop,
348                       "Validate JabaWS Server ?\n(Look in console output for results)",
349                       "Test Server?", JOptionPane.YES_NO_OPTION);
350       if (validate == JOptionPane.OK_OPTION)
351       {
352         if (jalview.ws.jws2.Jws2Discoverer.testServiceUrl(foo))
353         {
354           return foo.toString();
355         }
356         else
357         {
358           JOptionPane
359                   .showInternalMessageDialog(
360                           Desktop.desktop,
361                           "Service did not pass validation.\nCheck the Jalview Console for more details.");
362         }
363       }
364       else
365       {
366         // just return the URL anyway
367         return foo.toString();
368       }
369     }
370     return initUrl;
371   }
372
373   /*
374    * (non-Javadoc)
375    * 
376    * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
377    * ActionEvent)
378    */
379   @Override
380   protected void newWsUrl_actionPerformed(ActionEvent e)
381   {
382     String url = editUrl(null, "Add new JABAWS URL");
383     if (url != null)
384     {
385       if (!wsUrls.contains(url))
386       {
387         int selind = wsList.getSelectedIndex();
388         if (selind > -1)
389         {
390           wsUrls.insertElementAt(url, selind);
391         }
392         else
393         {
394           wsUrls.addElement(url);
395         }
396         update++;
397         updateList();
398       }
399     }
400   }
401
402   /*
403    * (non-Javadoc)
404    * 
405    * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
406    * ActionEvent)
407    */
408   @Override
409   protected void refreshWs_actionPerformed(ActionEvent e)
410   {
411     new Thread(new Runnable()
412     {
413
414       public void run()
415       {
416         // force a refresh.
417         lastrefresh = update - 1;
418         updateWsMenuConfig(false);
419         refreshWsMenu(true);
420       }
421     }).start();
422
423   }
424
425   /**
426    * Refresh the web services menus - but only if there has been a change in the
427    * configuration (indicated by update!=lastrefresh)
428    * 
429    * @param showProgress
430    *          show progress in dialog or on desktop
431    */
432   protected void refreshWsMenu(boolean showProgress)
433   {
434     if (showProgress)
435     {
436       new Thread(new Runnable()
437       {
438
439         public void run()
440         {
441           progressBar.setVisible(true);
442           validate();
443           progressBar.setIndeterminate(true);
444           if (lastrefresh != update)
445           {
446             lastrefresh = update;
447             Desktop.instance.startServiceDiscovery(true); // wait around for all
448                                                           // threads to complete
449           }
450           progressBar.setIndeterminate(false);
451           progressBar.setVisible(false);
452           validate();
453         }
454       }).start();
455
456     }
457     else
458     {
459       new Thread(new Runnable()
460       {
461
462         public void run()
463         {
464           long ct = System.currentTimeMillis();
465           Desktop.instance.setProgressBar("Refreshing Web Service Menus",
466                   ct);
467           if (lastrefresh != update)
468           {
469             lastrefresh = update;
470             Desktop.instance.startServiceDiscovery(true);
471           }
472           Desktop.instance.setProgressBar(null, ct);
473         }
474
475       }).start();
476     }
477   }
478
479   /**
480    * state counters for ensuring that updates only happen if config has changed.
481    */
482   private long update = 0, lastrefresh = 0;
483
484   /*
485    * (non-Javadoc)
486    * 
487    * @see
488    * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
489    * )
490    */
491   @Override
492   protected void resetWs_actionPerformed(ActionEvent e)
493   {
494     jalview.ws.jws2.Jws2Discoverer.setServiceUrls(null);
495     Vector nwsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
496     if (!wsUrls.equals(nwsUrls)) {
497       update++;
498     }
499     wsUrls=nwsUrls;
500     updateList();
501     
502     updateAndRefreshWsMenuConfig(true);
503   }
504
505   protected void ok_ActionPerformed(ActionEvent e)
506   {
507     // update everything regardless.
508     updateAndRefreshWsMenuConfig(false);
509   }
510
511   public void updateAndRefreshWsMenuConfig(
512           final boolean showProgressInDialog)
513   {
514     new Thread(new Runnable()
515     {
516
517       public void run()
518       {
519         updateWsMenuConfig(false);
520         refreshWsMenu(showProgressInDialog);
521       }
522     }).start();
523
524   }
525 }