001package jmri.jmrit.beantable;
002
003import jmri.*;
004
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008import javax.annotation.Nonnull;
009import javax.swing.*;
010
011/**
012 * TableDataModel for the Memory Table.
013 *
014 * Split from {@link MemoryTableAction}
015 *
016 * @author Bob Jacobsen Copyright (C) 2003
017 * @author Steve Young Copyright (C) 2021
018 */
019public class MemoryTableDataModel extends BeanTableDataModel<Memory> {
020
021    /**
022     * Create a new Memory Table Data Model.
023     * @param mgr Memory manager to use in the model, default MemoryManager always used.
024     */
025    public MemoryTableDataModel(Manager<Memory> mgr){
026        super();
027        setManager(mgr);
028    }
029
030    /**
031     * Constructor for use by preferences and messages system
032     */
033    public MemoryTableDataModel(){
034        super();
035        setManager(InstanceManager.getDefault(MemoryManager.class));
036    }
037
038    /** {@inheritDoc} */
039    @Override
040    public String getValue(String name) {
041        Memory mem = InstanceManager.getDefault(MemoryManager.class).getBySystemName(name);
042        if (mem == null) {
043            return "?";
044        }
045        Object m = mem.getValue();
046        if (m != null) {
047            if ( m instanceof Reportable) {
048                return ((Reportable) m).toReportString();
049            }
050            else {
051                return m.toString();
052            }
053        } else {
054            return "";
055        }
056    }
057
058    /** {@inheritDoc} */
059    @Override
060    public Manager<Memory> getManager() {
061        return InstanceManager.getDefault(MemoryManager.class);
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    public Memory getBySystemName(@Nonnull String name) {
067        return InstanceManager.getDefault(MemoryManager.class).getBySystemName(name);
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public Memory getByUserName(@Nonnull String name) {
073        return InstanceManager.getDefault(MemoryManager.class).getByUserName(name);
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    protected String getMasterClassName() {
079        return this.getClass().getName();
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public void clickOn(Memory t) {
085        // don't do anything on click; not used in this class, because
086        // we override setValueAt
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public void setValueAt(Object value, int row, int col) {
092        if (col == VALUECOL) {
093            getBySystemName(sysNameList.get(row)).setValue(value);
094            fireTableRowsUpdated(row, row);
095        } else {
096            super.setValueAt(value, row, col);
097        }
098    }
099
100    /** {@inheritDoc} */
101    @Override
102    public String getColumnName(int col) {
103        if (col == VALUECOL) {
104            return Bundle.getMessage("BlockValue");
105        }
106        return super.getColumnName(col);
107    }
108
109    /** {@inheritDoc} */
110    @Override
111    public Class<?> getColumnClass(int col) {
112        if (col == VALUECOL) {
113            return String.class;
114        } else {
115            return super.getColumnClass(col);
116        }
117    }
118
119    /** {@inheritDoc} */
120    @Override
121    public void configValueColumn(JTable table) {
122        // value column isn't button, so config is null
123    }
124
125    /** {@inheritDoc} */
126    @Override
127    protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
128        return true;
129        // return (e.getPropertyName().indexOf("alue")>=0);
130    }
131
132    /** {@inheritDoc} */
133    @Override
134    public JButton configureButton() {
135        log.error("configureButton should not have been called");
136        return null;
137    }
138
139    /**
140     * Additionally provide Memory value Class in value column.
141     * {@inheritDoc}
142     */
143    @Override
144    public String getCellToolTip(JTable table, int row, int col) {
145        switch (col) {
146            case VALUECOL:
147                Memory m = getBySystemName(sysNameList.get(row));
148                Object o = m.getValue();
149                return (o == null ? null : o.getClass().getName());
150            default:
151                return super.getCellToolTip(table, row, col);
152        }
153    }
154
155    private static final Logger log = LoggerFactory.getLogger(MemoryTableDataModel.class);
156
157}