001package jmri.jmrix.rfid.networkdriver;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import javax.swing.JComboBox;
006import javax.swing.JPanel;
007
008/**
009 * Definition of objects to handle configuring a layout connection via a
010 * NetworkDriverAdapter object.
011 *
012 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2015
013 * @author B. Milhaupt  Copyright (C) 2017
014 */
015public class ConnectionConfig extends jmri.jmrix.AbstractNetworkConnectionConfig {
016
017    public final static String NAME = "Network Interface"; // NOI18N
018
019    /**
020     * Ctor for an object being created during load process; Swing init is
021     * deferred.
022     * @param p network port adapter.
023     */
024    public ConnectionConfig(jmri.jmrix.NetworkPortAdapter p) {
025        super(p);
026    }
027
028    /**
029     * Ctor for a functional Swing object with no existing adapter.
030     */
031    public ConnectionConfig() {
032        super();
033    }
034
035    /**
036     * {@inheritDoc}
037     */
038    @SuppressWarnings("unchecked")
039    @Override
040    public void loadDetails(JPanel details) {
041        super.loadDetails(details);
042
043        //Add a listener to the combo box
044        ((JComboBox<Option>) options.get(adapter.getOption1Name()).getComponent()).addActionListener(new ActionListener() {
045            @Override
046            public void actionPerformed(ActionEvent e) {
047                enableOpt2(options.get(adapter.getOption1Name()).getItem());
048                enableOpt3(options.get(adapter.getOption1Name()).getItem());
049                enableOpt4(options.get(adapter.getOption3Name()).getItem());
050            }
051        });
052
053        enableOpt2(options.get(adapter.getOption1Name()).getItem());
054        enableOpt3(options.get(adapter.getOption1Name()).getItem());
055        enableOpt4(options.get(adapter.getOption3Name()).getItem());
056    }
057
058    private void enableOpt2(Object o) {
059        boolean enable = o.equals("MERG Concentrator"); // NOI18N
060        options.get(adapter.getOption2Name()).getLabel().setEnabled(enable);
061        options.get(adapter.getOption2Name()).getComponent().setEnabled(enable);
062        options.get(adapter.getOption2Name()).getComponent().setToolTipText(enable
063                ? Bundle.getMessage("RfidPrefsOption2ToolTipA")
064                : Bundle.getMessage("RfidPrefsOption2ToolTipB"));
065    }
066
067    @SuppressWarnings("unchecked")
068    private void enableOpt3(Object o) {
069        boolean enable = !o.equals("MERG Concentrator"); // NOI18N
070        options.get(adapter.getOption3Name()).getLabel().setEnabled(enable);
071        options.get(adapter.getOption3Name()).getComponent().setEnabled(enable);
072        options.get(adapter.getOption3Name()).getComponent().setEnabled(enable);
073        options.get(adapter.getOption3Name()).getComponent().setToolTipText(enable
074                ? Bundle.getMessage("RfidPrefsOption3ToolTipA")
075                : Bundle.getMessage("RfidPrefsOption3ToolTipB"));
076        if (!enable) {
077            ((JComboBox<Option>) options.get(adapter.getOption3Name()).getComponent()).setSelectedIndex(0);
078        }
079    }
080
081    @SuppressWarnings("unchecked")
082    private void enableOpt4(Object o) {
083        boolean enable = o.equals("Olimex"); // NOI18N
084        options.get(adapter.getOption4Name()).getLabel().setEnabled(enable);
085        options.get(adapter.getOption4Name()).getComponent().setEnabled(enable);
086        options.get(adapter.getOption4Name()).getComponent().setEnabled(enable);
087        options.get(adapter.getOption4Name()).getComponent().setToolTipText(enable
088                ? Bundle.getMessage("RfidPrefsOption4ToolTipA")
089                : Bundle.getMessage("RfidPrefsOption4ToolTipB"));
090        if (!enable) {
091            ((JComboBox<Option>) options.get(adapter.getOption4Name()).getComponent()).setSelectedIndex(0);
092        }
093    }
094
095     @Override
096    public String name() {
097        return NAME;
098    }
099
100    @Override
101    public boolean isPortAdvanced() {
102        return false;
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    protected void setInstance() {
110        if (adapter == null) {
111            adapter = new NetworkDriverAdapter();
112        }
113    }
114
115}