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