001package jmri.jmrit.display.layoutEditor.LayoutEditorDialogs;
002
003import java.awt.*;
004import java.awt.event.*;
005import java.text.MessageFormat;
006
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.InvokeOnGuiThread;
011import jmri.jmrit.display.layoutEditor.LayoutEditor;
012import jmri.util.JmriJFrame;
013import jmri.util.MathUtil;
014import jmri.util.swing.JmriJOptionPane;
015
016/**
017 * Layout Editor Dialogs implements some dialogs for the Layout Editor
018 *
019 * @author George Warner Copyright (c) 2019
020 */
021public class EnterGridSizesDialog {
022
023    // operational instance variables shared between dialogs
024    private LayoutEditor layoutEditor = null;
025
026    // constructor method
027    public EnterGridSizesDialog(@Nonnull LayoutEditor thePanel) {
028        layoutEditor = thePanel;
029    }
030
031    /*====================================*\
032    |* Dialog box to enter new grid sizes *|
033    \*====================================*/
034    //operational variables for enter grid sizes pane
035    private JmriJFrame enterGridSizesFrame = null;
036    private boolean enterGridSizesOpen = false;
037    private boolean gridSizesChange = false;
038    private final JTextField primaryGridSizeField = new JTextField(6);
039    private final JTextField secondaryGridSizeField = new JTextField(6);
040    private JButton gridSizesDone;
041    private JButton gridSizesCancel;
042
043    //display dialog for entering grid sizes
044    @InvokeOnGuiThread
045    public void enterGridSizes() {
046        if (enterGridSizesOpen) {
047            enterGridSizesFrame.setVisible(true);
048            return;
049        }
050
051        //Initialize if needed
052        if (enterGridSizesFrame == null) {
053            enterGridSizesFrame = new JmriJFrame(Bundle.getMessage("SetGridSizes"));
054            enterGridSizesFrame.addHelpMenu("package.jmri.jmrit.display.EnterGridSizes", true);
055            enterGridSizesFrame.setLocation(70, 30);
056            Container theContentPane = enterGridSizesFrame.getContentPane();
057            theContentPane.setLayout(new BoxLayout(theContentPane, BoxLayout.PAGE_AXIS));
058
059            //setup primary grid sizes
060            JPanel panel3 = new JPanel();
061            panel3.setLayout(new FlowLayout());
062            JLabel primaryGridSizeLabel = new JLabel(Bundle.getMessage("PrimaryGridSize"));
063            panel3.add(primaryGridSizeLabel);
064            primaryGridSizeLabel.setLabelFor(primaryGridSizeField);
065            panel3.add(primaryGridSizeField);
066            primaryGridSizeField.setToolTipText(Bundle.getMessage("PrimaryGridSizeHint"));
067            theContentPane.add(panel3);
068
069            //setup side track width
070            JPanel panel2 = new JPanel();
071            panel2.setLayout(new FlowLayout());
072            JLabel secondaryGridSizeLabel = new JLabel(Bundle.getMessage("SecondaryGridSize"));
073            panel2.add(secondaryGridSizeLabel);
074            secondaryGridSizeLabel.setLabelFor(secondaryGridSizeField);
075            panel2.add(secondaryGridSizeField);
076            secondaryGridSizeField.setName("SecondaryGridSize");
077            secondaryGridSizeField.setToolTipText(Bundle.getMessage("SecondaryGridSizeHint"));
078            theContentPane.add(panel2);
079
080            //set up Done and Cancel buttons
081            JPanel panel5 = new JPanel();
082            panel5.setLayout(new FlowLayout());
083            panel5.add(gridSizesDone = new JButton(Bundle.getMessage("ButtonDone")));
084            gridSizesDone.addActionListener(this::gridSizesDonePressed);
085            gridSizesDone.setToolTipText(Bundle.getMessage("DoneHint", Bundle.getMessage("ButtonDone")));
086
087            //Cancel
088            panel5.add(gridSizesCancel = new JButton(Bundle.getMessage("ButtonCancel")));
089            gridSizesCancel.addActionListener(this::gridSizesCancelPressed);
090            gridSizesCancel.setToolTipText(Bundle.getMessage("CancelHint", Bundle.getMessage("ButtonCancel")));
091            theContentPane.add(panel5);
092
093            //make this button the default button (return or enter activates)
094            JRootPane rootPane = SwingUtilities.getRootPane(gridSizesDone);
095            rootPane.setDefaultButton(gridSizesDone);
096        }
097
098        //Set up for Entry of Track Widths
099        primaryGridSizeField.setText(Integer.toString(layoutEditor.gContext.getGridSize()));
100        secondaryGridSizeField.setText(Integer.toString(layoutEditor.gContext.getGridSize2nd()));
101        enterGridSizesFrame.addWindowListener(new WindowAdapter() {
102            @Override
103            public void windowClosing(WindowEvent event) {
104                gridSizesCancelPressed(null);
105            }
106        });
107        enterGridSizesFrame.pack();
108        enterGridSizesFrame.setVisible(true);
109        gridSizesChange = false;
110        enterGridSizesOpen = true;
111    }
112
113    private void gridSizesDonePressed(@Nonnull ActionEvent event) {
114        String newGridSize = "";
115        float siz = 0.0F;
116
117        //get secondary grid size
118        newGridSize = secondaryGridSizeField.getText().trim();
119        try {
120            siz = Float.parseFloat(newGridSize);
121        } catch (NumberFormatException e) {
122            showEntryErrorDialog(enterGridSizesFrame, e);
123            return;
124        }
125
126        if ((siz < 5.0) || (siz > 100.0)) {
127            JmriJOptionPane.showMessageDialog(enterGridSizesFrame,
128                    MessageFormat.format(Bundle.getMessage("Error2a"), String.format(" %s ", siz)),
129                    Bundle.getMessage("ErrorTitle"),
130                    JmriJOptionPane.ERROR_MESSAGE);
131
132            return;
133        }
134
135        if (!MathUtil.equals(layoutEditor.gContext.getGridSize2nd(), siz)) {
136            layoutEditor.gContext.setGridSize2nd((int) siz);
137            gridSizesChange = true;
138        }
139
140        //get mainline track width
141        newGridSize = primaryGridSizeField.getText().trim();
142        try {
143            siz = Float.parseFloat(newGridSize);
144        } catch (NumberFormatException e) {
145            showEntryErrorDialog(enterGridSizesFrame, e);
146            return;
147        }
148
149        if ((siz < 5) || (siz > 100.0)) {
150            JmriJOptionPane.showMessageDialog(enterGridSizesFrame,
151                    MessageFormat.format(Bundle.getMessage("Error2a"), String.format(" %s ", siz)),
152                    Bundle.getMessage("ErrorTitle"),
153                    JmriJOptionPane.ERROR_MESSAGE);
154        } else {
155            if (!MathUtil.equals(layoutEditor.gContext.getGridSize(), siz)) {
156                layoutEditor.gContext.setGridSize((int) siz);
157                gridSizesChange = true;
158            }
159            gridSizesCancelPressed(null);
160        }
161    }
162
163    private void gridSizesCancelPressed(ActionEvent event) {
164        enterGridSizesOpen = false;
165        enterGridSizesFrame.setVisible(false);
166        enterGridSizesFrame.dispose();
167        enterGridSizesFrame = null;
168
169        if (gridSizesChange) {
170            layoutEditor.redrawPanel();
171            layoutEditor.setDirty();
172        }
173    }
174
175    /**
176     * showEntryErrorDialog(Component parentComponent, NumberFormatException e)
177     *
178     * @param parentComponent determines the <code>Frame</code> in which the
179     *                        dialog is displayed; if <code>null</code>, or if
180     *                        the <code>parentComponent</code> has no
181     *                        <code>Frame</code>, a default <code>Frame</code>
182     *                        is used
183     * @param e               Exception thrown to indicate that the application
184     *                        has attempted to convert a string to one of the
185     *                        numeric types, but that the string does not have
186     *                        the appropriate format.
187     */
188    private void showEntryErrorDialog(Component parentComponent, NumberFormatException e) {
189        JmriJOptionPane.showMessageDialog(parentComponent,
190                String.format("%s: %s %s", Bundle.getMessage("EntryError"),
191                        e, Bundle.getMessage("TryAgain")),
192                Bundle.getMessage("ErrorTitle"),
193                JmriJOptionPane.ERROR_MESSAGE);
194    }
195
196//    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EnterGridSizesDialog.class);
197
198}