001package jmri.jmrit.logixng.tools.swing;
002
003import java.awt.*;
004import java.awt.event.*;
005import java.util.List;
006
007import javax.swing.*;
008import javax.swing.table.AbstractTableModel;
009import javax.swing.table.TableCellEditor;
010import javax.swing.table.TableColumn;
011import javax.swing.table.TableColumnModel;
012
013import jmri.InstanceManager;
014import jmri.jmrit.logixng.LogixNG;
015import jmri.jmrit.logixng.LogixNG_InitializationManager;
016import jmri.jmrit.logixng.LogixNG_Manager;
017
018import jmri.util.JmriJFrame;
019import jmri.util.swing.BeanSelectPanel;
020import jmri.util.swing.JmriJOptionPane;
021import jmri.util.table.ButtonEditor;
022import jmri.util.table.ButtonRenderer;
023
024/**
025 * Table for LogixNG initialization.
026 *
027 * @author Daniel Bergqvist Copyright (C) 2021
028 */
029public class LogixNGInitializationTable extends JmriJFrame {
030
031    private static final int panelWidth700 = 700;
032    private static final int panelHeight500 = 500;
033
034    private final LogixNG_InitializationManager _initManager =
035            InstanceManager.getDefault(LogixNG_InitializationManager.class);
036
037    private InitializationTableModel _initTableModel = null;
038
039
040    @Override
041    public void initComponents() {
042        super.initComponents();
043        // build menu
044       JMenuBar menuBar = new JMenuBar();
045       setJMenuBar(menuBar);
046       addHelpMenu("package.jmri.jmrit.logixng.LogixNGInitializationTable", true); // NOI18N
047
048        Container contentPane = this.getContentPane();
049        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
050
051        _initTableModel = new InitializationTableModel();
052        JTable conditionalTable = new JTable(_initTableModel);
053        conditionalTable.setRowSelectionAllowed(false);
054        TableColumnModel conditionalColumnModel = conditionalTable
055                .getColumnModel();
056        TableColumn sNameColumn = conditionalColumnModel
057                .getColumn(InitializationTableModel.SYSTEM_NAME_COLUMN);
058        sNameColumn.setResizable(true);
059        sNameColumn.setMinWidth(100);
060        sNameColumn.setPreferredWidth(130);
061        TableColumn uNameColumn = conditionalColumnModel
062                .getColumn(InitializationTableModel.USER_NAME_COLUMN);
063        uNameColumn.setResizable(true);
064        uNameColumn.setMinWidth(210);
065        uNameColumn.setPreferredWidth(260);
066        TableColumn buttonDeleteColumn = conditionalColumnModel
067                .getColumn(InitializationTableModel.BUTTON_DELETE_COLUMN);
068        TableColumn buttonMoveUpColumn = conditionalColumnModel
069                .getColumn(InitializationTableModel.BUTTON_MOVE_UP_COLUMN);
070        TableColumn buttonMoveDownColumn = conditionalColumnModel
071                .getColumn(InitializationTableModel.BUTTON_MOVE_DOWN_COLUMN);
072
073        // install button renderer and editor
074        ButtonRenderer buttonRenderer = new ButtonRenderer();
075        conditionalTable.setDefaultRenderer(JButton.class, buttonRenderer);
076        TableCellEditor buttonEditor = new ButtonEditor(new JButton());
077        conditionalTable.setDefaultEditor(JButton.class, buttonEditor);
078        JButton testButton = new JButton("XXXXXXXXXX");  // NOI18N
079        conditionalTable.setRowHeight(testButton.getPreferredSize().height);
080        buttonDeleteColumn.setMinWidth(testButton.getPreferredSize().width);
081        buttonDeleteColumn.setMaxWidth(testButton.getPreferredSize().width);
082        buttonDeleteColumn.setResizable(false);
083        buttonMoveUpColumn.setMinWidth(testButton.getPreferredSize().width);
084        buttonMoveUpColumn.setMaxWidth(testButton.getPreferredSize().width);
085        buttonMoveUpColumn.setResizable(false);
086        buttonMoveDownColumn.setMinWidth(testButton.getPreferredSize().width);
087        buttonMoveDownColumn.setMaxWidth(testButton.getPreferredSize().width);
088        buttonMoveDownColumn.setResizable(false);
089
090        JScrollPane conditionalTableScrollPane = new JScrollPane(conditionalTable);
091        Dimension dim = conditionalTable.getPreferredSize();
092        dim.height = 450;
093        conditionalTableScrollPane.getViewport().setPreferredSize(dim);
094        contentPane.add(conditionalTableScrollPane);
095
096        // set up message
097        JPanel panel3 = new JPanel();
098        panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
099
100        contentPane.add(panel3);
101
102        // set up create and cancel buttons
103        JPanel panel5 = new JPanel();
104        panel5.setLayout(new FlowLayout());
105
106        // Add LogixNG
107        BeanSelectPanel<LogixNG> logixNG_SelectPanel =
108                new BeanSelectPanel<>(InstanceManager.getDefault(LogixNG_Manager.class), null);
109
110        panel5.add(logixNG_SelectPanel);
111        JButton addLogixNG = new JButton(Bundle.getMessage("LogixNG_Initialization_ButtonAddLogixNG"));    // NOI18N
112        panel5.add(addLogixNG);
113        addLogixNG.addActionListener((ActionEvent e) -> {
114            LogixNG logixNG = logixNG_SelectPanel.getNamedBean();
115            if (logixNG == null) {
116                JmriJOptionPane.showMessageDialog(this,
117                        Bundle.getMessage("LogixNG_Initialization_ErrorNoLogixNG_Selected"),
118                        Bundle.getMessage("LogixNG_Initialization_ErrorTitle"),
119                        JmriJOptionPane.ERROR_MESSAGE);
120                return;
121            }
122            for (LogixNG l : _initManager.getList()) {
123                if (logixNG == l) {
124                    JmriJOptionPane.showMessageDialog(this,
125                            Bundle.getMessage("LogixNG_Initialization_ErrorLogixNG_Exists"),
126                            Bundle.getMessage("LogixNG_Initialization_ErrorTitle"),
127                            JmriJOptionPane.ERROR_MESSAGE);
128                    return;
129                }
130            }
131            _initManager.add(logixNG);
132            _initTableModel.fireTableStructureChanged();
133        });
134
135        contentPane.add(panel5);
136
137        pack();
138
139        initMinimumSize(new Dimension(panelWidth700, panelHeight500));
140    }
141
142    public void initMinimumSize(Dimension dimension) {
143//         setMinimumSize(dimension);
144        pack();
145        setVisible(true);
146    }
147
148    // ------------ Table Models ------------
149
150    /**
151     * Table model for ConditionalNGs in the Edit LogixNG pane.
152     */
153    private final class InitializationTableModel extends AbstractTableModel {
154
155        public static final int SYSTEM_NAME_COLUMN = 0;
156        public static final int USER_NAME_COLUMN = SYSTEM_NAME_COLUMN + 1;
157        public static final int BUTTON_DELETE_COLUMN = USER_NAME_COLUMN + 1;
158        public static final int BUTTON_MOVE_UP_COLUMN = BUTTON_DELETE_COLUMN + 1;
159        public static final int BUTTON_MOVE_DOWN_COLUMN = BUTTON_MOVE_UP_COLUMN + 1;
160        public static final int NUM_COLUMNS = BUTTON_MOVE_DOWN_COLUMN + 1;
161
162        private final List<LogixNG> _logixNGs;
163
164
165        public InitializationTableModel() {
166            super();
167            _logixNGs = _initManager.getList();
168        }
169
170        @Override
171        public Class<?> getColumnClass(int c) {
172            if ((c == BUTTON_DELETE_COLUMN) || (c == BUTTON_MOVE_UP_COLUMN) || (c == BUTTON_MOVE_DOWN_COLUMN)) {
173                return JButton.class;
174            }
175            return String.class;
176        }
177
178        @Override
179        public int getColumnCount() {
180            return NUM_COLUMNS;
181        }
182
183        @Override
184        public int getRowCount() {
185            return _logixNGs.size();
186        }
187
188        @Override
189        public boolean isCellEditable(int r, int c) {
190            return ((c == BUTTON_DELETE_COLUMN) || (c == BUTTON_MOVE_UP_COLUMN) || (c == BUTTON_MOVE_DOWN_COLUMN));
191        }
192
193        @Override
194        public String getColumnName(int col) {
195            switch (col) {
196                case SYSTEM_NAME_COLUMN:
197                    return Bundle.getMessage("ColumnSystemName");  // NOI18N
198                case USER_NAME_COLUMN:
199                    return Bundle.getMessage("ColumnUserName");  // NOI18N
200                case BUTTON_DELETE_COLUMN:
201                case BUTTON_MOVE_UP_COLUMN:
202                case BUTTON_MOVE_DOWN_COLUMN:
203                    return ""; // no label
204                default:
205                    throw new IllegalArgumentException("Unknown column");
206            }
207        }
208
209        @Override
210        public Object getValueAt(int row, int col) {
211            if (row > _logixNGs.size()) {
212                return null;
213            }
214            switch (col) {
215                case SYSTEM_NAME_COLUMN:
216                    return _logixNGs.get(row).getSystemName();
217                case USER_NAME_COLUMN:
218                    return _logixNGs.get(row).getUserName();
219                case BUTTON_DELETE_COLUMN:
220                    return Bundle.getMessage("ButtonDelete");  // NOI18N
221                case BUTTON_MOVE_UP_COLUMN:
222                    return Bundle.getMessage("ButtonMoveUp");  // NOI18N
223                case BUTTON_MOVE_DOWN_COLUMN:
224                    return Bundle.getMessage("ButtonMoveDown");  // NOI18N
225                default:
226                    throw new IllegalArgumentException("Unknown column");
227            }
228        }
229
230        private void deleteLogixNG(int row) {
231            _initManager.delete(row);
232            fireTableRowsDeleted(row, row);
233        }
234
235        private void moveUp(int row) {
236            _initManager.moveUp(row);
237            fireTableRowsDeleted(row, row);
238        }
239
240        private void moveDown(int row) {
241            _initManager.moveDown(row);
242            fireTableRowsDeleted(row, row);
243        }
244
245        @Override
246        public void setValueAt(Object value, int row, int col) {
247            if (row > _logixNGs.size()) {
248                return;
249            }
250            switch (col) {
251                case SYSTEM_NAME_COLUMN:
252                    throw new IllegalArgumentException("System name cannot be changed");
253                case USER_NAME_COLUMN:
254                    throw new IllegalArgumentException("User name cannot be changed");
255                case BUTTON_DELETE_COLUMN:
256                    deleteLogixNG(row);
257                    break;
258                case BUTTON_MOVE_UP_COLUMN:
259                    moveUp(row);
260                    break;
261                case BUTTON_MOVE_DOWN_COLUMN:
262                    moveDown(row);
263                    break;
264                default:
265                    throw new IllegalArgumentException("Unknown column");
266            }
267        }
268    }
269}