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