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