001package jmri.jmrit.logixng.util.swing; 002 003import java.awt.*; 004import java.awt.event.ActionEvent; 005 006import javax.swing.*; 007import javax.swing.table.AbstractTableModel; 008 009import jmri.util.JmriJFrame; 010 011/** 012 * A dialog that shows the available enums in LogixNG_SelectEnum. 013 * 014 * @param <E> the type of enum 015 * 016 * @author Daniel Bergqvist (C) 2024 017 */ 018public class LogixNG_SelectEnumSwing_EnumDialog<E extends Enum<?>> { 019 020 private final JmriJFrame _frame; 021 private JTable _table; 022 private TableModel<E> _tableModel; 023 024 public LogixNG_SelectEnumSwing_EnumDialog(E[] enumArray, Runnable windowIsClosed) { 025 _frame = new JmriJFrame(Bundle.getMessage("LogixNG_SelectEnumSwing_ButtonEnumDialog")); 026// _frame.addHelpMenu( 027// "package.jmri.jmrit.beantable.LogixNGTable", true); // NOI18N 028 _frame.setLocation( 029 (Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - _frame.getWidth() / 2, 030 (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - _frame.getHeight() / 2); 031 Container contentPane = _frame.getContentPane(); 032 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 033 034 _table = new JTable(); 035 _tableModel = new TableModel<E>(enumArray); 036 _table.setModel(_tableModel); 037 JScrollPane scrollPane = new JScrollPane(_table); 038 _frame.add(scrollPane); 039 040 JPanel panel5 = new JPanel(); 041 JButton okButton = new JButton(Bundle.getMessage("ButtonOK")); // NOI18N 042 panel5.add(okButton); 043 okButton.addActionListener((ActionEvent e) -> { 044 windowIsClosed.run(); 045 dispose(); 046 }); 047 _frame.add(panel5); 048 049 _frame.pack(); 050 _frame.setVisible(true); 051 } 052 053 public void setVisible(boolean value) { 054 _frame.setVisible(value); 055 } 056 057 public void dispose() { 058 _frame.setVisible(false); 059 _frame.dispose(); 060 } 061 062 063 064 public static class TableModel<E extends Enum<?>> extends AbstractTableModel { 065 066 public static final int COLUMN_NAME = 0; 067 public static final int COLUMN_DESCRIPTION = COLUMN_NAME + 1; 068 069 private final E[] _enumArray; 070 071 public TableModel(E[] enumArray) { 072 _enumArray = enumArray; 073 } 074 075 /** {@inheritDoc} */ 076 @Override 077 public int getRowCount() { 078 return _enumArray.length; 079 } 080 081 /** {@inheritDoc} */ 082 @Override 083 public int getColumnCount() { 084 return 2; 085 } 086 087 /** {@inheritDoc} */ 088 @Override 089 public String getColumnName(int col) { 090 switch (col) { 091 case COLUMN_NAME: 092 return Bundle.getMessage("LogixNG_SelectEnumSwing_EnumDialog_ColumnName"); 093 case COLUMN_DESCRIPTION: 094 return Bundle.getMessage("LogixNG_SelectEnumSwing_EnumDialog_ColumnDescription"); 095 default: 096 throw new IllegalArgumentException("Invalid column"); 097 } 098 } 099 100 /** {@inheritDoc} */ 101 @Override 102 public Class<?> getColumnClass(int col) { 103 return String.class; 104 } 105 106 /** {@inheritDoc} */ 107 @Override 108 public boolean isCellEditable(int row, int col) { 109 return false; 110 } 111 112 /** {@inheritDoc} */ 113 @Override 114 public Object getValueAt(int rowIndex, int columnIndex) { 115 if (rowIndex >= _enumArray.length) throw new IllegalArgumentException("Invalid row"); 116 117 switch (columnIndex) { 118 case COLUMN_NAME: 119 return _enumArray[rowIndex].name(); 120 case COLUMN_DESCRIPTION: 121 return _enumArray[rowIndex].toString(); 122 default: 123 throw new IllegalArgumentException("Invalid column"); 124 } 125 } 126 127 } 128 129}