001package jmri.jmrix.loconet.logixng.swing;
002
003import java.awt.Color;
004import java.util.*;
005
006import javax.annotation.CheckForNull;
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.*;
011import jmri.jmrit.logixng.Base;
012import jmri.jmrit.logixng.DigitalActionManager;
013import jmri.jmrit.logixng.MaleSocket;
014import jmri.jmrit.logixng.actions.swing.AbstractDigitalActionSwing;
015import jmri.jmrix.loconet.logixng.ActionUpdateSlots;
016import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
017
018/**
019 * Configures an ExpressionTurnout object with a Swing JPanel.
020 * 
021 * @author Daniel Bergqvist Copyright 2020
022 */
023public class ActionUpdateSlotsSwing extends AbstractDigitalActionSwing {
024
025    private JComboBox<LocoNetConnection> _locoNetConnection;
026    
027    @Override
028    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
029        if ((object != null) && !(object instanceof ActionUpdateSlots)) {
030            throw new IllegalArgumentException("object must be an ActionUpdateSlots but is a: "+object.getClass().getName());
031        }
032        
033        ActionUpdateSlots action = (ActionUpdateSlots)object;
034        
035        panel = new JPanel();
036        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
037        
038        JPanel queryPanel = new JPanel();
039        queryPanel.setBorder(BorderFactory.createLineBorder(Color.black));
040        
041        JPanel locoNetPanel = new JPanel();
042        locoNetPanel.add(new JLabel(Bundle.getMessage("LocoNetConnection")));
043        
044        _locoNetConnection = new JComboBox<>();
045        List<LocoNetSystemConnectionMemo> systemConnections =
046                jmri.InstanceManager.getList(LocoNetSystemConnectionMemo.class);
047        for (LocoNetSystemConnectionMemo connection : systemConnections) {
048            LocoNetConnection c = new LocoNetConnection(connection);
049            _locoNetConnection.addItem(c);
050            if ((action != null) && (action.getMemo() == connection)) {
051                _locoNetConnection.setSelectedItem(c);
052            }
053        }
054        locoNetPanel.add(_locoNetConnection);
055        
056        panel.add(locoNetPanel);
057    }
058    
059    /** {@inheritDoc} */
060    @Override
061    public boolean validate(@Nonnull List<String> errorMessages) {
062        return true;
063    }
064    
065    /** {@inheritDoc} */
066    @Override
067    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
068        LocoNetSystemConnectionMemo memo =
069                _locoNetConnection.getItemAt(_locoNetConnection.getSelectedIndex())._memo;
070        
071        ActionUpdateSlots action = new ActionUpdateSlots(systemName, userName, memo);
072        updateObject(action);
073        
074        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
075    }
076    
077    /** {@inheritDoc} */
078    @Override
079    public void updateObject(@Nonnull Base object) {
080        if (! (object instanceof ActionUpdateSlots)) {
081            throw new IllegalArgumentException("object must be an ExpressionTurnout but is a: "+object.getClass().getName());
082        }
083        
084        ActionUpdateSlots action = (ActionUpdateSlots)object;
085        
086        action.setMemo(_locoNetConnection.getItemAt(_locoNetConnection.getSelectedIndex())._memo);
087    }
088    
089    /** {@inheritDoc} */
090    @Override
091    public String toString() {
092        return Bundle.getMessage("ActionUpdateSlots_Short");
093    }
094    
095    @Override
096    public void dispose() {
097    }
098    
099    
100    
101    private static class LocoNetConnection {
102        
103        private LocoNetSystemConnectionMemo _memo;
104        
105        public LocoNetConnection(LocoNetSystemConnectionMemo memo) {
106            _memo = memo;
107        }
108        
109        @Override
110        public String toString() {
111            return _memo.getUserName();
112        }
113    }
114    
115//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSlotUsageSwing.class);
116
117}