Proxy server, default browser, multiple links added
[jalview.git] / src / jalview / gui / Preferences.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import jalview.bin.*;\r
22 \r
23 import jalview.io.*;\r
24 \r
25 import jalview.jbgui.*;\r
26 \r
27 import jalview.schemes.*;\r
28 \r
29 import java.awt.*;\r
30 import java.awt.event.*;\r
31 \r
32 import javax.swing.*;\r
33 \r
34 import java.util.*;\r
35 \r
36 import jalview.bin.Cache;\r
37 \r
38 \r
39 /**\r
40  * DOCUMENT ME!\r
41  *\r
42  * @author $author$\r
43  * @version $Revision$\r
44  */\r
45 public class Preferences extends GPreferences\r
46 {\r
47     /** Holds name and link separated with | character. Sequence ID must be $SEQUENCE_ID$ */\r
48    public static Vector sequenceURLLinks = new Vector();\r
49     Vector nameLinks, urlLinks;\r
50 \r
51     JInternalFrame frame;\r
52 \r
53 \r
54     /**\r
55      * Creates a new Preferences object.\r
56      */\r
57     public Preferences()\r
58     {\r
59 \r
60         frame = new JInternalFrame();\r
61         frame.setContentPane(this);\r
62         Desktop.addInternalFrame(frame, "Preferences", 480, 390);\r
63         frame.setMinimumSize(new Dimension(480,390));\r
64 \r
65         fullID.setSelected(      Cache.getDefault("SHOW_FULL_ID", true));\r
66         fullScreen.setSelected(  Cache.getDefault("SHOW_FULLSCREEN", false));\r
67         annotations.setSelected( Cache.getDefault("SHOW_FULL_ID", true));\r
68         conservation.setEnabled( Cache.getDefault("SHOW_ANNOTATIONS", true));\r
69 \r
70         quality.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));\r
71         identity.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));\r
72 \r
73         conservation.setSelected(Cache.getDefault("SHOW_CONSERVATION", true));\r
74         quality.setSelected(Cache.getDefault("SHOW_QUALITY", true));\r
75         identity.setSelected(Cache.getDefault("SHOW_IDENTITY", true));\r
76 \r
77         for (int i = 0; i < 12; i++)\r
78         {\r
79             colour.addItem(ColourSchemeProperty.getColourName(i));\r
80         }\r
81 \r
82         String string = Cache.getProperty("DEFAULT_COLOUR");\r
83 \r
84         if (string != null)\r
85         {\r
86             colour.setSelectedItem(string);\r
87         }\r
88         else\r
89         {\r
90             colour.setSelectedIndex(11);\r
91         }\r
92 \r
93         String[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()\r
94                                                      .getAvailableFontFamilyNames();\r
95 \r
96         for (int i = 0; i < fonts.length; i++)\r
97         {\r
98             fontNameCB.addItem(fonts[i]);\r
99         }\r
100 \r
101         for (int i = 1; i < 31; i++)\r
102         {\r
103             fontSizeCB.addItem(i + "");\r
104         }\r
105 \r
106         fontStyleCB.addItem("plain");\r
107         fontStyleCB.addItem("bold");\r
108         fontStyleCB.addItem("italic");\r
109 \r
110         fontNameCB.setSelectedItem(Cache.getDefault("FONT_NAME", "SansSerif"));\r
111         fontSizeCB.setSelectedItem(Cache.getDefault("FONT_SIZE", "10"));\r
112         fontStyleCB.setSelectedItem(Cache.getDefault("FONT_STYLE", Font.PLAIN + "") );\r
113 \r
114         gapSymbolCB.addItem("-");\r
115         gapSymbolCB.addItem(".");\r
116 \r
117         gapSymbolCB.setSelectedItem( Cache.getDefault("GAP_SYMBOL", "-"));\r
118 \r
119         startupCheckbox.setSelected( Cache.getDefault("SHOW_STARTUP_FILE", true) );\r
120         startupFileTextfield.setText(Cache.getDefault("STARTUP_FILE",\r
121          "http://www.jalview.org/examples/exampleFile.jar"));\r
122 \r
123   /****************************************************\r
124    * Set up Connections\r
125    */\r
126 \r
127   string = Cache.getDefault("SEQUENCE_LINKS",\r
128                             "SRS|http://srs.ebi.ac.uk/srs7bin/cgi-bin/wgetz?-e+[uniprot-all:$SEQUENCE_ID$]+-vn+2");\r
129       sequenceURLLinks = new Vector();\r
130 \r
131         try{\r
132           StringTokenizer st = new StringTokenizer(string, "|");\r
133           while (st.hasMoreElements())\r
134           {\r
135             sequenceURLLinks.addElement(st.nextToken() + "|" + st.nextToken());\r
136           }\r
137         }catch(Exception ex)\r
138         {System.out.println(ex+"\nError parsing sequence links");}\r
139 \r
140         nameLinks = new Vector();\r
141         urlLinks = new Vector();\r
142         for(int i=0; i<sequenceURLLinks.size(); i++)\r
143         {\r
144           String link = sequenceURLLinks.elementAt(i).toString();\r
145           nameLinks.addElement(link.substring(0, link.indexOf("|")) );\r
146           urlLinks.addElement(link.substring(link.indexOf("|")+1));\r
147         }\r
148 \r
149         updateLinkData();\r
150 \r
151         useProxy.setSelected( Cache.getDefault("USE_PROXY", false));\r
152         proxyServerTB.setEnabled(useProxy.isSelected());\r
153         proxyPortTB.setEnabled(useProxy.isSelected());\r
154         proxyServerTB.setText( Cache.getDefault("PROXY_SERVER", ""));\r
155         proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));\r
156 \r
157         defaultBrowser.setText( Cache.getDefault("DEFAULT_BROWSER",""));\r
158     try\r
159     {\r
160       jbInit();\r
161     }\r
162     catch (Exception ex)\r
163     {\r
164       ex.printStackTrace();\r
165     }\r
166 \r
167   }\r
168 \r
169 \r
170     /**\r
171      * DOCUMENT ME!\r
172      *\r
173      * @param e DOCUMENT ME!\r
174      */\r
175     public void ok_actionPerformed(ActionEvent e)\r
176     {\r
177 \r
178       Cache.applicationProperties.setProperty("SHOW_FULL_ID", Boolean.toString(fullID.isSelected()));\r
179       Cache.applicationProperties.setProperty("SHOW_FULLSCREEN", Boolean.toString(fullScreen.isSelected()));\r
180 \r
181       Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS", Boolean.toString(annotations.isSelected()));\r
182       Cache.applicationProperties.setProperty("SHOW_CONSERVATION", Boolean.toString(conservation.isSelected()));\r
183       Cache.applicationProperties.setProperty("SHOW_QUALITY", Boolean.toString(quality.isSelected()));\r
184       Cache.applicationProperties.setProperty("SHOW_IDENTITY", Boolean.toString(identity.isSelected()));\r
185 \r
186       Cache.applicationProperties.setProperty("DEFAULT_COLOUR", colour.getSelectedItem().toString());\r
187       Cache.applicationProperties.setProperty("GAP_SYMBOL", gapSymbolCB.getSelectedItem().toString());\r
188 \r
189       Cache.applicationProperties.setProperty("FONT_NAME", fontNameCB.getSelectedItem().toString());\r
190       Cache.applicationProperties.setProperty("FONT_STYLE", fontStyleCB.getSelectedItem().toString());\r
191       Cache.applicationProperties.setProperty("FONT_SIZE", fontSizeCB.getSelectedItem().toString());\r
192 \r
193       Cache.applicationProperties.setProperty("STARTUP_FILE", startupFileTextfield.getText());\r
194       Cache.applicationProperties.setProperty("SHOW_STARTUP_FILE",  Boolean.toString(startupCheckbox.isSelected()));\r
195 \r
196       Cache.applicationProperties.setProperty("DEFAULT_BROWSER", defaultBrowser.getText());\r
197 \r
198         if(nameLinks.size()>0)\r
199         {\r
200           StringBuffer links = new StringBuffer();\r
201           sequenceURLLinks = new Vector();\r
202           for (int i = 0; i < nameLinks.size(); i++)\r
203           {\r
204             sequenceURLLinks.addElement(nameLinks.elementAt(i)+"|"+urlLinks.elementAt(i));\r
205             links.append(sequenceURLLinks.elementAt(i).toString());\r
206             links.append("|");\r
207           }\r
208           // remove last "|"\r
209           links.setLength( links.length()-1 );\r
210           Cache.applicationProperties.setProperty("SEQUENCE_LINKS", links.toString());\r
211         }\r
212 \r
213         Cache.applicationProperties.setProperty("USE_PROXY", Boolean.toString(useProxy.isSelected()));\r
214         Cache.applicationProperties.setProperty("PROXY_SERVER",proxyServerTB.getText());\r
215         Cache.applicationProperties.setProperty("PROXY_PORT", proxyPortTB.getText());\r
216 \r
217         if(useProxy.isSelected())\r
218         {\r
219           System.setProperty("http.proxyHost", proxyServerTB.getText());\r
220           System.setProperty("http.proxyPort", proxyPortTB.getText());\r
221         }\r
222 \r
223 \r
224 \r
225         Cache.saveProperties();\r
226         try\r
227         {\r
228             frame.setClosed(true);\r
229         }\r
230         catch (Exception ex)\r
231         {\r
232         }\r
233     }\r
234 \r
235     /**\r
236      * DOCUMENT ME!\r
237      */\r
238     public void startupFileTextfield_mouseClicked()\r
239     {\r
240         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(\r
241                     "LAST_DIRECTORY"),\r
242                 new String[]\r
243                 {\r
244                     "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",\r
245                     "jar"\r
246                 },\r
247                 new String[]\r
248                 {\r
249                     "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"\r
250                 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));\r
251         chooser.setFileView(new JalviewFileView());\r
252         chooser.setDialogTitle("Select startup file");\r
253 \r
254         int value = chooser.showOpenDialog(this);\r
255 \r
256         if (value == JalviewFileChooser.APPROVE_OPTION)\r
257         {\r
258             jalview.bin.Cache.applicationProperties.setProperty("DEFAULT_FILE_FORMAT",\r
259                 chooser.getSelectedFormat());\r
260             startupFileTextfield.setText(chooser.getSelectedFile()\r
261                                                 .getAbsolutePath());\r
262         }\r
263     }\r
264 \r
265     /**\r
266      * DOCUMENT ME!\r
267      *\r
268      * @param e DOCUMENT ME!\r
269      */\r
270     public void cancel_actionPerformed(ActionEvent e)\r
271     {\r
272         try\r
273         {\r
274             frame.setClosed(true);\r
275         }\r
276         catch (Exception ex)\r
277         {\r
278         }\r
279     }\r
280 \r
281     /**\r
282      * DOCUMENT ME!\r
283      *\r
284      * @param e DOCUMENT ME!\r
285      */\r
286     public void annotations_actionPerformed(ActionEvent e)\r
287     {\r
288         conservation.setEnabled(annotations.isSelected());\r
289         quality.setEnabled(annotations.isSelected());\r
290         identity.setEnabled(annotations.isSelected());\r
291     }\r
292 \r
293 \r
294     public void newLink_actionPerformed(ActionEvent e) {\r
295 \r
296       GSequenceLink link = new GSequenceLink();\r
297       boolean valid = false;\r
298       while( !valid )\r
299       {\r
300         if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,\r
301                                                   "New sequence URL link",\r
302                                                   JOptionPane.OK_CANCEL_OPTION\r
303                                                   ,-1, null)\r
304             == JOptionPane.OK_OPTION)\r
305         {\r
306           if (link.checkValid())\r
307           {\r
308             nameLinks.addElement(link.getName());\r
309             urlLinks.addElement(link.getURL());\r
310             updateLinkData();\r
311             valid = true;\r
312           }\r
313         }\r
314         else\r
315           break;\r
316       }\r
317     }\r
318 \r
319     public void editLink_actionPerformed(ActionEvent e) {\r
320       GSequenceLink link = new GSequenceLink();\r
321 \r
322       int index = linkNameList.getSelectedIndex();\r
323       if(index==-1)\r
324       {\r
325         JOptionPane.showInternalMessageDialog(Desktop.desktop, "No link selected!"\r
326             ,"No link selected", JOptionPane.WARNING_MESSAGE);\r
327         return;\r
328       }\r
329 \r
330       link.setName( nameLinks.elementAt(index).toString() );\r
331       link.setURL( urlLinks.elementAt(index).toString() );\r
332 \r
333       boolean valid = false;\r
334       while (!valid)\r
335       {\r
336 \r
337         if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,\r
338                                                   "New sequence URL link",\r
339                                                   JOptionPane.OK_CANCEL_OPTION\r
340                                                   ,-1, null)\r
341             == JOptionPane.OK_OPTION)\r
342         {\r
343           if (link.checkValid())\r
344           {\r
345             nameLinks.setElementAt(link.getName(), index);\r
346             urlLinks.setElementAt(link.getURL(), index);\r
347             updateLinkData();\r
348             valid = true;\r
349           }\r
350         }\r
351 \r
352         else\r
353           break;\r
354       }\r
355     }\r
356 \r
357     public void deleteLink_actionPerformed(ActionEvent e) {\r
358       int index = linkNameList.getSelectedIndex();\r
359       if(index==-1)\r
360       {\r
361         JOptionPane.showInternalMessageDialog(Desktop.desktop, "No link selected!"\r
362             ,"No link selected", JOptionPane.WARNING_MESSAGE);\r
363         return;\r
364       }\r
365       nameLinks.removeElementAt(index);\r
366       urlLinks.removeElementAt(index);\r
367       updateLinkData();\r
368     }\r
369 \r
370     void updateLinkData()\r
371     {\r
372       linkNameList.setListData(nameLinks);\r
373       linkURLList.setListData(urlLinks);\r
374     }\r
375 \r
376     public void defaultBrowser_mouseClicked(MouseEvent e)\r
377     {\r
378       JFileChooser chooser = new JFileChooser(".");\r
379       chooser.setDialogTitle("Select default web browser");\r
380 \r
381       int value = chooser.showOpenDialog(this);\r
382 \r
383       if (value == JFileChooser.APPROVE_OPTION)\r
384       {\r
385         defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());\r
386       }\r
387 \r
388   }\r
389 \r
390   private void jbInit()\r
391       throws Exception\r
392   {\r
393   }\r
394 }\r