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