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                    EnableClipboard.EnableClipboard,
086                    EnableRootRemoveCutCopy.EnableRootRemoveCutCopy,
087                    EnableRootPopup.EnableRootPopup,
088                    EnableExecuteEvaluate.DisableExecuteEvaluate,
089                    EnableChangeUsernameForRoot.EnableChangeUsername
090            );
091
092            ErrorModuleEditor.this.setTitle(Bundle.getMessage("TitleErrorHandlingModuleEditor"));
093
094            ErrorModuleEditor.this.setRootVisible(false);
095        }
096
097        /** {@inheritDoc} */
098        @Override
099        public void windowClosed(WindowEvent e) {
100            editorData.clear();
101            editorData.put("Finish", "Editor");  // NOI18N
102            fireEditorEvent();
103        }
104
105        public void addEditorEventListener(EditorEventListener listener) {
106            listenerList.add(listener);
107        }
108
109        /**
110         * Notify the listeners to check for new data.
111         */
112        void fireEditorEvent() {
113            for (EditorEventListener l : listenerList) {
114                l.editorEventOccurred();
115            }
116        }
117
118    }
119
120    @Override
121    public JmriPanel makePanel() {
122        throw new IllegalArgumentException("Should not be invoked");
123    }
124
125}