001package jmri.jmrit.display;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005import java.awt.event.ActionEvent;
006import javax.swing.AbstractAction;
007import javax.swing.SpinnerNumberModel;
008import javax.swing.event.ChangeListener;
009
010/**
011 * Displays and allows user to modify {@literal x & y} coordinates of
012 * positionable labels This class has been generalized to provide popup edit
013 * dialogs for positionable item properties when TextFields are needed to input
014 * data.
015 * <p>
016 * The class name no longer identifies the full purpose of the class, However
017 * the name is retained because coordinate editing was the genesis. The current
018 * list of properties served for editing is:
019 * <ul>
020 * <li>modify {@literal x & y} coordinates modify level modify tooltip modify
021 * border size</li>
022 * <li>modify margin size modify fixed size modify rotation degress modify
023 * scaling</li>
024 * <li>modify text labels modify zoom scaling modify panel name</li>
025 * </ul>
026 * To use, write a static method that provides the dialog frame. Then write an
027 * initX method that customizes the dialog for the property.
028 *
029 * @author Dan Boudreau Copyright (C) 2007
030 * @author Pete Cressman Copyright (C) 2010
031 */
032public class MemoryIconCoordinateEdit extends CoordinateEdit {
033
034    MemoryOrGVIcon pl;    // positional label tracked by this frame
035    int oldX;
036    int oldY;
037    double oldD;
038    String oldStr;
039
040    public void init(String title, MemoryOrGVIcon pos, boolean showName) {
041        super.init(title, pos, showName);
042        pl = pos;
043    }
044
045    public static AbstractAction getCoordinateEditAction(final MemoryOrGVIcon pos) {
046        return new AbstractAction(Bundle.getMessage("SetXY", "")) {
047            @Override
048            public void actionPerformed(ActionEvent e) {
049                MemoryIconCoordinateEdit f = new MemoryIconCoordinateEdit();
050                f.addHelpMenu("package.jmri.jmrit.display.CoordinateEdit", true);
051                f.init(Bundle.getMessage("SetXY", ""), pos, true);
052                f.initSetXY();
053                f.setVisible(true);
054                f.setLocationRelativeTo(pos);
055            }
056        };
057    }
058
059    @Override
060    public void initSetXY() {
061        oldX = pl.getOriginalX();
062        oldY = pl.getOriginalY();
063
064        textX = new javax.swing.JLabel();
065        textX.setText("X: " + pl.getOriginalX());
066        textX.setVisible(true);
067        textY = new javax.swing.JLabel();
068        textY.setText("Y: " + pl.getOriginalY());
069        textY.setVisible(true);
070
071        SpinnerNumberModel model = new SpinnerNumberModel(0, 0, 10000, 1);
072        ChangeListener listener = e -> {
073            int x = ((Number) spinX.getValue()).intValue();
074            int y = ((Number) spinY.getValue()).intValue();
075            pl.setLocation(x, y);
076            textX.setText(" X: " + pl.getOriginalX());
077            textY.setText(" Y: " + pl.getOriginalY());
078        };
079        spinX = new javax.swing.JSpinner(model);
080        spinX.setValue(pl.getOriginalX());
081        spinX.setToolTipText(Bundle.getMessage("EnterXcoord"));
082        spinX.setMaximumSize(new Dimension(
083                spinX.getMaximumSize().width, spinX.getPreferredSize().height));
084        spinX.addChangeListener(listener);
085        model = new javax.swing.SpinnerNumberModel(0, 0, 10000, 1);
086        spinY = new javax.swing.JSpinner(model);
087        spinY.setValue(pl.getOriginalY());
088        spinY.setToolTipText(Bundle.getMessage("EnterYcoord"));
089        spinY.setMaximumSize(new Dimension(
090                spinY.getMaximumSize().width, spinY.getPreferredSize().height));
091        spinY.addChangeListener(listener);
092
093        getContentPane().setLayout(new GridBagLayout());
094
095        addSpinItems(true);
096
097        okButton.addActionListener(e -> {
098            int x = ((Number) spinX.getValue()).intValue();
099            int y = ((Number) spinY.getValue()).intValue();
100            pl.setLocation(x, y);
101            textX.setText(" X: " + pl.getOriginalX());
102            textY.setText(" Y: " + pl.getOriginalY());
103            dispose();
104        });
105        okButton.getRootPane().setDefaultButton(okButton);
106
107        cancelButton.addActionListener(e -> {
108            pl.setLocation(oldX, oldY);
109            dispose();
110        });
111        // make large enough to easily move
112        setMinimumSize(new Dimension(250, 175));
113        pack();
114    }
115}