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