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 java.text.MessageFormat;
011import javax.annotation.Nonnull;
012import javax.swing.*;
013
014import jmri.InstanceManager;
015import jmri.InvokeOnGuiThread;
016import jmri.Reporter;
017import jmri.ReporterManager;
018import jmri.jmrit.display.layoutEditor.LayoutEditor;
019import jmri.util.JmriJFrame;
020import jmri.util.swing.JmriJOptionPane;
021
022/**
023 * Layout Editor Dialogs implements some dialogs for the Layout Editor
024 *
025 * @author George Warner Copyright (c) 2019
026 */
027public class EnterReporterDialog {
028
029    // operational instance variables shared between tools
030    private LayoutEditor layoutEditor = null;
031
032    // constructor method
033    public EnterReporterDialog(@Nonnull LayoutEditor thePanel) {
034        layoutEditor = thePanel;
035    }
036
037    /*=======================================*\
038    |* Dialog box to enter new reporter info *|
039    \*=======================================*/
040    //operational variables for enter reporter pane
041    private transient JmriJFrame enterReporterFrame = null;
042    private boolean reporterOpen = false;
043    private final transient JTextField xPositionField = new JTextField(6);
044    private final transient JTextField yPositionField = new JTextField(6);
045    private final transient JTextField reporterNameField = new JTextField(6);
046    private transient JButton reporterDone;
047    private transient JButton reporterCancel;
048
049    //display dialog for entering Reporters
050    @InvokeOnGuiThread
051    public void enterReporter(int defaultX, int defaultY) {
052        if (reporterOpen) {
053            enterReporterFrame.setVisible(true);
054            return;
055        }
056
057        //Initialize if needed
058        if (enterReporterFrame == null) {
059            enterReporterFrame = new JmriJFrame(Bundle.getMessage("AddReporter"));
060
061//enterReporterFrame.addHelpMenu("package.jmri.jmrit.display.AddReporterLabel", true);
062            enterReporterFrame.setLocation(70, 30);
063            Container theContentPane = enterReporterFrame.getContentPane();
064            theContentPane.setLayout(new BoxLayout(theContentPane, BoxLayout.PAGE_AXIS));
065
066            //setup reporter entry
067            JPanel panel2 = new JPanel();
068            panel2.setLayout(new FlowLayout());
069            JLabel reporterLabel = new JLabel(Bundle.getMessage("ReporterName"));
070            panel2.add(reporterLabel);
071            reporterLabel.setLabelFor(reporterNameField);
072            panel2.add(reporterNameField);
073            reporterNameField.setToolTipText(Bundle.getMessage("ReporterNameHint"));
074            theContentPane.add(panel2);
075
076            //setup coordinates entry
077            JPanel panel3 = new JPanel();
078            panel3.setLayout(new FlowLayout());
079
080            JLabel xCoordLabel = new JLabel(Bundle.getMessage("ReporterLocationX"));
081            panel3.add(xCoordLabel);
082            xCoordLabel.setLabelFor(xPositionField);
083            panel3.add(xPositionField);
084            xPositionField.setToolTipText(Bundle.getMessage("ReporterLocationXHint"));
085
086            JLabel yCoordLabel = new JLabel(Bundle.getMessage("ReporterLocationY"));
087            panel3.add(yCoordLabel);
088            yCoordLabel.setLabelFor(yPositionField);
089            panel3.add(yPositionField);
090            yPositionField.setToolTipText(Bundle.getMessage("ReporterLocationYHint"));
091
092            theContentPane.add(panel3);
093
094            //set up Add and Cancel buttons
095            JPanel panel5 = new JPanel();
096            panel5.setLayout(new FlowLayout());
097            panel5.add(reporterDone = new JButton(Bundle.getMessage("AddNewLabel")));
098            reporterDone.addActionListener(this::reporterDonePressed);
099            reporterDone.setToolTipText(Bundle.getMessage("ReporterDoneHint"));
100
101            //Cancel
102            panel5.add(reporterCancel = new JButton(Bundle.getMessage("ButtonCancel")));
103            reporterCancel.addActionListener((ActionEvent event) -> reporterCancelPressed());
104            reporterCancel.setToolTipText(Bundle.getMessage("CancelHint", Bundle.getMessage("ButtonCancel")));
105            theContentPane.add(panel5);
106
107            //make this button the default button (return or enter activates)
108            JRootPane rootPane = SwingUtilities.getRootPane(reporterDone);
109            rootPane.setDefaultButton(reporterDone);
110        }
111
112        //Set up for Entry of Reporter Icon
113        xPositionField.setText(Integer.toString(defaultX));
114        yPositionField.setText(Integer.toString(defaultY));
115        enterReporterFrame.addWindowListener(new WindowAdapter() {
116            @Override
117            public void windowClosing(WindowEvent event) {
118                reporterCancelPressed();
119            }
120        });
121        enterReporterFrame.pack();
122        enterReporterFrame.setVisible(true);
123        reporterOpen = true;
124    }
125
126    private void reporterDonePressed(@Nonnull ActionEvent event) {
127        //get x coordinate
128        String newX = "";
129        int xx = 0;
130
131        newX = xPositionField.getText().trim();
132        try {
133            xx = Integer.parseInt(newX);
134        } catch (NumberFormatException e) {
135            showEntryErrorDialog(enterReporterFrame, e);
136            return;
137        }
138
139        if ((xx <= 0) || (xx > layoutEditor.gContext.getLayoutWidth())) {
140            log.error("invalid x: {}, LayoutWidth: {}", xx, layoutEditor.gContext.getLayoutWidth());
141            JmriJOptionPane.showMessageDialog(enterReporterFrame,
142                    MessageFormat.format(Bundle.getMessage("Error2a"), String.format(" %s ", xx)),
143                    Bundle.getMessage("ErrorTitle"),
144                    JmriJOptionPane.ERROR_MESSAGE);
145
146            return;
147        }
148
149        // get y coordinate
150        String newY = "";
151        int yy = 0;
152        newY = yPositionField.getText().trim();
153        try {
154            yy = Integer.parseInt(newY);
155        } catch (NumberFormatException e) {
156            showEntryErrorDialog(enterReporterFrame, e);
157            return;
158        }
159
160        if ((yy <= 0) || (yy > layoutEditor.gContext.getLayoutHeight())) {
161            log.error("invalid y: {}, LayoutWidth: {}", yy, layoutEditor.gContext.getLayoutHeight());
162            JmriJOptionPane.showMessageDialog(enterReporterFrame,
163                    MessageFormat.format(Bundle.getMessage("Error2a"), String.format(" %s ", yy)),
164                    Bundle.getMessage("ErrorTitle"),
165                    JmriJOptionPane.ERROR_MESSAGE);
166
167            return;
168        }
169
170        // get reporter name
171        Reporter reporter = null;
172        String rName = reporterNameField.getText();
173
174        if (InstanceManager.getNullableDefault(ReporterManager.class) != null) {
175            try {
176                reporter = InstanceManager.getDefault(ReporterManager.class).provideReporter(rName);
177            } catch (IllegalArgumentException e) {
178                JmriJOptionPane.showMessageDialog(enterReporterFrame,
179                        MessageFormat.format(Bundle.getMessage("Error18"), rName), Bundle.getMessage("ErrorTitle"),
180                        JmriJOptionPane.ERROR_MESSAGE);
181                return;
182            }
183        } else {
184            JmriJOptionPane.showMessageDialog(enterReporterFrame,
185                    Bundle.getMessage("Error17"), Bundle.getMessage("ErrorTitle"),
186                    JmriJOptionPane.ERROR_MESSAGE);
187
188            return;
189        }
190
191        //add the reporter icon
192        layoutEditor.addReporter(reporter, xx, yy);
193
194        reporterCancelPressed();
195    }
196
197    private void reporterCancelPressed() {
198        reporterOpen = false;
199        enterReporterFrame.setVisible(false);
200        enterReporterFrame.dispose();
201        enterReporterFrame = null;
202        layoutEditor.redrawPanel();
203    }
204
205    /**
206     * showEntryErrorDialog(Component parentComponent, NumberFormatException e)
207     *
208     * @param parentComponent determines the <code>Frame</code> in which the
209     *                        dialog is displayed; if <code>null</code>, or if
210     *                        the <code>parentComponent</code> has no
211     *                        <code>Frame</code>, a default <code>Frame</code>
212     *                        is used
213     * @param e               Exception thrown to indicate that the application
214     *                        has attempted to convert a string to one of the
215     *                        numeric types, but that the string does not have
216     *                        the appropriate format.
217     */
218    private void showEntryErrorDialog(Component parentComponent, NumberFormatException e) {
219        JmriJOptionPane.showMessageDialog(parentComponent,
220                String.format("%s: %s %s", Bundle.getMessage("EntryError"),
221                        e, Bundle.getMessage("TryAgain")),
222                Bundle.getMessage("ErrorTitle"),
223                JmriJOptionPane.ERROR_MESSAGE);
224    }
225
226    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EnterReporterDialog.class);
227
228}