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