001package jmri.swing;
002
003import java.awt.Color;
004import java.beans.PropertyChangeEvent;
005import java.beans.PropertyChangeListener;
006import javax.swing.JLabel;
007import jmri.jmrix.ConnectionConfig;
008import jmri.jmrix.ConnectionStatus;
009import jmri.jmrix.JmrixConfigPane;
010
011/**
012 * A JLabel that listens to a system connection and reports its status
013 *
014 * @author Randall Wood
015 */
016public final class ConnectionLabel extends JLabel implements PropertyChangeListener {
017
018    ConnectionConfig connection;
019
020    public ConnectionLabel(ConnectionConfig connection) {
021        super();
022        this.connection = connection;
023        if (connection.name().equals(JmrixConfigPane.NONE)) {
024            this.setText("");
025        } else {
026            ConnectionStatus.instance().addConnection(connection.getConnectionName(), connection.getInfo());
027            this.update();
028        }
029        ConnectionStatus.instance().addPropertyChangeListener(this);
030    }
031
032    protected void update() {
033        if (this.connection.getDisabled()) {
034            return;
035        }
036        String name = this.connection.getConnectionName();
037        if (name == null) {
038            name = this.connection.getManufacturer();
039        }
040        if (ConnectionStatus.instance().isConnectionOk(this.connection.getConnectionName(),this.connection.getInfo())) {
041            this.setForeground(Color.BLACK);
042            this.setText(Bundle.getMessage("ConnectionSucceeded",
043                    name, this.connection.name(), this.connection.getInfo()));
044        } else {
045            this.setForeground(Color.RED);
046            this.setText(Bundle.getMessage("ConnectionFailed",
047                    name, this.connection.name(), this.connection.getInfo()));
048        }
049        this.revalidate();
050    }
051
052    @Override
053    public void propertyChange(PropertyChangeEvent evt) {
054        this.update();
055    }
056
057}