001package jmri.jmrix.dccpp.network;
002
003import java.awt.Component;
004import java.awt.GridBagConstraints;
005import javax.swing.JCheckBox;
006import javax.swing.JPanel;
007
008/**
009 * Handle configuring a DCC-EX layout connection via Ethernet.Port
010 * <p>
011 * This uses the {@link DCCppEthernetAdapter} class to do the actual connection.
012 *
013 * @author Paul Bender Copyright (C) 2011
014 * @author Mark Underwood Copyright (C) 2015
015  *
016 * Adapted from LIUSBEthernetAdapter
017 *
018 * @see jmri.jmrix.lenz.liusbethernet.LIUSBEthernetAdapter
019 */
020public class ConnectionConfig extends jmri.jmrix.AbstractNetworkConnectionConfig {
021
022    private final JCheckBox autoReconnectCheckBox =
023            new JCheckBox(Bundle.getMessage("AutoReconnectLabel"));
024
025    /**
026     * Ctor for an object being created during load process; Swing init is
027     * deferred.
028     * @param p network port adapter.
029     */
030    public ConnectionConfig(jmri.jmrix.NetworkPortAdapter p) {
031        super(p);
032
033    }
034
035    /**
036     * Ctor for a functional Swing object with no pre-existing adapter
037     */
038    public ConnectionConfig() {
039        super();
040    }
041
042    @Override
043    public String name() {
044        return "DCC-EX Ethernet";
045    }
046
047    /**
048     * {@inheritDoc}
049     */
050    @Override
051    protected void setInstance() {
052        if (adapter == null) {
053            adapter = new DCCppEthernetAdapter();
054        }
055    }
056
057    /**
058     * {@inheritDoc}
059     */
060    @Override
061    public void loadDetails(JPanel details) {
062        super.loadDetails(details);
063        hostNameField.setText(adapter.getHostName());
064        portFieldLabel.setText(Bundle.getMessage("CommunicationPortLabel"));
065        portField.setText(String.valueOf(adapter.getPort()));
066        portField.setEnabled(true);
067
068        autoReconnectCheckBox.setSelected(adapter.getAllowConnectionRecovery());
069        if (autoReconnectCheckBox.getItemListeners().length == 0) {
070            autoReconnectCheckBox.addItemListener(e -> {
071                boolean enabled = autoReconnectCheckBox.isSelected();
072                adapter.setAllowConnectionRecovery(enabled);
073                if (enabled) {
074                    adapter.setReconnectMaxAttempts(-1);
075                }
076            });
077        }
078    }
079
080    @Override
081    protected void showAdvancedItems() {
082        super.showAdvancedItems();
083        if (showAdvanced.isSelected()) {
084            int maxRow = -1;
085            for (Component c : _details.getComponents()) {
086                GridBagConstraints gbc = gbLayout.getConstraints(c);
087                if (gbc.gridy > maxRow) {
088                    maxRow = gbc.gridy;
089                }
090            }
091            cL.gridy = maxRow + 1;
092            cL.gridwidth = 2;
093            // Field initializer created before L&F is ready on some platforms; sync font/paint now.
094            autoReconnectCheckBox.updateUI();
095            gbLayout.setConstraints(autoReconnectCheckBox, cL);
096            _details.add(autoReconnectCheckBox);
097            cL.gridwidth = 1;
098            _details.revalidate();
099        }
100    }
101
102    @Override
103    public boolean isHostNameAdvanced() {
104        return showAutoConfig.isSelected();
105    }
106
107    @Override
108    public boolean isAutoConfigPossible() {
109        return true;
110    }
111
112}