001package jmri.jmrit.beantable;
002
003import java.text.DateFormat;
004import java.util.Date;
005
006import jmri.*;
007
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011import javax.annotation.Nonnull;
012import javax.swing.*;
013
014/**
015 * TableDataModel for the RailCom Table.
016 *
017 * Split from {@link RailComTableAction}
018 *
019 * @author  Bob Jacobsen Copyright (C) 2003
020 * @author  Matthew Harris Copyright (C) 2011
021 * @since 2.11.4
022 * @author Steve Young Copyright (C) 2021
023 */
024public class RailComTableDataModel extends BeanTableDataModel<IdTag> {
025
026    /**
027     * Create a new Memory Table Data Model.
028     * @param mgr Memory manager to use in the model, default MemoryManager always used.
029     */
030    public RailComTableDataModel(Manager<IdTag> mgr){
031        super();
032        setManager(mgr);
033    }
034
035    @SuppressWarnings("hiding")     // Field has same name as a field in the super class
036    static public final int VALUECOL = 0;
037    public static final int WHERECOL = VALUECOL + 1;
038    public static final int WHENCOL = WHERECOL + 1;
039    public static final int CLEARCOL = WHENCOL + 1;
040    public static final int SPEEDCOL = CLEARCOL + 1;
041    public static final int LOADCOL = SPEEDCOL + 1;
042    public static final int TEMPCOL = LOADCOL + 1;
043    public static final int FUELCOL = TEMPCOL + 1;
044    public static final int WATERCOL = FUELCOL + 1;
045    public static final int LOCATIONCOL = WATERCOL + 1;
046    public static final int ROUTINGCOL = LOCATIONCOL + 1;
047    @SuppressWarnings("hiding")     // Field has same name as a field in the super class
048    static public final int DELETECOL = ROUTINGCOL + 1;
049
050    @SuppressWarnings("hiding")     // Field has same name as a field in the super class
051    static public final int NUMCOLUMN = DELETECOL + 1;
052
053    @Override
054    public String getValue(String name) {
055        RailCom tag = (RailCom) getManager().getBySystemName(name);
056        if (tag == null) {
057            return "?";
058        }
059        return tag.getTagID();
060    }
061
062    private RailComManager manager;
063
064    @Override
065    public final void setManager(Manager<IdTag> mgr){
066        if (mgr instanceof RailComManager){
067            manager = (RailComManager)mgr;
068        }
069    }
070
071    @Override
072    public RailComManager getManager() {
073        return ( manager!=null ? manager : InstanceManager.getDefault(RailComManager.class));
074    }
075
076    @Override
077    public RailCom getBySystemName(@Nonnull String name) {
078        return (RailCom) getManager().getBySystemName(name);
079    }
080
081    @Override
082    public RailCom getByUserName(@Nonnull String name) {
083        return (RailCom) getManager().getByUserName(name);
084    }
085
086    @Override
087    public void clickOn(IdTag t) {
088        // don't do anything on click; not used in this class, because
089        // we override setValueAt
090    }
091
092    @Override
093    public void setValueAt(Object value, int row, int col) {
094        if (col == CLEARCOL) {
095            RailCom t = getBySystemName(sysNameList.get(row));
096            if (log.isDebugEnabled()) {
097                log.debug("Clear where & when last seen for {}", t.getSystemName());
098            }
099            t.setWhereLastSeen(null);
100            fireTableRowsUpdated(row, row);
101        } else if (col == DELETECOL) {
102            // button fired, delete Bean
103            deleteBean(row, col);
104        }
105    }
106
107    @Override
108    public int getColumnCount() {
109        return NUMCOLUMN;
110    }
111
112    @Override
113    public String getColumnName(int col) {
114        switch (col) {
115            case VALUECOL:
116                return Bundle.getMessage("ColumnAddress");
117            case WHERECOL:
118                return Bundle.getMessage("ColumnIdWhere");
119            case WHENCOL:
120                return Bundle.getMessage("ColumnIdWhen");
121            case SPEEDCOL:
122                return Bundle.getMessage("ColumnSpeed");
123            case LOADCOL:
124                return Bundle.getMessage("ColumnLoad");
125            case TEMPCOL:
126                return Bundle.getMessage("ColumnTemp");
127            case FUELCOL:
128                return Bundle.getMessage("ColumnFuelLevel");
129            case WATERCOL:
130                return Bundle.getMessage("ColumnWaterLevel");
131            case LOCATIONCOL:
132                return Bundle.getMessage("ColumnLocation");
133            case ROUTINGCOL:
134                return Bundle.getMessage("ColumnRouting");
135            case DELETECOL:
136                return "";
137            case CLEARCOL:
138                return "";
139            default:
140                return super.getColumnName(col);
141        }
142    }
143
144    @Override
145    public Class<?> getColumnClass(int col) {
146        switch (col) {
147            case DELETECOL:
148            case CLEARCOL:
149                return JButton.class;
150            default:
151                return String.class;
152        }
153    }
154
155    @Override
156    public boolean isCellEditable(int row, int col) {
157        switch (col) {
158            case DELETECOL:
159            case CLEARCOL:
160                return true;
161            default:
162                return false;
163        }
164    }
165
166    @Override
167    public Object getValueAt(int row, int col) {
168        RailCom t = getBySystemName(sysNameList.get(row));
169        if (t == null) {
170            return null;
171        }
172        switch (col) {
173            case VALUECOL:
174                return t.getLocoAddress().toString();
175            case WHERECOL:
176                Reporter r = t.getWhereLastSeen();
177                return (r != null ? r.getSystemName() : null);
178            case WHENCOL:
179                Date d;
180                return (((d = t.getWhenLastSeen()) != null)
181                        ? DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(d) : null);
182            case CLEARCOL:
183                return Bundle.getMessage("ButtonClear");
184            case SPEEDCOL:
185                return (t.getActualSpeed()!=-1 ? t.getActualSpeed() : null);
186            case LOADCOL:
187                return (t.getActualLoad()!=-1 ? t.getActualLoad() : null);
188            case TEMPCOL:
189                return (t.getActualTemperature()!=-1 ? t.getActualTemperature() : null);
190            case FUELCOL:
191                return (t.getFuelLevel()!=-1 ? t.getFuelLevel() : null);
192            case WATERCOL:
193                return (t.getWaterLevel()!=-1 ? t.getWaterLevel() : null);
194            case LOCATIONCOL:
195                return (t.getLocation()!=-1 ? t.getLocation() : null);
196            case ROUTINGCOL:
197                return (t.getRoutingNo()!=-1 ? t.getRoutingNo() : null);
198            case DELETECOL:  //
199                return Bundle.getMessage("ButtonDelete");
200            default:
201                return super.getValueAt(row, col);
202        }
203    }
204
205    @Override
206    public int getPreferredWidth(int col) {
207        switch (col) {
208            case WHERECOL:
209            case WHENCOL:
210                return new JTextField(12).getPreferredSize().width;
211            case VALUECOL:
212                return new JTextField(10).getPreferredSize().width;
213            case CLEARCOL:
214            case DELETECOL:
215                return new JButton(Bundle.getMessage("ButtonClear")).getPreferredSize().width + 4;
216            default:
217                return new JTextField(5).getPreferredSize().width;
218        }
219    }
220
221    @Override
222    public void configValueColumn(JTable table) {
223        // value column isn't button, so config is null
224    }
225
226    @Override
227    protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
228        return true;
229        // return (e.getPropertyName().indexOf("alue")>=0);
230    }
231
232    @Override
233    public JButton configureButton() {
234        log.error("configureButton should not have been called");
235        return null;
236    }
237
238    @Override
239    protected String getMasterClassName() {
240        return RailComTableAction.class.getName();
241    }
242
243    @Override
244    protected String getBeanType() {
245        return "ID Tag";
246    }
247
248    private static final Logger log = LoggerFactory.getLogger(RailComTableDataModel.class);
249
250}