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