001package jmri.jmrit.beantable.beanedit;
002
003import java.awt.event.ActionEvent;
004import javax.swing.AbstractAction;
005import javax.swing.JComboBox;
006import jmri.InstanceManager;
007import jmri.Sensor;
008
009/**
010 * Provides an edit panel for the sensor pull up/pull down configuration.
011 * This is designed so that it can be re-used in other panels. This is based
012 * on the SensorDebounceEditAction class.
013 *
014 * @author Kevin Dickerson Copyright (C) 2011
015 * @author Kevin Dickerson Copyright (C) 2017 
016 */
017public class SensorPullUpEditAction extends BeanEditAction<Sensor> {
018
019    @Override
020    public String helpTarget() {
021        return "package.jmri.jmrit.beantable.SensorAddEdit";
022    } // NOI18N
023
024    @Override
025    public Sensor getByUserName(String name) {
026        return InstanceManager.sensorManagerInstance().getByUserName(name);
027    }
028
029    JComboBox<Sensor.PullResistance> sensorPullUpComboBox = new JComboBox<>(Sensor.PullResistance.values());
030
031    @Override
032    protected void initPanels() {
033    }
034
035    @Override
036    public void setBean(Sensor bean) {
037        super.setBean(bean);
038        if (bean == null) {
039            enabled(false);
040        } else {
041            resetPullUpItems(null); //Get this to retrieve the current details.
042            enabled(true);
043        }
044    }
045
046    public BeanItemPanel sensorPullUp(BeanItemPanel basic) {
047        if (basic == null) {
048            basic = new BeanItemPanel();
049            basic.setName(Bundle.getMessage("SensorPullUp"));
050        }
051
052        basic.addItem(new BeanEditItem(null,null,Bundle.getMessage("SensorPullUpText")));
053        basic.addItem(new BeanEditItem(sensorPullUpComboBox,Bundle.getMessage("SensorPullUp"),null));
054
055        basic.setSaveItem(new AbstractAction() {
056            @Override
057            public void actionPerformed(ActionEvent e) {
058                savePullUpItems(e);
059            }
060        });
061        basic.setResetItem(new AbstractAction() {
062            @Override
063            public void actionPerformed(ActionEvent e) {
064                resetPullUpItems(e);
065            }
066        });
067
068        return basic;
069    }
070
071    protected void savePullUpItems(ActionEvent e) {
072        if (bean == null) {
073            return;
074        }
075
076        bean.setPullResistance((Sensor.PullResistance)sensorPullUpComboBox.getSelectedItem());
077    }
078
079    protected void resetPullUpItems(ActionEvent e) {
080        if (bean == null) {
081            enabled(false);
082            return;
083        }
084        enabled(true);
085        sensorPullUpComboBox.setSelectedItem(bean.getPullResistance());
086    }
087
088    public void enabled(Boolean boo) {
089        sensorPullUpComboBox.setEnabled(boo);
090
091    }
092
093}