001package jmri.jmrix.can.adapters.gridconnect.net;
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 * Definition of objects to handle configuring a connection via a
016 * NetworkDriverAdapter object.
017 *
018 * @author Bob Jacobsen Copyright (C) 2010
019 */
020public class ConnectionConfig extends jmri.jmrix.AbstractNetworkConnectionConfig {
021
022    public final static String NAME = "CAN via GridConnect Network Interface";
023
024    /**
025     * Create a connection configuration with a preexisting adapter. This is
026     * used principally when loading a configuration that defines this
027     * connection.
028     *
029     * @param p the adapter to create a connection configuration for
030     */
031    public ConnectionConfig(jmri.jmrix.NetworkPortAdapter p) {
032        super(p);
033    }
034
035    /**
036     * Ctor for a connection configuration with no preexisting adapter.
037     * {@link #setInstance()} will fill the adapter member.
038     */
039    public ConnectionConfig() {
040        super();
041    }
042
043    /**
044     * {@inheritDoc}
045     */
046    @Override
047    public void loadDetails(JPanel details) {
048        setInstance();
049        ConfigPaneHelper.maybeAddOpenLCBProtocolOptionsButton(this, additionalItems);
050        super.loadDetails(details);
051    }
052
053    @Override
054    public String name() {
055        return NAME;
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @SuppressWarnings("unchecked")
062    @Override
063    protected void checkInitDone() {
064        log.debug("init called for {}", name());
065        if (init) {
066            return;
067        }
068        super.checkInitDone();
069
070        updateUserNameField();
071
072        ((JComboBox<Option>) options.get("Protocol").getComponent()).addActionListener((ActionEvent e) -> {
073            updateUserNameField();
074        });
075    }
076
077    void updateUserNameField() {
078        if (!CanSystemConnectionMemo.DEFAULT_USERNAME.equals(adapter.getSystemConnectionMemo()
079                .getUserName())) {
080            // User name already set; do not overwrite it.
081            log.debug("Avoid overwriting user name {}.", adapter.getSystemConnectionMemo()
082                    .getUserName());
083            return;
084        }
085        log.debug("New user name based on manufacturer {}", getManufacturer());
086        String newUserName = getManufacturer();
087        connectionNameField.setText(newUserName);
088
089        if (!adapter.getSystemConnectionMemo().setUserName(newUserName)) {
090            for (int x = 2; x < 50; x++) {
091                if (adapter.getSystemConnectionMemo().setUserName(newUserName + x)) {
092                    connectionNameField.setText(adapter.getSystemConnectionMemo().getUserName());
093                    break;
094                }
095            }
096        }
097    }
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public boolean isPortAdvanced() {
104        return false;
105    }
106
107    public boolean isOptList1Advanced() {
108        return false;
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    protected void setInstance() {
116        if (adapter == null) {
117            adapter = new NetworkDriverAdapter();
118        }
119    }
120
121    protected ResourceBundle getActionModelResourceBundle() {
122        return ResourceBundle.getBundle("jmri.jmrix.can.CanActionListBundle");
123    }
124
125    private final static Logger log = LoggerFactory.getLogger(ConnectionConfig.class);
126
127}