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