001package jmri.jmrit.display.controlPanelEditor.shape;
002
003import java.awt.Rectangle;
004import java.awt.event.ActionEvent;
005import java.awt.geom.Rectangle2D;
006
007import javax.swing.BoxLayout;
008import javax.swing.JLabel;
009import javax.swing.JPanel;
010import javax.swing.JTextField;
011
012import jmri.jmrit.display.Editor;
013import jmri.util.swing.JmriMouseEvent;
014import jmri.util.swing.JmriMouseMotionListener;
015
016/**
017 * @author Pete Cressman Copyright (c) 2012
018 */
019public class DrawRectangle extends DrawFrame {
020
021    int _width;
022    int _height;
023    JTextField _widthText;
024    JTextField _heightText;
025
026    public DrawRectangle(String which, String title, PositionableShape ps, Editor ed, boolean create) {
027        super(which, title, ps, ed, create);
028        _lineWidth = 3;
029    }
030
031    @Override
032    protected JPanel makeParamsPanel() {
033        JPanel panel = super.makeParamsPanel();
034        JPanel p = new JPanel();
035        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
036        JPanel pp = new JPanel();
037        _widthText = new JTextField(6);
038        _width = _shape.getWidth();
039        _widthText.setText(Integer.toString(_width));
040        _widthText.setHorizontalAlignment(JTextField.RIGHT);
041        pp.add(_widthText);
042        pp.add(new JLabel(Bundle.getMessage("width")));
043
044        p.add(pp);
045        _widthText.addActionListener((ActionEvent e) -> {
046            _shape.setWidth(getInteger(_widthText, _width));
047            updateShape();
048        });
049
050        pp = new JPanel();
051        _heightText = new JTextField(6);
052        _height = _shape.getHeight();
053        _heightText.setText(Integer.toString(_height));
054        _heightText.setHorizontalAlignment(JTextField.RIGHT);
055        pp.add(_heightText);
056        pp.add(new JLabel(Bundle.getMessage("height")));
057        p.add(pp);
058        _heightText.addActionListener((ActionEvent e) -> {
059            _shape.setHeight(getInteger(_heightText, _height));
060            updateShape();
061        });
062        p.addMouseMotionListener(JmriMouseMotionListener.adapt(
063                new JmriMouseMotionListener() {
064                    @Override
065                    public void mouseDragged(JmriMouseEvent e) {
066                        updateShape();
067                    }
068
069                    @Override
070                    public void mouseMoved(JmriMouseEvent e) {
071                        _shape.setWidth(getInteger(_widthText, _width));
072                        _shape.setHeight(getInteger(_heightText, _height));
073                        updateShape();
074                    }
075                }));
076
077        panel.add(p);
078        return panel;
079    }
080
081    @Override
082    protected PositionableShape makeFigure(Rectangle r, Editor ed) {
083        if (r != null) {
084            Rectangle2D.Double rr = new Rectangle2D.Double(0, 0, _width, _height);
085            _shape = new PositionableRectangle(ed, rr);
086        }
087        return _shape;
088    }
089
090    @Override
091    void setDisplayWidth(int w) {
092        _widthText.setText(Integer.toString(w));
093    }
094
095    @Override
096    void setDisplayHeight(int h) {
097        _heightText.setText(Integer.toString(h));
098    }
099
100}