001package jmri.jmrix.rfid;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import javax.swing.JComboBox;
006import javax.swing.JPanel;
007
008/**
009 * Handle configuring a standalone RFID layout connection via an RfidStreamPortController
010 * adapter.
011 * <p>
012 * This uses the {@link RfidStreamPortController} class to do the actual connection.
013 *
014 * @author Bob Jacobsen Copyright (C) 2001, 2003
015 * @author Paul Bender Copyright (C) 2009
016 *
017 * @see RfidStreamPortController
018 */
019public class RfidStreamConnectionConfig extends jmri.jmrix.AbstractStreamConnectionConfig {
020
021    /**
022     * Ctor for an object being created during load process; Swing init is
023     * deferred.
024     * @param p stream port controller.
025     */
026    public RfidStreamConnectionConfig(jmri.jmrix.AbstractStreamPortController p) {
027        super(p);
028    }
029
030    /**
031     * Ctor for a connection configuration with no preexisting adapter.
032     * {@link #setInstance()} will fill the adapter member.
033     */
034    public RfidStreamConnectionConfig() {
035        super();
036    }
037
038    /**
039     * {@inheritDoc}
040     */
041    @SuppressWarnings("unchecked")
042    @Override
043    public void loadDetails(JPanel details) {
044        super.loadDetails(details);
045
046        // Add a listener to the combo box
047        ((JComboBox<Option>) options.get(adapter.getOption1Name()).getComponent()).addActionListener(new ActionListener() {
048            @Override
049            public void actionPerformed(ActionEvent e) {
050                enableOpt2(options.get(adapter.getOption1Name()).getItem());
051                enableOpt3(options.get(adapter.getOption1Name()).getItem());
052                enableOpt4(options.get(adapter.getOption3Name()).getItem());
053            }
054        });
055
056        // Add a listener to the combo box
057        ((JComboBox<Option>) options.get(adapter.getOption3Name()).getComponent()).addActionListener(new ActionListener() {
058            @Override
059            public void actionPerformed(ActionEvent e) {
060                enableOpt2(options.get(adapter.getOption1Name()).getItem());
061                enableOpt3(options.get(adapter.getOption1Name()).getItem());
062                enableOpt4(options.get(adapter.getOption3Name()).getItem());
063            }
064        });
065        
066        enableOpt2(options.get(adapter.getOption1Name()).getItem());
067        enableOpt3(options.get(adapter.getOption1Name()).getItem());
068        enableOpt4(options.get(adapter.getOption3Name()).getItem());
069    }
070
071    private void enableOpt2(Object o) {
072        boolean enable = o.equals("MERG Concentrator"); // NOI18N
073        options.get(adapter.getOption2Name()).getLabel().setEnabled(enable);
074        options.get(adapter.getOption2Name()).getComponent().setEnabled(enable);
075        options.get(adapter.getOption2Name()).getComponent().setToolTipText(enable
076                ? Bundle.getMessage("RfidPrefsOption2ToolTipA")
077                : Bundle.getMessage("RfidPrefsOption2ToolTipB"));
078    }
079
080    @SuppressWarnings("unchecked")
081    private void enableOpt3(Object o) {
082        boolean enable = !o.equals("MERG Concentrator"); // NOI18N
083        options.get(adapter.getOption3Name()).getLabel().setEnabled(enable);
084        options.get(adapter.getOption3Name()).getComponent().setEnabled(enable);
085        options.get(adapter.getOption3Name()).getComponent().setEnabled(enable);
086        options.get(adapter.getOption3Name()).getComponent().setToolTipText(enable
087                ? Bundle.getMessage("RfidPrefsOption3ToolTipA")
088                : Bundle.getMessage("RfidPrefsOption3ToolTipB"));
089        if (!enable) {
090            ((JComboBox<Option>) options.get(adapter.getOption3Name()).getComponent()).setSelectedIndex(0);
091        }
092    }
093
094    @SuppressWarnings("unchecked")
095    private void enableOpt4(Object o) {
096        boolean enable = o.equals("Olimex"); // NOI18N
097        options.get(adapter.getOption4Name()).getLabel().setEnabled(enable);
098        options.get(adapter.getOption4Name()).getComponent().setEnabled(enable);
099        options.get(adapter.getOption4Name()).getComponent().setEnabled(enable);
100        options.get(adapter.getOption4Name()).getComponent().setToolTipText(enable
101                ? Bundle.getMessage("RfidPrefsOption4ToolTipA")
102                : Bundle.getMessage("RfidPrefsOption4ToolTipB"));
103        if (!enable) {
104            ((JComboBox<Option>) options.get(adapter.getOption4Name()).getComponent()).setSelectedIndex(0);
105        }
106    }
107
108    @Override
109    public String name() {
110        return Bundle.getMessage("RfidStreamName");
111    }
112
113    String manufacturerName = "RFID"; // NOI18N
114
115    @Override
116    public String getManufacturer() {
117        return manufacturerName;
118    }
119
120    @Override
121    public void setManufacturer(String manu) {
122        manufacturerName = manu;
123    }
124
125    /**
126     * {@inheritDoc}
127     */
128    @Override
129    protected void setInstance() {
130        if (adapter == null) {
131            adapter = new RfidStreamPortController();
132        }
133    }
134
135}