001package jmri.jmrit.beantable.light;
002
003import javax.swing.*;
004
005import jmri.Light;
006import jmri.VariableLight;
007
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Panel to display Light Intensity options.
013 * 
014 * Code originally within LightTableAction.
015 * 
016 * @author Dave Duchamp Copyright (C) 2004
017 * @author Egbert Broerse Copyright (C) 2017
018 * @author Steve Young Copyright (C) 2021
019 */
020public class LightIntensityPane extends JPanel {
021    
022    private JPanel minPan;
023    private JPanel maxPan;
024    private JPanel transitionPan;
025    
026    private JSpinner minIntensity;
027    private JSpinner maxIntensity;
028    private JSpinner transitionTime;
029    
030    private final JLabel status1 = new JLabel();
031    
032    /**
033     * Create a new Light Intensity Panel.
034     * 
035     * @param vertical true for vertical, false for horizontal display.
036     */
037    public LightIntensityPane( boolean vertical){
038        super();
039        init(vertical);
040    }
041    
042    
043    private void init(boolean vertical){
044    
045        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS ));
046        minIntensity = new JSpinner();
047        maxIntensity = new JSpinner();
048        transitionTime = new JSpinner();
049        JPanel mainPanel = new JPanel();
050        mainPanel.setLayout(new BoxLayout(mainPanel, ( vertical ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS)));
051        
052        minPan = new JPanel();
053        minPan.add(new JLabel(" "));
054        minPan.add(new JLabel(Bundle.getMessage("LightMinIntensity")));
055        minIntensity.setModel(
056                new SpinnerNumberModel(Double.valueOf(0.0d), Double.valueOf(0.0d), Double.valueOf(0.99d), Double.valueOf(0.01d))); // 0 - 99%
057        minIntensity.setEditor(new JSpinner.NumberEditor(minIntensity, "##0 %"));
058        minIntensity.setToolTipText(Bundle.getMessage("LightMinIntensityHint"));
059        minIntensity.setValue(0.0d); // reset JSpinner1
060        minPan.add(minIntensity);
061        minPan.add(new JLabel("   "));
062        mainPanel.add(minPan);
063        
064        maxPan = new JPanel();
065        maxPan.add(new JLabel(Bundle.getMessage("LightMaxIntensity")));
066        maxIntensity.setModel(
067                new SpinnerNumberModel(Double.valueOf(1.0d), Double.valueOf(0.01d), Double.valueOf(1.0d), Double.valueOf(0.01d))); // 100 - 1%
068        maxIntensity.setEditor(new JSpinner.NumberEditor(maxIntensity, "##0 %"));
069        maxIntensity.setToolTipText(Bundle.getMessage("LightMaxIntensityHint"));
070        maxIntensity.setValue(1.0d); // reset JSpinner2
071        maxPan.add(maxIntensity);
072        maxPan.add(new JLabel("   "));
073        mainPanel.add(maxPan);
074        
075        transitionPan = new JPanel();
076        transitionPan.add(new JLabel(Bundle.getMessage("LightTransitionTime")));
077        transitionTime.setModel(
078                new SpinnerNumberModel(Double.valueOf(0d), Double.valueOf(0d), Double.valueOf(1000000d), Double.valueOf(0.01d)));
079        transitionTime.setEditor(new JSpinner.NumberEditor(transitionTime, "###0.00"));
080        transitionTime.setPreferredSize(new JTextField(8).getPreferredSize());
081        transitionTime.setToolTipText(Bundle.getMessage("LightTransitionTimeHint"));
082        transitionTime.setValue(0.0); // reset from possible previous use
083        transitionPan.add(transitionTime);
084        transitionPan.add(new JLabel(" "));
085        mainPanel.add(transitionPan);
086        
087        add(mainPanel);
088        
089        JPanel statusPanel = new JPanel();
090        statusPanel.add(status1);
091        add(statusPanel);
092    
093    }
094    
095    /**
096     * Set the panel to match a Light.
097     * @param light the Light to set Panel for.
098     */
099    public void setToLight(Light light){
100        if (light instanceof VariableLight) {
101            minIntensity.setValue(((VariableLight)light).getMinIntensity()); // displayed as percentage
102            maxIntensity.setValue(((VariableLight)light).getMaxIntensity());
103            if (((VariableLight)light).isTransitionAvailable()) {
104                transitionTime.setValue(((VariableLight)light).getTransitionTime()); // displays i18n decimal separator eg. 0,00 in _nl
105            }
106            setupVariableDisplay(true, ((VariableLight)light).isTransitionAvailable());
107        } else {
108            setupVariableDisplay(false, false);
109        }
110    }
111    
112    /**
113     * Set a Light to match the Panel.
114     * @param light The Light to edit details for.
115     */
116    public void setLightFromPane(VariableLight light){
117    
118        if ((Double) minIntensity.getValue() >= (Double) maxIntensity.getValue()) {
119            log.error("minInt value entered: {}", minIntensity.getValue());
120            // do not set intensity
121            status1.setText(Bundle.getMessage("LightWarn9"));
122            status1.setVisible(true);
123        } else {
124            light.setMinIntensity((Double) minIntensity.getValue());
125            light.setMaxIntensity((Double) maxIntensity.getValue());
126        }
127        if (light.isTransitionAvailable()) {
128            light.setTransitionTime((Double) transitionTime.getValue());
129        }
130    
131    }
132    
133    /**
134     * Set up panel for Variable Options.
135     *
136     * @param showIntensity  true to show light intensity; false otherwise
137     * @param showTransition true to show time light takes to transition between
138     *                       states; false otherwise
139     */
140    private void setupVariableDisplay(boolean showIntensity, boolean showTransition) {
141        minPan.setVisible(showIntensity);
142        maxPan.setVisible(showIntensity);
143        transitionPan.setVisible(showTransition);
144        setVisible(showIntensity || showTransition);
145    }
146    
147    private final static Logger log = LoggerFactory.getLogger(LightIntensityPane.class);
148    
149}