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