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