updated version to 2.6.1
[jalview.git] / src / jalview / gui / WsPreferences.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6.1)
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       wsUrls = (oldUrls == null) ? null : new Vector(oldUrls);
163     }
164     else
165     {
166
167     }
168     Cache.setProperty(
169             "SHOW_ENFIN_SERVICES",
170             Boolean.valueOf(
171                     old ? oldEnfin : enableEnfinServices.isSelected())
172                     .toString());
173     Cache.setProperty(
174             "SHOW_JWS1_SERVICES",
175             Boolean.valueOf(old ? oldJws1 : enableJws1Services.isSelected())
176                     .toString());
177     Cache.setProperty(
178             "SHOW_JWS2_SERVICES",
179             Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected())
180                     .toString());
181     Cache.setProperty(
182             "WSMENU_BYHOST",
183             Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected())
184                     .toString());
185     Cache.setProperty(
186             "WSMENU_BYTYPE",
187             Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected())
188                     .toString());
189     Cache.setProperty("SHOW_WSDISCOVERY_ERRORS",
190             Boolean.valueOf(old ? oldWsWarning : displayWsWarning.isSelected()).toString());
191     updateServiceList();
192   }
193
194   /*
195    * (non-Javadoc)
196    * 
197    * @see
198    * jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event
199    * .ActionEvent)
200    */
201   @Override
202   protected void moveWsUrlDown_actionPerformed(ActionEvent e)
203   {
204     int p = wsList.getSelectedIndex();
205     if (p > -1 && p < wsUrls.size() - 1)
206     {
207       String t = wsUrls.get(p + 1);
208       wsUrls.setElementAt(wsUrls.elementAt(p), p + 1);
209       wsUrls.setElementAt(t, p);
210       updateList();
211       wsList.setSelectedIndex(p + 1);
212     }
213   }
214
215   /*
216    * (non-Javadoc)
217    * 
218    * @see
219    * jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event
220    * .ActionEvent)
221    */
222   @Override
223   protected void moveWsUrlUp_actionPerformed(ActionEvent e)
224   {
225     int p = wsList.getSelectedIndex();
226     if (p > 0)
227     {
228       String t = wsUrls.get(p - 1);
229       wsUrls.setElementAt(wsUrls.elementAt(p), p - 1);
230       wsUrls.setElementAt(t, p);
231       updateList();
232       wsList.setSelectedIndex(p - 1);
233     }
234   }
235
236   private String editUrl(String initUrl, String title)
237   {
238     String url = initUrl;
239     URL foo = null;
240     if (url == null)
241     {
242       url = "";
243     }
244     JTextField urltf = new JTextField(url, 40);
245     JPanel panel = new JPanel(new BorderLayout());
246     JPanel pane12 = new JPanel(new BorderLayout());
247     pane12.add(new JLabel("URL: "), BorderLayout.CENTER);
248     pane12.add(urltf, BorderLayout.EAST);
249     panel.add(pane12, BorderLayout.NORTH);
250     boolean valid = false;
251     int resp = JOptionPane.CANCEL_OPTION;
252     while (!valid
253             && (resp = JOptionPane.showInternalConfirmDialog(
254                     Desktop.desktop, panel, title,
255                     JOptionPane.OK_CANCEL_OPTION)) == JOptionPane.OK_OPTION)
256     {
257       try
258       {
259         // TODO: do a better job of checking that the url is a valid discovery
260         // URL for web services.
261         foo = new URL(urltf.getText().trim());
262         valid = true;
263       } catch (Exception e)
264       {
265         valid = false;
266         JOptionPane.showInternalMessageDialog(Desktop.desktop,
267                 "Invalid URL !");
268       }
269     }
270     if (valid && resp == JOptionPane.OK_OPTION)
271     {
272       int validate = JOptionPane
273               .showInternalConfirmDialog(
274                       Desktop.desktop,
275                       "Validate JabaWS Server ?\n(Look in console output for results)",
276                       "Test Server?", JOptionPane.YES_NO_OPTION);
277       if (validate == JOptionPane.OK_OPTION)
278       {
279         if (jalview.ws.jws2.Jws2Discoverer.testServiceUrl(foo))
280         {
281           return foo.toString();
282         }
283         else
284         {
285           JOptionPane
286                   .showInternalMessageDialog(
287                           Desktop.desktop,
288                           "Service did not pass validation.\nCheck the Jalview Console for more details.");
289         }
290       }
291       else
292       {
293         // just return the URL anyway
294         return foo.toString();
295       }
296     }
297     return initUrl;
298   }
299
300   /*
301    * (non-Javadoc)
302    * 
303    * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.
304    * ActionEvent)
305    */
306   @Override
307   protected void newWsUrl_actionPerformed(ActionEvent e)
308   {
309     String url = editUrl(null, "Add new JWS2 URL");
310     if (url != null)
311     {
312       if (!wsUrls.contains(url))
313       {
314         int selind = wsList.getSelectedIndex();
315         if (selind > -1)
316         {
317           wsUrls.insertElementAt(url, selind);
318         }
319         else
320         {
321           wsUrls.addElement(url);
322         }
323         update++;
324         updateList();
325       }
326     }
327   }
328
329   /*
330    * (non-Javadoc)
331    * 
332    * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.
333    * ActionEvent)
334    */
335   @Override
336   protected void refreshWs_actionPerformed(ActionEvent e)
337   {
338     new Thread(new Runnable()
339     {
340
341       public void run()
342       {
343         // force a refresh.
344         lastrefresh = update - 1;
345         updateWsMenuConfig(false);
346         refreshWsMenu(true);
347       }
348     }).start();
349
350   }
351
352   /**
353    * Refresh the web services menus - but only if there has been a change in the
354    * configuration (indicated by update!=lastrefresh)
355    * 
356    * @param showProgress
357    *          show progress in dialog or on desktop
358    */
359   protected void refreshWsMenu(boolean showProgress)
360   {
361     if (showProgress)
362     {
363       new Thread(new Runnable()
364       {
365
366         public void run()
367         {
368           progressBar.setVisible(true);
369           validate();
370           progressBar.setIndeterminate(true);
371           if (lastrefresh != update)
372           {
373             lastrefresh = update;
374             Desktop.instance.startServiceDiscovery(true); // wait around for all
375                                                           // threads to complete
376           }
377           progressBar.setIndeterminate(false);
378           progressBar.setVisible(false);
379           validate();
380         }
381       }).start();
382
383     }
384     else
385     {
386       new Thread(new Runnable()
387       {
388
389         public void run()
390         {
391           long ct = System.currentTimeMillis();
392           Desktop.instance.setProgressBar("Refreshing Web Service Menus",
393                   ct);
394           if (lastrefresh != update)
395           {
396             lastrefresh = update;
397             Desktop.instance.startServiceDiscovery(true);
398           }
399           Desktop.instance.setProgressBar(null, ct);
400         }
401
402       }).start();
403     }
404   }
405
406   /**
407    * state counters for ensuring that updates only happen if config has changed.
408    */
409   private long update = 0, lastrefresh = 0;
410
411   /*
412    * (non-Javadoc)
413    * 
414    * @see
415    * jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent
416    * )
417    */
418   @Override
419   protected void resetWs_actionPerformed(ActionEvent e)
420   {
421     jalview.ws.jws2.Jws2Discoverer.setServiceUrls(null);
422     wsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
423     updateList();
424   }
425
426   protected void ok_ActionPerformed(ActionEvent e)
427   {
428     // update everything regardless.
429     updateAndRefreshWsMenuConfig(false);
430   }
431
432   public void updateAndRefreshWsMenuConfig(
433           final boolean showProgressInDialog)
434   {
435     new Thread(new Runnable()
436     {
437
438       public void run()
439       {
440         updateWsMenuConfig(false);
441         refreshWsMenu(showProgressInDialog);
442       }
443     }).start();
444
445   }
446 }