001package jmri.jmrit.display.controlPanelEditor;
002
003import java.awt.Dimension;
004import java.awt.event.ActionEvent;
005import javax.swing.JPanel;
006import javax.swing.JTextField;
007import javax.swing.JToggleButton;
008
009import jmri.jmrit.logix.OBlock;
010import jmri.util.swing.JmriJOptionPane;
011
012/**
013 * A simple panel to collect lengths with units bring either inches or centimeters
014 * 
015 * @author Pete Cressman Copyright: Copyright (c) 2019
016 *
017 */
018
019public class LengthPanel extends JPanel
020{
021    private final OBlock _block;
022    private float _length;
023    private String _label ;
024    private final JTextField _lengthField;
025    private final JToggleButton _units;
026    private static final java.text.DecimalFormat _lenFormat = new java.text.DecimalFormat("###,##0.0");
027    
028    public static final String PATH_LENGTH    = "pathLength" ;
029    public static final String BLOCK_LENGTH   = "blockLength" ;
030    public static final String ENTRANCE_SPACE = "entranceSpace" ;
031    
032
033    LengthPanel(OBlock block, String label, String tip) {
034        _block = block;
035        _label = label ;
036        
037        JPanel pp = new JPanel();
038        _lengthField = new JTextField();
039
040        _lengthField.setText(_lenFormat.format(0f));
041        pp.add(CircuitBuilder.makeTextBoxPanel(
042                false, _lengthField, _label, true, tip));
043        _lengthField.setPreferredSize(new Dimension(100, _lengthField.getPreferredSize().height));
044        _units = new JToggleButton("", !_block.isMetric());
045        _units.setToolTipText(Bundle.getMessage("TooltipPathUnitButton"));
046        _units.addActionListener((ActionEvent event) -> changeUnits());
047        pp.add(_units);
048        add(pp);
049    }
050
051    protected void changeUnits() {
052        String len = _lengthField.getText();
053        if (len == null || len.length() == 0) {
054            if (_block != null && _block.isMetric()) {
055                _units.setText("cm");
056            } else {
057                _units.setText("in");
058            }
059            return;
060        }
061        try {
062            float f = Float.parseFloat(len.replace(',', '.'));
063            if (_units.isSelected()) {
064                _lengthField.setText(_lenFormat.format(f / 2.54f));
065                _units.setText("in");
066            } else {
067                _lengthField.setText(_lenFormat.format(f * 2.54f));
068                _units.setText("cm");
069            }
070        } catch (NumberFormatException nfe) {
071            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("MustBeFloat", len),
072                    Bundle.getMessage("makePath"), JmriJOptionPane.INFORMATION_MESSAGE);
073        }
074    }
075
076    /**
077     * Display 
078     * @param len length in millimeters
079     */
080    protected void setLength(float len) {
081        _length = len;
082        if (_units.isSelected()) {
083            _lengthField.setText(_lenFormat.format(len / 25.4f));
084        } else {
085            _lengthField.setText(_lenFormat.format(len / 10));
086        }
087    }
088
089    protected float getLength() {
090        String num = null;
091        float f = -1;
092        try {
093            num = _lengthField.getText();
094            if (num == null || num.length() == 0) {
095                num = "0.0";
096            }
097            f = Float.parseFloat(num.replace(',', '.'));
098        } catch (NumberFormatException nfe) {
099        }
100        
101        if (_label.equals(PATH_LENGTH) || _label.equals(BLOCK_LENGTH)) {
102            if (f < 0.0f) {
103                JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("MustBeFloat", num),
104                        Bundle.getMessage("makePath"), JmriJOptionPane.INFORMATION_MESSAGE);
105            } else {
106                if (_units.isSelected()) {
107                    _length = f * 25.4f;
108                } else {
109                    _length = f * 10f;
110                }
111            }            
112        } else {
113            // ENTRANCE_SPACE
114                if (_units.isSelected()) {
115                    _length = f * 25.4f;
116                } else {
117                    _length = f * 10f;
118                }            
119        }
120        return _length;
121    }
122
123    protected boolean isChanged(float len) {
124        return Math.abs(getLength() - len) > .5;
125    }
126
127}