From e65df2c2289b3b05c94ace285ae64a7cbc4999a8 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Tue, 2 May 2017 17:56:13 +0100 Subject: [PATCH] JAL-1632 compare size of input against minimums for calculation and enable/disable buttons --- resources/lang/Messages.properties | 1 + src/jalview/gui/CalculationChooser.java | 72 +++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index f65102c..5ac4269 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -385,6 +385,7 @@ label.invalid_selection = Invalid Selection label.principal_component_analysis_must_take_least_four_input_sequences = Principal component analysis must take\nat least 4 input sequences. label.sequence_selection_insufficient = Sequence selection insufficient label.you_need_more_two_sequences_selected_build_tree = You need to have more than two sequences selected to build a tree! +label.you_need_more_than_n_sequences = You need to have more than {0} sequences label.not_enough_sequences = Not enough sequences label.selected_region_to_tree_may_only_contain_residues_or_gaps = The selected region to create a tree may\nonly contain residues or gaps.\nTry using the Pad function in the edit menu,\nor one of the multiple sequence alignment web services. label.sequences_selection_not_aligned = Sequences in selection are not aligned diff --git a/src/jalview/gui/CalculationChooser.java b/src/jalview/gui/CalculationChooser.java index 7f245b1..694b096 100644 --- a/src/jalview/gui/CalculationChooser.java +++ b/src/jalview/gui/CalculationChooser.java @@ -163,15 +163,18 @@ public class CalculationChooser extends JPanel calcTypes.add(pca); calcTypes.add(neighbourJoining); calcTypes.add(averageDistance); + + ActionListener calcChanged = new ActionListener() { @Override - public void itemStateChanged(ItemEvent e) + public void actionPerformed(ActionEvent e) { - neighbourJoining.setEnabled(tree.isSelected()); - averageDistance.setEnabled(tree.isSelected()); + validateCalcTypes(); } }; - + pca.addActionListener(calcChanged); + neighbourJoining.addActionListener(calcChanged); + averageDistance.addActionListener(calcChanged); /* * score models drop-down - with added tooltips! */ @@ -264,6 +267,67 @@ public class CalculationChooser extends JPanel } /** + * enable calculations applicable for the current alignment or selection. + */ + protected void validateCalcTypes() + { + int size = af.getViewport().getAlignment().getHeight(); + if (af.getViewport().getSelectionGroup() != null) + { + size = af.getViewport().getSelectionGroup().getSize(); + } + if (!(checkEnabled(pca, size, 4) + | checkEnabled(neighbourJoining, size, 3) | checkEnabled( + averageDistance, size, 3))) + { + ok.setToolTipText(null); + ok.setEnabled(true); + } + else + { + ok.setEnabled(false); + } + } + + /** + * Check the input and disable a calculation's radio button if necessary. A + * tooltip is shown for disabled calculations. + * + * @param calc + * - radio button for the calculation being validated + * @param size + * - size of input to calculation + * @param minsize + * - minimum size for calculation + * @return true if size < minsize *and* calc.isSelected + */ + private boolean checkEnabled(JRadioButton calc, int size, int minsize) + { + String ttip = MessageManager.formatMessage( + "label.you_need_more_than_n_sequences", minsize); + + calc.setEnabled(size >= minsize); + if (!calc.isEnabled()) + { + calc.setToolTipText(ttip); + } + else + { + calc.setToolTipText(null); + } + if (calc.isSelected()) + { + modelNames.setEnabled(calc.isEnabled()); + if (!calc.isEnabled()) + { + ok.setEnabled(false); + ok.setToolTipText(ttip); + return true; + } + } + return false; + } + /** * A rather elaborate helper method (blame Swing, not me) that builds a * drop-down list of score models (by name) with descriptions as tooltips. * There is also a tooltip shown for the currently selected item when hovering -- 1.7.10.2