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//        abortButton.setToolTipText(Bundle.getMessage("LogixNG_CancelButtonHint"));      // NOI18N
074        
075        // Continue
076        JButton continueButton = new JButton(Bundle.getMessage("ErrorHandlingDialog_Continue"));    // NOI18N
077        panel5.add(continueButton);
078        continueButton.addActionListener((ActionEvent e) -> {
079            continuePressed(null);
080        });
081//        continueButton.setToolTipText("LogixNG_ContinueButtonHint");      // NOI18N
082        
083        _errorDialog.addWindowListener(new java.awt.event.WindowAdapter() {
084            @Override
085            public void windowClosing(java.awt.event.WindowEvent e) {
086                continuePressed(null);
087            }
088        });
089/*        
090        _create = new JButton(Bundle.getMessage("ButtonCreate"));  // NOI18N
091        panel5.add(_create);
092        _create.addActionListener((ActionEvent e) -> {
093            cancelAddPressed(null);
094            
095            SwingConfiguratorInterface swingConfiguratorInterface =
096                    _swingConfiguratorComboBox.getItemAt(_swingConfiguratorComboBox.getSelectedIndex());
097//            System.err.format("swingConfiguratorInterface: %s%n", swingConfiguratorInterface.getClass().getName());
098            createAddFrame(femaleSocket, path, swingConfiguratorInterface);
099        });
100*/        
101        contentPanel.add(panel5);
102        
103//        addLogixNGFrame.setLocationRelativeTo(component);
104        _errorDialog.setLocationRelativeTo(null);
105        _errorDialog.pack();
106        _errorDialog.setVisible(true);
107        
108        return _abortExecution;
109    }
110    
111    private void handleCheckBoxes() {
112        if (_disableConditionalNGCheckBox.isSelected()) {
113            _item.getConditionalNG().setEnabled(false);
114        }
115        if (_disableLogixNGCheckBox.isSelected()) {
116            _item.getLogixNG().setEnabled(false);
117        }
118        if (_stopAllLogixNGCheckBox.isSelected()) {
119            InstanceManager.getDefault(LogixNG_Manager.class).deActivateAllLogixNGs();
120        }
121    }
122    
123    private void abortPressed(ActionEvent e) {
124        _errorDialog.setVisible(false);
125        _errorDialog.dispose();
126        _errorDialog = null;
127        _abortExecution = true;
128        handleCheckBoxes();
129    }
130    
131    private void continuePressed(ActionEvent e) {
132        _errorDialog.setVisible(false);
133        _errorDialog.dispose();
134        _errorDialog = null;
135        handleCheckBoxes();
136    }
137    
138}