diff --git a/src/enginuity/logger/EcuLogger.java b/src/enginuity/logger/EcuLogger.java index 342891b9..ebe12a1f 100644 --- a/src/enginuity/logger/EcuLogger.java +++ b/src/enginuity/logger/EcuLogger.java @@ -8,6 +8,7 @@ import enginuity.logger.definition.EcuSwitch; import enginuity.logger.io.serial.SerialPortRefresher; import enginuity.logger.ui.LoggerDataTableModel; import enginuity.logger.ui.MessageListener; +import enginuity.logger.ui.ParameterListTable; import enginuity.logger.ui.ParameterListTableModel; import enginuity.logger.ui.ParameterRegistrationBroker; import enginuity.logger.ui.ParameterRegistrationBrokerImpl; @@ -168,9 +169,9 @@ public final class EcuLogger extends JFrame implements WindowListener, PropertyC } private JTable buildParamListTable(TableModel tableModel) { - JTable paramListTable = new JTable(tableModel); + JTable paramListTable = new ParameterListTable(tableModel); changeColumnWidth(paramListTable, 0, 20, 55, 55); - changeColumnWidth(paramListTable, 2, 50, 60, 60); + changeColumnWidth(paramListTable, 2, 50, 250, 80); return paramListTable; } diff --git a/src/enginuity/logger/definition/EcuData.java b/src/enginuity/logger/definition/EcuData.java index 62998daf..caa6a35d 100644 --- a/src/enginuity/logger/definition/EcuData.java +++ b/src/enginuity/logger/definition/EcuData.java @@ -14,7 +14,7 @@ public interface EcuData { EcuDataConvertor[] getConvertors(); - void selectConvertor(int index); + void selectConvertor(EcuDataConvertor convertor); EcuDataType getDataType(); diff --git a/src/enginuity/logger/definition/EcuDerivedParameterConvertorImpl.java b/src/enginuity/logger/definition/EcuDerivedParameterConvertorImpl.java index ed9f07d5..dc82a90a 100644 --- a/src/enginuity/logger/definition/EcuDerivedParameterConvertorImpl.java +++ b/src/enginuity/logger/definition/EcuDerivedParameterConvertorImpl.java @@ -50,4 +50,8 @@ public final class EcuDerivedParameterConvertorImpl implements EcuDerivedParamet checkNotNullOrEmpty(ecuDatas, "ecuDatas"); this.ecuDatas = ecuDatas; } + + public String toString() { + return getUnits(); + } } diff --git a/src/enginuity/logger/definition/EcuDerivedParameterImpl.java b/src/enginuity/logger/definition/EcuDerivedParameterImpl.java index 53a32ae1..e11f15a8 100644 --- a/src/enginuity/logger/definition/EcuDerivedParameterImpl.java +++ b/src/enginuity/logger/definition/EcuDerivedParameterImpl.java @@ -50,13 +50,12 @@ public final class EcuDerivedParameterImpl implements EcuParameter { return convertors; } - public void selectConvertor(int index) { - if (index < 0) { - selectedConvertorIndex = 0; - } else if (index >= convertors.length) { - selectedConvertorIndex = convertors.length - 1; - } else { - selectedConvertorIndex = index; + public void selectConvertor(EcuDataConvertor convertor) { + for (int i = 0; i < convertors.length; i++) { + EcuDerivedParameterConvertor parameterConvertor = convertors[i]; + if (convertor == parameterConvertor) { + selectedConvertorIndex = i; + } } } diff --git a/src/enginuity/logger/definition/EcuParameterConvertorImpl.java b/src/enginuity/logger/definition/EcuParameterConvertorImpl.java index 6fb44d10..4d9d6f34 100644 --- a/src/enginuity/logger/definition/EcuParameterConvertorImpl.java +++ b/src/enginuity/logger/definition/EcuParameterConvertorImpl.java @@ -34,4 +34,8 @@ public final class EcuParameterConvertorImpl implements EcuDataConvertor { return format.format(value); } + public String toString() { + return getUnits(); + } + } diff --git a/src/enginuity/logger/definition/EcuParameterImpl.java b/src/enginuity/logger/definition/EcuParameterImpl.java index 7417ff1b..aafc3829 100644 --- a/src/enginuity/logger/definition/EcuParameterImpl.java +++ b/src/enginuity/logger/definition/EcuParameterImpl.java @@ -49,13 +49,12 @@ public final class EcuParameterImpl implements EcuParameter { return convertors; } - public void selectConvertor(int index) { - if (index < 0) { - selectedConvertorIndex = 0; - } else if (index >= convertors.length) { - selectedConvertorIndex = convertors.length - 1; - } else { - selectedConvertorIndex = index; + public void selectConvertor(EcuDataConvertor convertor) { + for (int i = 0; i < convertors.length; i++) { + EcuDataConvertor dataConvertor = convertors[i]; + if (convertor == dataConvertor) { + selectedConvertorIndex = i; + } } } diff --git a/src/enginuity/logger/definition/EcuSwitchConvertorImpl.java b/src/enginuity/logger/definition/EcuSwitchConvertorImpl.java index 5cbf7183..7268e96d 100644 --- a/src/enginuity/logger/definition/EcuSwitchConvertorImpl.java +++ b/src/enginuity/logger/definition/EcuSwitchConvertorImpl.java @@ -21,6 +21,10 @@ public final class EcuSwitchConvertorImpl implements EcuDataConvertor { return value > 0 ? "1" : "0"; } + public String toString() { + return getUnits(); + } + private void checkBit(int bit) { if (bit < 0 || bit > 7) { throw new IllegalArgumentException("Bit must be between 0 and 7 inclusive."); diff --git a/src/enginuity/logger/definition/EcuSwitchImpl.java b/src/enginuity/logger/definition/EcuSwitchImpl.java index fe258dd4..12f4d5c9 100644 --- a/src/enginuity/logger/definition/EcuSwitchImpl.java +++ b/src/enginuity/logger/definition/EcuSwitchImpl.java @@ -51,13 +51,12 @@ public final class EcuSwitchImpl implements EcuSwitch { return convertors; } - public void selectConvertor(int index) { - if (index < 0) { - selectedConvertorIndex = 0; - } else if (index >= convertors.length) { - selectedConvertorIndex = convertors.length - 1; - } else { - selectedConvertorIndex = index; + public void selectConvertor(EcuDataConvertor convertor) { + for (int i = 0; i < convertors.length; i++) { + EcuDataConvertor dataConvertor = convertors[i]; + if (convertor == dataConvertor) { + selectedConvertorIndex = i; + } } } diff --git a/src/enginuity/logger/ui/ParameterListTable.java b/src/enginuity/logger/ui/ParameterListTable.java new file mode 100644 index 00000000..f1e4cd9a --- /dev/null +++ b/src/enginuity/logger/ui/ParameterListTable.java @@ -0,0 +1,36 @@ +package enginuity.logger.ui; + +import enginuity.logger.definition.EcuData; + +import javax.swing.*; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableModel; + +public final class ParameterListTable extends JTable { + private UnitsComboBoxEditor comboBoxEditor = new UnitsComboBoxEditor(); + private UnitsComboBoxRenderer comboBoxRenderer = new UnitsComboBoxRenderer(); + + public ParameterListTable(TableModel tableModel) { + super(tableModel); + } + + public TableCellRenderer getCellRenderer(int row, int col) { + return displayComboBox(row, col) ? comboBoxRenderer : super.getCellRenderer(row, col); + } + + public TableCellEditor getCellEditor(int row, int col) { + return displayComboBox(row, col) ? comboBoxEditor : super.getCellEditor(row, col); + } + + private boolean displayComboBox(int row, int col) { + Object value = getValueAt(row, col); + if (EcuData.class.isAssignableFrom(value.getClass())) { + EcuData ecuData = (EcuData) value; + if (ecuData.getConvertors().length > 1) { + return true; + } + } + return false; + } +} diff --git a/src/enginuity/logger/ui/ParameterListTableModel.java b/src/enginuity/logger/ui/ParameterListTableModel.java index 0580d8c5..7e9bb65a 100644 --- a/src/enginuity/logger/ui/ParameterListTableModel.java +++ b/src/enginuity/logger/ui/ParameterListTableModel.java @@ -45,7 +45,8 @@ public final class ParameterListTableModel extends AbstractTableModel { case 1: return paramRow.getEcuData().getName(); case 2: - return paramRow.getEcuData().getSelectedConvertor().getUnits(); + EcuData ecuData = paramRow.getEcuData(); + return ecuData.getConvertors().length > 1 ? ecuData : ecuData.getSelectedConvertor().getUnits(); default: return "Error!"; } diff --git a/src/enginuity/logger/ui/UnitsComboBoxEditor.java b/src/enginuity/logger/ui/UnitsComboBoxEditor.java new file mode 100644 index 00000000..c1a470cb --- /dev/null +++ b/src/enginuity/logger/ui/UnitsComboBoxEditor.java @@ -0,0 +1,45 @@ +package enginuity.logger.ui; + +import enginuity.logger.definition.EcuData; +import enginuity.logger.definition.EcuDataConvertor; + +import javax.swing.*; +import javax.swing.table.TableCellEditor; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public final class UnitsComboBoxEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { + private static final String EDIT_COMMAND = "EDIT"; + private EcuData currentEcuData; + + public Object getCellEditorValue() { + return currentEcuData.getSelectedConvertor(); + } + + public Component getTableCellEditorComponent(JTable table, Object ecuData, boolean isSelected, int row, int column) { + currentEcuData = (EcuData) ecuData; + EcuDataConvertor[] convertors = currentEcuData.getConvertors(); + JComboBox comboBox = new JComboBox(); + for (EcuDataConvertor convertor : convertors) { + comboBox.addItem(convertor); + } + comboBox.setSelectedItem(currentEcuData.getSelectedConvertor()); + comboBox.setEditable(false); + comboBox.setEnabled(true); + comboBox.setActionCommand(EDIT_COMMAND); + comboBox.addActionListener(this); + return comboBox; + } + + public void actionPerformed(ActionEvent actionEvent) { + if (EDIT_COMMAND.equals(actionEvent.getActionCommand())) { + Object source = actionEvent.getSource(); + if (source != null && JComboBox.class.isAssignableFrom(source.getClass())) { + JComboBox comboBox = (JComboBox) source; + currentEcuData.selectConvertor((EcuDataConvertor) comboBox.getSelectedItem()); + fireEditingStopped(); + } + } + } +} diff --git a/src/enginuity/logger/ui/UnitsComboBoxRenderer.java b/src/enginuity/logger/ui/UnitsComboBoxRenderer.java new file mode 100644 index 00000000..58023615 --- /dev/null +++ b/src/enginuity/logger/ui/UnitsComboBoxRenderer.java @@ -0,0 +1,22 @@ +package enginuity.logger.ui; + +import enginuity.logger.definition.EcuData; +import enginuity.logger.definition.EcuDataConvertor; + +import javax.swing.*; +import javax.swing.table.TableCellRenderer; +import java.awt.*; + +public final class UnitsComboBoxRenderer extends JComboBox implements TableCellRenderer { + + public Component getTableCellRendererComponent(JTable table, Object ecuData, boolean isSelected, boolean hasFocus, int row, int column) { + EcuData currentEcuData = (EcuData) ecuData; + EcuDataConvertor[] convertors = currentEcuData.getConvertors(); + JComboBox comboBox = new JComboBox(); + for (EcuDataConvertor convertor : convertors) { + comboBox.addItem(convertor); + } + comboBox.setSelectedItem(currentEcuData.getSelectedConvertor()); + return comboBox; + } +}