001package jmri.jmrit.throttle;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005import javax.swing.table.AbstractTableModel;
006import jmri.DccLocoAddress;
007import jmri.DccThrottle;
008import jmri.LocoAddress;
009import jmri.Throttle;
010
011/**
012 * A TableModel to display active Throttles in a summary table
013 * (see ThrottlesListPanel)
014 * 
015 * @author Lionel Jeanson - 2011
016 * 
017 */
018
019public class ThrottlesTableModel extends AbstractTableModel implements AddressListener, java.beans.PropertyChangeListener {
020
021    private final ArrayList<ThrottleFrame> throttleFrames = new ArrayList<ThrottleFrame>(5);
022
023    @Override
024    public int getRowCount() {
025        return throttleFrames.size();
026    }
027
028    @Override
029    public int getColumnCount() {
030        return 1;
031    }
032
033    @Override
034    public Object getValueAt(int i, int i1) {
035        return throttleFrames.get(i);
036    }
037
038    public Iterator<ThrottleFrame> iterator() {
039        return throttleFrames.iterator();
040    }
041
042    public void addThrottleFrame(ThrottleFrame tf) {
043        throttleFrames.add(tf);
044        fireTableDataChanged();
045    }
046
047    public void removeThrottleFrame(ThrottleFrame tf, DccLocoAddress la) {
048        throttleFrames.remove(tf);
049        if (la != null) {
050            jmri.InstanceManager.throttleManagerInstance().removeListener(la, this);
051        }
052        fireTableDataChanged();
053    }
054
055    @Override
056    public void notifyAddressChosen(LocoAddress la) {
057    }
058
059    @Override
060    public void notifyAddressReleased(LocoAddress addr) {
061        if (addr instanceof DccLocoAddress ) {
062           DccLocoAddress la = (DccLocoAddress) addr;
063           fireTableDataChanged();
064           jmri.InstanceManager.throttleManagerInstance().removeListener(la, this);
065        }
066    }
067
068    @Override
069    public void notifyAddressThrottleFound(DccThrottle throttle) {
070        fireTableDataChanged();
071        throttle.addPropertyChangeListener(this);
072    }
073
074    @Override
075    public void notifyConsistAddressChosen(int newAddress, boolean isLong) {
076    }
077
078    @Override
079    public void notifyConsistAddressReleased(int address, boolean isLong) {
080    }
081
082    @Override
083    public void notifyConsistAddressThrottleFound(DccThrottle throttle) {
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            fireTableDataChanged();
092        }
093    }
094
095}