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.StringIO;
011import jmri.StringIOManager;
012
013
014/**
015 * Data model for a StringIO Table.
016 *
017 * @author Bob Jacobsen Copyright (C) 2003, 2024
018 * @author Steve Young Copyright (C) 2021
019 */
020public class StringIOTableDataModel extends BeanTableDataModel<StringIO> {
021
022    static final int KNOWNCOL = NUMCOLUMN;
023    
024    public StringIOTableDataModel(Manager<StringIO> mgr){
025        super();
026        setManager(mgr);
027    }
028
029    private StringIOManager stringIOManager;
030
031    /**
032     * {@inheritDoc}
033     */
034    @Override
035    @CheckForNull
036    public String getValue(String name) {
037        StringIO r = getManager().getBySystemName(name);
038        if (r == null) {
039            return "";
040        }
041        return r.getCommandedStringValue();
042    }
043
044    /**
045     * {@inheritDoc}
046     */
047    @Override
048    public final void setManager(Manager<StringIO> rm) {
049        if (!(rm instanceof StringIOManager)) {
050            return;
051        }
052        getManager().removePropertyChangeListener(this);
053        if (sysNameList != null) {
054            for (int i = 0; i < sysNameList.size(); i++) {
055                // if object has been deleted, it's not here; ignore it
056                NamedBean b = getBySystemName(sysNameList.get(i));
057                if (b != null) {
058                    b.removePropertyChangeListener(this);
059                }
060            }
061        }
062        stringIOManager = (StringIOManager) rm;
063        getManager().addPropertyChangeListener(this);
064        updateNameList();
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public StringIOManager getManager() {
072        return ( stringIOManager == null ? InstanceManager.getDefault(StringIOManager.class): stringIOManager);
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public StringIO getBySystemName(@Nonnull String name) {
080        return getManager().getBySystemName(name);
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public StringIO getByUserName(@Nonnull String name) {
088        return getManager().getByUserName(name);
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    protected String getMasterClassName() {
096        return StringIOTableAction.class.getName();
097    }
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public void clickOn(StringIO t) {
104        // don't do anything on click; not used in this class, because
105        // we override setValueAt
106    }
107
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    public int getPreferredWidth(int col) {
114        if (col == KNOWNCOL) {
115            return new JTextField(15).getPreferredSize().width; // TODO I18N using Bundle.getMessage()
116        }
117        return super.getPreferredWidth(col);
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public Object getValueAt(int row, int col) {
125        switch (col) {
126            case KNOWNCOL:
127                StringIO r = getManager().getBySystemName(sysNameList.get(row));
128                if (r == null) {
129                    return "";
130                }
131                return r.getKnownStringValue();
132            default:
133                return super.getValueAt(row, col);
134        }
135    }
136
137    /**
138     * {@inheritDoc}
139     */
140    @Override
141    public void setValueAt(Object value, int row, int col) {
142        switch (col) {
143            case VALUECOL:
144                try {
145                    getBySystemName(sysNameList.get(row)).setCommandedStringValue(value.toString());
146                } catch (jmri.JmriException ex) {
147                    log.error("Exception trying to set value at {},{} to {}", row, col, value);
148                }
149                fireTableRowsUpdated(row, row);
150                break;
151            default:
152                super.setValueAt(value, row, col);
153                break;
154        }
155    }
156
157    /**
158     * {@inheritDoc}
159     */
160    @Override
161    public int getColumnCount() {
162        return NUMCOLUMN + getPropertyColumnCount() + 1;  // +1 for known column
163    }
164
165    /**
166     * {@inheritDoc}
167     */
168    @Override
169    public String getColumnName(int col) {
170        switch (col) {
171            case VALUECOL:
172                return Bundle.getMessage("StringIOCommanded");
173            case KNOWNCOL:
174                return Bundle.getMessage("StringIOKnown");
175            default:
176                return super.getColumnName(col);
177        }
178    }
179
180    /**
181     * {@inheritDoc}
182     */
183    @Override
184    public Class<?> getColumnClass(int col) {
185        switch (col) {
186            case VALUECOL:
187                return String.class;
188            case KNOWNCOL:
189                return String.class;
190            default:
191                return super.getColumnClass(col);
192        }
193    }
194
195    /**
196     * {@inheritDoc}
197     */
198    @Override
199    public void configValueColumn(JTable table) {
200        // value column isn't button, so config is null
201    }
202
203    /**
204     * {@inheritDoc}
205     */
206    @Override
207    protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
208        return true;
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public JButton configureButton() {
216        log.error("configureButton should not have been called");
217        return null;
218    }
219
220    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StringIOTableDataModel.class);
221
222}