001package jmri.jmrix.can.cbus.swing.modules; 002 003import javax.swing.event.TableModelEvent; 004 005import jmri.jmrix.can.cbus.node.CbusNode; 006import static jmri.jmrix.can.cbus.node.CbusNodeConstants.*; 007import jmri.jmrix.can.cbus.node.CbusNodeNVTableDataModel; 008import static jmri.jmrix.can.cbus.node.CbusNodeNVTableDataModel.NV_SELECT_COLUMN; 009 010/** 011 * Abstract Node Variable edit Frame for a CBUS module 012 * 013 * @author Andrew Crosland Copyright (C) 2021 014 */ 015abstract public class AbstractEditNVPane extends jmri.jmrix.can.swing.CanPanel { 016 017 protected CbusNodeNVTableDataModel _dataModel; 018 protected CbusNode _node; 019 protected int _fwMaj = -1; 020 protected int _fwMin = -1; 021 protected int _fwBuild = -1; 022 023 024 public AbstractEditNVPane(CbusNodeNVTableDataModel dataModel, CbusNode node) { 025 super(); 026 _dataModel = dataModel; 027 _node = node; 028 _fwMaj = node.getNodeParamManager().getParameter(MAJOR_VER_IDX); 029 _fwMin = node.getNodeParamManager().getParameter(MINOR_VER_IDX); 030 _fwBuild = node.getNodeParamManager().getParameter(BETA_REV_IDX); 031 } 032 033 /** 034 * Build the edit gui for display 035 * 036 * @return the JPanel containing the edit gui 037 */ 038 abstract public AbstractEditNVPane getContent(); 039 040 /** 041 * The node table model has changed. 042 * 043 * Decode the event to update the edit gui 044 * 045 * @param e the change event 046 */ 047 abstract public void tableChanged(TableModelEvent e); 048 049 /** 050 * Get the NV value from NV_SELECT_COLUMN 051 * 052 * @param row index of NV 053 * 054 * @return the NV value, 0 if NV not available yet 055 */ 056 protected int getSelectValue8(int row) { 057 try { 058 return (int)_dataModel.getValueAt(row - 1, NV_SELECT_COLUMN); 059 } catch (NullPointerException ex) { 060 // NVs are not available yet, e.g. during resync 061 return 0; 062 } 063 } 064 065 /** 066 * Get the NV value from NV_SELECT_COLUMN 067 * 068 * @param row index of NV 069 * @param min minimum value to return 070 * 071 * @return the NV value, or min if NVs not available yet 072 */ 073 protected int getSelectValue8(int row, int min) { 074 try { 075 int val = (int)_dataModel.getValueAt(row - 1, NV_SELECT_COLUMN); 076 if (val < min) { 077 return min; 078 } else { 079 return val; 080 } 081 } catch (NullPointerException ex) { 082 // NVs are not available yet, e.g. during resync 083 return min; 084 } 085 } 086 087 /** 088 * Get the NV value from NV_SELECT_COLUMN 089 * 090 * @param row index of NV 091 * @param min minimum value to return 092 * @param max maximum value to return 093 * 094 * @return the NV value, or min if NVs not available yet 095 */ 096 protected int getSelectValue8(int row, int min, int max) { 097 try { 098 int val = (int)_dataModel.getValueAt(row - 1, NV_SELECT_COLUMN); 099 if (val < min) { 100 return min; 101 } else if (val > max) { 102 return max; 103 } else { 104 return val; 105 } 106 } catch (NullPointerException ex) { 107 // NVs are not available yet, e.g. during resync 108 return min; 109 } 110 } 111 112 /** 113 * Get the value of an NV pair from NV_SELECT_COLUMN 114 * 115 * @param rowHi index of hi byte NV 116 * @param rowLo index of lo byte NV 117 * @param min minimum value to return 118 * @param max maximum value to return 119 * 120 * @return the NV value, or min if NVs not available yet 121 */ 122 protected int getSelectValue16(int rowHi, int rowLo, int min, int max) { 123 int hi, lo, val; 124 try { 125 hi = (int)_dataModel.getValueAt(rowHi - 1, NV_SELECT_COLUMN); 126 lo = (int)_dataModel.getValueAt(rowLo - 1, NV_SELECT_COLUMN); 127 val = hi*256 + lo; 128 if (val < min) { 129 return min; 130 } else if (val > max) { 131 return max; 132 } else { 133 return val; 134 } 135 } catch (NullPointerException ex) { 136 // NVs are not available yet, e.g. during resync 137 return min; 138 } 139 } 140 141 /** 142 * Get the value of a 4-byte (32-bit) NV from NV_SELECT_COLUMN 143 * 144 * Hardware should return a count in range 0 .. max positive integer 145 * 146 * @param rowT index of top (MSB) byte 147 * 148 * @return the NV value, or 0 if NVs not available yet 149 */ 150 protected int getSelectValue32(int rowT) { 151 int t, u, h, l; 152 try { 153 t = (int)_dataModel.getValueAt(rowT - 1, NV_SELECT_COLUMN); 154 u = (int)_dataModel.getValueAt(rowT, NV_SELECT_COLUMN); 155 h = (int)_dataModel.getValueAt(rowT + 1, NV_SELECT_COLUMN); 156 l = (int)_dataModel.getValueAt(rowT + 2, NV_SELECT_COLUMN); 157 int val = ((t*256 + u)*256 + h)*256 + l; 158 if (val < 0) { 159 return 0; 160 } else { 161 return val; 162 } 163 } catch (NullPointerException ex) { 164 // NVs are not available yet, e.g. during resync 165 return 0; 166 } 167 } 168 169// private final static Logger log = LoggerFactory.getLogger(AbstractEditNVPane.class); 170 171}