added preferences for usage stats, questionnaire check and version check.
[jalview.git] / src / jalview / gui / Preferences.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 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 java.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import javax.swing.*;
27
28 import jalview.bin.*;
29 import jalview.io.*;
30 import jalview.jbgui.*;
31 import jalview.schemes.*;
32
33 /**
34  * DOCUMENT ME!
35  * 
36  * @author $author$
37  * @version $Revision$
38  */
39 public class Preferences extends GPreferences
40 {
41
42   /**
43    * Holds name and link separated with | character. Sequence ID must be
44    * $SEQUENCE_ID$ or $SEQUENCE_ID=/.possible | chars ./=$
45    */
46   public static Vector sequenceURLLinks;
47   static
48   {
49     String string = Cache
50             .getDefault(
51                     "SEQUENCE_LINKS",
52                     "SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
53     sequenceURLLinks = new Vector();
54
55     try
56     {
57       StringTokenizer st = new StringTokenizer(string, "|");
58       while (st.hasMoreElements())
59       {
60         String name = st.nextToken();
61         String url = st.nextToken();
62         // check for '|' within a regex
63         int rxstart = url.indexOf("$SEQUENCE_ID$");
64         while (rxstart == -1 && url.indexOf("/=$") == -1)
65         {
66           url = url + "|" + st.nextToken();
67         }
68         sequenceURLLinks.addElement(name + "|" + url);
69       }
70     } catch (Exception ex)
71     {
72       System.out.println(ex + "\nError parsing sequence links");
73     }
74   }
75
76   Vector nameLinks, urlLinks;
77
78   JInternalFrame frame;
79
80   DasSourceBrowser dasSource;
81
82   /**
83    * Creates a new Preferences object.
84    */
85   public Preferences()
86   {
87
88     frame = new JInternalFrame();
89     frame.setContentPane(this);
90     dasSource = new DasSourceBrowser();
91     dasPanel.add(dasSource, BorderLayout.CENTER);
92
93     int width = 500, height = 420;
94     if (new jalview.util.Platform().isAMac())
95     {
96       width = 570;
97       height = 460;
98     }
99
100     Desktop.addInternalFrame(frame, "Preferences", width, height);
101     frame.setMinimumSize(new Dimension(width, height));
102
103     seqLimit.setSelected(Cache.getDefault("SHOW_JVSUFFIX", true));
104     rightAlign.setSelected(Cache.getDefault("RIGHT_ALIGN_IDS", false));
105     fullScreen.setSelected(Cache.getDefault("SHOW_FULLSCREEN", false));
106     annotations.setSelected(Cache.getDefault("SHOW_ANNOTATIONS", true));
107
108     conservation.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));
109     quality.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));
110     identity.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));
111
112     conservation.setSelected(Cache.getDefault("SHOW_CONSERVATION", true));
113     quality.setSelected(Cache.getDefault("SHOW_QUALITY", true));
114     identity.setSelected(Cache.getDefault("SHOW_IDENTITY", true));
115     openoverv.setSelected(Cache.getDefault("SHOW_OVERVIEW", false));
116     showUnconserved.setSelected(Cache.getDefault("SHOW_UNCONSERVED", false));
117
118     for (int i = 0; i < 13; i++)
119     {
120       colour.addItem(ColourSchemeProperty.getColourName(i));
121     }
122
123     String string = Cache.getDefault("DEFAULT_COLOUR", "None");
124
125     colour.setSelectedItem(string);
126
127     String[] fonts = java.awt.GraphicsEnvironment
128             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
129
130     for (int i = 0; i < fonts.length; i++)
131     {
132       fontNameCB.addItem(fonts[i]);
133     }
134
135     for (int i = 1; i < 31; i++)
136     {
137       fontSizeCB.addItem(i + "");
138     }
139
140     fontStyleCB.addItem("plain");
141     fontStyleCB.addItem("bold");
142     fontStyleCB.addItem("italic");
143
144     fontNameCB.setSelectedItem(Cache.getDefault("FONT_NAME", "SansSerif"));
145     fontSizeCB.setSelectedItem(Cache.getDefault("FONT_SIZE", "10"));
146     fontStyleCB.setSelectedItem(Cache.getDefault("FONT_STYLE", Font.PLAIN
147             + ""));
148
149     smoothFont.setSelected(Cache.getDefault("ANTI_ALIAS", false));
150
151     idItalics.setSelected(Cache.getDefault("ID_ITALICS", true));
152
153     wrap.setSelected(Cache.getDefault("WRAP_ALIGNMENT", false));
154
155     gapSymbolCB.addItem("-");
156     gapSymbolCB.addItem(".");
157
158     gapSymbolCB.setSelectedItem(Cache.getDefault("GAP_SYMBOL", "-"));
159
160     startupCheckbox
161             .setSelected(Cache.getDefault("SHOW_STARTUP_FILE", true));
162     startupFileTextfield.setText(Cache.getDefault("STARTUP_FILE",
163             "http://www.jalview.org/examples/exampleFile_2_3.jar"));
164
165     sortby.addItem("No sort");
166     sortby.addItem("Id");
167     sortby.addItem("Pairwise Identity");
168     sortby.setSelectedItem(Cache.getDefault("SORT_ALIGNMENT", "No sort"));
169
170     epsRendering.addItem("Prompt each time");
171     epsRendering.addItem("Lineart");
172     epsRendering.addItem("Text");
173     epsRendering.setSelectedItem(Cache.getDefault("EPS_RENDERING",
174             "Prompt each time"));
175
176     blcjv.setSelected(Cache.getDefault("BLC_JVSUFFIX", true));
177     clustaljv.setSelected(Cache.getDefault("CLUSTAL_JVSUFFIX", true));
178     fastajv.setSelected(Cache.getDefault("FASTA_JVSUFFIX", true));
179     msfjv.setSelected(Cache.getDefault("MSF_JVSUFFIX", true));
180     pfamjv.setSelected(Cache.getDefault("PFAM_JVSUFFIX", true));
181     pileupjv.setSelected(Cache.getDefault("PILEUP_JVSUFFIX", true));
182     pirjv.setSelected(Cache.getDefault("PIR_JVSUFFIX", true));
183
184     modellerOutput.setSelected(Cache.getDefault("PIR_MODELLER", false));
185
186     autoCalculateConsCheck.setSelected(Cache.getDefault(
187             "AUTO_CALC_CONSENSUS", true));
188     padGaps.setSelected(Cache.getDefault("PAD_GAPS", false));
189
190     /***************************************************************************
191      * Set up Connections
192      */
193     nameLinks = new Vector();
194     urlLinks = new Vector();
195     for (int i = 0; i < sequenceURLLinks.size(); i++)
196     {
197       String link = sequenceURLLinks.elementAt(i).toString();
198       nameLinks.addElement(link.substring(0, link.indexOf("|")));
199       urlLinks.addElement(link.substring(link.indexOf("|") + 1));
200     }
201
202     updateLinkData();
203
204     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
205     proxyServerTB.setEnabled(useProxy.isSelected());
206     proxyPortTB.setEnabled(useProxy.isSelected());
207     proxyServerTB.setText(Cache.getDefault("PROXY_SERVER", ""));
208     proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));
209
210     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
211     
212     usagestats.setSelected(Cache.getDefault("USAGESTATS", false));
213     questionnaire.setSelected(Cache.getProperty("NOQUESTIONNAIRES")==null); // note antisense here
214     versioncheck.setSelected(Cache.getDefault("VERSION_CHECK", true)); // default is true
215
216     try
217     {
218       jbInit();
219     } catch (Exception ex)
220     {
221       ex.printStackTrace();
222     }
223
224   }
225
226   /**
227    * DOCUMENT ME!
228    * 
229    * @param e
230    *                DOCUMENT ME!
231    */
232   public void ok_actionPerformed(ActionEvent e)
233   {
234
235     Cache.applicationProperties.setProperty("SHOW_JVSUFFIX", Boolean
236             .toString(seqLimit.isSelected()));
237     Cache.applicationProperties.setProperty("RIGHT_ALIGN_IDS", Boolean
238             .toString(rightAlign.isSelected()));
239     Cache.applicationProperties.setProperty("SHOW_FULLSCREEN", Boolean
240             .toString(fullScreen.isSelected()));
241     Cache.applicationProperties.setProperty("SHOW_OVERVIEW", Boolean
242             .toString(openoverv.isSelected()));
243     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS", Boolean
244             .toString(annotations.isSelected()));
245     Cache.applicationProperties.setProperty("SHOW_CONSERVATION", Boolean
246             .toString(conservation.isSelected()));
247     Cache.applicationProperties.setProperty("SHOW_QUALITY", Boolean
248             .toString(quality.isSelected()));
249     Cache.applicationProperties.setProperty("SHOW_IDENTITY", Boolean
250             .toString(identity.isSelected()));
251
252     Cache.applicationProperties.setProperty("DEFAULT_COLOUR", colour
253             .getSelectedItem().toString());
254     Cache.applicationProperties.setProperty("GAP_SYMBOL", gapSymbolCB
255             .getSelectedItem().toString());
256
257     Cache.applicationProperties.setProperty("FONT_NAME", fontNameCB
258             .getSelectedItem().toString());
259     Cache.applicationProperties.setProperty("FONT_STYLE", fontStyleCB
260             .getSelectedItem().toString());
261     Cache.applicationProperties.setProperty("FONT_SIZE", fontSizeCB
262             .getSelectedItem().toString());
263
264     Cache.applicationProperties.setProperty("ID_ITALICS", Boolean
265             .toString(idItalics.isSelected()));
266     Cache.applicationProperties.setProperty("SHOW_UNCONSERVED",Boolean.toString(showUnconserved.isSelected()));
267     Cache.applicationProperties.setProperty("ANTI_ALIAS", Boolean
268             .toString(smoothFont.isSelected()));
269
270     Cache.applicationProperties.setProperty("WRAP_ALIGNMENT", Boolean
271             .toString(wrap.isSelected()));
272
273     Cache.applicationProperties.setProperty("STARTUP_FILE",
274             startupFileTextfield.getText());
275     Cache.applicationProperties.setProperty("SHOW_STARTUP_FILE", Boolean
276             .toString(startupCheckbox.isSelected()));
277
278     Cache.applicationProperties.setProperty("SORT_ALIGNMENT", sortby
279             .getSelectedItem().toString());
280
281     if (epsRendering.getSelectedItem().equals("Prompt each time"))
282     {
283       Cache.applicationProperties.remove("EPS_RENDERING");
284     }
285     else
286     {
287       Cache.applicationProperties.setProperty("EPS_RENDERING", epsRendering
288               .getSelectedItem().toString());
289     }
290
291     if (defaultBrowser.getText().trim().length() < 1)
292     {
293       Cache.applicationProperties.remove("DEFAULT_BROWSER");
294     }
295     else
296     {
297       Cache.applicationProperties.setProperty("DEFAULT_BROWSER",
298               defaultBrowser.getText());
299     }
300
301     jalview.util.BrowserLauncher.resetBrowser();
302
303     if (nameLinks.size() > 0)
304     {
305       StringBuffer links = new StringBuffer();
306       sequenceURLLinks = new Vector();
307       for (int i = 0; i < nameLinks.size(); i++)
308       {
309         sequenceURLLinks.addElement(nameLinks.elementAt(i) + "|"
310                 + urlLinks.elementAt(i));
311         links.append(sequenceURLLinks.elementAt(i).toString());
312         links.append("|");
313       }
314       // remove last "|"
315       links.setLength(links.length() - 1);
316       Cache.applicationProperties.setProperty("SEQUENCE_LINKS", links
317               .toString());
318     }
319     else
320     {
321       Cache.applicationProperties.remove("SEQUENCE_LINKS");
322     }
323
324     Cache.applicationProperties.setProperty("USE_PROXY", Boolean
325             .toString(useProxy.isSelected()));
326
327     if (proxyServerTB.getText().trim().length() < 1)
328     {
329       Cache.applicationProperties.remove("PROXY_SERVER");
330     }
331     else
332     {
333       Cache.applicationProperties.setProperty("PROXY_SERVER", proxyServerTB
334               .getText());
335     }
336
337     if (proxyPortTB.getText().trim().length() < 1)
338     {
339       Cache.applicationProperties.remove("PROXY_PORT");
340     }
341     else
342     {
343       Cache.applicationProperties.setProperty("PROXY_PORT", proxyPortTB
344               .getText());
345     }
346
347     if (useProxy.isSelected())
348     {
349       System.setProperty("http.proxyHost", proxyServerTB.getText());
350       System.setProperty("http.proxyPort", proxyPortTB.getText());
351     }
352     else
353     {
354       System.setProperty("http.proxyHost", "");
355       System.setProperty("http.proxyPort", "");
356     }
357     Cache.setProperty("VERSION_CHECK", Boolean
358             .toString(versioncheck.isSelected()));
359     if (Cache.getProperty("USAGESTATS")!=null || usagestats.isSelected()) {
360       // default is false - we only set this if the user has actively agreed
361       Cache.setProperty("USAGESTATS",Boolean
362             .toString(usagestats.isSelected()));
363     }
364     if (!questionnaire.isSelected())
365     {
366       Cache.setProperty("NOQUESTIONNAIRES", "true");
367     } else {
368       // special - made easy to edit a property file to disable questionnaires by just adding the given line
369       Cache.removeProperty("NOQUESTIONNAIRES");
370     }
371     Cache.applicationProperties.setProperty("BLC_JVSUFFIX", Boolean
372             .toString(blcjv.isSelected()));
373     Cache.applicationProperties.setProperty("CLUSTAL_JVSUFFIX", Boolean
374             .toString(clustaljv.isSelected()));
375     Cache.applicationProperties.setProperty("FASTA_JVSUFFIX", Boolean
376             .toString(fastajv.isSelected()));
377     Cache.applicationProperties.setProperty("MSF_JVSUFFIX", Boolean
378             .toString(msfjv.isSelected()));
379     Cache.applicationProperties.setProperty("PFAM_JVSUFFIX", Boolean
380             .toString(pfamjv.isSelected()));
381     Cache.applicationProperties.setProperty("PILEUP_JVSUFFIX", Boolean
382             .toString(pileupjv.isSelected()));
383     Cache.applicationProperties.setProperty("PIR_JVSUFFIX", Boolean
384             .toString(pirjv.isSelected()));
385     Cache.applicationProperties.setProperty("PIR_MODELLER", Boolean
386             .toString(modellerOutput.isSelected()));
387     jalview.io.PIRFile.useModellerOutput = modellerOutput.isSelected();
388
389     Cache.applicationProperties.setProperty("AUTO_CALC_CONSENSUS", Boolean
390             .toString(autoCalculateConsCheck.isSelected()));
391     Cache.applicationProperties.setProperty("PAD_GAPS", Boolean
392             .toString(padGaps.isSelected()));
393
394     dasSource.saveProperties(Cache.applicationProperties);
395
396     Cache.saveProperties();
397     try
398     {
399       frame.setClosed(true);
400     } catch (Exception ex)
401     {
402     }
403   }
404
405   /**
406    * DOCUMENT ME!
407    */
408   public void startupFileTextfield_mouseClicked()
409   {
410     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
411             .getProperty("LAST_DIRECTORY"), new String[]
412     { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "jar" },
413             new String[]
414             { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview" },
415             jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
416     chooser.setFileView(new JalviewFileView());
417     chooser.setDialogTitle("Select startup file");
418
419     int value = chooser.showOpenDialog(this);
420
421     if (value == JalviewFileChooser.APPROVE_OPTION)
422     {
423       jalview.bin.Cache.applicationProperties.setProperty(
424               "DEFAULT_FILE_FORMAT", chooser.getSelectedFormat());
425       startupFileTextfield.setText(chooser.getSelectedFile()
426               .getAbsolutePath());
427     }
428   }
429
430   /**
431    * DOCUMENT ME!
432    * 
433    * @param e
434    *                DOCUMENT ME!
435    */
436   public void cancel_actionPerformed(ActionEvent e)
437   {
438     try
439     {
440       frame.setClosed(true);
441     } catch (Exception ex)
442     {
443     }
444   }
445
446   /**
447    * DOCUMENT ME!
448    * 
449    * @param e
450    *                DOCUMENT ME!
451    */
452   public void annotations_actionPerformed(ActionEvent e)
453   {
454     conservation.setEnabled(annotations.isSelected());
455     quality.setEnabled(annotations.isSelected());
456     identity.setEnabled(annotations.isSelected());
457   }
458
459   public void newLink_actionPerformed(ActionEvent e)
460   {
461
462     GSequenceLink link = new GSequenceLink();
463     boolean valid = false;
464     while (!valid)
465     {
466       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
467               "New sequence URL link", JOptionPane.OK_CANCEL_OPTION, -1,
468               null) == JOptionPane.OK_OPTION)
469       {
470         if (link.checkValid())
471         {
472           nameLinks.addElement(link.getName());
473           urlLinks.addElement(link.getURL());
474           updateLinkData();
475           valid = true;
476         }
477       }
478       else
479       {
480         break;
481       }
482     }
483   }
484
485   public void editLink_actionPerformed(ActionEvent e)
486   {
487     GSequenceLink link = new GSequenceLink();
488
489     int index = linkNameList.getSelectedIndex();
490     if (index == -1)
491     {
492       JOptionPane.showInternalMessageDialog(Desktop.desktop,
493               "No link selected!", "No link selected",
494               JOptionPane.WARNING_MESSAGE);
495       return;
496     }
497
498     link.setName(nameLinks.elementAt(index).toString());
499     link.setURL(urlLinks.elementAt(index).toString());
500
501     boolean valid = false;
502     while (!valid)
503     {
504
505       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
506               "New sequence URL link", JOptionPane.OK_CANCEL_OPTION, -1,
507               null) == JOptionPane.OK_OPTION)
508       {
509         if (link.checkValid())
510         {
511           nameLinks.setElementAt(link.getName(), index);
512           urlLinks.setElementAt(link.getURL(), index);
513           updateLinkData();
514           valid = true;
515         }
516       }
517
518       else
519       {
520         break;
521       }
522     }
523   }
524
525   public void deleteLink_actionPerformed(ActionEvent e)
526   {
527     int index = linkNameList.getSelectedIndex();
528     if (index == -1)
529     {
530       JOptionPane.showInternalMessageDialog(Desktop.desktop,
531               "No link selected!", "No link selected",
532               JOptionPane.WARNING_MESSAGE);
533       return;
534     }
535     nameLinks.removeElementAt(index);
536     urlLinks.removeElementAt(index);
537     updateLinkData();
538   }
539
540   void updateLinkData()
541   {
542     linkNameList.setListData(nameLinks);
543     linkURLList.setListData(urlLinks);
544   }
545
546   public void defaultBrowser_mouseClicked(MouseEvent e)
547   {
548     JFileChooser chooser = new JFileChooser(".");
549     chooser.setDialogTitle("Select default web browser");
550
551     int value = chooser.showOpenDialog(this);
552
553     if (value == JFileChooser.APPROVE_OPTION)
554     {
555       defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());
556     }
557
558   }
559   /* (non-Javadoc)
560    * @see jalview.jbgui.GPreferences#showunconserved_actionPerformed(java.awt.event.ActionEvent)
561    */
562   protected void showunconserved_actionPerformed(ActionEvent e)
563   {
564     // TODO Auto-generated method stub
565     super.showunconserved_actionPerformed(e);
566   }
567
568   private void jbInit() throws Exception
569   {
570   }
571 }