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