001package jmri.jmrit.display.layoutEditor.LayoutEditorDialogs;
002
003import java.awt.Component;
004import java.awt.Container;
005import java.awt.FlowLayout;
006import java.awt.event.ActionEvent;
007import java.awt.event.WindowAdapter;
008import java.awt.event.WindowEvent;
009
010import javax.annotation.Nonnull;
011import javax.swing.*;
012
013import jmri.InvokeOnGuiThread;
014import jmri.jmrit.display.layoutEditor.LayoutEditor;
015import jmri.util.JmriJFrame;
016import jmri.util.swing.JmriJOptionPane;
017
018/**
019 * Implements the move selection dialog for the Layout Editor
020 *
021 * @author George Warner Copyright (c) 2019
022 */
023public class MoveSelectionDialog {
024
025    // operational instance variables
026    private LayoutEditor layoutEditor = null;
027
028    // constructor method
029    public MoveSelectionDialog(@Nonnull LayoutEditor thePanel) {
030        layoutEditor = thePanel;
031    }
032
033    /*=========================================*\
034    |* Dialog box to enter move selection info *|
035    \*=========================================*/
036    //operational variables for move selection pane
037    private JmriJFrame moveSelectionFrame = null;
038    private boolean moveSelectionOpen = false;
039    private final JTextField xMoveField = new JTextField(6);
040    private final JTextField yMoveField = new JTextField(6);
041    private JButton moveSelectionDone;
042    private JButton moveSelectionCancel;
043
044    //display dialog for translation a selection
045    @InvokeOnGuiThread
046    public void moveSelection() {
047        if (moveSelectionOpen) {
048            moveSelectionFrame.setVisible(true);
049            return;
050        }
051
052        //Initialize if needed
053        if (moveSelectionFrame == null) {
054            moveSelectionFrame = new JmriJFrame(Bundle.getMessage("TranslateSelection"));
055            moveSelectionFrame.addHelpMenu("package.jmri.jmrit.display.TranslateSelection", true);
056            moveSelectionFrame.setLocation(70, 30);
057            Container theContentPane = moveSelectionFrame.getContentPane();
058            theContentPane.setLayout(new BoxLayout(theContentPane, BoxLayout.PAGE_AXIS));
059
060            //setup x translate
061            JPanel panel31 = new JPanel();
062            panel31.setLayout(new FlowLayout());
063            JLabel xMoveLabel = new JLabel(Bundle.getMessage("XTranslateLabel"));
064            panel31.add(xMoveLabel);
065            xMoveLabel.setLabelFor(xMoveField);
066            panel31.add(xMoveField);
067            xMoveField.setName("XTranslateLabel");
068            xMoveField.setToolTipText(Bundle.getMessage("XTranslateHint"));
069            theContentPane.add(panel31);
070
071            //setup y translate
072            JPanel panel32 = new JPanel();
073            panel32.setLayout(new FlowLayout());
074            JLabel yMoveLabel = new JLabel(Bundle.getMessage("YTranslateLabel"));
075            panel32.add(yMoveLabel);
076            yMoveLabel.setLabelFor(yMoveField);
077            panel32.add(yMoveField);
078            yMoveField.setName("YTranslateLabel");
079            yMoveField.setToolTipText(Bundle.getMessage("YTranslateHint"));
080            theContentPane.add(panel32);
081
082            //setup information message
083            JPanel panel33 = new JPanel();
084            panel33.setLayout(new FlowLayout());
085            JLabel message1Label = new JLabel(Bundle.getMessage("Message3Label"));
086            panel33.add(message1Label);
087            theContentPane.add(panel33);
088
089            //set up Done and Cancel buttons
090            JPanel panel5 = new JPanel();
091            panel5.setLayout(new FlowLayout());
092            panel5.add(moveSelectionDone = new JButton(Bundle.getMessage("MoveSelection")));
093            moveSelectionDone.addActionListener(this::moveSelectionDonePressed);
094            moveSelectionDone.setToolTipText(Bundle.getMessage("MoveSelectionHint"));
095            panel5.add(moveSelectionCancel = new JButton(Bundle.getMessage("ButtonCancel")));
096            moveSelectionCancel.addActionListener((ActionEvent event) -> moveSelectionCancelPressed());
097            moveSelectionCancel.setToolTipText(Bundle.getMessage("CancelHint", Bundle.getMessage("ButtonCancel")));
098            theContentPane.add(panel5);
099
100            //make this button the default button (return or enter activates)
101            JRootPane rootPane = SwingUtilities.getRootPane(moveSelectionDone);
102            rootPane.setDefaultButton(moveSelectionDone);
103        }
104
105        //Set up for Entry of Translation
106        xMoveField.setText("0");
107        yMoveField.setText("0");
108        moveSelectionFrame.addWindowListener(new WindowAdapter() {
109            @Override
110            public void windowClosing(WindowEvent event) {
111                moveSelectionCancelPressed();
112            }
113        });
114        moveSelectionFrame.pack();
115        moveSelectionFrame.setVisible(true);
116        moveSelectionOpen = true;
117    }
118
119    private void moveSelectionDonePressed(@Nonnull ActionEvent event) {
120        String newText = "";
121        float xTranslation = 0.0F;
122        float yTranslation = 0.0F;
123
124        //get x translation
125        newText = xMoveField.getText().trim();
126        try {
127            xTranslation = Float.parseFloat(newText);
128        } catch (NumberFormatException e) {
129            showEntryErrorDialog(moveSelectionFrame, e);
130            return;
131        }
132
133        //get y translation
134        newText = yMoveField.getText().trim();
135        try {
136            yTranslation = Float.parseFloat(newText);
137        } catch (NumberFormatException e) {
138            showEntryErrorDialog(moveSelectionFrame, e);
139            return;
140        }
141
142        layoutEditor.translate(xTranslation, yTranslation);
143
144        moveSelectionCancelPressed();
145    }
146
147    private void moveSelectionCancelPressed() {
148        moveSelectionOpen = false;
149        moveSelectionFrame.setVisible(false);
150        moveSelectionFrame.dispose();
151        moveSelectionFrame = null;
152    }
153
154    /**
155     * showEntryErrorDialog(Component parentComponent, NumberFormatException e)
156     *
157     * @param parentComponent determines the <code>Frame</code> in which the
158     *                        dialog is displayed; if <code>null</code>, or if
159     *                        the <code>parentComponent</code> has no
160     *                        <code>Frame</code>, a default <code>Frame</code>
161     *                        is used
162     * @param e               Exception thrown to indicate that the application
163     *                        has attempted to convert a string to one of the
164     *                        numeric types, but that the string does not have
165     *                        the appropriate format.
166     */
167    private void showEntryErrorDialog(Component parentComponent, NumberFormatException e) {
168        JmriJOptionPane.showMessageDialog(parentComponent,
169                String.format("%s: %s %s", Bundle.getMessage("EntryError"),
170                        e, Bundle.getMessage("TryAgain")),
171                Bundle.getMessage("ErrorTitle"),
172                JmriJOptionPane.ERROR_MESSAGE);
173    }
174
175    //private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(
176    //        MoveSelectionDialog.class);
177}