JAL-1499 MEGA format, JAL-1421 List<String> url links
[jalview.git] / src / jalview / gui / Preferences.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
24 import jalview.bin.Cache;
25 import jalview.gui.Help.HelpId;
26 import jalview.gui.StructureViewer.ViewerType;
27 import jalview.io.JalviewFileChooser;
28 import jalview.io.JalviewFileView;
29 import jalview.jbgui.GPreferences;
30 import jalview.jbgui.GSequenceLink;
31 import jalview.schemes.ColourSchemeProperty;
32 import jalview.util.BrowserLauncher;
33 import jalview.util.MessageManager;
34 import jalview.util.Platform;
35
36 import java.awt.BorderLayout;
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.Font;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.MouseEvent;
43 import java.io.File;
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.StringTokenizer;
47
48 import javax.help.HelpSetException;
49 import javax.swing.JColorChooser;
50 import javax.swing.JFileChooser;
51 import javax.swing.JInternalFrame;
52 import javax.swing.JOptionPane;
53 import javax.swing.JPanel;
54
55 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
56
57 /**
58  * DOCUMENT ME!
59  * 
60  * @author $author$
61  * @version $Revision$
62  */
63 public class Preferences extends GPreferences
64 {
65   public static final String ENABLE_SPLIT_FRAME = "ENABLE_SPLIT_FRAME";
66
67   public static final String SCALE_PROTEIN_TO_CDNA = "SCALE_PROTEIN_TO_CDNA";
68
69   public static final String DEFAULT_COLOUR = "DEFAULT_COLOUR";
70
71   public static final String DEFAULT_COLOUR_PROT = "DEFAULT_COLOUR_PROT";
72
73   public static final String DEFAULT_COLOUR_NUC = "DEFAULT_COLOUR_NUC";
74
75   public static final String ADD_TEMPFACT_ANN = "ADD_TEMPFACT_ANN";
76
77   public static final String ADD_SS_ANN = "ADD_SS_ANN";
78
79   public static final String USE_RNAVIEW = "USE_RNAVIEW";
80
81   public static final String STRUCT_FROM_PDB = "STRUCT_FROM_PDB";
82
83   public static final String STRUCTURE_DISPLAY = "STRUCTURE_DISPLAY";
84
85   public static final String CHIMERA_PATH = "CHIMERA_PATH";
86
87   public static final String SORT_ANNOTATIONS = "SORT_ANNOTATIONS";
88
89   public static final String SHOW_AUTOCALC_ABOVE = "SHOW_AUTOCALC_ABOVE";
90
91   private static final int MIN_FONT_SIZE = 1;
92
93   private static final int MAX_FONT_SIZE = 30;
94
95   /**
96    * Holds name and link separated with | character. Sequence ID must be
97    * $SEQUENCE_ID$ or $SEQUENCE_ID=/.possible | chars ./=$
98    */
99   private static List<String> sequenceURLLinks;
100
101   /**
102    * Holds name and link separated with | character. Sequence IDS and Sequences
103    * must be $SEQUENCEIDS$ or $SEQUENCEIDS=/.possible | chars ./=$ and
104    * $SEQUENCES$ or $SEQUENCES=/.possible | chars ./=$ and separation character
105    * for first and second token specified after a pipe character at end |,|.
106    * (TODO: proper escape for using | to separate ids or sequences
107    */
108
109   private static List<String> groupURLLinks;
110
111   List<String> nameLinks;
112
113   List<String> urlLinks;
114
115   JInternalFrame frame;
116
117   DasSourceBrowser dasSource;
118
119   private WsPreferences wsPrefs;
120
121   static
122   {
123     String string = Cache
124             .getDefault(
125                     "SEQUENCE_LINKS",
126                     "EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$");
127     sequenceURLLinks = new ArrayList<String>();
128
129     try
130     {
131       StringTokenizer st = new StringTokenizer(string, "|");
132       while (st.hasMoreElements())
133       {
134         String name = st.nextToken();
135         String url = st.nextToken();
136         // check for '|' within a regex
137         int rxstart = url.indexOf("$SEQUENCE_ID$");
138         while (rxstart == -1 && url.indexOf("/=$") == -1)
139         {
140           url = url + "|" + st.nextToken();
141         }
142         sequenceURLLinks.add(name + "|" + url);
143       }
144     } catch (Exception ex)
145     {
146       System.out.println(ex + "\nError parsing sequence links");
147     }
148     {
149       // upgrade old SRS link
150       int srsPos = sequenceURLLinks
151               .indexOf("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
152       if (srsPos > -1)
153       {
154         sequenceURLLinks
155                 .set(srsPos,
156                         "EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$");
157       }
158     }
159
160     /**
161      * TODO: reformulate groupURL encoding so two or more can be stored in the
162      * .properties file as '|' separated strings
163      */
164
165     groupURLLinks = new ArrayList<String>();
166   }
167
168   /**
169    * Creates a new Preferences object.
170    */
171   public Preferences()
172   {
173     super();
174     frame = new JInternalFrame();
175     frame.setContentPane(this);
176     dasSource = new DasSourceBrowser();
177     dasTab.add(dasSource, BorderLayout.CENTER);
178     wsPrefs = new WsPreferences();
179     wsTab.add(wsPrefs, BorderLayout.CENTER);
180     int width = 500, height = 450;
181     if (Platform.isAMac())
182     {
183       width = 570;
184       height = 480;
185     }
186
187     Desktop.addInternalFrame(frame,
188             MessageManager.getString("label.preferences"), width, height);
189     frame.setMinimumSize(new Dimension(width, height));
190
191     /*
192      * Set Visual tab defaults
193      */
194     seqLimit.setSelected(Cache.getDefault("SHOW_JVSUFFIX", true));
195     rightAlign.setSelected(Cache.getDefault("RIGHT_ALIGN_IDS", false));
196     fullScreen.setSelected(Cache.getDefault("SHOW_FULLSCREEN", false));
197     annotations.setSelected(Cache.getDefault("SHOW_ANNOTATIONS", true));
198
199     conservation.setSelected(Cache.getDefault("SHOW_CONSERVATION", true));
200     quality.setSelected(Cache.getDefault("SHOW_QUALITY", true));
201     identity.setSelected(Cache.getDefault("SHOW_IDENTITY", true));
202     openoverv.setSelected(Cache.getDefault("SHOW_OVERVIEW", false));
203     showUnconserved
204             .setSelected(Cache.getDefault("SHOW_UNCONSERVED", false));
205     showGroupConsensus.setSelected(Cache.getDefault("SHOW_GROUP_CONSENSUS",
206             false));
207     showGroupConservation.setSelected(Cache.getDefault(
208             "SHOW_GROUP_CONSERVATION", false));
209     showConsensHistogram.setSelected(Cache.getDefault(
210             "SHOW_CONSENSUS_HISTOGRAM", true));
211     showConsensLogo.setSelected(Cache.getDefault("SHOW_CONSENSUS_LOGO",
212             false));
213     showNpTooltip.setSelected(Cache
214             .getDefault("SHOW_NPFEATS_TOOLTIP", true));
215     showDbRefTooltip.setSelected(Cache.getDefault("SHOW_DBREFS_TOOLTIP",
216             true));
217
218     String[] fonts = java.awt.GraphicsEnvironment
219             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
220     for (int i = 0; i < fonts.length; i++)
221     {
222       fontNameCB.addItem(fonts[i]);
223     }
224
225     for (int i = MIN_FONT_SIZE; i <= MAX_FONT_SIZE; i++)
226     {
227       fontSizeCB.addItem(i + "");
228     }
229
230     fontStyleCB.addItem("plain");
231     fontStyleCB.addItem("bold");
232     fontStyleCB.addItem("italic");
233
234     fontNameCB.setSelectedItem(Cache.getDefault("FONT_NAME", "SansSerif"));
235     fontSizeCB.setSelectedItem(Cache.getDefault("FONT_SIZE", "10"));
236     fontStyleCB.setSelectedItem(Cache.getDefault("FONT_STYLE", Font.PLAIN
237             + ""));
238
239     smoothFont.setSelected(Cache.getDefault("ANTI_ALIAS", false));
240     scaleProteinToCdna.setSelected(Cache.getDefault(SCALE_PROTEIN_TO_CDNA,
241             false));
242
243     idItalics.setSelected(Cache.getDefault("ID_ITALICS", true));
244
245     wrap.setSelected(Cache.getDefault("WRAP_ALIGNMENT", false));
246
247     gapSymbolCB.addItem("-");
248     gapSymbolCB.addItem(".");
249
250     gapSymbolCB.setSelectedItem(Cache.getDefault("GAP_SYMBOL", "-"));
251
252     sortby.addItem("No sort");
253     sortby.addItem("Id");
254     sortby.addItem("Pairwise Identity");
255     sortby.setSelectedItem(Cache.getDefault("SORT_ALIGNMENT", "No sort"));
256
257     sortAnnBy.addItem(SequenceAnnotationOrder.NONE.toString());
258     sortAnnBy
259             .addItem(SequenceAnnotationOrder.SEQUENCE_AND_LABEL.toString());
260     sortAnnBy
261             .addItem(SequenceAnnotationOrder.LABEL_AND_SEQUENCE.toString());
262     SequenceAnnotationOrder savedSort = SequenceAnnotationOrder
263             .valueOf(Cache.getDefault(SORT_ANNOTATIONS,
264                     SequenceAnnotationOrder.NONE.name()));
265     sortAnnBy.setSelectedItem(savedSort.toString());
266
267     sortAutocalc.addItem("Autocalculated first");
268     sortAutocalc.addItem("Autocalculated last");
269     final boolean showAbove = Cache.getDefault(SHOW_AUTOCALC_ABOVE, true);
270     sortAutocalc.setSelectedItem(showAbove ? sortAutocalc.getItemAt(0)
271             : sortAutocalc.getItemAt(1));
272     startupCheckbox
273             .setSelected(Cache.getDefault("SHOW_STARTUP_FILE", true));
274     startupFileTextfield.setText(Cache.getDefault("STARTUP_FILE",
275             Cache.getDefault("www.jalview.org", "http://www.jalview.org")
276                     + "/examples/exampleFile_2_3.jar"));
277
278     /*
279      * Set Colours tab defaults
280      */
281     for (int i = ColourSchemeProperty.FIRST_COLOUR; i <= ColourSchemeProperty.LAST_COLOUR; i++)
282     {
283       protColour.addItem(ColourSchemeProperty.getColourName(i));
284       nucColour.addItem(ColourSchemeProperty.getColourName(i));
285     }
286     String oldProp = Cache.getDefault(DEFAULT_COLOUR, "None");
287     String newProp = Cache.getDefault(DEFAULT_COLOUR_PROT, null);
288     protColour.setSelectedItem(newProp != null ? newProp : oldProp);
289     newProp = Cache.getDefault(DEFAULT_COLOUR_NUC, null);
290     nucColour.setSelectedItem(newProp != null ? newProp : oldProp);
291     minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
292             Color.orange));
293     maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
294             Color.red));
295
296     /*
297      * Set Structure tab defaults.
298      */
299     final boolean structSelected = Cache.getDefault(STRUCT_FROM_PDB, false);
300     structFromPdb.setSelected(structSelected);
301     useRnaView.setSelected(Cache.getDefault(USE_RNAVIEW, false));
302     useRnaView.setEnabled(structSelected);
303     addSecondaryStructure.setSelected(Cache.getDefault(ADD_SS_ANN, false));
304     addSecondaryStructure.setEnabled(structSelected);
305     addTempFactor.setSelected(Cache.getDefault(ADD_TEMPFACT_ANN, false));
306     addTempFactor.setEnabled(structSelected);
307     structViewer.setSelectedItem(Cache.getDefault(STRUCTURE_DISPLAY,
308             ViewerType.JMOL.name()));
309     chimeraPath.setText(Cache.getDefault(CHIMERA_PATH, ""));
310     chimeraPath.addActionListener(new ActionListener()
311     {
312       @Override
313       public void actionPerformed(ActionEvent e)
314       {
315         validateChimeraPath();
316       }
317     });
318
319     /*
320      * Set Connections tab defaults
321      */
322     nameLinks = new ArrayList<String>();
323     urlLinks = new ArrayList<String>();
324     for (int i = 0; i < sequenceURLLinks.size(); i++)
325     {
326       String link = sequenceURLLinks.get(i).toString();
327       nameLinks.add(link.substring(0, link.indexOf("|")));
328       urlLinks.add(link.substring(link.indexOf("|") + 1));
329     }
330
331     updateLinkData();
332
333     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
334     proxyServerTB.setEnabled(useProxy.isSelected());
335     proxyPortTB.setEnabled(useProxy.isSelected());
336     proxyServerTB.setText(Cache.getDefault("PROXY_SERVER", ""));
337     proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));
338
339     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
340
341     usagestats.setSelected(Cache.getDefault("USAGESTATS", false));
342     // note antisense here: default is true
343     questionnaire
344             .setSelected(Cache.getProperty("NOQUESTIONNAIRES") == null);
345     versioncheck.setSelected(Cache.getDefault("VERSION_CHECK", true));
346
347     /*
348      * Set Output tab defaults
349      */
350     epsRendering
351             .addItem(MessageManager.getString("label.prompt_each_time"));
352     epsRendering.addItem(MessageManager.getString("label.lineart"));
353     epsRendering.addItem(MessageManager.getString("action.text"));
354     epsRendering.setSelectedItem(Cache.getDefault("EPS_RENDERING",
355             "Prompt each time"));
356     autoIdWidth.setSelected(Cache.getDefault("FIGURE_AUTOIDWIDTH", false));
357     userIdWidth.setEnabled(!autoIdWidth.isSelected());
358     userIdWidthlabel.setEnabled(!autoIdWidth.isSelected());
359     Integer wi = Cache.getIntegerProperty("FIGURE_USERIDWIDTH");
360     userIdWidth.setText(wi == null ? "" : wi.toString());
361     blcjv.setSelected(Cache.getDefault("BLC_JVSUFFIX", true));
362     clustaljv.setSelected(Cache.getDefault("CLUSTAL_JVSUFFIX", true));
363     fastajv.setSelected(Cache.getDefault("FASTA_JVSUFFIX", true));
364     msfjv.setSelected(Cache.getDefault("MSF_JVSUFFIX", true));
365     pfamjv.setSelected(Cache.getDefault("PFAM_JVSUFFIX", true));
366     pileupjv.setSelected(Cache.getDefault("PILEUP_JVSUFFIX", true));
367     pirjv.setSelected(Cache.getDefault("PIR_JVSUFFIX", true));
368     modellerOutput.setSelected(Cache.getDefault("PIR_MODELLER", false));
369     embbedBioJSON.setSelected(Cache.getDefault("EXPORT_EMBBED_BIOJSON",
370             true));
371
372     /*
373      * Set Editing tab defaults
374      */
375     autoCalculateConsCheck.setSelected(Cache.getDefault(
376             "AUTO_CALC_CONSENSUS", true));
377     padGaps.setSelected(Cache.getDefault("PAD_GAPS", false));
378     sortByTree.setSelected(Cache.getDefault("SORT_BY_TREE", false));
379
380     annotations_actionPerformed(null); // update the display of the annotation
381                                        // settings
382   }
383
384   /**
385    * Save user selections on the Preferences tabs to the Cache and write out to
386    * file.
387    * 
388    * @param e
389    */
390   public void ok_actionPerformed(ActionEvent e)
391   {
392     if (!validateSettings())
393     {
394       return;
395     }
396
397     /*
398      * Save Visual settings
399      */
400     Cache.applicationProperties.setProperty("SHOW_JVSUFFIX",
401             Boolean.toString(seqLimit.isSelected()));
402     Cache.applicationProperties.setProperty("RIGHT_ALIGN_IDS",
403             Boolean.toString(rightAlign.isSelected()));
404     Cache.applicationProperties.setProperty("SHOW_FULLSCREEN",
405             Boolean.toString(fullScreen.isSelected()));
406     Cache.applicationProperties.setProperty("SHOW_OVERVIEW",
407             Boolean.toString(openoverv.isSelected()));
408     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
409             Boolean.toString(annotations.isSelected()));
410     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
411             Boolean.toString(conservation.isSelected()));
412     Cache.applicationProperties.setProperty("SHOW_QUALITY",
413             Boolean.toString(quality.isSelected()));
414     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
415             Boolean.toString(identity.isSelected()));
416
417     Cache.applicationProperties.setProperty("GAP_SYMBOL", gapSymbolCB
418             .getSelectedItem().toString());
419
420     Cache.applicationProperties.setProperty("FONT_NAME", fontNameCB
421             .getSelectedItem().toString());
422     Cache.applicationProperties.setProperty("FONT_STYLE", fontStyleCB
423             .getSelectedItem().toString());
424     Cache.applicationProperties.setProperty("FONT_SIZE", fontSizeCB
425             .getSelectedItem().toString());
426
427     Cache.applicationProperties.setProperty("ID_ITALICS",
428             Boolean.toString(idItalics.isSelected()));
429     Cache.applicationProperties.setProperty("SHOW_UNCONSERVED",
430             Boolean.toString(showUnconserved.isSelected()));
431     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSENSUS",
432             Boolean.toString(showGroupConsensus.isSelected()));
433     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSERVATION",
434             Boolean.toString(showGroupConservation.isSelected()));
435     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_HISTOGRAM",
436             Boolean.toString(showConsensHistogram.isSelected()));
437     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_LOGO",
438             Boolean.toString(showConsensLogo.isSelected()));
439     Cache.applicationProperties.setProperty("ANTI_ALIAS",
440             Boolean.toString(smoothFont.isSelected()));
441     Cache.applicationProperties.setProperty(SCALE_PROTEIN_TO_CDNA,
442             Boolean.toString(scaleProteinToCdna.isSelected()));
443     Cache.applicationProperties.setProperty("SHOW_NPFEATS_TOOLTIP",
444             Boolean.toString(showNpTooltip.isSelected()));
445     Cache.applicationProperties.setProperty("SHOW_DBREFS_TOOLTIP",
446             Boolean.toString(showDbRefTooltip.isSelected()));
447
448     Cache.applicationProperties.setProperty("WRAP_ALIGNMENT",
449             Boolean.toString(wrap.isSelected()));
450
451     Cache.applicationProperties.setProperty("STARTUP_FILE",
452             startupFileTextfield.getText());
453     Cache.applicationProperties.setProperty("SHOW_STARTUP_FILE",
454             Boolean.toString(startupCheckbox.isSelected()));
455
456     Cache.applicationProperties.setProperty("SORT_ALIGNMENT", sortby
457             .getSelectedItem().toString());
458
459     // convert description of sort order to enum name for save
460     SequenceAnnotationOrder annSortOrder = SequenceAnnotationOrder
461             .forDescription(sortAnnBy.getSelectedItem().toString());
462     if (annSortOrder != null)
463     {
464       Cache.applicationProperties.setProperty(SORT_ANNOTATIONS,
465               annSortOrder.name());
466     }
467
468     final boolean showAutocalcFirst = sortAutocalc.getSelectedIndex() == 0;
469     Cache.applicationProperties.setProperty(SHOW_AUTOCALC_ABOVE, Boolean
470             .valueOf(showAutocalcFirst).toString());
471
472     /*
473      * Save Colours settings
474      */
475     Cache.applicationProperties.setProperty(DEFAULT_COLOUR_PROT, protColour
476             .getSelectedItem().toString());
477     Cache.applicationProperties.setProperty(DEFAULT_COLOUR_NUC, nucColour
478             .getSelectedItem().toString());
479     Cache.setColourProperty("ANNOTATIONCOLOUR_MIN",
480             minColour.getBackground());
481     Cache.setColourProperty("ANNOTATIONCOLOUR_MAX",
482             maxColour.getBackground());
483
484     /*
485      * Save Structure settings
486      */
487     Cache.applicationProperties.setProperty(ADD_TEMPFACT_ANN,
488             Boolean.toString(addTempFactor.isSelected()));
489     Cache.applicationProperties.setProperty(ADD_SS_ANN,
490             Boolean.toString(addSecondaryStructure.isSelected()));
491     Cache.applicationProperties.setProperty(USE_RNAVIEW,
492             Boolean.toString(useRnaView.isSelected()));
493     Cache.applicationProperties.setProperty(STRUCT_FROM_PDB,
494             Boolean.toString(structFromPdb.isSelected()));
495     Cache.applicationProperties.setProperty(STRUCTURE_DISPLAY, structViewer
496             .getSelectedItem().toString());
497     Cache.setOrRemove(CHIMERA_PATH, chimeraPath.getText());
498
499     /*
500      * Save Output settings
501      */
502     if (epsRendering.getSelectedItem().equals("Prompt each time"))
503     {
504       Cache.applicationProperties.remove("EPS_RENDERING");
505     }
506     else
507     {
508       Cache.applicationProperties.setProperty("EPS_RENDERING", epsRendering
509               .getSelectedItem().toString());
510     }
511
512     /*
513      * Save Connections settings
514      */
515     Cache.setOrRemove("DEFAULT_BROWSER", defaultBrowser.getText());
516
517     BrowserLauncher.resetBrowser();
518
519     if (nameLinks.size() > 0)
520     {
521       StringBuffer links = new StringBuffer();
522       sequenceURLLinks = new ArrayList<String>();
523       for (int i = 0; i < nameLinks.size(); i++)
524       {
525         sequenceURLLinks.add(nameLinks.get(i) + "|"
526                 + urlLinks.get(i));
527         links.append(sequenceURLLinks.get(i).toString());
528         links.append("|");
529       }
530       // remove last "|"
531       links.setLength(links.length() - 1);
532       Cache.applicationProperties.setProperty("SEQUENCE_LINKS",
533               links.toString());
534     }
535     else
536     {
537       Cache.applicationProperties.remove("SEQUENCE_LINKS");
538     }
539
540     Cache.applicationProperties.setProperty("USE_PROXY",
541             Boolean.toString(useProxy.isSelected()));
542
543     Cache.setOrRemove("PROXY_SERVER", proxyServerTB.getText());
544
545     Cache.setOrRemove("PROXY_PORT", proxyPortTB.getText());
546
547     if (useProxy.isSelected())
548     {
549       System.setProperty("http.proxyHost", proxyServerTB.getText());
550       System.setProperty("http.proxyPort", proxyPortTB.getText());
551     }
552     else
553     {
554       System.setProperty("http.proxyHost", "");
555       System.setProperty("http.proxyPort", "");
556     }
557     Cache.setProperty("VERSION_CHECK",
558             Boolean.toString(versioncheck.isSelected()));
559     if (Cache.getProperty("USAGESTATS") != null || usagestats.isSelected())
560     {
561       // default is false - we only set this if the user has actively agreed
562       Cache.setProperty("USAGESTATS",
563               Boolean.toString(usagestats.isSelected()));
564     }
565     if (!questionnaire.isSelected())
566     {
567       Cache.setProperty("NOQUESTIONNAIRES", "true");
568     }
569     else
570     {
571       // special - made easy to edit a property file to disable questionnaires
572       // by just adding the given line
573       Cache.removeProperty("NOQUESTIONNAIRES");
574     }
575
576     /*
577      * Save Output settings
578      */
579     Cache.applicationProperties.setProperty("BLC_JVSUFFIX",
580             Boolean.toString(blcjv.isSelected()));
581     Cache.applicationProperties.setProperty("CLUSTAL_JVSUFFIX",
582             Boolean.toString(clustaljv.isSelected()));
583     Cache.applicationProperties.setProperty("FASTA_JVSUFFIX",
584             Boolean.toString(fastajv.isSelected()));
585     Cache.applicationProperties.setProperty("MEGA_JVSUFFIX",
586             Boolean.toString(megajv.isSelected()));
587     Cache.applicationProperties.setProperty("MSF_JVSUFFIX",
588             Boolean.toString(msfjv.isSelected()));
589     Cache.applicationProperties.setProperty("PFAM_JVSUFFIX",
590             Boolean.toString(pfamjv.isSelected()));
591     Cache.applicationProperties.setProperty("PILEUP_JVSUFFIX",
592             Boolean.toString(pileupjv.isSelected()));
593     Cache.applicationProperties.setProperty("PIR_JVSUFFIX",
594             Boolean.toString(pirjv.isSelected()));
595     Cache.applicationProperties.setProperty("PIR_MODELLER",
596             Boolean.toString(modellerOutput.isSelected()));
597     Cache.applicationProperties.setProperty("EXPORT_EMBBED_BIOJSON",
598             Boolean.toString(embbedBioJSON.isSelected()));
599     jalview.io.PIRFile.useModellerOutput = modellerOutput.isSelected();
600
601     Cache.applicationProperties.setProperty("FIGURE_AUTOIDWIDTH",
602             Boolean.toString(autoIdWidth.isSelected()));
603     userIdWidth_actionPerformed();
604     Cache.applicationProperties.setProperty("FIGURE_USERIDWIDTH",
605             userIdWidth.getText());
606
607     /*
608      * Save Editing settings
609      */
610     Cache.applicationProperties.setProperty("AUTO_CALC_CONSENSUS",
611             Boolean.toString(autoCalculateConsCheck.isSelected()));
612     Cache.applicationProperties.setProperty("SORT_BY_TREE",
613             Boolean.toString(sortByTree.isSelected()));
614     Cache.applicationProperties.setProperty("PAD_GAPS",
615             Boolean.toString(padGaps.isSelected()));
616
617     dasSource.saveProperties(Cache.applicationProperties);
618     wsPrefs.updateAndRefreshWsMenuConfig(false);
619     Cache.saveProperties();
620     Desktop.instance.doConfigureStructurePrefs();
621     try
622     {
623       frame.setClosed(true);
624     } catch (Exception ex)
625     {
626     }
627   }
628
629   /**
630    * Do any necessary validation before saving settings. Return focus to the
631    * first tab which fails validation.
632    * 
633    * @return
634    */
635   private boolean validateSettings()
636   {
637     if (!validateStructure())
638     {
639       structureTab.requestFocusInWindow();
640       return false;
641     }
642     return true;
643   }
644
645   @Override
646   protected boolean validateStructure()
647   {
648     return validateChimeraPath();
649
650   }
651
652   /**
653    * DOCUMENT ME!
654    */
655   public void startupFileTextfield_mouseClicked()
656   {
657     JalviewFileChooser chooser = new JalviewFileChooser(
658             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] {
659                 "fa, fasta, fastq", "aln", "pfam", "meg", "msf", "pir",
660                 "blc", "jar" }, new String[] { "Fasta", "Clustal", "PFAM",
661                 "MEGA", "MSF", "PIR", "BLC", "Jalview" },
662             jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
663     chooser.setFileView(new JalviewFileView());
664     chooser.setDialogTitle(MessageManager
665             .getString("label.select_startup_file"));
666
667     int value = chooser.showOpenDialog(this);
668
669     if (value == JalviewFileChooser.APPROVE_OPTION)
670     {
671       jalview.bin.Cache.applicationProperties.setProperty(
672               "DEFAULT_FILE_FORMAT", chooser.getSelectedFormat());
673       startupFileTextfield.setText(chooser.getSelectedFile()
674               .getAbsolutePath());
675     }
676   }
677
678   /**
679    * DOCUMENT ME!
680    * 
681    * @param e
682    *          DOCUMENT ME!
683    */
684   public void cancel_actionPerformed(ActionEvent e)
685   {
686     try
687     {
688       wsPrefs.updateWsMenuConfig(true);
689       wsPrefs.refreshWs_actionPerformed(e);
690       frame.setClosed(true);
691     } catch (Exception ex)
692     {
693     }
694   }
695
696   /**
697    * DOCUMENT ME!
698    * 
699    * @param e
700    *          DOCUMENT ME!
701    */
702   public void annotations_actionPerformed(ActionEvent e)
703   {
704     conservation.setEnabled(annotations.isSelected());
705     quality.setEnabled(annotations.isSelected());
706     identity.setEnabled(annotations.isSelected());
707     showGroupConsensus.setEnabled(annotations.isSelected());
708     showGroupConservation.setEnabled(annotations.isSelected());
709     showConsensHistogram.setEnabled(annotations.isSelected()
710             && (identity.isSelected() || showGroupConsensus.isSelected()));
711     showConsensLogo.setEnabled(annotations.isSelected()
712             && (identity.isSelected() || showGroupConsensus.isSelected()));
713   }
714
715   public void newLink_actionPerformed(ActionEvent e)
716   {
717
718     GSequenceLink link = new GSequenceLink();
719     boolean valid = false;
720     while (!valid)
721     {
722       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
723               MessageManager.getString("label.new_sequence_url_link"),
724               JOptionPane.OK_CANCEL_OPTION, -1, null) == JOptionPane.OK_OPTION)
725       {
726         if (link.checkValid())
727         {
728           nameLinks.add(link.getName());
729           urlLinks.add(link.getURL());
730           updateLinkData();
731           valid = true;
732         }
733       }
734       else
735       {
736         break;
737       }
738     }
739   }
740
741   public void editLink_actionPerformed(ActionEvent e)
742   {
743     GSequenceLink link = new GSequenceLink();
744
745     int index = linkNameList.getSelectedIndex();
746     if (index == -1)
747     {
748       JOptionPane.showInternalMessageDialog(Desktop.desktop,
749               MessageManager.getString("label.no_link_selected"),
750               MessageManager.getString("label.no_link_selected"),
751               JOptionPane.WARNING_MESSAGE);
752       return;
753     }
754
755     link.setName(nameLinks.get(index).toString());
756     link.setURL(urlLinks.get(index).toString());
757
758     boolean valid = false;
759     while (!valid)
760     {
761
762       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
763               MessageManager.getString("label.new_sequence_url_link"),
764               JOptionPane.OK_CANCEL_OPTION, -1, null) == JOptionPane.OK_OPTION)
765       {
766         if (link.checkValid())
767         {
768           nameLinks.set(index, link.getName());
769           urlLinks.set(index, link.getURL());
770           updateLinkData();
771           valid = true;
772         }
773       }
774
775       else
776       {
777         break;
778       }
779     }
780   }
781
782   public void deleteLink_actionPerformed(ActionEvent e)
783   {
784     int index = linkNameList.getSelectedIndex();
785     if (index == -1)
786     {
787       JOptionPane.showInternalMessageDialog(Desktop.desktop,
788               MessageManager.getString("label.no_link_selected"),
789               MessageManager.getString("label.no_link_selected"),
790               JOptionPane.WARNING_MESSAGE);
791       return;
792     }
793     nameLinks.remove(index);
794     urlLinks.remove(index);
795     updateLinkData();
796   }
797
798   void updateLinkData()
799   {
800     linkNameList.setListData(nameLinks.toArray(new String[nameLinks.size()]));
801     linkURLList.setListData(urlLinks.toArray(new String[urlLinks.size()]));
802   }
803
804   public void defaultBrowser_mouseClicked(MouseEvent e)
805   {
806     JFileChooser chooser = new JFileChooser(".");
807     chooser.setDialogTitle(MessageManager
808             .getString("label.select_default_browser"));
809
810     int value = chooser.showOpenDialog(this);
811
812     if (value == JFileChooser.APPROVE_OPTION)
813     {
814       defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());
815     }
816
817   }
818
819   public static List<String> getGroupURLLinks()
820   {
821     return groupURLLinks;
822   }
823
824   public static List<String> getSequenceURLLinks()
825   {
826     return sequenceURLLinks;
827   }
828
829   @Override
830   public void minColour_actionPerformed(JPanel panel)
831   {
832     Color col = JColorChooser.showDialog(this,
833             MessageManager.getString("label.select_colour_minimum_value"),
834             minColour.getBackground());
835     if (col != null)
836     {
837       panel.setBackground(col);
838     }
839     panel.repaint();
840   }
841
842   @Override
843   public void maxColour_actionPerformed(JPanel panel)
844   {
845     Color col = JColorChooser.showDialog(this,
846             MessageManager.getString("label.select_colour_maximum_value"),
847             maxColour.getBackground());
848     if (col != null)
849     {
850       panel.setBackground(col);
851     }
852     panel.repaint();
853   }
854
855   @Override
856   protected void userIdWidth_actionPerformed()
857   {
858     try
859     {
860       String val = userIdWidth.getText().trim();
861       if (val.length() > 0)
862       {
863         Integer iw = Integer.parseInt(val);
864         if (iw.intValue() < 12)
865         {
866           throw new NumberFormatException();
867         }
868         userIdWidth.setText(iw.toString());
869       }
870     } catch (NumberFormatException x)
871     {
872       JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
873               .getString("warn.user_defined_width_requirements"),
874               MessageManager.getString("label.invalid_id_column_width"),
875               JOptionPane.WARNING_MESSAGE);
876       userIdWidth.setText("");
877     }
878   }
879
880   @Override
881   protected void autoIdWidth_actionPerformed()
882   {
883     userIdWidth.setEnabled(!autoIdWidth.isSelected());
884     userIdWidthlabel.setEnabled(!autoIdWidth.isSelected());
885   }
886
887   /**
888    * Returns true if chimera path is to a valid executable, else show an error
889    * dialog.
890    */
891   private boolean validateChimeraPath()
892   {
893     if (chimeraPath.getText().trim().length() > 0)
894     {
895       File f = new File(chimeraPath.getText());
896       if (!f.canExecute())
897       {
898         JOptionPane.showInternalMessageDialog(Desktop.desktop,
899                 MessageManager.getString("label.invalid_chimera_path"),
900                 MessageManager.getString("label.invalid_name"),
901                 JOptionPane.ERROR_MESSAGE);
902         return false;
903       }
904     }
905     return true;
906   }
907
908   /**
909    * If Chimera is selected, check it can be found on default or user-specified
910    * path, if not show a warning/help dialog.
911    */
912   @Override
913   protected void structureViewer_actionPerformed(String selectedItem)
914   {
915     if (!selectedItem.equals(ViewerType.CHIMERA.name()))
916     {
917       return;
918     }
919     boolean found = false;
920
921     /*
922      * Try user-specified and standard paths for Chimera executable.
923      */
924     List<String> paths = StructureManager.getChimeraPaths();
925     paths.add(0, chimeraPath.getText());
926     for (String path : paths)
927     {
928       if (new File(path.trim()).canExecute())
929       {
930         found = true;
931         break;
932       }
933     }
934     if (!found)
935     {
936       String[] options = { "OK", "Help" };
937       int showHelp = JOptionPane.showInternalOptionDialog(
938               Desktop.desktop,
939               JvSwingUtils.wrapTooltip(true,
940                       MessageManager.getString("label.chimera_missing")),
941               "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
942               null, options, options[0]);
943       if (showHelp == JOptionPane.NO_OPTION)
944       {
945         try
946         {
947           Help.showHelpWindow(HelpId.StructureViewer);
948         } catch (HelpSetException e)
949         {
950           e.printStackTrace();
951         }
952       }
953     }
954   }
955
956 }