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