001package jmri.jmrit.beantable;
002
003import javax.annotation.CheckForNull;
004import javax.annotation.Nonnull;
005import javax.swing.*;
006
007import jmri.InstanceManager;
008import jmri.Manager;
009import jmri.NamedBean;
010import jmri.Reportable;
011import jmri.Reporter;
012import jmri.ReporterManager;
013
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * Data model for a Reporter Table.
019 * Code originally within ReporterTableAction.
020 *
021 * @author Bob Jacobsen Copyright (C) 2003
022 * @author Steve Young Copyright (C) 2021
023 */
024public class ReporterTableDataModel extends BeanTableDataModel<Reporter> {
025
026    public static final int LASTREPORTCOL = NUMCOLUMN;
027
028    public ReporterTableDataModel(Manager<Reporter> mgr){
029        super();
030        setManager(mgr);
031    }
032
033    private ReporterManager reporterManager;
034
035    /**
036     * {@inheritDoc}
037     */
038    @Override
039    @CheckForNull
040    public String getValue(String name) {
041        Object value;
042        Reporter r = getManager().getBySystemName(name);
043        if (r == null) {
044            return "";
045        }
046        value = r.getCurrentReport();
047        if (value == null) {
048            return null;
049        } else if (value instanceof Reportable) {
050            return ((Reportable) value).toReportString();
051        } else {
052            return value.toString();
053        }
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public final void setManager(Manager<Reporter> rm) {
061        if (!(rm instanceof ReporterManager)) {
062            return;
063        }
064        getManager().removePropertyChangeListener(this);
065        if (sysNameList != null) {
066            for (int i = 0; i < sysNameList.size(); i++) {
067                // if object has been deleted, it's not here; ignore it
068                NamedBean b = getBySystemName(sysNameList.get(i));
069                if (b != null) {
070                    b.removePropertyChangeListener(this);
071                }
072            }
073        }
074        reporterManager = (ReporterManager) rm;
075        getManager().addPropertyChangeListener(this);
076        updateNameList();
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public ReporterManager getManager() {
084        return ( reporterManager == null ? InstanceManager.getDefault(ReporterManager.class): reporterManager);
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public Reporter getBySystemName(@Nonnull String name) {
092        return getManager().getBySystemName(name);
093    }
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public Reporter getByUserName(@Nonnull String name) {
100        return getManager().getByUserName(name);
101    }
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    protected String getMasterClassName() {
108        return ReporterTableAction.class.getName();
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    public void clickOn(Reporter t) {
116        // don't do anything on click; not used in this class, because
117        // we override setValueAt
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public void setValueAt(Object value, int row, int col) {
125        switch (col) {
126            case VALUECOL:
127                getBySystemName(sysNameList.get(row)).setReport(value);
128                fireTableRowsUpdated(row, row);
129                break;
130            case LASTREPORTCOL: // do nothing
131                break;
132            default:
133                super.setValueAt(value, row, col);
134                break;
135        }
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public int getColumnCount() {
143        return LASTREPORTCOL + getPropertyColumnCount() +1;
144    }
145
146    /**
147     * {@inheritDoc}
148     */
149    @Override
150    public String getColumnName(int col) {
151        switch (col) {
152            case VALUECOL:
153                return Bundle.getMessage("LabelReport");
154            case LASTREPORTCOL:
155                return Bundle.getMessage("LabelLastReport");
156            default:
157                return super.getColumnName(col);
158        }
159    }
160
161    /**
162     * {@inheritDoc}
163     */
164    @Override
165    public Class<?> getColumnClass(int col) {
166        switch (col) {
167            case VALUECOL:
168            case LASTREPORTCOL:
169                return String.class;
170            default:
171                return super.getColumnClass(col);
172        }
173    }
174
175    /**
176     * {@inheritDoc}
177     */
178    @Override
179    public boolean isCellEditable(int row, int col) {
180        if (col == LASTREPORTCOL) {
181            return false;
182        } else
183        return super.isCellEditable(row, col);
184    }
185
186    /**
187     * {@inheritDoc}
188     */
189    @Override
190    public Object getValueAt(int row, int col) {
191        if (col == LASTREPORTCOL) {
192
193            String name = sysNameList.get(row);
194            Object value;
195            Reporter r = getManager().getBySystemName(name);
196            if (r == null) {
197                return "";
198            }
199            value = r.getLastReport();
200            if (value == null) {
201                return null;
202            } else if (value instanceof Reportable) {
203                return ((Reportable) value).toReportString();
204            } else {
205                return value.toString();
206            }
207        } else
208        return super.getValueAt(row, col);
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public int getPreferredWidth(int col) {
216        if (col == LASTREPORTCOL) {
217            return super.getPreferredWidth(VALUECOL);
218        } else
219        return super.getPreferredWidth(col);
220    }
221
222    /**
223     * {@inheritDoc}
224     */
225    @Override
226    public void configValueColumn(JTable table) {
227        // value column isn't button, so config is null
228    }
229
230    /**
231     * {@inheritDoc}
232     */
233    @Override
234    protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
235        return true;
236        // return (e.getPropertyName().indexOf("Report")>=0);
237    }
238
239    /**
240     * {@inheritDoc}
241     */
242    @Override
243    public JButton configureButton() {
244        log.error("configureButton should not have been called");
245        return null;
246    }
247
248    private final static Logger log = LoggerFactory.getLogger(ReporterTableDataModel.class);
249
250}