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