From: Jim Procter Date: Wed, 3 May 2017 16:48:11 +0000 (+0100) Subject: JAL-2393 JAL-2416 refresh the score model menu when updating gui enabled/disabled... X-Git-Tag: Release_2_10_2~3^2~105^2~2^2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=2a35abbd49049cce7f7bb1abb0543e444e72485c;p=jalview.git JAL-2393 JAL-2416 refresh the score model menu when updating gui enabled/disabled settings --- diff --git a/src/jalview/gui/CalculationChooser.java b/src/jalview/gui/CalculationChooser.java index fe6336e..05f1fba 100644 --- a/src/jalview/gui/CalculationChooser.java +++ b/src/jalview/gui/CalculationChooser.java @@ -47,6 +47,7 @@ import java.util.List; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; +import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; @@ -300,6 +301,7 @@ public class CalculationChooser extends JPanel { ok.setEnabled(false); } + updateScoreModels(comboBox, tips); } /** @@ -340,6 +342,13 @@ public class CalculationChooser extends JPanel } return false; } + + final JComboBox comboBox = new JComboBox(); + + final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer(); + + List tips = new ArrayList(); + /** * A rather elaborate helper method (blame Swing, not me) that builds a * drop-down list of score models (by name) with descriptions as tooltips. @@ -348,17 +357,14 @@ public class CalculationChooser extends JPanel */ protected JComboBox buildModelOptionsList() { - final JComboBox comboBox = new JComboBox(); - ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer(); comboBox.setRenderer(renderer); - final List tips = new ArrayList(); /* * show tooltip on mouse over the combobox * note the listener has to be on the components that make up * the combobox, doesn't work if just on the combobox */ - MouseAdapter mouseListener = new MouseAdapter() + final MouseAdapter mouseListener = new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) @@ -377,17 +383,39 @@ public class CalculationChooser extends JPanel c.addMouseListener(mouseListener); } + updateScoreModels(comboBox, tips); + + /* + * set the list of tooltips on the combobox's renderer + */ + renderer.setTooltips(tips); + + return comboBox; + } + + private void updateScoreModels(JComboBox comboBox, List tips) + { + Object curSel = comboBox.getSelectedItem(); + tips.clear(); + DefaultComboBoxModel model = new DefaultComboBoxModel(); + /* * now we can actually add entries to the combobox, * remembering their descriptions for tooltips */ ScoreModels scoreModels = ScoreModels.getInstance(); + boolean selectedIsPresent = false; for (ScoreModelI sm : scoreModels.getModels()) { boolean nucleotide = af.getViewport().getAlignment().isNucleotide(); if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide) { - comboBox.addItem(sm.getName()); + if (curSel != null && sm.getName().equals(curSel)) + { + selectedIsPresent = true; + curSel = sm.getName(); + } + model.addElement(sm.getName()); /* * tooltip is description if provided, else text lookup with @@ -401,14 +429,13 @@ public class CalculationChooser extends JPanel } tips.add(tooltip); } - - /* - * set the list of tooltips on the combobox's renderer - */ - renderer.setTooltips(tips); } - - return comboBox; + if (selectedIsPresent) + { + model.setSelectedItem(curSel); + } + // finally, update the model + comboBox.setModel(model); } /**