001package jmri.jmrit.throttle;
002
003import java.util.*;
004
005import javax.annotation.CheckForNull;
006import javax.swing.table.AbstractTableModel;
007
008import jmri.ConsistListListener;
009import jmri.DccLocoAddress;
010import jmri.Throttle;
011
012/**
013 * A TableModel to display active Throttles in a summary table
014 * (see ThrottlesListPanel)
015 *
016 * @author Lionel Jeanson - 2011
017 *
018 */
019
020public class ThrottlesTableModel extends AbstractTableModel implements java.beans.PropertyChangeListener, ConsistListListener{
021
022    private final List<ThrottleFrame> throttleFrames = new LinkedList<>();
023
024    @Override
025    public int getRowCount() {
026        return throttleFrames.size();
027    }
028
029    @Override
030    public int getColumnCount() {
031        return 1;
032    }
033
034    @Override
035    public Object getValueAt(int i, int i1) {
036        return throttleFrames.get(i);
037    }
038
039    public Iterator<ThrottleFrame> iterator() {
040        return throttleFrames.iterator();
041    }
042
043    public void addThrottleFrame(ThrottleWindow tw, ThrottleFrame ntf) {
044        int loc = -1;
045        int idx = 0;
046        // insert it after the last one from its containing throttle window
047        for (ThrottleFrame tf: throttleFrames) {
048            if ( tf.getThrottleWindow() == tw) {
049                loc = idx;
050            }
051            idx++;
052        }
053        if (loc != -1) {
054            throttleFrames.add(loc+1, ntf);
055        } else {
056            throttleFrames.add(ntf);
057        }        
058        fireTableDataChanged();
059    }
060
061    /**
062     * Get the number of usages of a particular Loco Address.
063     * @param la the Loco Address, can be null.
064     * @return 0 if no usages, else number of AddressPanel usages.
065     */
066    public int getNumberOfEntriesFor(@CheckForNull DccLocoAddress la) {
067        if (la == null) { 
068            return 0; 
069        }
070        int ret = 0;
071        for (ThrottleFrame tf: throttleFrames) {
072            AddressPanel ap = tf.getAddressPanel();
073            if ( ap.getThrottle() != null && 
074                ( la.equals( ap.getCurrentAddress()) || la.equals(ap.getConsistAddress()) ) ) {
075                ret++; // in use, increment count.
076            }
077        }
078        return ret;
079    }
080
081    public void removeThrottleFrame(ThrottleFrame tf, DccLocoAddress la) {
082        throttleFrames.remove(tf);
083        fireTableDataChanged();
084    }
085
086    @Override
087    public void propertyChange(java.beans.PropertyChangeEvent e) {
088        if ((e.getPropertyName().equals(Throttle.SPEEDSETTING)) ||
089                (e.getPropertyName().equals(Throttle.SPEEDSTEPS)) ||
090                (e.getPropertyName().equals(Throttle.ISFORWARD)) ||
091                (e.getPropertyName().equals("ThrottleFrame"))) {
092            fireTableDataChanged();
093        }
094    }
095
096    @Override
097    public void notifyConsistListChanged() {
098        fireTableDataChanged();
099    }
100
101    // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ThrottlesTableModel.class);
102
103}