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.ActionListenOnBeansTable; 012import jmri.jmrit.logixng.NamedBeanType; 013import jmri.util.swing.BeanSelectPanel; 014import jmri.util.swing.JComboBoxUtil; 015 016/** 017 * Configures an ActionListenOnBeansTable object with a Swing JPanel. 018 * 019 * @author Daniel Bergqvist Copyright 2021 020 */ 021public class ActionListenOnBeansTableSwing extends AbstractDigitalActionSwing { 022 023 private BeanSelectPanel<NamedTable> _tableBeanPanel; 024 private JComboBox<TableRowOrColumn> _tableRowOrColumnComboBox; 025 private JComboBox<String> _rowOrColumnNameComboBox; 026 private JCheckBox _includeCellsWithoutHeaderCheckBox; 027 private JComboBox<NamedBeanType> _namedBeanTypeComboBox; 028 private JCheckBox _listenOnAllPropertiesCheckBox; 029 private JTextField _localVariableNamedBean; 030 private JTextField _localVariableEvent; 031 private JTextField _localVariableNewValue; 032 033 @Override 034 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 035 if ((object != null) && !(object instanceof ActionListenOnBeansTable)) { 036 throw new IllegalArgumentException("object must be an ActionListenOnBeansTable but is a: "+object.getClass().getName()); 037 } 038 039 ActionListenOnBeansTable action = (ActionListenOnBeansTable)object; 040 041 panel = new JPanel(); 042 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 043 044 JPanel tableBeanPanelPanel = new JPanel(); 045 tableBeanPanelPanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansTableSwing_Table"))); 046 _tableBeanPanel = new BeanSelectPanel<>(InstanceManager.getDefault(NamedTableManager.class), null); 047 tableBeanPanelPanel.add(_tableBeanPanel); 048 panel.add(tableBeanPanelPanel); 049 050 _rowOrColumnNameComboBox = new JComboBox<>(); 051 _tableRowOrColumnComboBox = new JComboBox<>(); 052 for (TableRowOrColumn item : TableRowOrColumn.values()) { 053 _tableRowOrColumnComboBox.addItem(item); 054 } 055 JComboBoxUtil.setupComboBoxMaxRows(_tableRowOrColumnComboBox); 056 _tableRowOrColumnComboBox.addActionListener((evt) -> { 057 comboListener(action); 058 }); 059 _tableBeanPanel.getBeanCombo().addActionListener((evt) -> { 060 comboListener(action); 061 }); 062 063 JPanel tableRowOrColumnPanel = new JPanel(); 064 tableRowOrColumnPanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansTableSwing_RowOrColumn"))); 065 tableRowOrColumnPanel.add(_tableRowOrColumnComboBox); 066 panel.add(tableRowOrColumnPanel); 067 068 JPanel rowOrColumnNamePanel = new JPanel(); 069 rowOrColumnNamePanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansTableSwing_RowOrColumnName"))); 070 rowOrColumnNamePanel.add(_rowOrColumnNameComboBox); 071 panel.add(rowOrColumnNamePanel); 072 JComboBoxUtil.setupComboBoxMaxRows(_rowOrColumnNameComboBox); 073 074 JPanel includeCellsWithoutHeaderPanel = new JPanel(); 075 includeCellsWithoutHeaderPanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansTableSwing_IncludeCellsWithoutHeader"))); 076 _includeCellsWithoutHeaderCheckBox = new JCheckBox(); 077 includeCellsWithoutHeaderPanel.add(_includeCellsWithoutHeaderCheckBox); 078 panel.add(includeCellsWithoutHeaderPanel); 079 080 JPanel namedBeanTypePanel = new JPanel(); 081 namedBeanTypePanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansTableSwing_NamedBeanType"))); 082 _namedBeanTypeComboBox = new JComboBox<>(); 083 for (NamedBeanType item : NamedBeanType.values()) { 084 _namedBeanTypeComboBox.addItem(item); 085 } 086 JComboBoxUtil.setupComboBoxMaxRows(_namedBeanTypeComboBox); 087 namedBeanTypePanel.add(_namedBeanTypeComboBox); 088 panel.add(namedBeanTypePanel); 089 090 JPanel listenOnAllPropertiesPanel = new JPanel(); 091 listenOnAllPropertiesPanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansTableSwing_ListenOnAllPropertiesCheckBox"))); 092 _listenOnAllPropertiesCheckBox = new JCheckBox(); 093 listenOnAllPropertiesPanel.add(_listenOnAllPropertiesCheckBox); 094 panel.add(listenOnAllPropertiesPanel); 095 096 JPanel localVariableNamedBeanPanel = new JPanel(); 097 localVariableNamedBeanPanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansSwing_LocalVariableNamedBean"))); 098 _localVariableNamedBean = new JTextField(20); 099 localVariableNamedBeanPanel.add(_localVariableNamedBean); 100 panel.add(localVariableNamedBeanPanel); 101 102 JPanel localVariableNamedEventPanel = new JPanel(); 103 localVariableNamedEventPanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansSwing_LocalVariableEvent"))); 104 _localVariableEvent = new JTextField(20); 105 localVariableNamedEventPanel.add(_localVariableEvent); 106 panel.add(localVariableNamedEventPanel); 107 108 JPanel localVariableNewValuePanel = new JPanel(); 109 localVariableNewValuePanel.add(new JLabel(Bundle.getMessage("ActionListenOnBeansSwing_LocalVariableNewValue"))); 110 _localVariableNewValue = new JTextField(20); 111 localVariableNewValuePanel.add(_localVariableNewValue); 112 panel.add(localVariableNewValuePanel); 113 114 if (action != null) { 115 if (action.getSelectNamedBean().getNamedBean() != null) { 116 _tableBeanPanel.setDefaultNamedBean(action.getSelectNamedBean().getNamedBean().getBean()); 117 } 118 _tableRowOrColumnComboBox.setSelectedItem(action.getTableRowOrColumn()); 119 _includeCellsWithoutHeaderCheckBox.setSelected(action.getIncludeCellsWithoutHeader()); 120 _namedBeanTypeComboBox.setSelectedItem(action.getNamedBeanType()); 121 _listenOnAllPropertiesCheckBox.setSelected(action.getListenOnAllProperties()); 122 123 _localVariableNamedBean.setText(action.getLocalVariableNamedBean()); 124 _localVariableEvent.setText(action.getLocalVariableEvent()); 125 _localVariableNewValue.setText(action.getLocalVariableNewValue()); 126 } 127 } 128 129 private void comboListener( ActionListenOnBeansTable action ) { 130 131 _rowOrColumnNameComboBox.removeAllItems(); 132 NamedTable table = _tableBeanPanel.getNamedBean(); 133 if (table != null) { 134 if (_tableRowOrColumnComboBox.getItemAt(_tableRowOrColumnComboBox.getSelectedIndex()) == TableRowOrColumn.Column) { 135 for (int column=0; column <= table.numColumns(); column++) { 136 // If the header is null or empty, treat the row as a comment 137 Object header = table.getCell(0, column); 138 if ((header != null) && (!header.toString().isEmpty())) { 139 _rowOrColumnNameComboBox.addItem(header.toString()); 140 } 141 } 142 } else { 143 for (int row=0; row <= table.numRows(); row++) { 144 // If the header is null or empty, treat the row as a comment 145 Object header = table.getCell(row, 0); 146 if ((header != null) && (!header.toString().isEmpty())) { 147 _rowOrColumnNameComboBox.addItem(header.toString()); 148 } 149 } 150 } 151 if (action != null) { 152 _rowOrColumnNameComboBox.setSelectedItem(action.getRowOrColumnName()); 153 } 154 } 155 } 156 157 /** {@inheritDoc} */ 158 @Override 159 public boolean validate(@Nonnull List<String> errorMessages) { 160 return true; 161 } 162 163 /** {@inheritDoc} */ 164 @Override 165 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 166 ActionListenOnBeansTable action = new ActionListenOnBeansTable(systemName, userName); 167 updateObject(action); 168 return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action); 169 } 170 171 /** {@inheritDoc} */ 172 @Override 173 public void updateObject(@Nonnull Base object) { 174 if (!(object instanceof ActionListenOnBeansTable)) { 175 throw new IllegalArgumentException("object must be an ActionListenOnBeansTable but is a: "+object.getClass().getName()); 176 } 177 if ( _tableBeanPanel == null ){ 178 throw new UnsupportedOperationException("No Bean Panel Present "); 179 } 180 181 ActionListenOnBeansTable action = (ActionListenOnBeansTable)object; 182 NamedTable table = _tableBeanPanel.getNamedBean(); 183 if (table != null) { 184 NamedBeanHandle<NamedTable> handle 185 = InstanceManager.getDefault(NamedBeanHandleManager.class) 186 .getNamedBeanHandle(table.getDisplayName(), table); 187 action.getSelectNamedBean().setNamedBean(handle); 188 } else { 189 action.getSelectNamedBean().removeNamedBean(); 190 } 191 action.setTableRowOrColumn(_tableRowOrColumnComboBox.getItemAt(_tableRowOrColumnComboBox.getSelectedIndex())); 192 if (_rowOrColumnNameComboBox.getSelectedIndex() != -1) { 193 action.setRowOrColumnName(_rowOrColumnNameComboBox.getItemAt(_rowOrColumnNameComboBox.getSelectedIndex())); 194 } else { 195 action.setRowOrColumnName(""); 196 } 197 if (_namedBeanTypeComboBox.getSelectedIndex() != -1) { 198 action.setNamedBeanType(_namedBeanTypeComboBox.getItemAt(_namedBeanTypeComboBox.getSelectedIndex())); 199 } 200 action.setIncludeCellsWithoutHeader(_includeCellsWithoutHeaderCheckBox.isSelected()); 201 action.setListenOnAllProperties(_listenOnAllPropertiesCheckBox.isSelected()); 202 203 action.setLocalVariableNamedBean(_localVariableNamedBean.getText()); 204 action.setLocalVariableEvent(_localVariableEvent.getText()); 205 action.setLocalVariableNewValue(_localVariableNewValue.getText()); 206 } 207 208 /** {@inheritDoc} */ 209 @Override 210 public String toString() { 211 return Bundle.getMessage("ActionListenOnBeansTable_Short"); 212 } 213 214 @Override 215 public void dispose() { 216 } 217 218 219// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionListenOnBeansTableSwing.class); 220 221}