View Javadoc
1   package com.github.celldynamics.quimp.plugin.protanalysis;
2   
3   import java.awt.event.ActionEvent;
4   
5   import javax.swing.JComboBox;
6   
7   import org.apache.commons.lang3.mutable.MutableInt;
8   
9   /**
10   * Create action for updating numbers in options from JComboBox<>.
11   * 
12   * <p>Only Number and String are supported as items of JComboBox.
13   * 
14   * @author p.baniukiewicz
15   *
16   */
17  @SuppressWarnings("serial")
18  public class ActionUpdateOptionsNumber extends ProtAnalysisAbstractAction {
19  
20    private MutableInt val;
21  
22    /**
23     * Main constructor.
24     * 
25     * @param name name
26     * @param desc description
27     * @param ui reference to outer class
28     * @param option reference to parameter to be changed
29     */
30    public ActionUpdateOptionsNumber(String name, String desc, ProtAnalysisUi ui,
31            MutableInt option) {
32      super(name, desc, ui);
33      this.val = option;
34    }
35  
36    /*
37     * (non-Javadoc)
38     * 
39     * @see
40     * com.github.celldynamics.quimp.plugin.protanalysis.ProtAnalysisAbstractAction#actionPerformed(
41     * java.awt.event.ActionEvent)
42     */
43    @Override
44    public void actionPerformed(ActionEvent e) {
45      JComboBox<?> cmp = ((JComboBox<?>) e.getSource());
46      Object item = cmp.getSelectedItem();
47      if (item instanceof Number) { // for JComboBox<Number> just get selected value
48        Number num = (Number) item;
49        val.setValue(num);
50      } else {
51        if (item instanceof String) { // for JComboBox<String> get index of selected item
52          int num = cmp.getSelectedIndex();
53          val.setValue(num);
54        } else {
55          throw new RuntimeException("This JComboBox is not supported");
56        }
57      }
58  
59      logger.trace(ui.getModel().getOptions().serialize());
60  
61    }
62  
63  }