001package jmri.jmrit.display.controlPanelEditor.shape;
002
003import java.awt.Rectangle;
004import java.awt.event.ActionEvent;
005import java.awt.geom.RoundRectangle2D;
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 DrawRoundRect extends DrawRectangle {
020
021    JTextField _radiusText;
022
023    public DrawRoundRect(String which, String title, PositionableShape ps, Editor ed, boolean create) {
024        super(which, title, ps, ed, create);
025    }
026
027    @Override
028    protected JPanel makeParamsPanel() {
029        JPanel panel = super.makeParamsPanel();
030        PositionableRoundRect shape = (PositionableRoundRect)_shape;
031        JPanel p = new JPanel();
032        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
033        JPanel pp = new JPanel();
034        _radiusText = new JTextField(6);
035        _radiusText.setText(Integer.toString(shape.getCornerRadius()));
036        _radiusText.setHorizontalAlignment(JTextField.RIGHT);
037        pp.add(_radiusText);
038        _radiusText.addActionListener((ActionEvent e) -> {
039            shape.setCornerRadius(getInteger(_radiusText, shape.getCornerRadius()));
040            updateShape();
041        });
042        _radiusText.addMouseMotionListener(JmriMouseMotionListener.adapt(
043                new JmriMouseMotionListener() {
044                    @Override
045                    public void mouseDragged(JmriMouseEvent e) {
046                        updateShape();
047                    }
048
049                    @Override
050                    public void mouseMoved(JmriMouseEvent e) {
051                        shape.setCornerRadius(getInteger(_radiusText, shape.getCornerRadius()));
052                        updateShape();
053                    }
054                }));
055        pp.add(new JLabel(Bundle.getMessage("cornerRadius")));
056        p.add(pp);
057        panel.add(p);
058        return panel;
059    }
060
061    @Override
062    protected PositionableShape makeFigure(Rectangle r, Editor ed) {
063        if (r != null) {
064            RoundRectangle2D.Double rr = new RoundRectangle2D.Double(0, 0, r.width, r.height, 40, 40);
065            _shape = new PositionableRoundRect(ed, rr);
066        }
067        return _shape;
068    }
069
070}