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