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