001package jmri.jmrit.logixng.tools.swing;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.WindowEvent;
005import java.util.*;
006
007import jmri.InstanceManager;
008import jmri.jmrit.logixng.LogixNG_Manager;
009import jmri.util.swing.JmriAbstractAction;
010import jmri.util.swing.JmriPanel;
011
012/**
013 * Swing action to edit the error handling module.
014 *
015 * @author Daniel Bergqvist Copyright (C) 2025
016 */
017public class EditErrorHandlingModuleAction extends JmriAbstractAction {
018
019    private ErrorModuleEditor _errorModuleEditor;
020
021    public EditErrorHandlingModuleAction() {
022        super(getTitle());
023        InstanceManager.getDefault(LogixNG_Manager.class)
024                .addPropertyChangeListener(LogixNG_Manager.PROPERTY_SETUP, (evt) -> {
025                    setName(getTitle());
026                });
027    }
028
029    public static String getTitle() {
030        if (InstanceManager.getDefault(LogixNG_Manager.class).isErrorHandlingModuleEnabled()) {
031            return Bundle.getMessage("TitleErrorHandlingModuleEditor_Enabled");
032        } else {
033            return Bundle.getMessage("TitleErrorHandlingModuleEditor");
034        }
035    }
036
037    @Override
038    public void actionPerformed(ActionEvent e) {
039        if (_errorModuleEditor == null) {
040            _errorModuleEditor = new ErrorModuleEditor();
041            _errorModuleEditor.initComponents();
042            _errorModuleEditor.setVisible(true);
043
044            _errorModuleEditor.addEditorEventListener(() -> {
045                _errorModuleEditor.editorData.forEach((key, value) -> {
046                    if (key.equals("Finish")) {                  // NOI18N
047                        _errorModuleEditor = null;
048                        setName(getTitle());
049                    }
050                });
051            });
052        } else {
053            _errorModuleEditor.setVisible(true);
054        }
055    }
056
057
058    public interface EditorEventListener extends EventListener {
059
060        public void editorEventOccurred();
061    }
062
063
064    /**
065     * Editor of the error handling module.
066     */
067    public static class ErrorModuleEditor extends TreeEditor {
068
069        /**
070         * Maintain a list of listeners -- normally only one.
071         */
072        private final List<EditorEventListener> listenerList = new ArrayList<>();
073
074        /**
075         * This contains a list of commands to be processed by the listener
076         * recipient.
077         */
078        final HashMap<String, String> editorData = new HashMap<>();
079
080        /**
081         * Construct a ConditionalEditor.
082         */
083        public ErrorModuleEditor() {
084            super(InstanceManager.getDefault(LogixNG_Manager.class).getErrorHandlingModuleSocket(),
085                    TreeEditor.EnableClipboard.EnableClipboard,
086                    TreeEditor.EnableRootRemoveCutCopy.EnableRootRemoveCutCopy,
087                    TreeEditor.EnableRootPopup.EnableRootPopup,
088                    TreeEditor.EnableExecuteEvaluate.DisableExecuteEvaluate
089            );
090
091            ErrorModuleEditor.this.setTitle(Bundle.getMessage("TitleErrorHandlingModuleEditor"));
092
093            ErrorModuleEditor.this.setRootVisible(false);
094        }
095
096        /** {@inheritDoc} */
097        @Override
098        public void windowClosed(WindowEvent e) {
099            editorData.clear();
100            editorData.put("Finish", "Editor");  // NOI18N
101            fireEditorEvent();
102        }
103
104        public void addEditorEventListener(EditorEventListener listener) {
105            listenerList.add(listener);
106        }
107
108        /**
109         * Notify the listeners to check for new data.
110         */
111        void fireEditorEvent() {
112            for (EditorEventListener l : listenerList) {
113                l.editorEventOccurred();
114            }
115        }
116
117    }
118
119    @Override
120    public JmriPanel makePanel() {
121        throw new IllegalArgumentException("Should not be invoked");
122    }
123
124}