001package jmri.jmrit.logixng.implementation.swing;
002
003import java.awt.*;
004import java.awt.event.*;
005import java.util.List;
006import javax.swing.*;
007
008import jmri.InstanceManager;
009import jmri.jmrit.logixng.Base;
010import jmri.jmrit.logixng.LogixNG_Manager;
011
012/**
013 *
014 * @author Daniel Bergqvist 2021
015 */
016public class ErrorHandlingDialog_MultiLine {
017
018    private Base _item;
019    private JDialog _errorDialog;
020
021    private final JCheckBox _disableConditionalNGCheckBox =
022            new JCheckBox(Bundle.getMessage("ErrorHandlingDialog_DisableConditionalNG"));   // NOI18N
023
024    private final JCheckBox _disableLogixNGCheckBox =
025            new JCheckBox(Bundle.getMessage("ErrorHandlingDialog_DisableLogixNG"));   // NOI18N
026
027    private final JCheckBox _stopAllLogixNGCheckBox =
028            new JCheckBox(Bundle.getMessage("ErrorHandlingDialog_StopAllLogixNGs"));   // NOI18N
029
030    private boolean _abortExecution = false;
031
032
033    public boolean showDialog(Base item, String errorMessage, List<String> errorMessageList) {
034
035        _item = item;
036
037        _errorDialog = new JDialog(
038                (JDialog)null,
039                Bundle.getMessage("ErrorHandlingDialog_Title"),
040                true);
041
042        Container contentPanel = _errorDialog.getContentPane();
043        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
044
045        contentPanel.add(new JLabel(Bundle.getMessage(
046                "ErrorHandlingDialog_Name", item.getShortDescription())));
047        contentPanel.add(Box.createVerticalStrut(10));
048        contentPanel.add(new JLabel(errorMessage));
049        contentPanel.add(Box.createVerticalStrut(10));
050        String errorMsg = String.join("<br>", errorMessageList);
051        contentPanel.add(new JLabel("<html>"+errorMsg+"</html>"));
052        contentPanel.add(Box.createVerticalStrut(10));
053
054        contentPanel.add(_disableConditionalNGCheckBox);
055        _disableConditionalNGCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
056        contentPanel.add(_disableLogixNGCheckBox);
057        _disableLogixNGCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
058        contentPanel.add(_stopAllLogixNGCheckBox);
059        _stopAllLogixNGCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
060
061        // set up message
062        JPanel panel3 = new JPanel();
063        panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
064
065        contentPanel.add(panel3);
066
067        // set up create and cancel buttons
068        JPanel panel5 = new JPanel();
069        panel5.setLayout(new FlowLayout());
070
071        // Abort
072        JButton abortButton = new JButton(Bundle.getMessage("ErrorHandlingDialog_Abort"));  // NOI18N
073        panel5.add(abortButton);
074        abortButton.addActionListener((ActionEvent e) -> {
075            abortPressed(null);
076        });
077//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
078//        abortButton.setToolTipText("CancelLogixButtonHint");      // NOI18N
079
080        // Continue
081        JButton continueButton = new JButton(Bundle.getMessage("ErrorHandlingDialog_Continue"));    // NOI18N
082        panel5.add(continueButton);
083        continueButton.addActionListener((ActionEvent e) -> {
084            continuePressed(null);
085        });
086//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
087//        continueButton.setToolTipText("CancelLogixButtonHint");      // NOI18N
088
089        _errorDialog.addWindowListener(new java.awt.event.WindowAdapter() {
090            @Override
091            public void windowClosing(java.awt.event.WindowEvent e) {
092                continuePressed(null);
093            }
094        });
095
096        contentPanel.add(panel5);
097
098        // Sometimes the dialog isn't high enough. Add some extra space at the bottom.
099        contentPanel.add(Box.createVerticalStrut(10));
100
101//        addLogixNGFrame.setLocationRelativeTo(component);
102        _errorDialog.setLocationRelativeTo(null);
103        _errorDialog.pack();
104        _errorDialog.setVisible(true);
105
106        return _abortExecution;
107    }
108
109    private void handleCheckBoxes() {
110        if (_disableConditionalNGCheckBox.isSelected()) {
111            _item.getConditionalNG().setEnabled(false);
112        }
113        if (_disableLogixNGCheckBox.isSelected()) {
114            _item.getLogixNG().setEnabled(false);
115        }
116        if (_stopAllLogixNGCheckBox.isSelected()) {
117            InstanceManager.getDefault(LogixNG_Manager.class).deActivateAllLogixNGs();
118        }
119    }
120
121    private void abortPressed(ActionEvent e) {
122        _errorDialog.setVisible(false);
123        _errorDialog.dispose();
124        _errorDialog = null;
125        _abortExecution = true;
126        handleCheckBoxes();
127    }
128
129    private void continuePressed(ActionEvent e) {
130        _errorDialog.setVisible(false);
131        _errorDialog.dispose();
132        _errorDialog = null;
133        handleCheckBoxes();
134    }
135
136}