001package jmri.jmrix.jmriclient.networkdriver;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import java.awt.event.FocusEvent;
006import java.awt.event.FocusListener;
007import javax.swing.JLabel;
008import javax.swing.JTextField;
009import jmri.jmrix.jmriclient.JMRIClientSystemConnectionMemo;
010
011/**
012 * Definition of objects to handle configuring a connection to a remote JMRI
013 * instance via the JMRI Network Protocol.
014 *
015 * @author Paul Bender Copyright (C) 2010
016 */
017public class ConnectionConfig extends jmri.jmrix.AbstractNetworkConnectionConfig {
018
019    protected JLabel transmitPrefixLabel = new JLabel("Server Connection Prefix");
020    protected JTextField transmitPrefixField = new JTextField(10);
021
022    /**
023     * Constructor for an object being created during load process; Swing init
024     * is deferred.
025     * @param p network port adapter.
026     */
027    public ConnectionConfig(jmri.jmrix.NetworkPortAdapter p) {
028        super(p);
029    }
030
031    /**
032     * Ctor for a connection configuration with no preexisting adapter.
033     * {@link #setInstance()} will fill the adapter member.
034     */
035    public ConnectionConfig() {
036        super();
037    }
038
039    @Override
040    public String name() {
041        return "Simple Network Connection";
042    }
043
044    @Override
045    protected void setInstance() {
046        if (adapter == null) {
047            adapter = new NetworkDriverAdapter();
048        }
049    }
050
051    @Override
052    public boolean isPortAdvanced() {
053        return true;
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    protected void checkInitDone() {
061        super.checkInitDone();
062        if (adapter.getSystemConnectionMemo() != null) {
063            transmitPrefixField.setText(((JMRIClientSystemConnectionMemo) adapter.getSystemConnectionMemo()).getTransmitPrefix());
064            transmitPrefixField.addActionListener(new ActionListener() {
065                @Override
066                public void actionPerformed(ActionEvent e) {
067                    ((JMRIClientSystemConnectionMemo) adapter.getSystemConnectionMemo()).setTransmitPrefix(transmitPrefixField.getText());
068                    transmitPrefixField.setText(((JMRIClientSystemConnectionMemo) adapter.getSystemConnectionMemo()).getTransmitPrefix());
069                }
070            });
071            transmitPrefixField.addFocusListener(new FocusListener() {
072                @Override
073                public void focusLost(FocusEvent e) {
074                    ((JMRIClientSystemConnectionMemo) adapter.getSystemConnectionMemo()).setTransmitPrefix(transmitPrefixField.getText());
075                    transmitPrefixField.setText(((JMRIClientSystemConnectionMemo) adapter.getSystemConnectionMemo()).getTransmitPrefix());
076                }
077
078                @Override
079                public void focusGained(FocusEvent e) {
080                }
081            });
082        }
083    }
084
085    @Override
086    protected void showAdvancedItems() {
087        super.showAdvancedItems(); // we're adding to the normal advanced items.
088        if (adapter.getSystemConnectionMemo() != null) {
089            cR.gridy += 2;
090            cL.gridy += 2;
091            gbLayout.setConstraints(transmitPrefixLabel, cL);
092            gbLayout.setConstraints(transmitPrefixField, cR);
093            _details.add(transmitPrefixLabel);
094            _details.add(transmitPrefixField);
095        }
096        if (_details.getParent() != null) {
097            _details.getParent().revalidate();
098            _details.getParent().repaint();
099        }
100    }
101
102    @Override
103    public void updateAdapter() {
104        super.updateAdapter(); // we're adding more details to the connection.
105        if (adapter.getSystemConnectionMemo() != null) {
106            ((JMRIClientSystemConnectionMemo) adapter.getSystemConnectionMemo()).setTransmitPrefix(transmitPrefixField.getText());
107        }
108    }
109
110    @Override
111    public boolean isHostNameAdvanced() {
112        return showAutoConfig.isSelected();
113    }
114
115    @Override
116    public boolean isAutoConfigPossible() {
117        return true;
118    }
119
120}