001package jmri.jmrix.can.adapters;
002
003import java.awt.event.ActionEvent;
004import java.util.ResourceBundle;
005import javax.swing.JComboBox;
006import javax.swing.JPanel;
007
008import jmri.jmrix.can.CanSystemConnectionMemo;
009import jmri.jmrix.openlcb.swing.protocoloptions.ConfigPaneHelper;
010
011import org.slf4j.Logger;
012import org.slf4j.LoggerFactory;
013
014/**
015 * Abstract base for objects to handle configuring a layout connection via
016 * various types of SerialDriverAdapter object.
017 *
018 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2012
019 * @author Andrew Crosland 2008
020 */
021abstract public class ConnectionConfig extends jmri.jmrix.AbstractSerialConnectionConfig {
022
023    /**
024     * Create a connection configuration with a preexisting adapter.
025     * This is used principally when loading a configuration that defines this
026     * connection.
027     *
028     * @param p the adapter to create a connection configuration for
029     */
030    public ConnectionConfig(jmri.jmrix.SerialPortAdapter p) {
031        super(p);
032    }
033
034    /**
035     * Ctor for a connection configuration with no preexisting adapter.
036     * {@link #setInstance()} will fill the adapter member.
037     */
038    public ConnectionConfig() {
039        super();
040    }
041
042    /**
043     * {@inheritDoc}
044     */
045    @SuppressWarnings("unchecked")
046    @Override
047    protected void checkInitDone() {
048        log.debug("init called for {}", name());
049        if (init) {
050            return;
051        }
052        super.checkInitDone();
053
054        updateUserNameField();
055
056        ((JComboBox<Option>) options.get("Protocol").getComponent()).addActionListener((ActionEvent e) -> {
057            updateUserNameField();
058        });
059    }
060
061    void updateUserNameField() {
062        if (!CanSystemConnectionMemo.DEFAULT_USERNAME.equals(adapter.getSystemConnectionMemo()
063                .getUserName())) {
064            // User name already set; do not overwrite it.
065            log.debug("Avoid overwriting user name {}.", adapter.getSystemConnectionMemo().getUserName());
066            return;
067        }
068        log.debug("New user name based on manufacturer {}", getManufacturer());
069        String newUserName = getManufacturer();
070        connectionNameField.setText(newUserName);
071
072        if (!adapter.getSystemConnectionMemo().setUserName(newUserName)) {
073            for (int x = 2; x < 50; x++) {
074                if (adapter.getSystemConnectionMemo().setUserName(newUserName + x)) {
075                    connectionNameField.setText(adapter.getSystemConnectionMemo().getUserName());
076                    break;
077                }
078            }
079        }
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public void loadDetails(JPanel details) {
087        setInstance();
088        ConfigPaneHelper.maybeAddOpenLCBProtocolOptionsButton(this, additionalItems);
089        super.loadDetails(details);
090    }
091
092    @Override
093    abstract public String name();
094
095    @Override
096    protected ResourceBundle getActionModelResourceBundle() {
097        return ResourceBundle.getBundle("jmri.jmrix.can.CanActionListBundle");
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    abstract protected void setInstance(); // necessary to get correct type
105
106    private final static Logger log = LoggerFactory.getLogger(ConnectionConfig.class);
107
108}