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