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