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