JAL-2937 Cygwin path preference, method refactoring
[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.hmmer.HmmerCommand;
28 import jalview.io.FileFormatI;
29 import jalview.io.JalviewFileChooser;
30 import jalview.io.JalviewFileView;
31 import jalview.jbgui.GPreferences;
32 import jalview.jbgui.GSequenceLink;
33 import jalview.schemes.ColourSchemeI;
34 import jalview.schemes.ColourSchemes;
35 import jalview.schemes.ResidueColourScheme;
36 import jalview.urls.UrlLinkTableModel;
37 import jalview.urls.api.UrlProviderFactoryI;
38 import jalview.urls.api.UrlProviderI;
39 import jalview.urls.desktop.DesktopUrlProviderFactory;
40 import jalview.util.FileUtils;
41 import jalview.util.MessageManager;
42 import jalview.util.Platform;
43 import jalview.util.UrlConstants;
44 import jalview.ws.sifts.SiftsSettings;
45
46 import java.awt.BorderLayout;
47 import java.awt.Color;
48 import java.awt.Component;
49 import java.awt.Dimension;
50 import java.awt.Font;
51 import java.awt.event.ActionEvent;
52 import java.awt.event.ActionListener;
53 import java.awt.event.FocusAdapter;
54 import java.awt.event.FocusEvent;
55 import java.awt.event.MouseEvent;
56 import java.io.File;
57 import java.util.ArrayList;
58 import java.util.List;
59
60 import javax.help.HelpSetException;
61 import javax.swing.JColorChooser;
62 import javax.swing.JFileChooser;
63 import javax.swing.JInternalFrame;
64 import javax.swing.JPanel;
65 import javax.swing.JTextField;
66 import javax.swing.ListSelectionModel;
67 import javax.swing.RowFilter;
68 import javax.swing.RowSorter;
69 import javax.swing.SortOrder;
70 import javax.swing.event.DocumentEvent;
71 import javax.swing.event.DocumentListener;
72 import javax.swing.event.ListSelectionEvent;
73 import javax.swing.event.ListSelectionListener;
74 import javax.swing.table.TableCellRenderer;
75 import javax.swing.table.TableColumn;
76 import javax.swing.table.TableModel;
77 import javax.swing.table.TableRowSorter;
78
79 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
80
81 /**
82  * DOCUMENT ME!
83  * 
84  * @author $author$
85  * @version $Revision$
86  */
87 public class Preferences extends GPreferences
88 {
89   public static final String ENABLE_SPLIT_FRAME = "ENABLE_SPLIT_FRAME";
90
91   public static final String SCALE_PROTEIN_TO_CDNA = "SCALE_PROTEIN_TO_CDNA";
92
93   public static final String DEFAULT_COLOUR = "DEFAULT_COLOUR";
94
95   public static final String DEFAULT_COLOUR_PROT = "DEFAULT_COLOUR_PROT";
96
97   public static final String DEFAULT_COLOUR_NUC = "DEFAULT_COLOUR_NUC";
98
99   public static final String ADD_TEMPFACT_ANN = "ADD_TEMPFACT_ANN";
100
101   public static final String ADD_SS_ANN = "ADD_SS_ANN";
102
103   public static final String USE_RNAVIEW = "USE_RNAVIEW";
104
105   public static final String STRUCT_FROM_PDB = "STRUCT_FROM_PDB";
106
107   public static final String STRUCTURE_DISPLAY = "STRUCTURE_DISPLAY";
108
109   public static final String CHIMERA_PATH = "CHIMERA_PATH";
110   
111   public static final String HMMER_PATH = "HMMER_PATH";
112
113   public static final String CYGWIN_PATH = "CYGWIN_PATH";
114
115   public static final String HMMSEARCH_DB_PATHS = "HMMSEARCH_DB_PATHS";
116
117   public static final String HMMSEARCH_DBS = "HMMSEARCH_DBS";
118
119   public static final String SORT_ANNOTATIONS = "SORT_ANNOTATIONS";
120
121   public static final String SHOW_AUTOCALC_ABOVE = "SHOW_AUTOCALC_ABOVE";
122
123   public static final String SHOW_OCCUPANCY = "SHOW_OCCUPANCY";
124
125   public static final String SHOW_OV_HIDDEN_AT_START = "SHOW_OV_HIDDEN_AT_START";
126
127   public static final String USE_LEGACY_GAP = "USE_LEGACY_GAP";
128
129   public static final String GAP_COLOUR = "GAP_COLOUR";
130
131   public static final String HIDDEN_COLOUR = "HIDDEN_COLOUR";
132
133   private static final int MIN_FONT_SIZE = 1;
134
135   private static final int MAX_FONT_SIZE = 30;
136
137   /**
138    * Holds name and link separated with | character. Sequence ID must be
139    * $SEQUENCE_ID$ or $SEQUENCE_ID=/.possible | chars ./=$
140    */
141   public static UrlProviderI sequenceUrlLinks;
142
143   public static UrlLinkTableModel dataModel;
144
145   /**
146    * Holds name and link separated with | character. Sequence IDS and Sequences
147    * must be $SEQUENCEIDS$ or $SEQUENCEIDS=/.possible | chars ./=$ and
148    * $SEQUENCES$ or $SEQUENCES=/.possible | chars ./=$ and separation character
149    * for first and second token specified after a pipe character at end |,|.
150    * (TODO: proper escape for using | to separate ids or sequences
151    */
152
153   public static List<String> groupURLLinks;
154   static
155   {
156     // get links selected to be in the menu (SEQUENCE_LINKS)
157     // and links entered by the user but not selected (STORED_LINKS)
158     String inMenuString = Cache.getDefault("SEQUENCE_LINKS", "");
159     String notInMenuString = Cache.getDefault("STORED_LINKS", "");
160     String defaultUrl = Cache.getDefault("DEFAULT_URL",
161             UrlConstants.DEFAULT_LABEL);
162
163     // if both links lists are empty, add the DEFAULT_URL link
164     // otherwise we assume the default link is in one of the lists
165     if (inMenuString.isEmpty() && notInMenuString.isEmpty())
166     {
167       inMenuString = UrlConstants.DEFAULT_STRING;
168     }
169     UrlProviderFactoryI factory = new DesktopUrlProviderFactory(defaultUrl,
170             inMenuString, notInMenuString);
171     sequenceUrlLinks = factory.createUrlProvider();
172     dataModel = new UrlLinkTableModel(sequenceUrlLinks);
173
174     /**
175      * TODO: reformulate groupURL encoding so two or more can be stored in the
176      * .properties file as '|' separated strings
177      */
178
179     groupURLLinks = new ArrayList<>();
180   }
181
182   JInternalFrame frame;
183
184   DasSourceBrowser dasSource;
185
186   private WsPreferences wsPrefs;
187
188   private OptionsParam promptEachTimeOpt = new OptionsParam(
189           MessageManager.getString("label.prompt_each_time"),
190           "Prompt each time");
191
192   private OptionsParam lineArtOpt = new OptionsParam(
193           MessageManager.getString("label.lineart"), "Lineart");
194
195   private OptionsParam textOpt = new OptionsParam(
196           MessageManager.getString("action.text"), "Text");
197
198   /**
199    * Creates a new Preferences object.
200    */
201   public Preferences()
202   {
203     super();
204     frame = new JInternalFrame();
205     frame.setContentPane(this);
206     dasSource = new DasSourceBrowser();
207     dasTab.add(dasSource, BorderLayout.CENTER);
208     wsPrefs = new WsPreferences();
209     wsTab.add(wsPrefs, BorderLayout.CENTER);
210     int width = 500, height = 450;
211     new jalview.util.Platform();
212     if (Platform.isAMac())
213     {
214       width = 570;
215       height = 480;
216     }
217
218     Desktop.addInternalFrame(frame,
219             MessageManager.getString("label.preferences"), width, height);
220     frame.setMinimumSize(new Dimension(width, height));
221
222     /*
223      * Set HMMER tab defaults
224      */
225     hmmrTrimTermini.setSelected(Cache.getDefault("TRIM_TERMINI", false));
226     if (Cache.getDefault("USE_UNIPROT", false))
227     {
228       hmmerBackgroundUniprot.setSelected(true);
229     }
230     else
231     {
232       hmmerBackgroundAlignment.setSelected(true);
233     }
234     hmmerSequenceCount
235             .setText(Cache.getProperty("SEQUENCES_TO_KEEP"));
236     hmmerPath.setText(Cache.getProperty(HMMER_PATH));
237     hmmerPath.addActionListener(new ActionListener()
238     {
239       @Override
240       public void actionPerformed(ActionEvent e)
241       {
242         validateHmmerPath();
243       }
244     });
245     hmmerPath.addFocusListener(new FocusAdapter()
246     {
247       @Override
248       public void focusLost(FocusEvent e)
249       {
250         validateHmmerPath();
251       }
252     });
253     cygwinPath.setText(Cache.getProperty(CYGWIN_PATH));
254     cygwinPath.addActionListener(new ActionListener()
255     {
256       @Override
257       public void actionPerformed(ActionEvent e)
258       {
259         validateCygwinPath();
260       }
261     });
262     cygwinPath.addFocusListener(new FocusAdapter()
263     {
264       @Override
265       public void focusLost(FocusEvent e)
266       {
267         validateCygwinPath();
268       }
269     });
270
271     /*
272      * Set Visual tab defaults
273      */
274     seqLimit.setSelected(Cache.getDefault("SHOW_JVSUFFIX", true));
275     rightAlign.setSelected(Cache.getDefault("RIGHT_ALIGN_IDS", false));
276     fullScreen.setSelected(Cache.getDefault("SHOW_FULLSCREEN", false));
277     annotations.setSelected(Cache.getDefault("SHOW_ANNOTATIONS", true));
278
279     conservation.setSelected(Cache.getDefault("SHOW_CONSERVATION", true));
280     quality.setSelected(Cache.getDefault("SHOW_QUALITY", true));
281     identity.setSelected(Cache.getDefault("SHOW_IDENTITY", true));
282     openoverv.setSelected(Cache.getDefault("SHOW_OVERVIEW", false));
283     showUnconserved
284             .setSelected(Cache.getDefault("SHOW_UNCONSERVED", false));
285     showOccupancy.setSelected(Cache.getDefault(SHOW_OCCUPANCY, false));
286     showGroupConsensus
287             .setSelected(Cache.getDefault("SHOW_GROUP_CONSENSUS", false));
288     showGroupConservation.setSelected(
289             Cache.getDefault("SHOW_GROUP_CONSERVATION", false));
290     showConsensHistogram.setSelected(
291             Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM", true));
292     showConsensLogo
293             .setSelected(Cache.getDefault("SHOW_CONSENSUS_LOGO", false));
294     showInformationHistogram.setSelected(
295             Cache.getDefault("SHOW_INFORMATION_HISTOGRAM", true));
296     showHMMLogo.setSelected(Cache.getDefault("SHOW_HMM_LOGO", false));
297     showNpTooltip
298             .setSelected(Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true));
299     showDbRefTooltip
300             .setSelected(Cache.getDefault("SHOW_DBREFS_TOOLTIP", true));
301
302     String[] fonts = java.awt.GraphicsEnvironment
303             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
304     for (int i = 0; i < fonts.length; i++)
305     {
306       fontNameCB.addItem(fonts[i]);
307     }
308
309     for (int i = MIN_FONT_SIZE; i <= MAX_FONT_SIZE; i++)
310     {
311       fontSizeCB.addItem(i + "");
312     }
313
314     fontStyleCB.addItem("plain");
315     fontStyleCB.addItem("bold");
316     fontStyleCB.addItem("italic");
317
318     fontNameCB.setSelectedItem(Cache.getDefault("FONT_NAME", "SansSerif"));
319     fontSizeCB.setSelectedItem(Cache.getDefault("FONT_SIZE", "10"));
320     fontStyleCB.setSelectedItem(
321             Cache.getDefault("FONT_STYLE", Font.PLAIN + ""));
322
323     smoothFont.setSelected(Cache.getDefault("ANTI_ALIAS", false));
324     scaleProteinToCdna
325             .setSelected(Cache.getDefault(SCALE_PROTEIN_TO_CDNA, false));
326
327     idItalics.setSelected(Cache.getDefault("ID_ITALICS", true));
328
329     wrap.setSelected(Cache.getDefault("WRAP_ALIGNMENT", false));
330
331     gapSymbolCB.addItem("-");
332     gapSymbolCB.addItem(".");
333
334     gapSymbolCB.setSelectedItem(Cache.getDefault("GAP_SYMBOL", "-"));
335
336     sortby.addItem("No sort");
337     sortby.addItem("Id");
338     sortby.addItem("Pairwise Identity");
339     sortby.setSelectedItem(Cache.getDefault("SORT_ALIGNMENT", "No sort"));
340
341     sortAnnBy.addItem(SequenceAnnotationOrder.NONE.toString());
342     sortAnnBy
343             .addItem(SequenceAnnotationOrder.SEQUENCE_AND_LABEL.toString());
344     sortAnnBy
345             .addItem(SequenceAnnotationOrder.LABEL_AND_SEQUENCE.toString());
346     SequenceAnnotationOrder savedSort = SequenceAnnotationOrder
347             .valueOf(Cache.getDefault(SORT_ANNOTATIONS,
348                     SequenceAnnotationOrder.NONE.name()));
349     sortAnnBy.setSelectedItem(savedSort.toString());
350
351     sortAutocalc.addItem("Autocalculated first");
352     sortAutocalc.addItem("Autocalculated last");
353     final boolean showAbove = Cache.getDefault(SHOW_AUTOCALC_ABOVE, true);
354     sortAutocalc.setSelectedItem(showAbove ? sortAutocalc.getItemAt(0)
355             : sortAutocalc.getItemAt(1));
356     startupCheckbox
357             .setSelected(Cache.getDefault("SHOW_STARTUP_FILE", true));
358     startupFileTextfield.setText(Cache.getDefault("STARTUP_FILE",
359             Cache.getDefault("www.jalview.org", "http://www.jalview.org")
360                     + "/examples/exampleFile_2_3.jar"));
361
362     /*
363      * Set Colours tab defaults
364      */
365     protColour.addItem(ResidueColourScheme.NONE);
366     nucColour.addItem(ResidueColourScheme.NONE);
367     for (ColourSchemeI cs : ColourSchemes.getInstance().getColourSchemes())
368     {
369       String name = cs.getSchemeName();
370       protColour.addItem(name);
371       nucColour.addItem(name);
372     }
373     String oldProp = Cache.getDefault(DEFAULT_COLOUR,
374             ResidueColourScheme.NONE);
375     String newProp = Cache.getDefault(DEFAULT_COLOUR_PROT, null);
376     protColour.setSelectedItem(newProp != null ? newProp : oldProp);
377     newProp = Cache.getDefault(DEFAULT_COLOUR_NUC, null);
378     nucColour.setSelectedItem(newProp != null ? newProp : oldProp);
379     minColour.setBackground(
380             Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
381     maxColour.setBackground(
382             Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
383
384     /*
385      * Set overview panel defaults
386      */
387     gapColour.setBackground(
388             Cache.getDefaultColour(GAP_COLOUR,
389                     jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP));
390     hiddenColour.setBackground(
391             Cache.getDefaultColour(HIDDEN_COLOUR,
392                     jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_HIDDEN));
393     useLegacyGap.setSelected(Cache.getDefault(USE_LEGACY_GAP, false));
394     gapLabel.setEnabled(!useLegacyGap.isSelected());
395     gapColour.setEnabled(!useLegacyGap.isSelected());
396     showHiddenAtStart
397             .setSelected(Cache.getDefault(SHOW_OV_HIDDEN_AT_START, true));
398
399     /*
400      * Set Structure tab defaults.
401      */
402     final boolean structSelected = Cache.getDefault(STRUCT_FROM_PDB, false);
403     structFromPdb.setSelected(structSelected);
404     useRnaView.setSelected(Cache.getDefault(USE_RNAVIEW, false));
405     useRnaView.setEnabled(structSelected);
406     addSecondaryStructure.setSelected(Cache.getDefault(ADD_SS_ANN, false));
407     addSecondaryStructure.setEnabled(structSelected);
408     addTempFactor.setSelected(Cache.getDefault(ADD_TEMPFACT_ANN, false));
409     addTempFactor.setEnabled(structSelected);
410     structViewer.setSelectedItem(
411             Cache.getDefault(STRUCTURE_DISPLAY, ViewerType.JMOL.name()));
412     chimeraPath.setText(Cache.getDefault(CHIMERA_PATH, ""));
413     chimeraPath.addActionListener(new ActionListener()
414     {
415       @Override
416       public void actionPerformed(ActionEvent e)
417       {
418         validateChimeraPath();
419       }
420     });
421
422     if (Cache.getDefault("MAP_WITH_SIFTS", false))
423     {
424       siftsMapping.setSelected(true);
425     }
426     else
427     {
428       nwMapping.setSelected(true);
429     }
430
431     SiftsSettings
432             .setMapWithSifts(Cache.getDefault("MAP_WITH_SIFTS", false));
433
434     /*
435      * Set Connections tab defaults
436      */
437
438     // set up sorting
439     linkUrlTable.setModel(dataModel);
440     final TableRowSorter<TableModel> sorter = new TableRowSorter<>(
441             linkUrlTable.getModel());
442     linkUrlTable.setRowSorter(sorter);
443     List<RowSorter.SortKey> sortKeys = new ArrayList<>();
444
445     UrlLinkTableModel m = (UrlLinkTableModel) linkUrlTable.getModel();
446     sortKeys.add(new RowSorter.SortKey(m.getPrimaryColumn(),
447             SortOrder.DESCENDING));
448     sortKeys.add(new RowSorter.SortKey(m.getSelectedColumn(),
449             SortOrder.DESCENDING));
450     sortKeys.add(
451             new RowSorter.SortKey(m.getNameColumn(), SortOrder.ASCENDING));
452
453     sorter.setSortKeys(sortKeys);
454     sorter.sort();
455
456     // set up filtering
457     ActionListener onReset;
458     onReset = new ActionListener()
459     {
460       @Override
461       public void actionPerformed(ActionEvent e)
462       {
463         filterTB.setText("");
464         sorter.setRowFilter(RowFilter.regexFilter(""));
465       }
466
467     };
468     doReset.addActionListener(onReset);
469
470     // filter to display only custom urls
471     final RowFilter<TableModel, Object> customUrlFilter = new RowFilter<TableModel, Object>()
472     {
473       @Override
474       public boolean include(
475               Entry<? extends TableModel, ? extends Object> entry)
476       {
477         return ((UrlLinkTableModel) entry.getModel()).isUserEntry(entry);
478       }
479     };
480
481     final TableRowSorter<TableModel> customSorter = new TableRowSorter<>(
482             linkUrlTable.getModel());
483     customSorter.setRowFilter(customUrlFilter);
484
485     ActionListener onCustomOnly;
486     onCustomOnly = new ActionListener()
487     {
488       @Override
489       public void actionPerformed(ActionEvent e)
490       {
491         filterTB.setText("");
492         sorter.setRowFilter(customUrlFilter);
493       }
494     };
495     userOnly.addActionListener(onCustomOnly);
496
497     filterTB.getDocument().addDocumentListener(new DocumentListener()
498     {
499       String caseInsensitiveFlag = "(?i)";
500
501       @Override
502       public void changedUpdate(DocumentEvent e)
503       {
504         sorter.setRowFilter(RowFilter
505                 .regexFilter(caseInsensitiveFlag + filterTB.getText()));
506       }
507
508       @Override
509       public void removeUpdate(DocumentEvent e)
510       {
511         sorter.setRowFilter(RowFilter
512                 .regexFilter(caseInsensitiveFlag + filterTB.getText()));
513       }
514
515       @Override
516       public void insertUpdate(DocumentEvent e)
517       {
518         sorter.setRowFilter(RowFilter
519                 .regexFilter(caseInsensitiveFlag + filterTB.getText()));
520       }
521     });
522
523     // set up list selection functionality
524     linkUrlTable.getSelectionModel()
525             .addListSelectionListener(new UrlListSelectionHandler());
526
527     // set up radio buttons
528     int onClickCol = ((UrlLinkTableModel) linkUrlTable.getModel())
529             .getPrimaryColumn();
530     String onClickName = linkUrlTable.getColumnName(onClickCol);
531     linkUrlTable.getColumn(onClickName)
532             .setCellRenderer(new RadioButtonRenderer());
533     linkUrlTable.getColumn(onClickName)
534             .setCellEditor(new RadioButtonEditor());
535
536     // get boolean columns and resize those to min possible
537     for (int column = 0; column < linkUrlTable.getColumnCount(); column++)
538     {
539       if (linkUrlTable.getModel().getColumnClass(column)
540               .equals(Boolean.class))
541       {
542         TableColumn tableColumn = linkUrlTable.getColumnModel()
543                 .getColumn(column);
544         int preferredWidth = tableColumn.getMinWidth();
545
546         TableCellRenderer cellRenderer = linkUrlTable.getCellRenderer(0,
547                 column);
548         Component c = linkUrlTable.prepareRenderer(cellRenderer, 0, column);
549         int cwidth = c.getPreferredSize().width
550                 + linkUrlTable.getIntercellSpacing().width;
551         preferredWidth = Math.max(preferredWidth, cwidth);
552
553         tableColumn.setPreferredWidth(preferredWidth);
554       }
555     }
556
557     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
558     useProxy_actionPerformed(); // make sure useProxy is correctly initialised
559     proxyServerTB.setText(Cache.getDefault("PROXY_SERVER", ""));
560     proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));
561
562     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
563
564     usagestats.setSelected(Cache.getDefault("USAGESTATS", false));
565     // note antisense here: default is true
566     questionnaire
567             .setSelected(Cache.getProperty("NOQUESTIONNAIRES") == null);
568     versioncheck.setSelected(Cache.getDefault("VERSION_CHECK", true));
569
570     /*
571      * Set Output tab defaults
572      */
573     epsRendering.addItem(promptEachTimeOpt);
574     epsRendering.addItem(lineArtOpt);
575     epsRendering.addItem(textOpt);
576     String defaultEPS = Cache.getDefault("EPS_RENDERING",
577             "Prompt each time");
578     if (defaultEPS.equalsIgnoreCase("Text"))
579     {
580       epsRendering.setSelectedItem(textOpt);
581     }
582     else if (defaultEPS.equalsIgnoreCase("Lineart"))
583     {
584       epsRendering.setSelectedItem(lineArtOpt);
585     }
586     else
587     {
588       epsRendering.setSelectedItem(promptEachTimeOpt);
589     }
590     autoIdWidth.setSelected(Cache.getDefault("FIGURE_AUTOIDWIDTH", false));
591     userIdWidth.setEnabled(!autoIdWidth.isSelected());
592     userIdWidthlabel.setEnabled(!autoIdWidth.isSelected());
593     Integer wi = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH");
594     userIdWidth.setText(wi == null ? "" : wi.toString());
595     // TODO: refactor to use common enum via FormatAdapter and allow extension
596     // for new flat file formats
597     blcjv.setSelected(Cache.getDefault("BLC_JVSUFFIX", true));
598     clustaljv.setSelected(Cache.getDefault("CLUSTAL_JVSUFFIX", true));
599     fastajv.setSelected(Cache.getDefault("FASTA_JVSUFFIX", true));
600     msfjv.setSelected(Cache.getDefault("MSF_JVSUFFIX", true));
601     pfamjv.setSelected(Cache.getDefault("PFAM_JVSUFFIX", true));
602     pileupjv.setSelected(Cache.getDefault("PILEUP_JVSUFFIX", true));
603     pirjv.setSelected(Cache.getDefault("PIR_JVSUFFIX", true));
604     modellerOutput.setSelected(Cache.getDefault("PIR_MODELLER", false));
605     embbedBioJSON
606             .setSelected(Cache.getDefault("EXPORT_EMBBED_BIOJSON", true));
607
608     /*
609      * Set Editing tab defaults
610      */
611     autoCalculateConsCheck
612             .setSelected(Cache.getDefault("AUTO_CALC_CONSENSUS", true));
613     padGaps.setSelected(Cache.getDefault("PAD_GAPS", false));
614     sortByTree.setSelected(Cache.getDefault("SORT_BY_TREE", false));
615
616     annotations_actionPerformed(null); // update the display of the annotation
617                                        // settings
618   }
619
620   /**
621    * Save user selections on the Preferences tabs to the Cache and write out to
622    * file.
623    * 
624    * @param e
625    */
626   @Override
627   public void ok_actionPerformed(ActionEvent e)
628   {
629     if (!validateSettings())
630     {
631       return;
632     }
633
634     /*
635      * Save Visual settings
636      */
637     Cache.applicationProperties.setProperty("SHOW_JVSUFFIX",
638             Boolean.toString(seqLimit.isSelected()));
639     Cache.applicationProperties.setProperty("RIGHT_ALIGN_IDS",
640             Boolean.toString(rightAlign.isSelected()));
641     Cache.applicationProperties.setProperty("SHOW_FULLSCREEN",
642             Boolean.toString(fullScreen.isSelected()));
643     Cache.applicationProperties.setProperty("SHOW_OVERVIEW",
644             Boolean.toString(openoverv.isSelected()));
645     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
646             Boolean.toString(annotations.isSelected()));
647     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
648             Boolean.toString(conservation.isSelected()));
649     Cache.applicationProperties.setProperty("SHOW_QUALITY",
650             Boolean.toString(quality.isSelected()));
651     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
652             Boolean.toString(identity.isSelected()));
653
654     Cache.applicationProperties.setProperty("GAP_SYMBOL",
655             gapSymbolCB.getSelectedItem().toString());
656
657     Cache.applicationProperties.setProperty("FONT_NAME",
658             fontNameCB.getSelectedItem().toString());
659     Cache.applicationProperties.setProperty("FONT_STYLE",
660             fontStyleCB.getSelectedItem().toString());
661     Cache.applicationProperties.setProperty("FONT_SIZE",
662             fontSizeCB.getSelectedItem().toString());
663
664     Cache.applicationProperties.setProperty("ID_ITALICS",
665             Boolean.toString(idItalics.isSelected()));
666     Cache.applicationProperties.setProperty("SHOW_UNCONSERVED",
667             Boolean.toString(showUnconserved.isSelected()));
668     Cache.applicationProperties.setProperty(SHOW_OCCUPANCY,
669             Boolean.toString(showOccupancy.isSelected()));
670     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSENSUS",
671             Boolean.toString(showGroupConsensus.isSelected()));
672     Cache.applicationProperties.setProperty("SHOW_GROUP_CONSERVATION",
673             Boolean.toString(showGroupConservation.isSelected()));
674     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_HISTOGRAM",
675             Boolean.toString(showConsensHistogram.isSelected()));
676     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_LOGO",
677             Boolean.toString(showConsensLogo.isSelected()));
678     Cache.applicationProperties.setProperty("SHOW_INFORMATION_HISTOGRAM",
679             Boolean.toString(showConsensHistogram.isSelected()));
680     Cache.applicationProperties.setProperty("SHOW_HMM_LOGO",
681             Boolean.toString(showHMMLogo.isSelected()));
682     Cache.applicationProperties.setProperty("ANTI_ALIAS",
683             Boolean.toString(smoothFont.isSelected()));
684     Cache.applicationProperties.setProperty(SCALE_PROTEIN_TO_CDNA,
685             Boolean.toString(scaleProteinToCdna.isSelected()));
686     Cache.applicationProperties.setProperty("SHOW_NPFEATS_TOOLTIP",
687             Boolean.toString(showNpTooltip.isSelected()));
688     Cache.applicationProperties.setProperty("SHOW_DBREFS_TOOLTIP",
689             Boolean.toString(showDbRefTooltip.isSelected()));
690
691     Cache.applicationProperties.setProperty("WRAP_ALIGNMENT",
692             Boolean.toString(wrap.isSelected()));
693
694     Cache.applicationProperties.setProperty("STARTUP_FILE",
695             startupFileTextfield.getText());
696     Cache.applicationProperties.setProperty("SHOW_STARTUP_FILE",
697             Boolean.toString(startupCheckbox.isSelected()));
698
699     Cache.applicationProperties.setProperty("SORT_ALIGNMENT",
700             sortby.getSelectedItem().toString());
701
702     // convert description of sort order to enum name for save
703     SequenceAnnotationOrder annSortOrder = SequenceAnnotationOrder
704             .forDescription(sortAnnBy.getSelectedItem().toString());
705     if (annSortOrder != null)
706     {
707       Cache.applicationProperties.setProperty(SORT_ANNOTATIONS,
708               annSortOrder.name());
709     }
710
711     final boolean showAutocalcFirst = sortAutocalc.getSelectedIndex() == 0;
712     Cache.applicationProperties.setProperty(SHOW_AUTOCALC_ABOVE,
713             Boolean.valueOf(showAutocalcFirst).toString());
714
715     /*
716      * Save Colours settings
717      */
718     Cache.applicationProperties.setProperty(DEFAULT_COLOUR_PROT,
719             protColour.getSelectedItem().toString());
720     Cache.applicationProperties.setProperty(DEFAULT_COLOUR_NUC,
721             nucColour.getSelectedItem().toString());
722     Cache.setColourProperty("ANNOTATIONCOLOUR_MIN",
723             minColour.getBackground());
724     Cache.setColourProperty("ANNOTATIONCOLOUR_MAX",
725             maxColour.getBackground());
726
727     /*
728      * Save HMMER settings
729      */
730     Cache.applicationProperties.setProperty("TRIM_TERMINI",
731             Boolean.toString(hmmrTrimTermini.isSelected()));
732     Cache.applicationProperties.setProperty("USE_UNIPROT",
733             Boolean.toString(hmmerBackgroundUniprot.isSelected()));
734     Cache.applicationProperties.setProperty("SEQUENCES_TO_KEEP",
735             hmmerSequenceCount.getText());
736     Cache.setOrRemove(HMMER_PATH, hmmerPath.getText());
737     Cache.setOrRemove(CYGWIN_PATH, cygwinPath.getText());
738     AlignFrame[] frames = Desktop.getAlignFrames();
739     if (frames != null && frames.length > 0)
740     {
741       for (AlignFrame f : frames)
742       {
743         f.updateHMMERStatus();
744       }
745     }
746     
747     hmmrTrimTermini.setSelected(Cache.getDefault("TRIM_TERMINI", false));
748     if (Cache.getDefault("USE_UNIPROT", false))
749     {
750       hmmerBackgroundUniprot.setSelected(true);
751     }
752     else
753     {
754       hmmerBackgroundAlignment.setSelected(true);
755     }
756     hmmerSequenceCount
757             .setText(Cache.getProperty("SEQUENCES_TO_KEEP"));
758     hmmerPath.setText(Cache.getProperty(HMMER_PATH));
759
760     /*
761      * Save Overview settings
762      */
763     Cache.setColourProperty(GAP_COLOUR, gapColour.getBackground());
764     Cache.setColourProperty(HIDDEN_COLOUR, hiddenColour.getBackground());
765     Cache.applicationProperties.setProperty(USE_LEGACY_GAP,
766             Boolean.toString(useLegacyGap.isSelected()));
767     Cache.applicationProperties.setProperty(SHOW_OV_HIDDEN_AT_START,
768             Boolean.toString(showHiddenAtStart.isSelected()));
769
770     /*
771      * Save Structure settings
772      */
773     Cache.applicationProperties.setProperty(ADD_TEMPFACT_ANN,
774             Boolean.toString(addTempFactor.isSelected()));
775     Cache.applicationProperties.setProperty(ADD_SS_ANN,
776             Boolean.toString(addSecondaryStructure.isSelected()));
777     Cache.applicationProperties.setProperty(USE_RNAVIEW,
778             Boolean.toString(useRnaView.isSelected()));
779     Cache.applicationProperties.setProperty(STRUCT_FROM_PDB,
780             Boolean.toString(structFromPdb.isSelected()));
781     Cache.applicationProperties.setProperty(STRUCTURE_DISPLAY,
782             structViewer.getSelectedItem().toString());
783     Cache.setOrRemove(CHIMERA_PATH, chimeraPath.getText());
784     Cache.applicationProperties.setProperty("MAP_WITH_SIFTS",
785             Boolean.toString(siftsMapping.isSelected()));
786     SiftsSettings.setMapWithSifts(siftsMapping.isSelected());
787
788     /*
789      * Save Output settings
790      */
791     Cache.applicationProperties.setProperty("EPS_RENDERING",
792             ((OptionsParam) epsRendering.getSelectedItem()).getCode());
793
794     /*
795      * Save Connections settings
796      */
797     Cache.setOrRemove("DEFAULT_BROWSER", defaultBrowser.getText());
798
799     jalview.util.BrowserLauncher.resetBrowser();
800
801     // save user-defined and selected links
802     String menuLinks = sequenceUrlLinks.writeUrlsAsString(true);
803     if (menuLinks.isEmpty())
804     {
805       Cache.applicationProperties.remove("SEQUENCE_LINKS");
806     }
807     else
808     {
809       Cache.applicationProperties.setProperty("SEQUENCE_LINKS",
810               menuLinks.toString());
811     }
812
813     String nonMenuLinks = sequenceUrlLinks.writeUrlsAsString(false);
814     if (nonMenuLinks.isEmpty())
815     {
816       Cache.applicationProperties.remove("STORED_LINKS");
817     }
818     else
819     {
820       Cache.applicationProperties.setProperty("STORED_LINKS",
821               nonMenuLinks.toString());
822     }
823
824     Cache.applicationProperties.setProperty("DEFAULT_URL",
825             sequenceUrlLinks.getPrimaryUrlId());
826
827     Cache.applicationProperties.setProperty("USE_PROXY",
828             Boolean.toString(useProxy.isSelected()));
829
830     Cache.setOrRemove("PROXY_SERVER", proxyServerTB.getText());
831
832     Cache.setOrRemove("PROXY_PORT", proxyPortTB.getText());
833
834     if (useProxy.isSelected())
835     {
836       System.setProperty("http.proxyHost", proxyServerTB.getText());
837       System.setProperty("http.proxyPort", proxyPortTB.getText());
838     }
839     else
840     {
841       System.setProperty("http.proxyHost", "");
842       System.setProperty("http.proxyPort", "");
843     }
844     Cache.setProperty("VERSION_CHECK",
845             Boolean.toString(versioncheck.isSelected()));
846     if (Cache.getProperty("USAGESTATS") != null || usagestats.isSelected())
847     {
848       // default is false - we only set this if the user has actively agreed
849       Cache.setProperty("USAGESTATS",
850               Boolean.toString(usagestats.isSelected()));
851     }
852     if (!questionnaire.isSelected())
853     {
854       Cache.setProperty("NOQUESTIONNAIRES", "true");
855     }
856     else
857     {
858       // special - made easy to edit a property file to disable questionnaires
859       // by just adding the given line
860       Cache.removeProperty("NOQUESTIONNAIRES");
861     }
862
863     /*
864      * Save Output settings
865      */
866     Cache.applicationProperties.setProperty("BLC_JVSUFFIX",
867             Boolean.toString(blcjv.isSelected()));
868     Cache.applicationProperties.setProperty("CLUSTAL_JVSUFFIX",
869             Boolean.toString(clustaljv.isSelected()));
870     Cache.applicationProperties.setProperty("FASTA_JVSUFFIX",
871             Boolean.toString(fastajv.isSelected()));
872     Cache.applicationProperties.setProperty("MSF_JVSUFFIX",
873             Boolean.toString(msfjv.isSelected()));
874     Cache.applicationProperties.setProperty("PFAM_JVSUFFIX",
875             Boolean.toString(pfamjv.isSelected()));
876     Cache.applicationProperties.setProperty("PILEUP_JVSUFFIX",
877             Boolean.toString(pileupjv.isSelected()));
878     Cache.applicationProperties.setProperty("PIR_JVSUFFIX",
879             Boolean.toString(pirjv.isSelected()));
880     Cache.applicationProperties.setProperty("PIR_MODELLER",
881             Boolean.toString(modellerOutput.isSelected()));
882     Cache.applicationProperties.setProperty("EXPORT_EMBBED_BIOJSON",
883             Boolean.toString(embbedBioJSON.isSelected()));
884     jalview.io.PIRFile.useModellerOutput = modellerOutput.isSelected();
885
886     Cache.applicationProperties.setProperty("FIGURE_AUTOIDWIDTH",
887             Boolean.toString(autoIdWidth.isSelected()));
888     userIdWidth_actionPerformed();
889     Cache.applicationProperties.setProperty("FIGURE_FIXEDIDWIDTH",
890             userIdWidth.getText());
891
892     /*
893      * Save Editing settings
894      */
895     Cache.applicationProperties.setProperty("AUTO_CALC_CONSENSUS",
896             Boolean.toString(autoCalculateConsCheck.isSelected()));
897     Cache.applicationProperties.setProperty("SORT_BY_TREE",
898             Boolean.toString(sortByTree.isSelected()));
899     Cache.applicationProperties.setProperty("PAD_GAPS",
900             Boolean.toString(padGaps.isSelected()));
901
902     dasSource.saveProperties(Cache.applicationProperties);
903     wsPrefs.updateAndRefreshWsMenuConfig(false);
904     Cache.saveProperties();
905     Desktop.instance.doConfigureStructurePrefs();
906     try
907     {
908       frame.setClosed(true);
909     } catch (Exception ex)
910     {
911     }
912   }
913
914   /**
915    * Do any necessary validation before saving settings. Return focus to the
916    * first tab which fails validation.
917    * 
918    * @return
919    */
920   private boolean validateSettings()
921   {
922     if (!validateStructure())
923     {
924       structureTab.requestFocusInWindow();
925       return false;
926     }
927     return true;
928   }
929
930   @Override
931   protected boolean validateStructure()
932   {
933     return validateChimeraPath();
934
935   }
936
937   /**
938    * DOCUMENT ME!
939    */
940   @Override
941   public void startupFileTextfield_mouseClicked()
942   {
943     String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
944     JalviewFileChooser chooser = JalviewFileChooser
945             .forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat);
946     chooser.setFileView(new JalviewFileView());
947     chooser.setDialogTitle(
948             MessageManager.getString("label.select_startup_file"));
949
950     int value = chooser.showOpenDialog(this);
951
952     if (value == JalviewFileChooser.APPROVE_OPTION)
953     {
954       FileFormatI format = chooser.getSelectedFormat();
955       if (format != null)
956       {
957         Cache.applicationProperties.setProperty("DEFAULT_FILE_FORMAT",
958                 format.getName());
959       }
960       startupFileTextfield
961               .setText(chooser.getSelectedFile().getAbsolutePath());
962     }
963   }
964
965   /**
966    * DOCUMENT ME!
967    * 
968    * @param e
969    *          DOCUMENT ME!
970    */
971   @Override
972   public void cancel_actionPerformed(ActionEvent e)
973   {
974     try
975     {
976       wsPrefs.updateWsMenuConfig(true);
977       wsPrefs.refreshWs_actionPerformed(e);
978       frame.setClosed(true);
979     } catch (Exception ex)
980     {
981     }
982   }
983
984   /**
985    * DOCUMENT ME!
986    * 
987    * @param e
988    *          DOCUMENT ME!
989    */
990   @Override
991   public void annotations_actionPerformed(ActionEvent e)
992   {
993     conservation.setEnabled(annotations.isSelected());
994     quality.setEnabled(annotations.isSelected());
995     identity.setEnabled(annotations.isSelected());
996     showOccupancy.setEnabled(annotations.isSelected());
997     showGroupConsensus.setEnabled(annotations.isSelected());
998     showGroupConservation.setEnabled(annotations.isSelected());
999     showConsensHistogram.setEnabled(annotations.isSelected()
1000             && (identity.isSelected() || showGroupConsensus.isSelected()));
1001     showConsensLogo.setEnabled(annotations.isSelected()
1002             && (identity.isSelected() || showGroupConsensus.isSelected()));
1003     showInformationHistogram.setEnabled(annotations.isSelected());
1004     showHMMLogo.setEnabled(annotations.isSelected());
1005   }
1006
1007   @Override
1008   public void newLink_actionPerformed(ActionEvent e)
1009   {
1010     GSequenceLink link = new GSequenceLink();
1011     boolean valid = false;
1012     while (!valid)
1013     {
1014       if (JvOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
1015               MessageManager.getString("label.new_sequence_url_link"),
1016               JvOptionPane.OK_CANCEL_OPTION, -1,
1017               null) == JvOptionPane.OK_OPTION)
1018       {
1019         if (link.checkValid())
1020         {
1021           if (((UrlLinkTableModel) linkUrlTable.getModel())
1022                   .isUniqueName(link.getName()))
1023           {
1024             ((UrlLinkTableModel) linkUrlTable.getModel())
1025                     .insertRow(link.getName(), link.getURL());
1026             valid = true;
1027           }
1028           else
1029           {
1030             link.notifyDuplicate();
1031             continue;
1032           }
1033         }
1034       }
1035       else
1036       {
1037         break;
1038       }
1039     }
1040   }
1041
1042   @Override
1043   public void editLink_actionPerformed(ActionEvent e)
1044   {
1045     GSequenceLink link = new GSequenceLink();
1046
1047     int index = linkUrlTable.getSelectedRow();
1048     if (index == -1)
1049     {
1050       // button no longer enabled if row is not selected
1051       Cache.log.debug("Edit with no row selected in linkUrlTable");
1052       return;
1053     }
1054
1055     int nameCol = ((UrlLinkTableModel) linkUrlTable.getModel())
1056             .getNameColumn();
1057     int urlCol = ((UrlLinkTableModel) linkUrlTable.getModel())
1058             .getUrlColumn();
1059     String oldName = linkUrlTable.getValueAt(index, nameCol).toString();
1060     link.setName(oldName);
1061     link.setURL(linkUrlTable.getValueAt(index, urlCol).toString());
1062
1063     boolean valid = false;
1064     while (!valid)
1065     {
1066       if (JvOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
1067               MessageManager.getString("label.edit_sequence_url_link"),
1068               JvOptionPane.OK_CANCEL_OPTION, -1,
1069               null) == JvOptionPane.OK_OPTION)
1070       {
1071         if (link.checkValid())
1072         {
1073           if ((oldName.equals(link.getName()))
1074                   || (((UrlLinkTableModel) linkUrlTable.getModel())
1075                           .isUniqueName(link.getName())))
1076           {
1077             linkUrlTable.setValueAt(link.getName(), index, nameCol);
1078             linkUrlTable.setValueAt(link.getURL(), index, urlCol);
1079             valid = true;
1080           }
1081           else
1082           {
1083             link.notifyDuplicate();
1084             continue;
1085           }
1086         }
1087       }
1088       else
1089       {
1090         break;
1091       }
1092     }
1093   }
1094
1095   @Override
1096   public void deleteLink_actionPerformed(ActionEvent e)
1097   {
1098     int index = linkUrlTable.getSelectedRow();
1099     int modelIndex = -1;
1100     if (index == -1)
1101     {
1102       // button no longer enabled if row is not selected
1103       Cache.log.debug("Delete with no row selected in linkUrlTable");
1104       return;
1105     }
1106     else
1107     {
1108       modelIndex = linkUrlTable.convertRowIndexToModel(index);
1109     }
1110
1111     // make sure we use the model index to delete, and not the table index
1112     ((UrlLinkTableModel) linkUrlTable.getModel()).removeRow(modelIndex);
1113   }
1114
1115   @Override
1116   public void defaultBrowser_mouseClicked(MouseEvent e)
1117   {
1118     JFileChooser chooser = new JFileChooser(".");
1119     chooser.setDialogTitle(
1120             MessageManager.getString("label.select_default_browser"));
1121
1122     int value = chooser.showOpenDialog(this);
1123
1124     if (value == JFileChooser.APPROVE_OPTION)
1125     {
1126       defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());
1127     }
1128
1129   }
1130
1131   /*
1132    * (non-Javadoc)
1133    * 
1134    * @see
1135    * jalview.jbgui.GPreferences#showunconserved_actionPerformed(java.awt.event
1136    * .ActionEvent)
1137    */
1138   @Override
1139   protected void showunconserved_actionPerformed(ActionEvent e)
1140   {
1141     // TODO Auto-generated method stub
1142     super.showunconserved_actionPerformed(e);
1143   }
1144
1145   public static List<String> getGroupURLLinks()
1146   {
1147     return groupURLLinks;
1148   }
1149
1150   @Override
1151   public void minColour_actionPerformed(JPanel panel)
1152   {
1153     Color col = JColorChooser.showDialog(this,
1154             MessageManager.getString("label.select_colour_minimum_value"),
1155             minColour.getBackground());
1156     if (col != null)
1157     {
1158       panel.setBackground(col);
1159     }
1160     panel.repaint();
1161   }
1162
1163   @Override
1164   public void maxColour_actionPerformed(JPanel panel)
1165   {
1166     Color col = JColorChooser.showDialog(this,
1167             MessageManager.getString("label.select_colour_maximum_value"),
1168             maxColour.getBackground());
1169     if (col != null)
1170     {
1171       panel.setBackground(col);
1172     }
1173     panel.repaint();
1174   }
1175
1176   @Override
1177   public void gapColour_actionPerformed(JPanel gap)
1178   {
1179     if (!useLegacyGap.isSelected())
1180     {
1181       Color col = JColorChooser.showDialog(this,
1182               MessageManager.getString("label.select_gap_colour"),
1183               gapColour.getBackground());
1184       if (col != null)
1185       {
1186         gap.setBackground(col);
1187       }
1188       gap.repaint();
1189     }
1190   }
1191
1192   @Override
1193   public void hiddenColour_actionPerformed(JPanel hidden)
1194   {
1195     Color col = JColorChooser.showDialog(this,
1196             MessageManager.getString("label.select_hidden_colour"),
1197             hiddenColour.getBackground());
1198     if (col != null)
1199     {
1200       hidden.setBackground(col);
1201     }
1202     hidden.repaint();
1203   }
1204
1205   @Override
1206   protected void useLegacyGaps_actionPerformed(ActionEvent e)
1207   {
1208     boolean enabled = useLegacyGap.isSelected();
1209     if (enabled)
1210     {
1211       gapColour.setBackground(
1212               jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_LEGACY_GAP);
1213     }
1214     else
1215     {
1216       gapColour.setBackground(
1217               jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP);
1218     }
1219     gapColour.setEnabled(!enabled);
1220     gapLabel.setEnabled(!enabled);
1221   }
1222
1223   @Override
1224   protected void resetOvDefaults_actionPerformed(ActionEvent e)
1225   {
1226     useLegacyGap.setSelected(false);
1227     useLegacyGaps_actionPerformed(null);
1228     showHiddenAtStart.setSelected(true);
1229     hiddenColour.setBackground(
1230             jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_HIDDEN);
1231   }
1232
1233   @Override
1234   protected void userIdWidth_actionPerformed()
1235   {
1236     try
1237     {
1238       String val = userIdWidth.getText().trim();
1239       if (val.length() > 0)
1240       {
1241         Integer iw = Integer.parseInt(val);
1242         if (iw.intValue() < 12)
1243         {
1244           throw new NumberFormatException();
1245         }
1246         userIdWidth.setText(iw.toString());
1247       }
1248     } catch (NumberFormatException x)
1249     {
1250       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1251               MessageManager
1252                       .getString("warn.user_defined_width_requirements"),
1253               MessageManager.getString("label.invalid_id_column_width"),
1254               JvOptionPane.WARNING_MESSAGE);
1255       userIdWidth.setText("");
1256     }
1257   }
1258
1259   @Override
1260   protected void autoIdWidth_actionPerformed()
1261   {
1262     userIdWidth.setEnabled(!autoIdWidth.isSelected());
1263     userIdWidthlabel.setEnabled(!autoIdWidth.isSelected());
1264   }
1265
1266   /**
1267    * Returns true if chimera path is to a valid executable, else show an error
1268    * dialog.
1269    */
1270   private boolean validateChimeraPath()
1271   {
1272     if (chimeraPath.getText().trim().length() > 0)
1273     {
1274       File f = new File(chimeraPath.getText());
1275       if (!f.canExecute())
1276       {
1277         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1278                 MessageManager.getString("label.invalid_chimera_path"),
1279                 MessageManager.getString("label.invalid_name"),
1280                 JvOptionPane.ERROR_MESSAGE);
1281         return false;
1282       }
1283     }
1284     return true;
1285   }
1286   
1287   /**
1288    * Returns true if the given text field contains a path to a folder that
1289    * contains an executable with the given name, else false (after showing a
1290    * warning dialog). The executable name will be tried with .exe appended if not
1291    * found.
1292    * 
1293    * @param textField
1294    * @param executable
1295    */
1296   protected boolean validateExecutablePath(JTextField textField, String executable)
1297   {
1298     String folder = textField.getText().trim();
1299
1300     if (FileUtils.getExecutable(executable, folder) != null)
1301     {
1302       return true;
1303     }
1304     if (folder.length() > 0)
1305     {
1306       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1307               MessageManager.formatMessage("label.executable_not_found",
1308                       executable),
1309               MessageManager.getString("label.invalid_folder"),
1310               JvOptionPane.ERROR_MESSAGE);
1311     }
1312     return false;
1313   }
1314
1315   /**
1316    * Checks if a file can be executed
1317    * 
1318    * @param path
1319    *          the path to the file
1320    * @return
1321    */
1322   public boolean canExecute(String path)
1323   {
1324     File file = new File(path);
1325     if (!file.canExecute())
1326     {
1327       file = new File(path + ".exe");
1328       {
1329         if (!file.canExecute())
1330         {
1331           return false;
1332         }
1333       }
1334     }
1335     return true;
1336   }
1337
1338   /**
1339    * If Chimera is selected, check it can be found on default or user-specified
1340    * path, if not show a warning/help dialog.
1341    */
1342   @Override
1343   protected void structureViewer_actionPerformed(String selectedItem)
1344   {
1345     if (!selectedItem.equals(ViewerType.CHIMERA.name()))
1346     {
1347       return;
1348     }
1349     boolean found = false;
1350
1351     /*
1352      * Try user-specified and standard paths for Chimera executable.
1353      */
1354     List<String> paths = StructureManager.getChimeraPaths();
1355     paths.add(0, chimeraPath.getText());
1356     for (String path : paths)
1357     {
1358       if (new File(path.trim()).canExecute())
1359       {
1360         found = true;
1361         break;
1362       }
1363     }
1364     if (!found)
1365     {
1366       String[] options = { "OK", "Help" };
1367       int showHelp = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
1368               JvSwingUtils.wrapTooltip(true,
1369                       MessageManager.getString("label.chimera_missing")),
1370               "", JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE,
1371               null, options, options[0]);
1372       if (showHelp == JvOptionPane.NO_OPTION)
1373       {
1374         try
1375         {
1376           Help.showHelpWindow(HelpId.StructureViewer);
1377         } catch (HelpSetException e)
1378         {
1379           e.printStackTrace();
1380         }
1381       }
1382     }
1383   }
1384
1385   @Override
1386   protected void validateHmmerPath()
1387   {
1388     validateExecutablePath(hmmerPath, HmmerCommand.HMMBUILD);
1389   }
1390
1391   @Override
1392   protected void validateCygwinPath()
1393   {
1394     validateExecutablePath(cygwinPath, "run");
1395   }
1396
1397   public class OptionsParam
1398   {
1399     private String name;
1400
1401     private String code;
1402
1403     public OptionsParam(String name, String code)
1404     {
1405       this.name = name;
1406       this.code = code;
1407     }
1408
1409     public String getName()
1410     {
1411       return name;
1412     }
1413
1414     public void setName(String name)
1415     {
1416       this.name = name;
1417     }
1418
1419     public String getCode()
1420     {
1421       return code;
1422     }
1423
1424     public void setCode(String code)
1425     {
1426       this.code = code;
1427     }
1428
1429     @Override
1430     public String toString()
1431     {
1432       return name;
1433     }
1434
1435     @Override
1436     public boolean equals(Object that)
1437     {
1438       if (!(that instanceof OptionsParam))
1439       {
1440         return false;
1441       }
1442       return this.code.equalsIgnoreCase(((OptionsParam) that).code);
1443     }
1444
1445     @Override
1446     public int hashCode()
1447     {
1448       return name.hashCode() + code.hashCode();
1449     }
1450   }
1451
1452   private class UrlListSelectionHandler implements ListSelectionListener
1453   {
1454
1455     @Override
1456     public void valueChanged(ListSelectionEvent e)
1457     {
1458       ListSelectionModel lsm = (ListSelectionModel) e.getSource();
1459
1460       int index = lsm.getMinSelectionIndex();
1461       if (index == -1)
1462       {
1463         // no selection, so disable delete/edit buttons
1464         editLink.setEnabled(false);
1465         deleteLink.setEnabled(false);
1466         return;
1467       }
1468       int modelIndex = linkUrlTable.convertRowIndexToModel(index);
1469
1470       // enable/disable edit and delete link buttons
1471       if (((UrlLinkTableModel) linkUrlTable.getModel())
1472               .isRowDeletable(modelIndex))
1473       {
1474         deleteLink.setEnabled(true);
1475       }
1476       else
1477       {
1478         deleteLink.setEnabled(false);
1479       }
1480
1481       if (((UrlLinkTableModel) linkUrlTable.getModel())
1482               .isRowEditable(modelIndex))
1483       {
1484         editLink.setEnabled(true);
1485       }
1486       else
1487       {
1488         editLink.setEnabled(false);
1489       }
1490     }
1491   }
1492 }