b88f27ef30ff21da054f93fbe8cd1141939af087
[jalview.git] / src / jalview / gui / WsPreferences.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, 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
35 public class WsPreferences extends GWsPreferences
36 {
37
38   public WsPreferences()
39   {
40     super();
41     initFromPreferences();
42   }
43
44   Vector<String> wsUrls, oldUrls;
45
46   private boolean needWsMenuUpdate;
47
48   private boolean oldJws1, oldJws2, oldIndexByHost, oldIndexByType,
49           oldEnfin, oldWsWarning;
50
51   private void initFromPreferences()
52   {
53
54     wsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
55     if (wsUrls != null)
56     {
57       oldUrls = new Vector<String>(wsUrls);
58     }
59     else
60     {
61       oldUrls = null;
62       wsUrls = new Vector<String>();
63     }
64     updateList();
65     enableEnfinServices.setSelected(oldEnfin = Cache.getDefault(
66             "SHOW_ENFIN_SERVICES", true));
67     enableEnfinServices.addActionListener(updateAction);
68     enableJws1Services.setSelected(oldJws1 = Cache.getDefault(
69             "SHOW_JWS1_SERVICES", true));
70     enableJws1Services.addActionListener(updateAction);
71     enableJws2Services.setSelected(oldJws2 = Cache.getDefault(
72             "SHOW_JWS2_SERVICES", true));
73     enableJws2Services.addActionListener(updateAction);
74     indexByHost.setSelected(oldIndexByHost = Cache.getDefault(
75             "WSMENU_BYHOST", true));
76     indexByHost.addActionListener(updateAction);
77     indexByType.setSelected(oldIndexByType = Cache.getDefault(
78             "WSMENU_BYTYPE", true));
79     indexByType.addActionListener(updateAction);
80     displayWsWarning.setSelected(oldWsWarning = Cache.getDefault(
81             "SHOW_WSDISCOVERY_ERRORS", true));
82   }
83
84   ActionListener updateAction = new ActionListener()
85   {
86
87     @Override
88     public void actionPerformed(ActionEvent e)
89     {
90       update++;
91     }
92
93   };
94
95   private void updateList()
96   {
97     wsList.setListData(wsUrls);
98   }
99
100   private void updateServiceList()
101   {
102     jalview.ws.jws2.Jws2Discoverer.setServiceUrls(wsUrls);
103   }
104
105   /*
106    * (non-Javadoc)
107    * 
108    * @see
109    * jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event
110    * .ActionEvent)
111    */
112   @Override
113   protected void deleteWsUrl_actionPerformed(ActionEvent e)
114   {
115     int sel = wsList.getSelectedIndex();
116     if (sel > -1)
117     {
118       wsUrls.removeElementAt(sel);
119       update++;
120       updateList();
121     }
122   }
123
124   /*
125    * (non-Javadoc)
126    * 
127    * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.
128    * ActionEvent)
129    */
130   @Override
131   protected void editWsUrl_actionPerformed(ActionEvent e)
132   {
133     int sel = wsList.getSelectedIndex();
134     if (sel > -1)
135     {
136       String url = editUrl(wsUrls.elementAt(sel), "Edit JWS2 URL");
137       if (url != null)
138       {
139         int present = wsUrls.indexOf(url);
140         if (present == -1)
141         {
142           update++;
143           wsUrls.setElementAt(url, sel);
144           updateList();
145         }
146         else
147         {
148           if (present != sel)
149           {
150             wsUrls.removeElementAt(sel);
151             updateList();
152           }
153         }
154       }
155     }
156   }
157
158   void updateWsMenuConfig(boolean old)
159   {
160     if (old)
161     {
162       if (oldUrls!=wsUrls || (wsUrls!=null && oldUrls!=null && !wsUrls.equals(oldUrls)))
163       {
164         update++;
165       }
166       wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
167     }
168     else
169     {
170
171     }
172     Cache.setProperty(
173             "SHOW_ENFIN_SERVICES",
174             Boolean.valueOf(
175                     old ? oldEnfin : enableEnfinServices.isSelected())
176                     .toString());
177     Cache.setProperty(
178             "SHOW_JWS1_SERVICES",
179             Boolean.valueOf(old ? oldJws1 : enableJws1Services.isSelected())
180                     .toString());
181     Cache.setProperty(
182             "SHOW_JWS2_SERVICES",
183             Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
184                     .toString());
185     Cache.setProperty(
186             "WSMENU_BYHOST",
187             Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
188                     .toString());
189     Cache.setProperty(
190             "WSMENU_BYTYPE",
191             Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
192                     .toString());
193     Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
194             Boolean.valueOf(old ? oldWsWarning : displayWsWarning.isSelected()).toString());
195     updateServiceList();
196   }
197
198   /*
199    * (non-Javadoc)
200    * 
201    * @see
202    * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
203    * .ActionEvent)
204    */
205   @Override
206   protected void moveWsUrlDown_actionPerformed(ActionEvent e)
207   {
208     int p = wsList.getSelectedIndex();
209     if (p > -1 && p < wsUrls.size() - 1)
210     {
211       String t = wsUrls.get(p + 1);
212       wsUrls.setElementAt(wsUrls.elementAt(p), p + 1);
213       wsUrls.setElementAt(t, p);
214       updateList();
215       wsList.setSelectedIndex(p + 1);
216     }
217   }
218
219   /*
220    * (non-Javadoc)
221    * 
222    * @see
223    * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
224    * .ActionEvent)
225    */
226   @Override
227   protected void moveWsUrlUp_actionPerformed(ActionEvent e)
228   {
229     int p = wsList.getSelectedIndex();
230     if (p > 0)
231     {
232       String t = wsUrls.get(p - 1);
233       wsUrls.setElementAt(wsUrls.elementAt(p), p - 1);
234       wsUrls.setElementAt(t, p);
235       updateList();
236       wsList.setSelectedIndex(p - 1);
237     }
238   }
239
240   private String editUrl(String initUrl, String title)
241   {
242     String url = initUrl;
243     URL foo = null;
244     if (url == null)
245     {
246       url = "";
247     }
248     JTextField urltf = new JTextField(url, 40);
249     JPanel panel = new JPanel(new BorderLayout());
250     JPanel pane12 = new JPanel(new BorderLayout());
251     pane12.add(new JLabel("URL: "), BorderLayout.CENTER);
252     pane12.add(urltf, BorderLayout.EAST);
253     panel.add(pane12, BorderLayout.NORTH);
254     boolean valid = false;
255     int resp = JOptionPane.CANCEL_OPTION;
256     while (!valid
257             && (resp = JOptionPane.showInternalConfirmDialog(
258                     Desktop.desktop, panel, title,
259                     JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
260     {
261       try
262       {
263         // TODO: do a better job of checking that the url is a valid discovery
264         // URL for web services.
265         String tx = urltf.getText().trim();
266         while (tx.length()>0 && tx.lastIndexOf('/')==tx.length()-1)
267         {
268           tx = tx.substring(0, tx.length()-1);
269         }
270         foo = new URL(tx);
271         valid = true;
272         urltf.setText(tx);
273       } catch (Exception e)
274       {
275         valid = false;
276         JOptionPane.showInternalMessageDialog(Desktop.desktop,
277                 "Invalid URL !");
278       }
279     }
280     if (valid && resp == JOptionPane.OK_OPTION)
281     {
282       int validate = JOptionPane
283               .showInternalConfirmDialog(
284                       Desktop.desktop,
285                       "Validate JabaWS Server ?\n(Look in console output for results)",
286                       "Test Server?", JOptionPane.YES_NO_OPTION);
287       if (validate == JOptionPane.OK_OPTION)
288       {
289         if (jalview.ws.jws2.Jws2Discoverer.testServiceUrl(foo))
290         {
291           return foo.toString();
292         }
293         else
294         {
295           JOptionPane
296                   .showInternalMessageDialog(
297                           Desktop.desktop,
298                           "Service did not pass validation.\nCheck the Jalview Console for more details.");
299         }
300       }
301       else
302       {
303         // just return the URL anyway
304         return foo.toString();
305       }
306     }
307     return initUrl;
308   }
309
310   /*
311    * (non-Javadoc)
312    * 
313    * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
314    * ActionEvent)
315    */
316   @Override
317   protected void newWsUrl_actionPerformed(ActionEvent e)
318   {
319     String url = editUrl(null, "Add new JWS2 URL");
320     if (url != null)
321     {
322       if (!wsUrls.contains(url))
323       {
324         int selind = wsList.getSelectedIndex();
325         if (selind > -1)
326         {
327           wsUrls.insertElementAt(url, selind);
328         }
329         else
330         {
331           wsUrls.addElement(url);
332         }
333         update++;
334         updateList();
335       }
336     }
337   }
338
339   /*
340    * (non-Javadoc)
341    * 
342    * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
343    * ActionEvent)
344    */
345   @Override
346   protected void refreshWs_actionPerformed(ActionEvent e)
347   {
348     new Thread(new Runnable()
349     {
350
351       public void run()
352       {
353         // force a refresh.
354         lastrefresh = update - 1;
355         updateWsMenuConfig(false);
356         refreshWsMenu(true);
357       }
358     }).start();
359
360   }
361
362   /**
363    * Refresh the web services menus - but only if there has been a change in the
364    * configuration (indicated by update!=lastrefresh)
365    * 
366    * @param showProgress
367    *          show progress in dialog or on desktop
368    */
369   protected void refreshWsMenu(boolean showProgress)
370   {
371     if (showProgress)
372     {
373       new Thread(new Runnable()
374       {
375
376         public void run()
377         {
378           progressBar.setVisible(true);
379           validate();
380           progressBar.setIndeterminate(true);
381           if (lastrefresh != update)
382           {
383             lastrefresh = update;
384             Desktop.instance.startServiceDiscovery(true); // wait around for all
385                                                           // threads to complete
386           }
387           progressBar.setIndeterminate(false);
388           progressBar.setVisible(false);
389           validate();
390         }
391       }).start();
392
393     }
394     else
395     {
396       new Thread(new Runnable()
397       {
398
399         public void run()
400         {
401           long ct = System.currentTimeMillis();
402           Desktop.instance.setProgressBar("Refreshing Web Service Menus",
403                   ct);
404           if (lastrefresh != update)
405           {
406             lastrefresh = update;
407             Desktop.instance.startServiceDiscovery(true);
408           }
409           Desktop.instance.setProgressBar(null, ct);
410         }
411
412       }).start();
413     }
414   }
415
416   /**
417    * state counters for ensuring that updates only happen if config has changed.
418    */
419   private long update = 0, lastrefresh = 0;
420
421   /*
422    * (non-Javadoc)
423    * 
424    * @see
425    * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
426    * )
427    */
428   @Override
429   protected void resetWs_actionPerformed(ActionEvent e)
430   {
431     jalview.ws.jws2.Jws2Discoverer.setServiceUrls(null);
432     Vector nwsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
433     if (!wsUrls.equals(nwsUrls)) {
434       update++;
435     }
436     wsUrls=nwsUrls;
437     updateList();
438     
439     updateAndRefreshWsMenuConfig(true);
440   }
441
442   protected void ok_ActionPerformed(ActionEvent e)
443   {
444     // update everything regardless.
445     updateAndRefreshWsMenuConfig(false);
446   }
447
448   public void updateAndRefreshWsMenuConfig(
449           final boolean showProgressInDialog)
450   {
451     new Thread(new Runnable()
452     {
453
454       public void run()
455       {
456         updateWsMenuConfig(false);
457         refreshWsMenu(showProgressInDialog);
458       }
459     }).start();
460
461   }
462 }