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