001package jmri.jmrix.lenz.xntcp;
002
003import java.awt.event.ActionEvent;
004import javax.swing.JComboBox;
005import javax.swing.JPanel;
006import javax.swing.JTextField;
007import jmri.jmrix.JmrixConfigPane;
008
009/**
010 * Handle configuring an XpressNet layout connection via a XnTcp adapter.
011 * <p>
012 * This uses the {@link XnTcpAdapter} class to do the actual connection.
013 *
014 * @author Giorgio Terdina Copyright (C) 2008-2011, based on LI100 Action by Bob
015 * Jacobsen, Copyright (C) 2003
016 * GT - May 2008 - Added possibility of manually defining the IP address and the TCP port number
017 * GT - May 2011 - Fixed problems arising from recent refactoring
018 * GT - Dec 2011 - Fixed problems in 2.14 arising from changes introduced since May
019 *
020 * @see XnTcpAdapter
021 */
022public class ConnectionConfig extends jmri.jmrix.AbstractNetworkConnectionConfig {
023
024    private static final String XN_TCP_INTERFACE = "XnTcpInterface";
025    private static final String MANUAL = "Manual";
026
027    /**
028     * Ctor for an object being created during load process; Swing init is
029     * deferred.
030     * @param p network port adapter.
031     */
032    public ConnectionConfig(jmri.jmrix.NetworkPortAdapter p) {
033        super(p);
034
035        String h = adapter.getHostName();
036        if (h != null && !h.equals(JmrixConfigPane.NONE)) {
037            hostNameField = new JTextField(h);
038        }
039        String t = "" + adapter.getPort();
040        if (!t.equals("0")) {
041            portField = new JTextField(t);
042        }
043    }
044
045    /**
046     * Ctor for a connection configuration with no preexisting adapter.
047     * {@link #setInstance()} will fill the adapter member.
048     */
049    public ConnectionConfig() {
050        super();
051    }
052
053    @Override
054    public String name() {
055        return Bundle.getMessage("XnTcpName");
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    protected void setInstance() {
063        if (adapter == null) {
064            adapter = new XnTcpAdapter();
065        }
066    }
067
068    @Override
069    public String getInfo() {
070        // GT 2.14 retrieving adapter name from CurrentOption1Setting, since Opt1Box now returns null
071        String x = adapter.getOptionState(XN_TCP_INTERFACE);
072        if (x == null) {
073            return JmrixConfigPane.NONE;
074        }
075        if (x.equals(Bundle.getMessage(MANUAL))) {
076            x = "";
077        } else {
078            x += ":";
079        }
080        String t = adapter.getHostName();
081        int p = adapter.getPort();
082        if (t != null && !t.equals("")) {
083            if (p != 0) {
084                return x + t + ":" + p;
085            }
086            return x + t;
087        } else {
088            return JmrixConfigPane.NONE;
089        }
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    @SuppressWarnings("unchecked")
096    @Override
097    public void loadDetails(final JPanel d) {
098        super.loadDetails(d);
099
100        if (options.get(XN_TCP_INTERFACE).getComponent() instanceof JComboBox) {
101            ((JComboBox<Option>) options.get(XN_TCP_INTERFACE).getComponent()).addActionListener((ActionEvent e) -> enableInput());
102        }
103    }
104
105    @Override
106    protected void showAdvancedItems() {
107        super.showAdvancedItems();
108        enableInput();
109        _details.repaint();
110    }
111
112    private void enableInput() {
113        String choice = options.get(XN_TCP_INTERFACE).getItem();
114        //GT 2.14 - Added test for null, now returned by opt1Box at startup (somewhere the initialization is missing)
115        boolean manualInput = false;
116        if (choice != null) {
117            manualInput = (choice.equals(Bundle.getMessage(MANUAL)) || choice.equals(MANUAL)); // support pre-i18n configurations
118        }
119        hostNameField.setEnabled(manualInput);
120        portField.setEnabled(manualInput);
121        adapter.configureOption1(choice);
122        adapter.setHostName(hostNameField.getText());
123        adapter.setPort(portField.getText());
124    }
125
126    String manufacturerName = jmri.jmrix.lenz.LenzConnectionTypeList.LENZ;
127
128    @Override
129    public String getManufacturer() {
130        return manufacturerName;
131    }
132
133    @Override
134    public void setManufacturer(String manu) {
135        manufacturerName = manu;
136    }
137
138    @Override
139    public boolean isHostNameAdvanced() {
140        return true;
141    }
142
143}