Jalview 2.6 source licence
[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   private boolean needWsMenuUpdate;
45   private boolean oldJws1,oldJws2,oldIndexByHost,oldIndexByType,oldEnfin;
46   private void initFromPreferences()
47   {
48
49     wsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls();
50     if (wsUrls!=null) {
51       oldUrls = new Vector<String>(wsUrls);
52     } else {
53       oldUrls=null;
54       wsUrls=new Vector<String>();
55     }
56     updateList();
57     enableEnfinServices.setSelected(oldEnfin=Cache.getDefault(
58             "SHOW_ENFIN_SERVICES", true));
59     enableJws1Services.setSelected(oldJws1=Cache.getDefault("SHOW_JWS1_SERVICES",
60             true));
61     enableJws2Services.setSelected(oldJws2=Cache.getDefault("SHOW_JWS2_SERVICES",
62             true));
63     indexByHost.setSelected(oldIndexByHost=Cache.getDefault("WSMENU_BYHOST", true));
64     indexByType.setSelected(oldIndexByType=Cache.getDefault("WSMENU_BYTYPE", true));
65   }
66   private void updateList() {
67     wsList.setListData(wsUrls);
68   }
69   private void updateServiceList() {
70     jalview.ws.jws2.Jws2Discoverer.setServiceUrls(wsUrls);
71   }
72   /* (non-Javadoc)
73    * @see jalview.jbgui.GWsPreferences#deleteWsUrl_actionPerformed(java.awt.event.ActionEvent)
74    */
75   @Override
76   protected void deleteWsUrl_actionPerformed(ActionEvent e)
77   {
78     int sel = wsList.getSelectedIndex();
79     if (sel>-1)
80     {
81       wsUrls.removeElementAt(sel);
82       updateList();
83     }
84   }
85
86   /* (non-Javadoc)
87    * @see jalview.jbgui.GWsPreferences#editWsUrl_actionPerformed(java.awt.event.ActionEvent)
88    */
89   @Override
90   protected void editWsUrl_actionPerformed(ActionEvent e)
91   {
92     int sel = wsList.getSelectedIndex();
93     if (sel>-1)
94     {
95       String url=editUrl(wsUrls.elementAt(sel),"Edit JWS2 URL");
96       if (url!=null)
97       {
98         int present = wsUrls.indexOf(url);
99         if (present==-1)
100         {
101           wsUrls.setElementAt(url,sel);
102           updateList();
103         } else {
104           if (present!=sel)
105           {
106             wsUrls.removeElementAt(sel);
107             updateList();
108           }
109         }
110       }
111     }
112   }
113   
114   void updateWsMenuConfig(boolean old) {
115     if (old) {
116       wsUrls = (oldUrls==null) ? null : new Vector(oldUrls);
117     } else {
118       
119     }
120     Cache.setProperty("SHOW_ENFIN_SERVICES", Boolean.valueOf(old ? oldEnfin : enableEnfinServices.isSelected()).toString());
121     Cache.setProperty("SHOW_JWS1_SERVICES", Boolean.valueOf(old ? oldJws1 : enableJws1Services.isSelected()).toString());
122     Cache.setProperty("SHOW_JWS2_SERVICES", Boolean.valueOf(old ? oldJws2 : enableJws2Services.isSelected()).toString());
123     Cache.setProperty("WSMENU_BYHOST", Boolean.valueOf(old ? oldIndexByHost : indexByHost.isSelected()).toString());
124     Cache.setProperty("WSMENU_BYTYPE", Boolean.valueOf(old ? oldIndexByType : indexByType.isSelected()).toString());
125     updateServiceList();
126   }
127   
128   /* (non-Javadoc)
129    * @see jalview.jbgui.GWsPreferences#moveWsUrlDown_actionPerformed(java.awt.event.ActionEvent)
130    */
131   @Override
132   protected void moveWsUrlDown_actionPerformed(ActionEvent e)
133   {
134     int p=wsList.getSelectedIndex();
135     if (p>-1 && p<wsUrls.size()-1)
136     {
137       String t = wsUrls.get(p+1);
138       wsUrls.setElementAt(wsUrls.elementAt(p), p+1);
139       wsUrls.setElementAt(t, p);
140       updateList();
141       wsList.setSelectedIndex(p+1);
142     }
143   }
144
145   /* (non-Javadoc)
146    * @see jalview.jbgui.GWsPreferences#moveWsUrlUp_actionPerformed(java.awt.event.ActionEvent)
147    */
148   @Override
149   protected void moveWsUrlUp_actionPerformed(ActionEvent e)
150   {
151     int p=wsList.getSelectedIndex();
152     if (p>0)
153     {
154       String t = wsUrls.get(p-1);
155       wsUrls.setElementAt(wsUrls.elementAt(p), p-1);
156       wsUrls.setElementAt(t, p);
157       updateList();
158       wsList.setSelectedIndex(p-1);
159     }
160   }
161
162   private String editUrl(String initUrl, String title)
163   {
164     String url=initUrl;
165     URL foo=null;
166     if (url==null)
167     {
168       url = "";
169     }
170     JTextField urltf = new JTextField(url, 40);
171     JPanel panel = new JPanel(new BorderLayout());
172     JPanel pane12 = new JPanel(new BorderLayout());
173     pane12.add(new JLabel("URL: "), BorderLayout.CENTER);
174     pane12.add(urltf, BorderLayout.EAST);
175     panel.add(pane12, BorderLayout.NORTH);
176     boolean valid=false;
177     int resp=JOptionPane.CANCEL_OPTION;
178     while (!valid && (resp=JOptionPane.showInternalConfirmDialog(Desktop.desktop, panel, title, JOptionPane.OK_CANCEL_OPTION))==JOptionPane.OK_OPTION)
179     {
180       try {
181         // TODO: do a better job of checking that the url is a valid discovery URL for web services.
182         foo = new URL(urltf.getText().trim());
183         valid = true;
184       } catch (Exception e)
185       {
186         valid = false;
187         JOptionPane.showInternalMessageDialog(Desktop.desktop, "Invalid URL !");
188       }
189     }
190     if (valid && resp==JOptionPane.OK_OPTION)
191     {
192       int validate = JOptionPane.showInternalConfirmDialog(Desktop.desktop, "Validate JabaWS Server ?\n(Look in console output for results)", "Test Server?", JOptionPane.YES_NO_OPTION);
193       if (validate == JOptionPane.OK_OPTION)
194       {
195         if (jalview.ws.jws2.Jws2Discoverer.testServiceUrl(foo)) 
196         {
197           return foo.toString();
198         } else {
199           JOptionPane.showInternalMessageDialog(Desktop.desktop, "Service did not pass validation.\nCheck the Jalview Console for more details.");
200         }
201       } else {
202         // just return the URL anyway
203         return foo.toString();
204       }
205     }
206     return initUrl; 
207   }
208   /* (non-Javadoc)
209    * @see jalview.jbgui.GWsPreferences#newWsUrl_actionPerformed(java.awt.event.ActionEvent)
210    */
211   @Override
212   protected void newWsUrl_actionPerformed(ActionEvent e)
213   {
214     String url=editUrl(null,"Add new JWS2 URL");
215     if (url!=null)
216     {
217       if (!wsUrls.contains(url))
218       {
219         int selind = wsList.getSelectedIndex();
220         if (selind>-1)
221         {
222           wsUrls.insertElementAt(url, selind);
223         } else {
224           wsUrls.addElement(url);
225         }
226         updateList();
227       }
228     }
229   }
230
231   /* (non-Javadoc)
232    * @see jalview.jbgui.GWsPreferences#refreshWs_actionPerformed(java.awt.event.ActionEvent)
233    */
234   @Override
235   protected void refreshWs_actionPerformed(ActionEvent e)
236   {
237     new Thread(new Runnable() {
238
239       public void run()
240       {
241         updateWsMenuConfig(false);
242         refreshWsMenu(true);
243       }      
244     }).start();
245     
246   }
247   /**
248    * 
249    * @param showProgress show progress in dialog or on desktop
250    */
251   protected void refreshWsMenu(boolean showProgress)
252   {
253     if (showProgress)
254     {
255       new Thread(new Runnable() {
256
257         public void run()
258         {
259           progressBar.setVisible(true);
260           validate();
261           progressBar.setIndeterminate(true);
262           Desktop.instance.startServiceDiscovery(true); // wait around for all threads to complete
263           progressBar.setIndeterminate(false);
264           progressBar.setVisible(false);
265           validate();
266         }      
267       }).start();
268       
269     } else {
270       new Thread(new Runnable() {
271
272         public void run()
273         {
274           long ct = System.currentTimeMillis();
275           Desktop.instance.setProgressBar("Refreshing Web Service Menus", ct);
276           Desktop.instance.startServiceDiscovery(true);
277           Desktop.instance.setProgressBar("Refreshing Web Service Menus", ct);
278         }
279         
280       }).start();
281     }
282   }
283
284   /* (non-Javadoc)
285    * @see jalview.jbgui.GWsPreferences#resetWs_actionPerformed(java.awt.event.ActionEvent)
286    */
287   @Override
288   protected void resetWs_actionPerformed(ActionEvent e)
289   {
290     jalview.ws.jws2.Jws2Discoverer.setServiceUrls(null);
291     wsUrls = jalview.ws.jws2.Jws2Discoverer.getServiceUrls(); 
292     updateList();
293   }
294   protected void ok_ActionPerformed(ActionEvent e) {
295     // update everything regardless.
296     updateAndRefreshWsMenuConfig(false);
297   }
298   public void updateAndRefreshWsMenuConfig(final boolean showProgressInDialog)
299   {
300     new Thread(new Runnable() {
301
302       public void run()
303       {
304         updateWsMenuConfig(false);
305         refreshWsMenu(showProgressInDialog);
306       }      
307     }).start();
308     
309   }
310 }