001package jmri.jmrix.openlcb.swing;
002
003import javax.swing.JLabel;
004import javax.swing.Timer;
005
006import jmri.jmrix.can.CanListener;
007import jmri.jmrix.can.CanMessage;
008import jmri.jmrix.can.CanReply;
009import jmri.jmrix.can.CanSystemConnectionMemo;
010import jmri.util.ThreadingUtil;
011
012/**
013 *
014 * @author Bob Jacobsen
015 */
016public class TrafficStatusLabel extends JLabel implements CanListener {
017    
018    private static final int INTERVAL = 200;
019    CanSystemConnectionMemo memo;
020    Timer timer;
021    boolean active;
022    
023    public TrafficStatusLabel(CanSystemConnectionMemo memo) {
024        super("Active");
025        this.setEnabled(false);
026        this.memo = memo;
027        this.active = false;
028        
029        // set up traffic listener
030        memo.getTrafficController().addCanConsoleListener(this);
031        
032        // start the process
033        displayActive();
034    }
035    
036    void traffic() {
037        timer.stop();
038        active = true;
039        displayActive();
040    }
041    
042    void displayActive() {
043        if (active != isEnabled()) setEnabled(active); // reduce redisplays 
044        timer = ThreadingUtil.runOnLayoutDelayed(() -> { active = false; displayActive(); }, INTERVAL);
045    }
046
047    public synchronized void message(CanMessage l) {
048        traffic();
049    }
050
051    @Override
052    public synchronized void reply(CanReply l) { 
053        traffic();
054    }
055    
056    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TrafficStatusLabel.class);
057}