001package jmri.jmrix.rfid.serialdriver;
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.
010 *
011 * @author Bob Jacobsen Copyright (C) 2003, 2006, 2007, 2008
012 * @author Matthew Harris Copyright (C) 2011
013 * @author B. Milhaupt    Copyright (C) 2017
014 * @since 2.11.4
015 */
016public class ConnectionConfig extends jmri.jmrix.AbstractSerialConnectionConfig {
017
018    /**
019     * Ctor for an object being created during load process; Swing init is
020     * deferred.
021     *
022     * @param p {@link jmri.jmrix.SerialPortAdapter} to configure
023     */
024    public ConnectionConfig(jmri.jmrix.SerialPortAdapter p) {
025        super(p);
026    }
027
028    /**
029     * Ctor for a connection configuration with no preexisting adapter.
030     * {@link #setInstance()} will fill the adapter member.
031     */
032    public ConnectionConfig() {
033        super();
034    }
035
036    /**
037     * {@inheritDoc}
038     */
039    @SuppressWarnings("unchecked")
040    @Override
041    public void loadDetails(JPanel details) {
042        super.loadDetails(details);
043
044        // Add a listener to the combo box
045        ((JComboBox<Option>) options.get(adapter.getOption1Name()).getComponent()).addActionListener(new ActionListener() {
046            @Override
047            public void actionPerformed(ActionEvent e) {
048                enableOpt2(options.get(adapter.getOption1Name()).getItem());
049                enableOpt3(options.get(adapter.getOption1Name()).getItem());
050                enableOpt4(options.get(adapter.getOption3Name()).getItem());
051            }
052        });
053
054        // Add a listener to the combo box
055        ((JComboBox<Option>) options.get(adapter.getOption3Name()).getComponent()).addActionListener(new ActionListener() {
056            @Override
057            public void actionPerformed(ActionEvent e) {
058                enableOpt2(options.get(adapter.getOption1Name()).getItem());
059                enableOpt3(options.get(adapter.getOption1Name()).getItem());
060                enableOpt4(options.get(adapter.getOption3Name()).getItem());
061            }
062        });
063        
064        enableOpt2(options.get(adapter.getOption1Name()).getItem());
065        enableOpt3(options.get(adapter.getOption1Name()).getItem());
066        enableOpt4(options.get(adapter.getOption3Name()).getItem());
067    }
068
069    private void enableOpt2(Object o) {
070        boolean enable = o.equals("MERG Concentrator"); // NOI18N
071        options.get(adapter.getOption2Name()).getLabel().setEnabled(enable);
072        options.get(adapter.getOption2Name()).getComponent().setEnabled(enable);
073        options.get(adapter.getOption2Name()).getComponent().setToolTipText(enable
074                ? Bundle.getMessage("RfidPrefsOption2ToolTipA")
075                : Bundle.getMessage("RfidPrefsOption2ToolTipB"));
076    }
077
078    @SuppressWarnings("unchecked")
079    private void enableOpt3(Object o) {
080        boolean enable = !o.equals("MERG Concentrator"); // NOI18N
081        options.get(adapter.getOption3Name()).getLabel().setEnabled(enable);
082        options.get(adapter.getOption3Name()).getComponent().setEnabled(enable);
083        options.get(adapter.getOption3Name()).getComponent().setEnabled(enable);
084        options.get(adapter.getOption3Name()).getComponent().setToolTipText(enable
085                ? Bundle.getMessage("RfidPrefsOption3ToolTipA")
086                : Bundle.getMessage("RfidPrefsOption3ToolTipB"));
087        if (!enable) {
088            ((JComboBox<Option>) options.get(adapter.getOption3Name()).getComponent()).setSelectedIndex(0);
089        }
090    }
091
092    @SuppressWarnings("unchecked")
093    private void enableOpt4(Object o) {
094        boolean enable = o.equals("Olimex"); // NOI18N
095        options.get(adapter.getOption4Name()).getLabel().setEnabled(enable);
096        options.get(adapter.getOption4Name()).getComponent().setEnabled(enable);
097        options.get(adapter.getOption4Name()).getComponent().setEnabled(enable);
098        options.get(adapter.getOption4Name()).getComponent().setToolTipText(enable
099                ? Bundle.getMessage("RfidPrefsOption4ToolTipA")
100                : Bundle.getMessage("RfidPrefsOption4ToolTipB"));
101        if (!enable) {
102            ((JComboBox<Option>) options.get(adapter.getOption4Name()).getComponent()).setSelectedIndex(0);
103        }
104    }
105
106    @Override
107    public String name() {
108        return "Direct Serial Connection";
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    protected void setInstance() {
116        if (adapter == null) {
117            adapter = new SerialDriverAdapter();
118        }
119    }
120
121}