001package jmri.jmrix.rps.swing.polling;
002
003import javax.swing.JComboBox;
004import javax.swing.table.AbstractTableModel;
005import jmri.jmrix.rps.Distributor;
006import jmri.jmrix.rps.Engine;
007import jmri.jmrix.rps.Measurement;
008import jmri.jmrix.rps.MeasurementListener;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Pane for user management of RPS alignment.
014 *
015 * @author Bob Jacobsen Copyright (C) 2008
016 */
017public class PollDataModel extends AbstractTableModel implements MeasurementListener {
018
019    static final int NAMECOL = 0;
020    static final int IDCOL = 1;
021    static final int ADDRCOL = 2;
022    static final int LONGCOL = 3;
023    static final int POLLCOL = 4;
024    static final int LASTXCOL = 5;
025    static final int LASTYCOL = 6;
026    static final int LASTZCOL = 7;
027    static final int LASTTIME = 8;
028
029    static final int LAST = 8;
030    jmri.ModifiedFlag modifiedFlag;
031
032    static final int TYPECOL = -1;
033
034    public PollDataModel(jmri.ModifiedFlag flag) {
035        super();
036        this.modifiedFlag = flag;
037        Distributor.instance().addMeasurementListener(this);
038        fireTableDataChanged();
039    }
040
041    @Override
042    public int getColumnCount() {
043        return LAST + 1;
044    }
045
046    @Override
047    public int getRowCount() {
048        return Engine.instance().getNumTransmitters();
049    }
050
051    @Override
052    public String getColumnName(int c) {
053        switch (c) {
054            case NAMECOL:
055                return Bundle.getMessage("TitleName");
056            case IDCOL:
057                return Bundle.getMessage("TitleIdCol");
058            case ADDRCOL:
059                return Bundle.getMessage("TitleAddrCol");
060            case LONGCOL:
061                return Bundle.getMessage("TitleLongCol");
062            case POLLCOL:
063                return Bundle.getMessage("TitlePollCol");
064            case TYPECOL:
065                return Bundle.getMessage("TitleTypeCol");
066            case LASTXCOL:
067                return Bundle.getMessage("TitleXCol");
068            case LASTYCOL:
069                return Bundle.getMessage("TitleYCol");
070            case LASTZCOL:
071                return Bundle.getMessage("TitleZCol");
072            case LASTTIME:
073                return Bundle.getMessage("TitleTime");
074            default:
075                return "";
076        }
077    }
078
079    @Override
080    public Class<?> getColumnClass(int c) {
081        if (c == LONGCOL || c == POLLCOL) {
082            return Boolean.class;
083        } else if (c == ADDRCOL || c == LASTTIME) {
084            return Integer.class;
085        } else if (c == TYPECOL) {
086            return JComboBox.class;
087        } else if (c == LASTXCOL || c == LASTYCOL || c == LASTZCOL) {
088            return Double.class;
089        } else {
090            return String.class;
091        }
092    }
093
094    @Override
095    public boolean isCellEditable(int r, int c) {
096        if (c == IDCOL || c == POLLCOL || c == TYPECOL) {
097            return true;
098        } else {
099            return false;
100        }
101    }
102
103    @Override
104    public Object getValueAt(int r, int c) {
105        // r is row number, from 0
106        Measurement m;
107        if (Engine.instance() == null) {
108            log.warn("returning null because of missing Engine.instance()");
109        }
110        if (Engine.instance().getTransmitter(r) == null) {
111            log.warn("returning null because of missing Engine.instance().getTransmitter({})", r);
112        }
113
114        double val;
115        switch (c) {
116            case NAMECOL:
117                return Engine.instance().getTransmitter(r).getRosterName();
118            case IDCOL:
119                return Engine.instance().getTransmitter(r).getId();
120            case ADDRCOL:
121                return Integer.valueOf(Engine.instance().getTransmitter(r).getAddress());
122            case LONGCOL:
123                return Boolean.valueOf(Engine.instance().getTransmitter(r).isLongAddress());
124            case POLLCOL:
125                return Boolean.valueOf(Engine.instance().getTransmitter(r).isPolled());
126            case TYPECOL:
127                JComboBox<String> b = new JComboBox<String>(new String[]{"F2", "F3", "BSCI"});
128                return b;
129            case LASTXCOL:
130                m = Engine.instance().getTransmitter(r).getLastMeasurement();
131                if (m == null) {
132                    return null;
133                }
134                val = m.getX();
135                return Double.valueOf(val);
136            case LASTYCOL:
137                m = Engine.instance().getTransmitter(r).getLastMeasurement();
138                if (m == null) {
139                    return null;
140                }
141                val = m.getY();
142                return Double.valueOf(val);
143            case LASTZCOL:
144                m = Engine.instance().getTransmitter(r).getLastMeasurement();
145                if (m == null) {
146                    return null;
147                }
148                val = m.getZ();
149                return Double.valueOf(val);
150            case LASTTIME:
151                m = Engine.instance().getTransmitter(r).getLastMeasurement();
152                if (m == null) {
153                    return null;
154                }
155                int time = m.getReading().getTime();
156                return Integer.valueOf(time);
157            default:
158                return null;
159        }
160    }
161
162    @Override
163    public void setValueAt(Object value, int r, int c) {
164        // r is row number, from 0
165        switch (c) {
166            case IDCOL:
167                String s = ((String) value);
168                Engine.instance().getTransmitter(r).setId(s);
169                modifiedFlag.setModifiedFlag(true);
170                return;
171            case POLLCOL:
172                boolean p = ((Boolean) value).booleanValue();
173                Engine.instance().getTransmitter(r).setPolled(p);
174                modifiedFlag.setModifiedFlag(true);
175                return;
176            case TYPECOL:
177                log.error("Got {} but did not act", ((JComboBox<?>) value).getSelectedItem());
178                break;
179            default:
180                log.warn("Unhandled col {}", c);
181                break;
182        }
183    }
184
185    // When a measurement happens, mark data as changed.
186    // It would be better to just mark one line...
187    @Override
188    public void notify(Measurement m) {
189        fireTableDataChanged();
190    }
191
192    public void dispose() {
193        Distributor.instance().removeMeasurementListener(this);
194    }
195
196    private final static Logger log = LoggerFactory.getLogger(PollDataModel.class);
197
198}