001package jmri.jmrit.logixng.actions.swing; 002 003import java.util.List; 004 005import javax.annotation.CheckForNull; 006import javax.annotation.Nonnull; 007import javax.swing.*; 008 009import jmri.*; 010import jmri.jmrit.logixng.*; 011import jmri.jmrit.logixng.actions.ActionFindTableRowOrColumn; 012import jmri.util.swing.BeanSelectPanel; 013import jmri.util.swing.JComboBoxUtil; 014 015/** 016 * Configures an ActionFindTableRowOrColumn object with a Swing JPanel. 017 * 018 * @author Daniel Bergqvist Copyright 2022 019 */ 020public class ActionFindTableRowOrColumnSwing extends AbstractDigitalActionSwing { 021 022 private BeanSelectPanel<NamedTable> _tableBeanPanel; 023 private JComboBox<TableRowOrColumn> _tableRowOrColumnComboBox; 024 private JComboBox<String> _rowOrColumnNameComboBox; 025 private JCheckBox _includeCellsWithoutHeaderCheckBox; 026 private JTextField _localVariableNamedBean; 027 private JTextField _localVariableRow; 028 029 @Override 030 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 031 if ((object != null) && !(object instanceof ActionFindTableRowOrColumn)) { 032 throw new IllegalArgumentException("object must be an ActionFindTableRowOrColumn but is a: "+object.getClass().getName()); 033 } 034 035 ActionFindTableRowOrColumn action = (ActionFindTableRowOrColumn)object; 036 037 panel = new JPanel(); 038 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 039 040 JPanel tableBeanPanelPanel = new JPanel(); 041 tableBeanPanelPanel.add(new JLabel(Bundle.getMessage("ActionFindTableRowOrColumnSwing_Table"))); 042 _tableBeanPanel = new BeanSelectPanel<>(InstanceManager.getDefault(NamedTableManager.class), null); 043 tableBeanPanelPanel.add(_tableBeanPanel); 044 panel.add(tableBeanPanelPanel); 045 046 _rowOrColumnNameComboBox = new JComboBox<>(); 047 _tableRowOrColumnComboBox = new JComboBox<>(); 048 for (TableRowOrColumn item : TableRowOrColumn.values()) { 049 _tableRowOrColumnComboBox.addItem(item); 050 } 051 JComboBoxUtil.setupComboBoxMaxRows(_tableRowOrColumnComboBox); 052 _tableRowOrColumnComboBox.addActionListener((evt) -> { 053 comboListener(action); 054 }); 055 _tableBeanPanel.getBeanCombo().addActionListener((evt) -> { 056 comboListener(action); 057 }); 058 059 JPanel tableRowOrColumnPanel = new JPanel(); 060 tableRowOrColumnPanel.add(new JLabel(Bundle.getMessage("ActionFindTableRowOrColumnSwing_RowOrColumn"))); 061 tableRowOrColumnPanel.add(_tableRowOrColumnComboBox); 062 panel.add(tableRowOrColumnPanel); 063 064 JPanel rowOrColumnNamePanel = new JPanel(); 065 rowOrColumnNamePanel.add(new JLabel(Bundle.getMessage("ActionFindTableRowOrColumnSwing_RowOrColumnName"))); 066 rowOrColumnNamePanel.add(_rowOrColumnNameComboBox); 067 panel.add(rowOrColumnNamePanel); 068 JComboBoxUtil.setupComboBoxMaxRows(_rowOrColumnNameComboBox); 069 070 JPanel includeCellsWithoutHeaderPanel = new JPanel(); 071 includeCellsWithoutHeaderPanel.add(new JLabel(Bundle.getMessage("ActionFindTableRowOrColumnSwing_IncludeCellsWithoutHeader"))); 072 _includeCellsWithoutHeaderCheckBox = new JCheckBox(); 073 includeCellsWithoutHeaderPanel.add(_includeCellsWithoutHeaderCheckBox); 074 panel.add(includeCellsWithoutHeaderPanel); 075 076 JPanel localVariableNamedBeanPanel = new JPanel(); 077 localVariableNamedBeanPanel.add(new JLabel(Bundle.getMessage("ActionFindTableRowOrColumnSwing_LocalVariableNamedBean"))); 078 _localVariableNamedBean = new JTextField(20); 079 localVariableNamedBeanPanel.add(_localVariableNamedBean); 080 panel.add(localVariableNamedBeanPanel); 081 082 JPanel localVariableRowPanel = new JPanel(); 083 localVariableRowPanel.add(new JLabel(Bundle.getMessage("ActionFindTableRowOrColumnSwing_LocalVariableRow"))); 084 _localVariableRow = new JTextField(20); 085 localVariableRowPanel.add(_localVariableRow); 086 panel.add(localVariableRowPanel); 087 088 if (action != null) { 089 if (action.getSelectNamedBean().getNamedBean() != null) { 090 _tableBeanPanel.setDefaultNamedBean(action.getSelectNamedBean().getNamedBean().getBean()); 091 } 092 _tableRowOrColumnComboBox.setSelectedItem(action.getTableRowOrColumn()); 093 _includeCellsWithoutHeaderCheckBox.setSelected(action.getIncludeCellsWithoutHeader()); 094 095 _localVariableNamedBean.setText(action.getLocalVariableNamedBean()); 096 _localVariableRow.setText(action.getLocalVariableRow()); 097 } 098 } 099 100 private void comboListener( ActionFindTableRowOrColumn action ) { 101 102 _rowOrColumnNameComboBox.removeAllItems(); 103 NamedTable table = _tableBeanPanel.getNamedBean(); 104 if (table != null) { 105 if (_tableRowOrColumnComboBox.getItemAt(_tableRowOrColumnComboBox.getSelectedIndex()) == TableRowOrColumn.Column) { 106 for (int column=0; column <= table.numColumns(); column++) { 107 // If the header is null or empty, treat the row as a comment 108 Object header = table.getCell(0, column); 109 if ((header != null) && (!header.toString().isEmpty())) { 110 _rowOrColumnNameComboBox.addItem(header.toString()); 111 } 112 } 113 } else { 114 for (int row=0; row <= table.numRows(); row++) { 115 // If the header is null or empty, treat the row as a comment 116 Object header = table.getCell(row, 0); 117 if ((header != null) && (!header.toString().isEmpty())) { 118 _rowOrColumnNameComboBox.addItem(header.toString()); 119 } 120 } 121 } 122 if (action != null) { 123 _rowOrColumnNameComboBox.setSelectedItem(action.getRowOrColumnName()); 124 } 125 } 126 } 127 128 /** {@inheritDoc} */ 129 @Override 130 public boolean validate(@Nonnull List<String> errorMessages) { 131 return true; 132 } 133 134 /** {@inheritDoc} */ 135 @Override 136 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 137 ActionFindTableRowOrColumn action = new ActionFindTableRowOrColumn(systemName, userName); 138 updateObject(action); 139 return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action); 140 } 141 142 /** {@inheritDoc} */ 143 @Override 144 public void updateObject(@Nonnull Base object) { 145 if (!(object instanceof ActionFindTableRowOrColumn)) { 146 throw new IllegalArgumentException("object must be an ActionFindTableRowOrColumn but is a: "+object.getClass().getName()); 147 } 148 if ( _tableBeanPanel == null ){ 149 throw new UnsupportedOperationException("No Bean Panel Present "); 150 } 151 152 ActionFindTableRowOrColumn action = (ActionFindTableRowOrColumn)object; 153 NamedTable table = _tableBeanPanel.getNamedBean(); 154 if (table != null) { 155 NamedBeanHandle<NamedTable> handle 156 = InstanceManager.getDefault(NamedBeanHandleManager.class) 157 .getNamedBeanHandle(table.getDisplayName(), table); 158 action.getSelectNamedBean().setNamedBean(handle); 159 } else { 160 action.getSelectNamedBean().removeNamedBean(); 161 } 162 action.setTableRowOrColumn(_tableRowOrColumnComboBox.getItemAt(_tableRowOrColumnComboBox.getSelectedIndex())); 163 if (_rowOrColumnNameComboBox.getSelectedIndex() != -1) { 164 action.setRowOrColumnName(_rowOrColumnNameComboBox.getItemAt(_rowOrColumnNameComboBox.getSelectedIndex())); 165 } else { 166 action.setRowOrColumnName(""); 167 } 168 action.setIncludeCellsWithoutHeader(_includeCellsWithoutHeaderCheckBox.isSelected()); 169 170 action.setLocalVariableNamedBean(_localVariableNamedBean.getText()); 171 action.setLocalVariableRow(_localVariableRow.getText()); 172 } 173 174 /** {@inheritDoc} */ 175 @Override 176 public String toString() { 177 return Bundle.getMessage("ActionFindTableRowOrColumn_Short"); 178 } 179 180 @Override 181 public void dispose() { 182 } 183 184 185// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionFindTableRowOrColumnSwing.class); 186 187}