JAL-1911 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   /**
168    * Creates a new Preferences object.
169    */
170   public Preferences()
171   {
172     super();
173     frame = new JInternalFrame();
174     frame.setContentPane(this);
175     dasSource = new DasSourceBrowser();
176     dasTab.add(dasSource, BorderLayout.CENTER);
177     wsPrefs = new WsPreferences();
178     wsTab.add(wsPrefs, BorderLayout.CENTER);
179     int width = 500, height = 450;
180     new jalview.util.Platform();
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     if (Cache.getDefault("MAP_WITH_SIFTS", false))
320     {
321       siftsMapping.setSelected(true);
322     }
323     else
324     {
325       nwMapping.setSelected(true);
326     }
327
328     SiftsSettings
329             .setMapWithSifts(Cache.getDefault("MAP_WITH_SIFTS", false));
330
331     /*
332      * Set Connections tab defaults
333      */
334     nameLinks = new Vector<String>();
335     urlLinks = new Vector<String>();
336     for (int i = 0; i < sequenceURLLinks.size(); i++)
337     {
338       String link = sequenceURLLinks.elementAt(i).toString();
339       nameLinks.addElement(link.substring(0, link.indexOf("|")));
340       urlLinks.addElement(link.substring(link.indexOf("|") + 1));
341     }
342
343     updateLinkData();
344
345     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
346     proxyServerTB.setEnabled(useProxy.isSelected());
347     proxyPortTB.setEnabled(useProxy.isSelected());
348     proxyServerTB.setText(Cache.getDefault("PROXY_SERVER", ""));
349     proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));
350
351     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
352
353     usagestats.setSelected(Cache.getDefault("USAGESTATS", false));
354     // note antisense here: default is true
355     questionnaire
356             .setSelected(Cache.getProperty("NOQUESTIONNAIRES") == null);
357     versioncheck.setSelected(Cache.getDefault("VERSION_CHECK", true));
358
359     /*
360      * Set Output tab defaults
361      */
362     epsRendering.addItem(new OptionsParam(MessageManager
363             .getString("label.prompt_each_time"), "Prompt each time"));
364     epsRendering.addItem(new OptionsParam(MessageManager
365             .getString("label.lineart"), "Lineart"));
366     epsRendering.addItem(new OptionsParam(MessageManager
367             .getString("action.text"), "Text"));
368     String defaultEPS = Cache.getDefault("EPS_RENDERING",
369             "Prompt each time");
370     if (defaultEPS.equalsIgnoreCase("Text"))
371     {
372       epsRendering.setSelectedItem(new OptionsParam(MessageManager
373               .getString("action.text"), "Text"));
374     }
375     else if (defaultEPS.equalsIgnoreCase("Lineart"))
376     {
377       epsRendering.setSelectedItem(new OptionsParam(MessageManager
378               .getString("label.lineart"), "Lineart"));
379     }
380     else
381     {
382       epsRendering.setSelectedItem(new OptionsParam(MessageManager
383               .getString("label.prompt_each_time"), "Prompt each time"));
384     }
385     autoIdWidth.setSelected(Cache.getDefault("FIGURE_AUTOIDWIDTH", false));
386     userIdWidth.setEnabled(!autoIdWidth.isSelected());
387     userIdWidthlabel.setEnabled(!autoIdWidth.isSelected());
388     Integer wi = Cache.getIntegerProperty("FIGURE_USERIDWIDTH");
389     userIdWidth.setText(wi == null ? "" : wi.toString());
390     blcjv.setSelected(Cache.getDefault("BLC_JVSUFFIX", true));
391     clustaljv.setSelected(Cache.getDefault("CLUSTAL_JVSUFFIX", true));
392     fastajv.setSelected(Cache.getDefault("FASTA_JVSUFFIX", true));
393     msfjv.setSelected(Cache.getDefault("MSF_JVSUFFIX", true));
394     pfamjv.setSelected(Cache.getDefault("PFAM_JVSUFFIX", true));
395     pileupjv.setSelected(Cache.getDefault("PILEUP_JVSUFFIX", true));
396     pirjv.setSelected(Cache.getDefault("PIR_JVSUFFIX", true));
397     modellerOutput.setSelected(Cache.getDefault("PIR_MODELLER", false));
398     embbedBioJSON.setSelected(Cache.getDefault("EXPORT_EMBBED_BIOJSON",
399             true));
400
401     /*
402      * Set Editing tab defaults
403      */
404     autoCalculateConsCheck.setSelected(Cache.getDefault(
405             "AUTO_CALC_CONSENSUS", true));
406     padGaps.setSelected(Cache.getDefault("PAD_GAPS", false));
407     sortByTree.setSelected(Cache.getDefault("SORT_BY_TREE", false));
408
409     annotations_actionPerformed(null); // update the display of the annotation
410                                        // settings
411   }
412
413   /**
414    * Save user selections on the Preferences tabs to the Cache and write out to
415    * file.
416    * 
417    * @param e
418    */
419   @Override
420   public void ok_actionPerformed(ActionEvent e)
421   {
422     if (!validateSettings())
423     {
424       return;
425     }
426
427     /*
428      * Save Visual settings
429      */
430     Cache.applicationProperties.setProperty("SHOW_JVSUFFIX",
431             Boolean.toString(seqLimit.isSelected()));
432     Cache.applicationProperties.setProperty("RIGHT_ALIGN_IDS",
433             Boolean.toString(rightAlign.isSelected()));
434     Cache.applicationProperties.setProperty("SHOW_FULLSCREEN",
435             Boolean.toString(fullScreen.isSelected()));
436     Cache.applicationProperties.setProperty("SHOW_OVERVIEW",
437             Boolean.toString(openoverv.isSelected()));
438     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
439             Boolean.toString(annotations.isSelected()));
440     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
441             Boolean.toString(conservation.isSelected()));
442     Cache.applicationProperties.setProperty("SHOW_QUALITY",
443             Boolean.toString(quality.isSelected()));
444     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
445             Boolean.toString(identity.isSelected()));
446
447     Cache.applicationProperties.setProperty("GAP_SYMBOL", gapSymbolCB
448             .getSelectedItem().toString());
449
450     Cache.applicationProperties.setProperty("FONT_NAME", fontNameCB
451             .getSelectedItem().toString());
452     Cache.applicationProperties.setProperty("FONT_STYLE", fontStyleCB
453             .getSelectedItem().toString());
454     Cache.applicationProperties.setProperty("FONT_SIZE", fontSizeCB
455             .getSelectedItem().toString());
456
457     Cache.applicationProperties.setProperty("ID_ITALICS",
458             Boolean.toString(idItalics.isSelected()));
459     Cache.applicationProperties.setProperty("SHOW_UNCONSERVED",
460             Boolean.toString(showUnconserved.isSelected()));
461     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSENSUS",
462             Boolean.toString(showGroupConsensus.isSelected()));
463     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSERVATION",
464             Boolean.toString(showGroupConservation.isSelected()));
465     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_HISTOGRAM",
466             Boolean.toString(showConsensHistogram.isSelected()));
467     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_LOGO",
468             Boolean.toString(showConsensLogo.isSelected()));
469     Cache.applicationProperties.setProperty("ANTI_ALIAS",
470             Boolean.toString(smoothFont.isSelected()));
471     Cache.applicationProperties.setProperty(SCALE_PROTEIN_TO_CDNA,
472             Boolean.toString(scaleProteinToCdna.isSelected()));
473     Cache.applicationProperties.setProperty("SHOW_NPFEATS_TOOLTIP",
474             Boolean.toString(showNpTooltip.isSelected()));
475     Cache.applicationProperties.setProperty("SHOW_DBREFS_TOOLTIP",
476             Boolean.toString(showDbRefTooltip.isSelected()));
477
478     Cache.applicationProperties.setProperty("WRAP_ALIGNMENT",
479             Boolean.toString(wrap.isSelected()));
480
481     Cache.applicationProperties.setProperty("STARTUP_FILE",
482             startupFileTextfield.getText());
483     Cache.applicationProperties.setProperty("SHOW_STARTUP_FILE",
484             Boolean.toString(startupCheckbox.isSelected()));
485
486     Cache.applicationProperties.setProperty("SORT_ALIGNMENT", sortby
487             .getSelectedItem().toString());
488
489     // convert description of sort order to enum name for save
490     SequenceAnnotationOrder annSortOrder = SequenceAnnotationOrder
491             .forDescription(sortAnnBy.getSelectedItem().toString());
492     if (annSortOrder != null)
493     {
494       Cache.applicationProperties.setProperty(SORT_ANNOTATIONS,
495               annSortOrder.name());
496     }
497
498     final boolean showAutocalcFirst = sortAutocalc.getSelectedIndex() == 0;
499     Cache.applicationProperties.setProperty(SHOW_AUTOCALC_ABOVE, Boolean
500             .valueOf(showAutocalcFirst).toString());
501
502     /*
503      * Save Colours settings
504      */
505     Cache.applicationProperties.setProperty(DEFAULT_COLOUR_PROT, protColour
506             .getSelectedItem().toString());
507     Cache.applicationProperties.setProperty(DEFAULT_COLOUR_NUC, nucColour
508             .getSelectedItem().toString());
509     Cache.setColourProperty("ANNOTATIONCOLOUR_MIN",
510             minColour.getBackground());
511     Cache.setColourProperty("ANNOTATIONCOLOUR_MAX",
512             maxColour.getBackground());
513
514     /*
515      * Save Structure settings
516      */
517     Cache.applicationProperties.setProperty(ADD_TEMPFACT_ANN,
518             Boolean.toString(addTempFactor.isSelected()));
519     Cache.applicationProperties.setProperty(ADD_SS_ANN,
520             Boolean.toString(addSecondaryStructure.isSelected()));
521     Cache.applicationProperties.setProperty(USE_RNAVIEW,
522             Boolean.toString(useRnaView.isSelected()));
523     Cache.applicationProperties.setProperty(STRUCT_FROM_PDB,
524             Boolean.toString(structFromPdb.isSelected()));
525     Cache.applicationProperties.setProperty(STRUCTURE_DISPLAY, structViewer
526             .getSelectedItem().toString());
527     Cache.setOrRemove(CHIMERA_PATH, chimeraPath.getText());
528     Cache.applicationProperties.setProperty("MAP_WITH_SIFTS",
529             Boolean.toString(siftsMapping.isSelected()));
530     SiftsSettings.setMapWithSifts(siftsMapping.isSelected());
531
532     /*
533      * Save Output settings
534      */
535     if (epsRendering.getSelectedItem().equals("Prompt each time"))
536     {
537       Cache.applicationProperties.remove("EPS_RENDERING");
538     }
539     else
540     {
541       Cache.applicationProperties.setProperty("EPS_RENDERING",
542               ((OptionsParam) epsRendering.getSelectedItem()).getCode());
543     }
544
545     /*
546      * Save Connections settings
547      */
548     Cache.setOrRemove("DEFAULT_BROWSER", defaultBrowser.getText());
549
550     jalview.util.BrowserLauncher.resetBrowser();
551
552     if (nameLinks.size() > 0)
553     {
554       StringBuffer links = new StringBuffer();
555       sequenceURLLinks = new Vector<String>();
556       for (int i = 0; i < nameLinks.size(); i++)
557       {
558         sequenceURLLinks.addElement(nameLinks.elementAt(i) + "|"
559                 + urlLinks.elementAt(i));
560         links.append(sequenceURLLinks.elementAt(i).toString());
561         links.append("|");
562       }
563       // remove last "|"
564       links.setLength(links.length() - 1);
565       Cache.applicationProperties.setProperty("SEQUENCE_LINKS",
566               links.toString());
567     }
568     else
569     {
570       Cache.applicationProperties.remove("SEQUENCE_LINKS");
571     }
572
573     Cache.applicationProperties.setProperty("USE_PROXY",
574             Boolean.toString(useProxy.isSelected()));
575
576     Cache.setOrRemove("PROXY_SERVER", proxyServerTB.getText());
577
578     Cache.setOrRemove("PROXY_PORT", proxyPortTB.getText());
579
580     if (useProxy.isSelected())
581     {
582       System.setProperty("http.proxyHost", proxyServerTB.getText());
583       System.setProperty("http.proxyPort", proxyPortTB.getText());
584     }
585     else
586     {
587       System.setProperty("http.proxyHost", "");
588       System.setProperty("http.proxyPort", "");
589     }
590     Cache.setProperty("VERSION_CHECK",
591             Boolean.toString(versioncheck.isSelected()));
592     if (Cache.getProperty("USAGESTATS") != null || usagestats.isSelected())
593     {
594       // default is false - we only set this if the user has actively agreed
595       Cache.setProperty("USAGESTATS",
596               Boolean.toString(usagestats.isSelected()));
597     }
598     if (!questionnaire.isSelected())
599     {
600       Cache.setProperty("NOQUESTIONNAIRES", "true");
601     }
602     else
603     {
604       // special - made easy to edit a property file to disable questionnaires
605       // by just adding the given line
606       Cache.removeProperty("NOQUESTIONNAIRES");
607     }
608
609     /*
610      * Save Output settings
611      */
612     Cache.applicationProperties.setProperty("BLC_JVSUFFIX",
613             Boolean.toString(blcjv.isSelected()));
614     Cache.applicationProperties.setProperty("CLUSTAL_JVSUFFIX",
615             Boolean.toString(clustaljv.isSelected()));
616     Cache.applicationProperties.setProperty("FASTA_JVSUFFIX",
617             Boolean.toString(fastajv.isSelected()));
618     Cache.applicationProperties.setProperty("MSF_JVSUFFIX",
619             Boolean.toString(msfjv.isSelected()));
620     Cache.applicationProperties.setProperty("PFAM_JVSUFFIX",
621             Boolean.toString(pfamjv.isSelected()));
622     Cache.applicationProperties.setProperty("PILEUP_JVSUFFIX",
623             Boolean.toString(pileupjv.isSelected()));
624     Cache.applicationProperties.setProperty("PIR_JVSUFFIX",
625             Boolean.toString(pirjv.isSelected()));
626     Cache.applicationProperties.setProperty("PIR_MODELLER",
627             Boolean.toString(modellerOutput.isSelected()));
628     Cache.applicationProperties.setProperty("EXPORT_EMBBED_BIOJSON",
629             Boolean.toString(embbedBioJSON.isSelected()));
630     jalview.io.PIRFile.useModellerOutput = modellerOutput.isSelected();
631
632     Cache.applicationProperties.setProperty("FIGURE_AUTOIDWIDTH",
633             Boolean.toString(autoIdWidth.isSelected()));
634     userIdWidth_actionPerformed();
635     Cache.applicationProperties.setProperty("FIGURE_USERIDWIDTH",
636             userIdWidth.getText());
637
638     /*
639      * Save Editing settings
640      */
641     Cache.applicationProperties.setProperty("AUTO_CALC_CONSENSUS",
642             Boolean.toString(autoCalculateConsCheck.isSelected()));
643     Cache.applicationProperties.setProperty("SORT_BY_TREE",
644             Boolean.toString(sortByTree.isSelected()));
645     Cache.applicationProperties.setProperty("PAD_GAPS",
646             Boolean.toString(padGaps.isSelected()));
647
648     dasSource.saveProperties(Cache.applicationProperties);
649     wsPrefs.updateAndRefreshWsMenuConfig(false);
650     Cache.saveProperties();
651     Desktop.instance.doConfigureStructurePrefs();
652     try
653     {
654       frame.setClosed(true);
655     } catch (Exception ex)
656     {
657     }
658   }
659
660   /**
661    * Do any necessary validation before saving settings. Return focus to the
662    * first tab which fails validation.
663    * 
664    * @return
665    */
666   private boolean validateSettings()
667   {
668     if (!validateStructure())
669     {
670       structureTab.requestFocusInWindow();
671       return false;
672     }
673     return true;
674   }
675
676   @Override
677   protected boolean validateStructure()
678   {
679     return validateChimeraPath();
680
681   }
682
683   /**
684    * DOCUMENT ME!
685    */
686   @Override
687   public void startupFileTextfield_mouseClicked()
688   {
689     JalviewFileChooser chooser = new JalviewFileChooser(
690             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] {
691                 "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
692                 "jar" }, new String[] { "Fasta", "Clustal", "PFAM", "MSF",
693                 "PIR", "BLC", "Jalview" },
694             jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
695     chooser.setFileView(new JalviewFileView());
696     chooser.setDialogTitle(MessageManager
697             .getString("label.select_startup_file"));
698
699     int value = chooser.showOpenDialog(this);
700
701     if (value == JalviewFileChooser.APPROVE_OPTION)
702     {
703       jalview.bin.Cache.applicationProperties.setProperty(
704               "DEFAULT_FILE_FORMAT", chooser.getSelectedFormat());
705       startupFileTextfield.setText(chooser.getSelectedFile()
706               .getAbsolutePath());
707     }
708   }
709
710   /**
711    * DOCUMENT ME!
712    * 
713    * @param e
714    *          DOCUMENT ME!
715    */
716   @Override
717   public void cancel_actionPerformed(ActionEvent e)
718   {
719     try
720     {
721       wsPrefs.updateWsMenuConfig(true);
722       wsPrefs.refreshWs_actionPerformed(e);
723       frame.setClosed(true);
724     } catch (Exception ex)
725     {
726     }
727   }
728
729   /**
730    * DOCUMENT ME!
731    * 
732    * @param e
733    *          DOCUMENT ME!
734    */
735   @Override
736   public void annotations_actionPerformed(ActionEvent e)
737   {
738     conservation.setEnabled(annotations.isSelected());
739     quality.setEnabled(annotations.isSelected());
740     identity.setEnabled(annotations.isSelected());
741     showGroupConsensus.setEnabled(annotations.isSelected());
742     showGroupConservation.setEnabled(annotations.isSelected());
743     showConsensHistogram.setEnabled(annotations.isSelected()
744             && (identity.isSelected() || showGroupConsensus.isSelected()));
745     showConsensLogo.setEnabled(annotations.isSelected()
746             && (identity.isSelected() || showGroupConsensus.isSelected()));
747   }
748
749   @Override
750   public void newLink_actionPerformed(ActionEvent e)
751   {
752
753     GSequenceLink link = new GSequenceLink();
754     boolean valid = false;
755     while (!valid)
756     {
757       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
758               MessageManager.getString("label.new_sequence_url_link"),
759               JOptionPane.OK_CANCEL_OPTION, -1, null) == JOptionPane.OK_OPTION)
760       {
761         if (link.checkValid())
762         {
763           nameLinks.addElement(link.getName());
764           urlLinks.addElement(link.getURL());
765           updateLinkData();
766           valid = true;
767         }
768       }
769       else
770       {
771         break;
772       }
773     }
774   }
775
776   @Override
777   public void editLink_actionPerformed(ActionEvent e)
778   {
779     GSequenceLink link = new GSequenceLink();
780
781     int index = linkNameList.getSelectedIndex();
782     if (index == -1)
783     {
784       JOptionPane.showInternalMessageDialog(Desktop.desktop,
785               MessageManager.getString("label.no_link_selected"),
786               MessageManager.getString("label.no_link_selected"),
787               JOptionPane.WARNING_MESSAGE);
788       return;
789     }
790
791     link.setName(nameLinks.elementAt(index).toString());
792     link.setURL(urlLinks.elementAt(index).toString());
793
794     boolean valid = false;
795     while (!valid)
796     {
797
798       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
799               MessageManager.getString("label.new_sequence_url_link"),
800               JOptionPane.OK_CANCEL_OPTION, -1, null) == JOptionPane.OK_OPTION)
801       {
802         if (link.checkValid())
803         {
804           nameLinks.setElementAt(link.getName(), index);
805           urlLinks.setElementAt(link.getURL(), index);
806           updateLinkData();
807           valid = true;
808         }
809       }
810
811       else
812       {
813         break;
814       }
815     }
816   }
817
818   @Override
819   public void deleteLink_actionPerformed(ActionEvent e)
820   {
821     int index = linkNameList.getSelectedIndex();
822     if (index == -1)
823     {
824       JOptionPane.showInternalMessageDialog(Desktop.desktop,
825               MessageManager.getString("label.no_link_selected"),
826               MessageManager.getString("label.no_link_selected"),
827               JOptionPane.WARNING_MESSAGE);
828       return;
829     }
830     nameLinks.removeElementAt(index);
831     urlLinks.removeElementAt(index);
832     updateLinkData();
833   }
834
835   void updateLinkData()
836   {
837     linkNameList.setListData(nameLinks);
838     linkURLList.setListData(urlLinks);
839   }
840
841   @Override
842   public void defaultBrowser_mouseClicked(MouseEvent e)
843   {
844     JFileChooser chooser = new JFileChooser(".");
845     chooser.setDialogTitle(MessageManager
846             .getString("label.select_default_browser"));
847
848     int value = chooser.showOpenDialog(this);
849
850     if (value == JFileChooser.APPROVE_OPTION)
851     {
852       defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());
853     }
854
855   }
856
857   /*
858    * (non-Javadoc)
859    * 
860    * @see
861    * jalview.jbgui.GPreferences#showunconserved_actionPerformed(java.awt.event
862    * .ActionEvent)
863    */
864   @Override
865   protected void showunconserved_actionPerformed(ActionEvent e)
866   {
867     // TODO Auto-generated method stub
868     super.showunconserved_actionPerformed(e);
869   }
870
871   public static List<String> getGroupURLLinks()
872   {
873     return groupURLLinks;
874   }
875
876   @Override
877   public void minColour_actionPerformed(JPanel panel)
878   {
879     Color col = JColorChooser.showDialog(this,
880             MessageManager.getString("label.select_colour_minimum_value"),
881             minColour.getBackground());
882     if (col != null)
883     {
884       panel.setBackground(col);
885     }
886     panel.repaint();
887   }
888
889   @Override
890   public void maxColour_actionPerformed(JPanel panel)
891   {
892     Color col = JColorChooser.showDialog(this,
893             MessageManager.getString("label.select_colour_maximum_value"),
894             maxColour.getBackground());
895     if (col != null)
896     {
897       panel.setBackground(col);
898     }
899     panel.repaint();
900   }
901
902   @Override
903   protected void userIdWidth_actionPerformed()
904   {
905     try
906     {
907       String val = userIdWidth.getText().trim();
908       if (val.length() > 0)
909       {
910         Integer iw = Integer.parseInt(val);
911         if (iw.intValue() < 12)
912         {
913           throw new NumberFormatException();
914         }
915         userIdWidth.setText(iw.toString());
916       }
917     } catch (NumberFormatException x)
918     {
919       JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
920               .getString("warn.user_defined_width_requirements"),
921               MessageManager.getString("label.invalid_id_column_width"),
922               JOptionPane.WARNING_MESSAGE);
923       userIdWidth.setText("");
924     }
925   }
926
927   @Override
928   protected void autoIdWidth_actionPerformed()
929   {
930     userIdWidth.setEnabled(!autoIdWidth.isSelected());
931     userIdWidthlabel.setEnabled(!autoIdWidth.isSelected());
932   }
933
934   /**
935    * Returns true if chimera path is to a valid executable, else show an error
936    * dialog.
937    */
938   private boolean validateChimeraPath()
939   {
940     if (chimeraPath.getText().trim().length() > 0)
941     {
942       File f = new File(chimeraPath.getText());
943       if (!f.canExecute())
944       {
945         JOptionPane.showInternalMessageDialog(Desktop.desktop,
946                 MessageManager.getString("label.invalid_chimera_path"),
947                 MessageManager.getString("label.invalid_name"),
948                 JOptionPane.ERROR_MESSAGE);
949         return false;
950       }
951     }
952     return true;
953   }
954
955   /**
956    * If Chimera is selected, check it can be found on default or user-specified
957    * path, if not show a warning/help dialog.
958    */
959   @Override
960   protected void structureViewer_actionPerformed(String selectedItem)
961   {
962     if (!selectedItem.equals(ViewerType.CHIMERA.name()))
963     {
964       return;
965     }
966     boolean found = false;
967
968     /*
969      * Try user-specified and standard paths for Chimera executable.
970      */
971     List<String> paths = StructureManager.getChimeraPaths();
972     paths.add(0, chimeraPath.getText());
973     for (String path : paths)
974     {
975       if (new File(path.trim()).canExecute())
976       {
977         found = true;
978         break;
979       }
980     }
981     if (!found)
982     {
983       String[] options = { "OK", "Help" };
984       int showHelp = JOptionPane.showInternalOptionDialog(
985               Desktop.desktop,
986               JvSwingUtils.wrapTooltip(true,
987                       MessageManager.getString("label.chimera_missing")),
988               "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
989               null, options, options[0]);
990       if (showHelp == JOptionPane.NO_OPTION)
991       {
992         try
993         {
994           Help.showHelpWindow(HelpId.StructureViewer);
995         } catch (HelpSetException e)
996         {
997           e.printStackTrace();
998         }
999       }
1000     }
1001   }
1002
1003   public class OptionsParam
1004   {
1005     private String name;
1006
1007     private String code;
1008
1009     public OptionsParam(String name, String code)
1010     {
1011       this.name = name;
1012       this.code = code;
1013     }
1014
1015     public String getName()
1016     {
1017       return name;
1018     }
1019
1020     public void setName(String name)
1021     {
1022       this.name = name;
1023     }
1024
1025     public String getCode()
1026     {
1027       return code;
1028     }
1029
1030     public void setCode(String code)
1031     {
1032       this.code = code;
1033     }
1034
1035     @Override
1036     public String toString()
1037     {
1038       return name;
1039     }
1040
1041     @Override
1042     public boolean equals(Object that)
1043     {
1044       if (!(that instanceof OptionsParam))
1045       {
1046         return false;
1047       }
1048       return this.code.equalsIgnoreCase(((OptionsParam) that).code);
1049     }
1050
1051     @Override
1052     public int hashCode(){
1053       return name.hashCode() + code.hashCode();
1054     }
1055   }
1056 }