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//        abortButton.setToolTipText(Bundle.getMessage("LogixNG_AbortButtonHint"));      // NOI18N
078
079        // Continue
080        JButton continueButton = new JButton(Bundle.getMessage("ErrorHandlingDialog_Continue"));    // NOI18N
081        panel5.add(continueButton);
082        continueButton.addActionListener((ActionEvent e) -> {
083            continuePressed(null);
084        });
085//        continueButton.setToolTipText("LogixNG_ContinueButtonHint");      // NOI18N
086
087        _errorDialog.addWindowListener(new java.awt.event.WindowAdapter() {
088            @Override
089            public void windowClosing(java.awt.event.WindowEvent e) {
090                continuePressed(null);
091            }
092        });
093
094        contentPanel.add(panel5);
095
096        // Sometimes the dialog isn't high enough. Add some extra space at the bottom.
097        contentPanel.add(Box.createVerticalStrut(10));
098
099//        addLogixNGFrame.setLocationRelativeTo(component);
100        _errorDialog.setLocationRelativeTo(null);
101        _errorDialog.pack();
102        _errorDialog.setVisible(true);
103
104        return _abortExecution;
105    }
106
107    private void handleCheckBoxes() {
108        if (_disableConditionalNGCheckBox.isSelected()) {
109            _item.getConditionalNG().setEnabled(false);
110        }
111        if (_disableLogixNGCheckBox.isSelected()) {
112            _item.getLogixNG().setEnabled(false);
113        }
114        if (_stopAllLogixNGCheckBox.isSelected()) {
115            InstanceManager.getDefault(LogixNG_Manager.class).deActivateAllLogixNGs();
116        }
117    }
118
119    private void abortPressed(ActionEvent e) {
120        _errorDialog.setVisible(false);
121        _errorDialog.dispose();
122        _errorDialog = null;
123        _abortExecution = true;
124        handleCheckBoxes();
125    }
126
127    private void continuePressed(ActionEvent e) {
128        _errorDialog.setVisible(false);
129        _errorDialog.dispose();
130        _errorDialog = null;
131        handleCheckBoxes();
132    }
133
134}