001package jmri.jmrit.beantable.beanedit; 002 003import java.awt.event.ActionEvent; 004import javax.swing.AbstractAction; 005import javax.swing.JCheckBox; 006import javax.swing.JSpinner; 007import javax.swing.JTextField; 008import javax.swing.SpinnerNumberModel; 009 010import jmri.InstanceManager; 011import jmri.Sensor; 012import jmri.SensorManager; 013 014/** 015 * Provides an edit panel for a sensor debounce object. 016 * <p> 017 * This is designed so that it can be re-used in other panels. 018 * {@link jmri.jmrit.beantable.beanedit.BlockEditAction#sensor()} 019 * 020 * @author Kevin Dickerson Copyright (C) 2011 021 * @author Egbert Broerse Copyright (C) 2017 022 */ 023public class SensorDebounceEditAction extends BeanEditAction<Sensor> { 024 025 @Override 026 public String helpTarget() { 027 return "package.jmri.jmrit.beantable.SensorAddEdit"; 028 } // NOI18N 029 030 @Override 031 public Sensor getByUserName(String name) { 032 return InstanceManager.sensorManagerInstance().getByUserName(name); 033 } 034 035 private final JCheckBox sensorDebounceGlobalCheck = new JCheckBox(); 036 private final JSpinner sensorDebounceInactiveSpinner = new JSpinner(); 037 private final JSpinner sensorDebounceActiveSpinner = new JSpinner(); 038 039 @Override 040 protected void initPanels() { 041 } 042 043 @Override 044 public void setBean(Sensor bean) { 045 super.setBean(bean); 046 if (bean == null) { 047 enabled(false); 048 } else { 049 resetDebounceItems(null); // Get this to retrieve the current details. 050 enabled(true); 051 } 052 } 053 054 public BeanItemPanel sensorDebounce(BeanItemPanel basic) { 055 if (basic == null) { 056 basic = new BeanItemPanel(); 057 basic.setName(Bundle.getMessage("SensorDebounce")); 058 } 059 060 sensorDebounceGlobalCheck.addActionListener((ActionEvent e) -> { 061 sensorDebounceInactiveSpinner.setEnabled(!sensorDebounceGlobalCheck.isSelected()); 062 sensorDebounceActiveSpinner.setEnabled(!sensorDebounceGlobalCheck.isSelected()); 063 }); 064 065 sensorDebounceInactiveSpinner.setModel( 066 new SpinnerNumberModel((Long)0L, (Long)0L, Sensor.MAX_DEBOUNCE, (Long)1L)); // MAX_DEBOUNCE is a Long; casts are to force needed signature 067 sensorDebounceInactiveSpinner.setPreferredSize(new JTextField(Long.toString(Sensor.MAX_DEBOUNCE).length()+1).getPreferredSize()); 068 sensorDebounceActiveSpinner.setModel( 069 new SpinnerNumberModel((Long)0L, (Long)0L, Sensor.MAX_DEBOUNCE, (Long)1L)); // MAX_DEBOUNCE is a Long; casts are to force needed signature 070 sensorDebounceActiveSpinner.setPreferredSize(new JTextField(Long.toString(Sensor.MAX_DEBOUNCE).length()+1).getPreferredSize()); 071 072 basic.addItem(new BeanEditItem(null, null, Bundle.getMessage("SensorDebounceText"))); 073 basic.addItem(new BeanEditItem(sensorDebounceGlobalCheck, Bundle.getMessage("SensorDebounceUseGlobalText"), Bundle.getMessage("SensorGlobalActiveInactiveDelays", 074 InstanceManager.getDefault(SensorManager.class).getDefaultSensorDebounceGoingActive(), 075 InstanceManager.getDefault(SensorManager.class).getDefaultSensorDebounceGoingInActive()))); 076 basic.addItem(new BeanEditItem(sensorDebounceActiveSpinner, Bundle.getMessage("SensorActiveDebounce"), Bundle.getMessage("SensorActiveDebounceText"))); 077 basic.addItem(new BeanEditItem(sensorDebounceInactiveSpinner, Bundle.getMessage("SensorInActiveDebounce"), Bundle.getMessage("SensorInActiveDebounceText"))); 078 079 basic.setSaveItem(new AbstractAction() { 080 @Override 081 public void actionPerformed(ActionEvent e) { 082 saveDebounceItems(e); 083 } 084 }); 085 basic.setResetItem(new AbstractAction() { 086 @Override 087 public void actionPerformed(ActionEvent e) { 088 resetDebounceItems(e); 089 } 090 }); 091 092 return basic; 093 } 094 095 protected void saveDebounceItems(ActionEvent e) { 096 if (bean == null) { 097 return; 098 } 099 100 long time = (long) sensorDebounceActiveSpinner.getValue(); 101 bean.setSensorDebounceGoingActiveTimer(time); 102 103 time = (long) sensorDebounceInactiveSpinner.getValue(); 104 bean.setSensorDebounceGoingInActiveTimer(time); 105 bean.setUseDefaultTimerSettings(sensorDebounceGlobalCheck.isSelected()); 106 } 107 108 protected void resetDebounceItems(ActionEvent e) { 109 if (bean == null) { 110 enabled(false); 111 return; 112 } 113 enabled(true); 114 sensorDebounceGlobalCheck.setSelected(bean.getUseDefaultTimerSettings()); 115 116 sensorDebounceActiveSpinner.setEnabled(!bean.getUseDefaultTimerSettings()); 117 sensorDebounceInactiveSpinner.setEnabled(!bean.getUseDefaultTimerSettings()); 118 119 sensorDebounceActiveSpinner.setValue(bean.getSensorDebounceGoingActiveTimer()); // as long 120 sensorDebounceInactiveSpinner.setValue(bean.getSensorDebounceGoingInActiveTimer()); 121 } 122 123 public void enabled(Boolean boo) { 124 sensorDebounceGlobalCheck.setEnabled(boo); 125 sensorDebounceInactiveSpinner.setEnabled(boo); 126 sensorDebounceActiveSpinner.setEnabled(boo); 127 } 128 129}