ID tooltip preference settings and doc
[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     showNpTooltip.setSelected(Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true));
131     showDbRefTooltip.setSelected(Cache.getDefault("SHOW_DBREFS_TOOLTIP", true));
132
133     for (int i = 0; i < 13; i++)
134     {
135       colour.addItem(ColourSchemeProperty.getColourName(i));
136     }
137
138     String string = Cache.getDefault("DEFAULT_COLOUR", "None");
139
140     colour.setSelectedItem(string);
141
142     String[] fonts = java.awt.GraphicsEnvironment
143             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
144
145     for (int i = 0; i < fonts.length; i++)
146     {
147       fontNameCB.addItem(fonts[i]);
148     }
149
150     for (int i = 1; i < 31; i++)
151     {
152       fontSizeCB.addItem(i + "");
153     }
154
155     fontStyleCB.addItem("plain");
156     fontStyleCB.addItem("bold");
157     fontStyleCB.addItem("italic");
158
159     fontNameCB.setSelectedItem(Cache.getDefault("FONT_NAME", "SansSerif"));
160     fontSizeCB.setSelectedItem(Cache.getDefault("FONT_SIZE", "10"));
161     fontStyleCB.setSelectedItem(Cache.getDefault("FONT_STYLE", Font.PLAIN
162             + ""));
163
164     smoothFont.setSelected(Cache.getDefault("ANTI_ALIAS", false));
165
166     idItalics.setSelected(Cache.getDefault("ID_ITALICS", true));
167
168     wrap.setSelected(Cache.getDefault("WRAP_ALIGNMENT", false));
169
170     gapSymbolCB.addItem("-");
171     gapSymbolCB.addItem(".");
172
173     gapSymbolCB.setSelectedItem(Cache.getDefault("GAP_SYMBOL", "-"));
174
175     startupCheckbox
176             .setSelected(Cache.getDefault("SHOW_STARTUP_FILE", true));
177     startupFileTextfield.setText(Cache.getDefault("STARTUP_FILE",
178             "http://www.jalview.org/examples/exampleFile_2_3.jar"));
179
180     sortby.addItem("No sort");
181     sortby.addItem("Id");
182     sortby.addItem("Pairwise Identity");
183     sortby.setSelectedItem(Cache.getDefault("SORT_ALIGNMENT", "No sort"));
184
185     epsRendering.addItem("Prompt each time");
186     epsRendering.addItem("Lineart");
187     epsRendering.addItem("Text");
188     epsRendering.setSelectedItem(Cache.getDefault("EPS_RENDERING",
189             "Prompt each time"));
190
191     blcjv.setSelected(Cache.getDefault("BLC_JVSUFFIX", true));
192     clustaljv.setSelected(Cache.getDefault("CLUSTAL_JVSUFFIX", true));
193     fastajv.setSelected(Cache.getDefault("FASTA_JVSUFFIX", true));
194     msfjv.setSelected(Cache.getDefault("MSF_JVSUFFIX", true));
195     pfamjv.setSelected(Cache.getDefault("PFAM_JVSUFFIX", true));
196     pileupjv.setSelected(Cache.getDefault("PILEUP_JVSUFFIX", true));
197     pirjv.setSelected(Cache.getDefault("PIR_JVSUFFIX", true));
198
199     modellerOutput.setSelected(Cache.getDefault("PIR_MODELLER", false));
200
201     autoCalculateConsCheck.setSelected(Cache.getDefault(
202             "AUTO_CALC_CONSENSUS", true));
203     showGroupConsensus.setSelected(Cache.getDefault("SHOW_GROUP_CONSENSUS",false));
204     showGroupConservation.setSelected(Cache.getDefault("SHOW_GROUP_CONSERVATION", false));
205     showConsensHistogram.setSelected(Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",true));
206     showConsensLogo.setSelected(Cache.getDefault("SHOW_CONSENSUS_LOGO",false));
207     
208     padGaps.setSelected(Cache.getDefault("PAD_GAPS", false));
209
210     /***************************************************************************
211      * Set up Connections
212      */
213     nameLinks = new Vector();
214     urlLinks = new Vector();
215     for (int i = 0; i < sequenceURLLinks.size(); i++)
216     {
217       String link = sequenceURLLinks.elementAt(i).toString();
218       nameLinks.addElement(link.substring(0, link.indexOf("|")));
219       urlLinks.addElement(link.substring(link.indexOf("|") + 1));
220     }
221
222     updateLinkData();
223
224     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
225     proxyServerTB.setEnabled(useProxy.isSelected());
226     proxyPortTB.setEnabled(useProxy.isSelected());
227     proxyServerTB.setText(Cache.getDefault("PROXY_SERVER", ""));
228     proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));
229
230     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
231     
232     usagestats.setSelected(Cache.getDefault("USAGESTATS", false));
233     questionnaire.setSelected(Cache.getProperty("NOQUESTIONNAIRES")==null); // note antisense here
234     versioncheck.setSelected(Cache.getDefault("VERSION_CHECK", true)); // default is true
235     annotations_actionPerformed(null); // update the display of the annotation settings
236     try
237     {
238       jbInit();
239     } catch (Exception ex)
240     {
241       ex.printStackTrace();
242     }
243
244   }
245
246   /**
247    * DOCUMENT ME!
248    * 
249    * @param e
250    *                DOCUMENT ME!
251    */
252   public void ok_actionPerformed(ActionEvent e)
253   {
254
255     Cache.applicationProperties.setProperty("SHOW_JVSUFFIX", Boolean
256             .toString(seqLimit.isSelected()));
257     Cache.applicationProperties.setProperty("RIGHT_ALIGN_IDS", Boolean
258             .toString(rightAlign.isSelected()));
259     Cache.applicationProperties.setProperty("SHOW_FULLSCREEN", Boolean
260             .toString(fullScreen.isSelected()));
261     Cache.applicationProperties.setProperty("SHOW_OVERVIEW", Boolean
262             .toString(openoverv.isSelected()));
263     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS", Boolean
264             .toString(annotations.isSelected()));
265     Cache.applicationProperties.setProperty("SHOW_CONSERVATION", Boolean
266             .toString(conservation.isSelected()));
267     Cache.applicationProperties.setProperty("SHOW_QUALITY", Boolean
268             .toString(quality.isSelected()));
269     Cache.applicationProperties.setProperty("SHOW_IDENTITY", Boolean
270             .toString(identity.isSelected()));
271
272     Cache.applicationProperties.setProperty("DEFAULT_COLOUR", colour
273             .getSelectedItem().toString());
274     Cache.applicationProperties.setProperty("GAP_SYMBOL", gapSymbolCB
275             .getSelectedItem().toString());
276
277     Cache.applicationProperties.setProperty("FONT_NAME", fontNameCB
278             .getSelectedItem().toString());
279     Cache.applicationProperties.setProperty("FONT_STYLE", fontStyleCB
280             .getSelectedItem().toString());
281     Cache.applicationProperties.setProperty("FONT_SIZE", fontSizeCB
282             .getSelectedItem().toString());
283
284     Cache.applicationProperties.setProperty("ID_ITALICS", Boolean
285             .toString(idItalics.isSelected()));
286     Cache.applicationProperties.setProperty("SHOW_UNCONSERVED",Boolean.toString(showUnconserved.isSelected()));
287     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSENSUS",Boolean.toString(showGroupConsensus.isSelected()));
288     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSERVATION",Boolean.toString(showGroupConservation.isSelected()));
289     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_HISTOGRAM",Boolean.toString(showConsensHistogram.isSelected()));
290     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_LOGO",Boolean.toString(showConsensLogo.isSelected()));
291     Cache.applicationProperties.setProperty("ANTI_ALIAS", Boolean
292             .toString(smoothFont.isSelected()));
293     Cache.applicationProperties.setProperty("SHOW_NPFEATS_TOOLTIP", Boolean.toString(showNpTooltip.isSelected()));
294     Cache.applicationProperties.setProperty("SHOW_DBREFS_TOOLTIP", Boolean.toString(showDbRefTooltip.isSelected()));
295
296     Cache.applicationProperties.setProperty("WRAP_ALIGNMENT", Boolean
297             .toString(wrap.isSelected()));
298
299     Cache.applicationProperties.setProperty("STARTUP_FILE",
300             startupFileTextfield.getText());
301     Cache.applicationProperties.setProperty("SHOW_STARTUP_FILE", Boolean
302             .toString(startupCheckbox.isSelected()));
303
304     Cache.applicationProperties.setProperty("SORT_ALIGNMENT", sortby
305             .getSelectedItem().toString());
306
307     if (epsRendering.getSelectedItem().equals("Prompt each time"))
308     {
309       Cache.applicationProperties.remove("EPS_RENDERING");
310     }
311     else
312     {
313       Cache.applicationProperties.setProperty("EPS_RENDERING", epsRendering
314               .getSelectedItem().toString());
315     }
316
317     if (defaultBrowser.getText().trim().length() < 1)
318     {
319       Cache.applicationProperties.remove("DEFAULT_BROWSER");
320     }
321     else
322     {
323       Cache.applicationProperties.setProperty("DEFAULT_BROWSER",
324               defaultBrowser.getText());
325     }
326
327     jalview.util.BrowserLauncher.resetBrowser();
328
329     if (nameLinks.size() > 0)
330     {
331       StringBuffer links = new StringBuffer();
332       sequenceURLLinks = new Vector();
333       for (int i = 0; i < nameLinks.size(); i++)
334       {
335         sequenceURLLinks.addElement(nameLinks.elementAt(i) + "|"
336                 + urlLinks.elementAt(i));
337         links.append(sequenceURLLinks.elementAt(i).toString());
338         links.append("|");
339       }
340       // remove last "|"
341       links.setLength(links.length() - 1);
342       Cache.applicationProperties.setProperty("SEQUENCE_LINKS", links
343               .toString());
344     }
345     else
346     {
347       Cache.applicationProperties.remove("SEQUENCE_LINKS");
348     }
349
350     Cache.applicationProperties.setProperty("USE_PROXY", Boolean
351             .toString(useProxy.isSelected()));
352
353     if (proxyServerTB.getText().trim().length() < 1)
354     {
355       Cache.applicationProperties.remove("PROXY_SERVER");
356     }
357     else
358     {
359       Cache.applicationProperties.setProperty("PROXY_SERVER", proxyServerTB
360               .getText());
361     }
362
363     if (proxyPortTB.getText().trim().length() < 1)
364     {
365       Cache.applicationProperties.remove("PROXY_PORT");
366     }
367     else
368     {
369       Cache.applicationProperties.setProperty("PROXY_PORT", proxyPortTB
370               .getText());
371     }
372
373     if (useProxy.isSelected())
374     {
375       System.setProperty("http.proxyHost", proxyServerTB.getText());
376       System.setProperty("http.proxyPort", proxyPortTB.getText());
377     }
378     else
379     {
380       System.setProperty("http.proxyHost", "");
381       System.setProperty("http.proxyPort", "");
382     }
383     Cache.setProperty("VERSION_CHECK", Boolean
384             .toString(versioncheck.isSelected()));
385     if (Cache.getProperty("USAGESTATS")!=null || usagestats.isSelected()) {
386       // default is false - we only set this if the user has actively agreed
387       Cache.setProperty("USAGESTATS",Boolean
388             .toString(usagestats.isSelected()));
389     }
390     if (!questionnaire.isSelected())
391     {
392       Cache.setProperty("NOQUESTIONNAIRES", "true");
393     } else {
394       // special - made easy to edit a property file to disable questionnaires by just adding the given line
395       Cache.removeProperty("NOQUESTIONNAIRES");
396     }
397     Cache.applicationProperties.setProperty("BLC_JVSUFFIX", Boolean
398             .toString(blcjv.isSelected()));
399     Cache.applicationProperties.setProperty("CLUSTAL_JVSUFFIX", Boolean
400             .toString(clustaljv.isSelected()));
401     Cache.applicationProperties.setProperty("FASTA_JVSUFFIX", Boolean
402             .toString(fastajv.isSelected()));
403     Cache.applicationProperties.setProperty("MSF_JVSUFFIX", Boolean
404             .toString(msfjv.isSelected()));
405     Cache.applicationProperties.setProperty("PFAM_JVSUFFIX", Boolean
406             .toString(pfamjv.isSelected()));
407     Cache.applicationProperties.setProperty("PILEUP_JVSUFFIX", Boolean
408             .toString(pileupjv.isSelected()));
409     Cache.applicationProperties.setProperty("PIR_JVSUFFIX", Boolean
410             .toString(pirjv.isSelected()));
411     Cache.applicationProperties.setProperty("PIR_MODELLER", Boolean
412             .toString(modellerOutput.isSelected()));
413     jalview.io.PIRFile.useModellerOutput = modellerOutput.isSelected();
414
415     Cache.applicationProperties.setProperty("AUTO_CALC_CONSENSUS", Boolean
416             .toString(autoCalculateConsCheck.isSelected()));
417     Cache.applicationProperties.setProperty("PAD_GAPS", Boolean
418             .toString(padGaps.isSelected()));
419
420     dasSource.saveProperties(Cache.applicationProperties);
421
422     Cache.saveProperties();
423     try
424     {
425       frame.setClosed(true);
426     } catch (Exception ex)
427     {
428     }
429   }
430
431   /**
432    * DOCUMENT ME!
433    */
434   public void startupFileTextfield_mouseClicked()
435   {
436     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
437             .getProperty("LAST_DIRECTORY"), new String[]
438     { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "jar" },
439             new String[]
440             { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview" },
441             jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
442     chooser.setFileView(new JalviewFileView());
443     chooser.setDialogTitle("Select startup file");
444
445     int value = chooser.showOpenDialog(this);
446
447     if (value == JalviewFileChooser.APPROVE_OPTION)
448     {
449       jalview.bin.Cache.applicationProperties.setProperty(
450               "DEFAULT_FILE_FORMAT", chooser.getSelectedFormat());
451       startupFileTextfield.setText(chooser.getSelectedFile()
452               .getAbsolutePath());
453     }
454   }
455
456   /**
457    * DOCUMENT ME!
458    * 
459    * @param e
460    *                DOCUMENT ME!
461    */
462   public void cancel_actionPerformed(ActionEvent e)
463   {
464     try
465     {
466       frame.setClosed(true);
467     } catch (Exception ex)
468     {
469     }
470   }
471
472   /**
473    * DOCUMENT ME!
474    * 
475    * @param e
476    *                DOCUMENT ME!
477    */
478   public void annotations_actionPerformed(ActionEvent e)
479   {
480     conservation.setEnabled(annotations.isSelected());
481     quality.setEnabled(annotations.isSelected());
482     identity.setEnabled(annotations.isSelected());
483     showGroupConsensus.setEnabled(annotations.isSelected());
484     showGroupConservation.setEnabled(annotations.isSelected());
485     showConsensHistogram.setEnabled(annotations.isSelected() && (identity.isSelected() || showGroupConsensus.isSelected()));
486     showConsensLogo.setEnabled(annotations.isSelected() && (identity.isSelected() || showGroupConsensus.isSelected()));
487   }
488
489   public void newLink_actionPerformed(ActionEvent e)
490   {
491
492     GSequenceLink link = new GSequenceLink();
493     boolean valid = false;
494     while (!valid)
495     {
496       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
497               "New sequence URL link", JOptionPane.OK_CANCEL_OPTION, -1,
498               null) == JOptionPane.OK_OPTION)
499       {
500         if (link.checkValid())
501         {
502           nameLinks.addElement(link.getName());
503           urlLinks.addElement(link.getURL());
504           updateLinkData();
505           valid = true;
506         }
507       }
508       else
509       {
510         break;
511       }
512     }
513   }
514
515   public void editLink_actionPerformed(ActionEvent e)
516   {
517     GSequenceLink link = new GSequenceLink();
518
519     int index = linkNameList.getSelectedIndex();
520     if (index == -1)
521     {
522       JOptionPane.showInternalMessageDialog(Desktop.desktop,
523               "No link selected!", "No link selected",
524               JOptionPane.WARNING_MESSAGE);
525       return;
526     }
527
528     link.setName(nameLinks.elementAt(index).toString());
529     link.setURL(urlLinks.elementAt(index).toString());
530
531     boolean valid = false;
532     while (!valid)
533     {
534
535       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
536               "New sequence URL link", JOptionPane.OK_CANCEL_OPTION, -1,
537               null) == JOptionPane.OK_OPTION)
538       {
539         if (link.checkValid())
540         {
541           nameLinks.setElementAt(link.getName(), index);
542           urlLinks.setElementAt(link.getURL(), index);
543           updateLinkData();
544           valid = true;
545         }
546       }
547
548       else
549       {
550         break;
551       }
552     }
553   }
554
555   public void deleteLink_actionPerformed(ActionEvent e)
556   {
557     int index = linkNameList.getSelectedIndex();
558     if (index == -1)
559     {
560       JOptionPane.showInternalMessageDialog(Desktop.desktop,
561               "No link selected!", "No link selected",
562               JOptionPane.WARNING_MESSAGE);
563       return;
564     }
565     nameLinks.removeElementAt(index);
566     urlLinks.removeElementAt(index);
567     updateLinkData();
568   }
569
570   void updateLinkData()
571   {
572     linkNameList.setListData(nameLinks);
573     linkURLList.setListData(urlLinks);
574   }
575
576   public void defaultBrowser_mouseClicked(MouseEvent e)
577   {
578     JFileChooser chooser = new JFileChooser(".");
579     chooser.setDialogTitle("Select default web browser");
580
581     int value = chooser.showOpenDialog(this);
582
583     if (value == JFileChooser.APPROVE_OPTION)
584     {
585       defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());
586     }
587
588   }
589   /* (non-Javadoc)
590    * @see jalview.jbgui.GPreferences#showunconserved_actionPerformed(java.awt.event.ActionEvent)
591    */
592   protected void showunconserved_actionPerformed(ActionEvent e)
593   {
594     // TODO Auto-generated method stub
595     super.showunconserved_actionPerformed(e);
596   }
597
598   private void jbInit() throws Exception
599   {
600   }
601
602   public static Collection getGroupURLLinks()
603   {
604     return groupURLLinks;
605   }
606 }