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