001package jmri.jmrit.logixng.tools.swing;
002
003import java.awt.event.*;
004import java.util.ArrayList;
005import java.util.EventListener;
006import java.util.HashMap;
007import java.util.List;
008
009import javax.annotation.Nonnull;
010
011import jmri.jmrit.logixng.FemaleSocket;
012import jmri.InstanceManager;
013import jmri.jmrit.logixng.*;
014
015/**
016 * Editor of ConditionalNG
017 * 
018 * @author Daniel Bergqvist 2018
019 */
020public class ConditionalNGEditor extends TreeEditor {
021
022    protected final ConditionalNG _conditionalNG;
023    
024    
025    /**
026     * Maintain a list of listeners -- normally only one.
027     */
028    private final List<ConditionalNGEventListener> listenerList = new ArrayList<>();
029    
030    /**
031     * This contains a list of commands to be processed by the listener
032     * recipient.
033     */
034    final HashMap<String, String> logixNGData = new HashMap<>();
035    
036    /**
037     * Construct a ConditionalEditor.
038     * <p>
039     * This is used by JmriUserPreferencesManager since it tries to create an
040     * instance of this class.
041     */
042    public ConditionalNGEditor() {
043        super(InstanceManager.getDefault(DigitalActionManager.class).createFemaleSocket(null, new FemaleSocketListener(){
044            @Override
045            public void connected(FemaleSocket socket) {
046                throw new UnsupportedOperationException("Not supported");
047            }
048
049            @Override
050            public void disconnected(FemaleSocket socket) {
051                throw new UnsupportedOperationException("Not supported");
052            }
053        }, "A"),
054                EnableClipboard.EnableClipboard,
055                EnableRootRemoveCutCopy.EnableRootRemoveCutCopy,
056                EnableRootPopup.EnableRootPopup,
057                EnableExecuteEvaluate.EnableExecuteEvaluate
058        );
059        
060        _conditionalNG = null;
061    }
062    
063    /**
064     * Construct a ConditionalEditor.
065     *
066     * @param conditionalNG the ConditionalNG to be edited
067     */
068    public ConditionalNGEditor(@Nonnull ConditionalNG conditionalNG) {
069        super(conditionalNG.getFemaleSocket(),
070                EnableClipboard.EnableClipboard,
071                EnableRootRemoveCutCopy.EnableRootRemoveCutCopy,
072                EnableRootPopup.EnableRootPopup,
073                EnableExecuteEvaluate.EnableExecuteEvaluate
074        );
075        
076        _conditionalNG = conditionalNG;
077        
078        if (_conditionalNG.getUserName() == null) {
079            ConditionalNGEditor.this.setTitle(
080                    Bundle.getMessage("TitleEditConditionalNG",
081                            _conditionalNG.getSystemName()));
082        } else {
083            ConditionalNGEditor.this.setTitle(
084                    Bundle.getMessage("TitleEditConditionalNG2", 
085                            _conditionalNG.getSystemName(),
086                            _conditionalNG.getUserName()));
087        }
088    }
089    
090    /** {@inheritDoc} */
091    @Override
092    public void windowClosed(WindowEvent e) {
093        logixNGData.clear();
094        logixNGData.put("Finish", _conditionalNG.getSystemName());  // NOI18N
095        fireLogixNGEvent();
096    }
097    
098    public void addLogixNGEventListener(ConditionalNGEventListener listener) {
099        listenerList.add(listener);
100    }
101    
102    /**
103     * Notify the listeners to check for new data.
104     */
105    void fireLogixNGEvent() {
106        for (ConditionalNGEventListener l : listenerList) {
107            l.conditionalNGEventOccurred();
108        }
109    }
110    
111    
112    public interface ConditionalNGEventListener extends EventListener {
113        
114        public void conditionalNGEventOccurred();
115    }
116    
117    
118//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConditionalNGEditor.class);
119
120}