001package jmri.jmrit.beantable.light;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import javax.swing.JButton;
007import javax.swing.JTable;
008import javax.swing.table.TableColumn;
009import javax.swing.table.TableColumnModel;
010
011import jmri.implementation.DefaultLightControl;
012import jmri.InstanceManager;
013import jmri.Light;
014import jmri.LightControl;
015import jmri.util.table.ButtonEditor;
016import jmri.util.table.ButtonRenderer;
017
018import static jmri.jmrit.beantable.LightTableAction.getDescriptionText;
019
020// import org.slf4j.Logger;
021// import org.slf4j.LoggerFactory;
022
023/**
024 * Table model for Light Controls in the Add/Edit Light windows.
025 * No direct access to this class is normally required, access via
026 * LightControlPane.java
027 *
028 * Code originally within LightTableAction.
029 *
030 * @author Dave Duchamp Copyright (C) 2004
031 * @author Egbert Broerse Copyright (C) 2017
032 * @author Steve Young Copyright (C) 2021
033 */
034public class LightControlTableModel extends javax.swing.table.AbstractTableModel {
035
036    public static final int TYPE_COLUMN = 0;
037    public static final int DESCRIPTION_COLUMN = 1;
038    public static final int EDIT_COLUMN = 2;
039    public static final int REMOVE_COLUMN = 3;
040
041    private final LightControlPane lcp;
042    private final ArrayList<LightControl> controlList;
043
044    protected static final String sensorControl = Bundle.getMessage("LightSensorControl");
045    protected static final String fastClockControl = Bundle.getMessage("LightFastClockControl");
046    protected static final String turnoutStatusControl = Bundle.getMessage("LightTurnoutStatusControl");
047    protected static final String timedOnControl = Bundle.getMessage("LightTimedOnControl");
048    protected static final String twoSensorControl = Bundle.getMessage("LightTwoSensorControl");
049    protected static final String noControl = Bundle.getMessage("LightNoControl");
050
051    static final String[] controlTypes = new String[]{
052        noControl,
053        sensorControl,
054        fastClockControl,
055        turnoutStatusControl,
056        timedOnControl,
057        twoSensorControl };
058
059    protected static final List<String> getControlTypeTips(){
060        ArrayList<String> typeTooltips = new ArrayList<>();
061        typeTooltips.add(null); // no Control Type selected
062        typeTooltips.add(Bundle.getMessage("LightSensorControlTip"));
063        typeTooltips.add(Bundle.getMessage("LightFastClockControlTip"));
064        typeTooltips.add(Bundle.getMessage("LightTurnoutStatusControlTip",
065                InstanceManager.turnoutManagerInstance().getClosedText(),
066                InstanceManager.turnoutManagerInstance().getThrownText()));
067        typeTooltips.add(Bundle.getMessage("LightTimedOnControlTip"));
068        typeTooltips.add(Bundle.getMessage("LightTwoSensorControlTip"));
069        return typeTooltips;
070    }
071    /**
072     * Get text showing the type of Light Control.
073     *
074     * @param type the type of Light Control
075     * @return name of type or the description for {@link jmri.Light#NO_CONTROL}
076     *         if type is not recognized
077     */
078    public static String getControlTypeText(int type) {
079        switch (type) {
080            case Light.SENSOR_CONTROL:
081                return sensorControl;
082            case Light.FAST_CLOCK_CONTROL:
083                return fastClockControl;
084            case Light.TURNOUT_STATUS_CONTROL:
085                return turnoutStatusControl;
086            case Light.TIMED_ON_CONTROL:
087                return timedOnControl;
088            case Light.TWO_SENSOR_CONTROL:
089                return twoSensorControl;
090            case Light.NO_CONTROL:
091            default:
092                return noControl;
093        }
094    }
095
096    public LightControlTableModel(LightControlPane pane) {
097        super();
098        controlList = new ArrayList<>();
099        lcp = pane;
100    }
101
102    /**
103     * Get the Current Light Control List for the Table.
104     * @return unmodifiable List of Light Controls.
105     */
106    public List<LightControl> getControlList(){
107        return java.util.Collections.unmodifiableList(controlList);
108    }
109
110    public void setTableToLight(Light light){
111        // Get a fresh copy of the LightControl list
112        controlList.clear();
113        light.getLightControlList().forEach((lightControlList1) -> controlList.add(new DefaultLightControl(lightControlList1)));
114        fireTableDataChanged();
115    }
116
117    public void addControl(LightControl lc){
118        controlList.add(lc);
119        fireTableDataChanged();
120    }
121
122    public void removeControl(LightControl lc){
123        controlList.remove(lc);
124        fireTableDataChanged();
125    }
126
127    /**
128     * {@inheritDoc}
129     */
130    @Override
131    public Class<?> getColumnClass(int c) {
132        switch (c) {
133            case EDIT_COLUMN:
134            case REMOVE_COLUMN:
135                return JButton.class;
136            case TYPE_COLUMN:
137            case DESCRIPTION_COLUMN:
138            default:
139                return String.class;
140        }
141    }
142
143    /**
144     * {@inheritDoc}
145     */
146    @Override
147    public int getColumnCount() {
148        return REMOVE_COLUMN + 1;
149    }
150
151    /**
152     * {@inheritDoc}
153     */
154    @Override
155    public int getRowCount() {
156        return controlList.size();
157    }
158
159    /**
160     * {@inheritDoc}
161     */
162    @Override
163    public boolean isCellEditable(int r, int c) {
164        switch (c) {
165            case EDIT_COLUMN:
166            case REMOVE_COLUMN:
167                return true;
168            case TYPE_COLUMN:
169            case DESCRIPTION_COLUMN:
170            default:
171                return false;
172        }
173    }
174
175    /**
176     * {@inheritDoc}
177     */
178    @Override
179    public String getColumnName(int col) {
180        if (col == TYPE_COLUMN) {
181            return Bundle.getMessage("LightControlType");
182        } else if (col == DESCRIPTION_COLUMN) {
183            return Bundle.getMessage("LightControlDescription");
184        }
185        return "";
186    }
187
188    /**
189     * {@inheritDoc}
190     */
191    @Override
192    public Object getValueAt(int r, int c) {
193        LightControl lc = controlList.get(r);
194        switch (c) {
195            case TYPE_COLUMN:
196                return (getControlTypeText(lc.getControlType()));
197            case DESCRIPTION_COLUMN:
198                return (getDescriptionText(lc, lc.getControlType()));
199            case EDIT_COLUMN:
200                return Bundle.getMessage("ButtonEdit");
201            case REMOVE_COLUMN:
202                return Bundle.getMessage("ButtonDelete");
203            default:
204                return "";
205        }
206    }
207
208    /**
209     * {@inheritDoc}
210     */
211    @Override
212    public void setValueAt(Object value, int row, int col) {
213        if (col == EDIT_COLUMN) {
214            // set up to edit. Use separate Runnable so window is created on top
215            javax.swing.SwingUtilities.invokeLater(() -> {
216                lcp.editControlAction(controlList.get(row));
217            });
218        }
219        else if (col == REMOVE_COLUMN) {
220            controlList.remove(row);
221            fireTableDataChanged();
222        }
223    }
224
225    protected void configureJTable(JTable table){
226
227        table.setRowSelectionAllowed(false);
228
229        TableColumnModel lightControlColumnModel = table.getColumnModel();
230        TableColumn typeColumn = lightControlColumnModel.getColumn(LightControlTableModel.TYPE_COLUMN);
231        typeColumn.setResizable(true);
232        typeColumn.setMinWidth(130);
233        typeColumn.setMaxWidth(170);
234        TableColumn descriptionColumn = lightControlColumnModel.getColumn(
235                LightControlTableModel.DESCRIPTION_COLUMN);
236        descriptionColumn.setResizable(true);
237        descriptionColumn.setMinWidth(270);
238        descriptionColumn.setMaxWidth(380);
239
240        table.setDefaultRenderer(JButton.class, new ButtonRenderer());
241        table.setDefaultEditor(JButton.class, new ButtonEditor(new JButton()));
242
243        JButton testButton = new JButton(Bundle.getMessage("ButtonDelete"));
244        table.setRowHeight(testButton.getPreferredSize().height);
245        TableColumn editColumn = lightControlColumnModel.getColumn(LightControlTableModel.EDIT_COLUMN);
246        editColumn.setResizable(false);
247        editColumn.setMinWidth(new JButton(Bundle.getMessage("ButtonEdit")).getPreferredSize().width);
248        TableColumn removeColumn = lightControlColumnModel.getColumn(LightControlTableModel.REMOVE_COLUMN);
249        removeColumn.setResizable(false);
250        removeColumn.setMinWidth(testButton.getPreferredSize().width);
251
252    }
253
254    // private final static Logger log = LoggerFactory.getLogger(LightControlTableModel.class);
255
256}