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