001package jmri.jmrix.loconet.bluetooth;
002
003import java.io.IOException;
004import java.util.Vector;
005import javax.bluetooth.DiscoveryAgent;
006import javax.bluetooth.LocalDevice;
007import javax.bluetooth.RemoteDevice;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Definition of objects to handle configuring a LocoNet Bluetooth layout
013 * connection via a LocoNetBluetoothAdapter object.
014 */
015public class ConnectionConfig extends jmri.jmrix.AbstractSerialConnectionConfig {
016
017    /**
018     * Ctor for an object being created during load process; Swing init is
019     * deferred.
020     * @param p serial port adapter.
021     */
022    public ConnectionConfig(jmri.jmrix.SerialPortAdapter p) {
023        super(p);
024    }
025
026    /**
027     * Ctor for a connection configuration with no preexisting adapter.
028     * {@link #setInstance()} will fill the adapter member.
029     */
030    public ConnectionConfig() {
031        super();
032    }
033
034    @Override
035    public String name() {
036        return "BT Locobridge";
037    }
038
039    /**
040     * {@inheritDoc}
041     */
042    @Override
043    protected void setInstance() {
044        if (adapter == null) {
045            adapter = new LocoNetBluetoothAdapter();
046        }
047    }
048
049    /**
050     * Overrides super method to remove unnecessary ui components (baud rate)
051     * and change the label "Serial Port: " to "Bluetooth adapter: ".
052     */
053    @Override
054    protected void showAdvancedItems() {
055        super.showAdvancedItems();
056        _details.remove(baudBoxLabel);
057        _details.remove(baudBox);
058        portBoxLabel.setText("Bluetooth adapter: ");
059    }
060
061    /**
062     * Overrides super method to remove unnecessary ui components (baud rate)
063     * and change the label "Serial Port: " to "Bluetooth adapter: ".
064     */
065    @Override
066    protected int addStandardDetails(boolean incAdvanced, int i) {
067        int out = super.addStandardDetails(incAdvanced, i);
068        _details.remove(baudBoxLabel);
069        _details.remove(baudBox);
070        portBoxLabel.setText("Bluetooth adapter: ");
071        return out;
072    }
073
074    @Override
075    protected Vector<String> getPortNames() {
076        Vector<String> portNameVector = new Vector<String>();
077        try {
078            RemoteDevice[] devices = LocalDevice.getLocalDevice().getDiscoveryAgent().retrieveDevices(DiscoveryAgent.PREKNOWN);
079            for (RemoteDevice device : devices) {
080                portNameVector.add(device.getFriendlyName(false));
081            }
082        } catch (IOException ex) {
083            log.error("Unable to use bluetooth device", ex);
084        }
085        return portNameVector;
086    }
087
088    @Override
089    protected String[] getPortFriendlyNames() {
090        return new String[]{};
091    }
092
093    private final static Logger log = LoggerFactory.getLogger(ConnectionConfig.class);
094
095}