001package jmri.jmrix.ecos.utilities;
002
003import java.awt.event.ActionEvent;
004import javax.swing.AbstractAction;
005import javax.swing.JComboBox;
006
007import jmri.jmrit.roster.Roster;
008import jmri.jmrit.roster.RosterEntry;
009import jmri.jmrix.ecos.EcosLocoAddress;
010import jmri.jmrix.ecos.EcosLocoAddressManager;
011import jmri.jmrix.ecos.EcosSystemConnectionMemo;
012import jmri.util.swing.JmriJOptionPane;
013
014/**
015 * Add a Roster Entry to the Ecos
016 *
017 * @author Kevin Dickerson Copyright (C) 2009
018 */
019public class AddRosterEntryToEcos extends AbstractAction {
020
021    /**
022     * @param s Name of this action, e.g. in menus.
023     * @param memo system connection.
024     */
025    public AddRosterEntryToEcos(String s, EcosSystemConnectionMemo memo) {
026        super(s);
027        adaptermemo = memo;
028        objEcosLocoManager = adaptermemo.getLocoAddressManager();
029    }
030
031    private final EcosSystemConnectionMemo adaptermemo;
032    private final EcosLocoAddressManager objEcosLocoManager;
033    private final JComboBox<String> rosterEntry = new JComboBox<>();
034    private Roster roster;
035
036    @Override
037    public void actionPerformed(ActionEvent event) {
038
039        roster = Roster.getDefault();
040
041        rosterEntryUpdate();
042
043        int retval = JmriJOptionPane.showConfirmDialog(null,
044                new Object[]{Bundle.getMessage("AddToEcosDialog"), rosterEntry},
045                Bundle.getMessage("AddToEcosTitle"),
046                JmriJOptionPane.OK_CANCEL_OPTION );
047        log.debug("Dialog value {} selected, {}:{}", retval, rosterEntry.getSelectedIndex(), rosterEntry.getSelectedItem());
048        if (retval != JmriJOptionPane.OK_OPTION || rosterEntry.getItemCount() == 0) {
049            return;
050        }
051
052        String selEntry = (String) rosterEntry.getSelectedItem();
053        RosterEntry re = roster.entryFromTitle(selEntry);
054        log.debug("Add {} to ECoS", re.getId());
055        RosterToEcos rosterToEcos = new RosterToEcos(adaptermemo);
056        rosterToEcos.createEcosLoco(re);
057    }
058
059    void rosterEntryUpdate() {
060        rosterEntry.removeAllItems();
061        for (RosterEntry r : roster.getAllEntries()) {
062            // Add only those locos to the drop-down list that are in the JMRI Roster but not in the ECoS
063            String dccAddress = r.getDccAddress();
064            EcosLocoAddress ecosAddress = null;
065            if (dccAddress != null) {
066                log.debug("DccAddress={}", dccAddress);
067                try {
068                    ecosAddress = objEcosLocoManager.getByDccAddress(Integer.parseInt(dccAddress));
069                } catch (NullPointerException npe) {
070                    log.warn("Could not connect to ECoS roster via objEcosLocoManager to loop up Loco {}", dccAddress);
071                    return;
072                }
073            }
074            if ( ecosAddress == null && r.getProtocol() != jmri.LocoAddress.Protocol.MFX ) {
075                // It is not possible to create MFX locomotives in the ECoS. They are auto-discovered.
076                rosterEntry.addItem(r.titleString());
077            }
078        }
079    }
080
081    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AddRosterEntryToEcos.class);
082
083}