001package jmri.jmrit.beantable; 002 003import java.text.DateFormat; 004import java.util.Date; 005 006import jmri.*; 007 008import javax.annotation.Nonnull; 009import javax.swing.*; 010 011/** 012 * TableDataModel for the RailCom Table. 013 * 014 * Split from {@link RailComTableAction} 015 * 016 * @author Bob Jacobsen Copyright (C) 2003 017 * @author Matthew Harris Copyright (C) 2011 018 * @since 2.11.4 019 * @author Steve Young Copyright (C) 2021 020 */ 021public class RailComTableDataModel extends BeanTableDataModel<IdTag> { 022 023 /** 024 * Create a new Memory Table Data Model. 025 * @param mgr Memory manager to use in the model, default MemoryManager always used. 026 */ 027 public RailComTableDataModel(Manager<IdTag> mgr){ 028 super(); 029 setManager(mgr); 030 } 031 032 @SuppressWarnings("hiding") // Field has same name as a field in the super class 033 static public final int VALUECOL = 0; 034 public static final int WHERECOL = VALUECOL + 1; 035 public static final int WHENCOL = WHERECOL + 1; 036 public static final int CLEARCOL = WHENCOL + 1; 037 public static final int SPEEDCOL = CLEARCOL + 1; 038 public static final int DCCADDRESSCOL = SPEEDCOL + 1; 039 public static final int ORIENTATIONCOL = DCCADDRESSCOL + 1; 040 public static final int DIRECTIONCOL = ORIENTATIONCOL + 1; 041 public static final int MOTIONCOL = DIRECTIONCOL + 1; 042 public static final int QOSCOL = MOTIONCOL + 1; 043 public static final int LOADCOL = QOSCOL + 1; 044 public static final int TEMPCOL = LOADCOL + 1; 045 public static final int FUELCOL = TEMPCOL + 1; 046 public static final int WATERCOL = FUELCOL + 1; 047 public static final int LOCATIONCOL = WATERCOL + 1; 048 public static final int ROUTINGCOL = LOCATIONCOL + 1; 049 @SuppressWarnings("hiding") // Field has same name as a field in the super class 050 static public final int DELETECOL = ROUTINGCOL + 1; 051 052 @SuppressWarnings("hiding") // Field has same name as a field in the super class 053 static public final int NUMCOLUMN = DELETECOL + 1; 054 055 @Override 056 public String getValue(String name) { 057 RailCom tag = (RailCom) getManager().getBySystemName(name); 058 if (tag == null) { 059 return "?"; 060 } 061 return tag.getTagID(); 062 } 063 064 private RailComManager manager; 065 066 @Override 067 public final void setManager(Manager<IdTag> mgr){ 068 if (mgr instanceof RailComManager){ 069 manager = (RailComManager)mgr; 070 } 071 } 072 073 @Override 074 public RailComManager getManager() { 075 return ( manager!=null ? manager : InstanceManager.getDefault(RailComManager.class)); 076 } 077 078 @Override 079 public RailCom getBySystemName(@Nonnull String name) { 080 return (RailCom) getManager().getBySystemName(name); 081 } 082 083 @Override 084 public RailCom getByUserName(@Nonnull String name) { 085 return (RailCom) getManager().getByUserName(name); 086 } 087 088 @Override 089 public void clickOn(IdTag t) { 090 // don't do anything on click; not used in this class, because 091 // we override setValueAt 092 } 093 094 @Override 095 public void setValueAt(Object value, int row, int col) { 096 if (col == CLEARCOL) { 097 RailCom t = getBySystemName(sysNameList.get(row)); 098 if (log.isDebugEnabled()) { 099 log.debug("Clear where & when last seen for {}", t.getSystemName()); 100 } 101 t.setWhereLastSeen(null); 102 fireTableRowsUpdated(row, row); 103 } else if (col == DELETECOL) { 104 // button fired, delete Bean 105 deleteBean(row, col); 106 } 107 } 108 109 @Override 110 public int getColumnCount() { 111 return NUMCOLUMN; 112 } 113 114 @Override 115 public String getColumnName(int col) { 116 switch (col) { 117 case VALUECOL: 118 return Bundle.getMessage("ColumnAddress"); 119 case WHERECOL: 120 return Bundle.getMessage("ColumnIdWhere"); 121 case WHENCOL: 122 return Bundle.getMessage("ColumnIdWhen"); 123 case SPEEDCOL: 124 return Bundle.getMessage("ColumnSpeed"); 125 case DCCADDRESSCOL: 126 return Bundle.getMessage("ColumnDccAddress"); 127 case ORIENTATIONCOL: 128 return Bundle.getMessage("ColumnOrientation"); 129 case DIRECTIONCOL: 130 return Bundle.getMessage("ColumnDirection"); 131 case MOTIONCOL: 132 return Bundle.getMessage("ColumnMotion"); 133 case QOSCOL: 134 return Bundle.getMessage("ColumnQoS"); 135 case LOADCOL: 136 return Bundle.getMessage("ColumnLoad"); 137 case TEMPCOL: 138 return Bundle.getMessage("ColumnTemp"); 139 case FUELCOL: 140 return Bundle.getMessage("ColumnFuelLevel"); 141 case WATERCOL: 142 return Bundle.getMessage("ColumnWaterLevel"); 143 case LOCATIONCOL: 144 return Bundle.getMessage("ColumnLocation"); 145 case ROUTINGCOL: 146 return Bundle.getMessage("ColumnRouting"); 147 case DELETECOL: 148 return ""; 149 case CLEARCOL: 150 return ""; 151 default: 152 return super.getColumnName(col); 153 } 154 } 155 156 @Override 157 public Class<?> getColumnClass(int col) { 158 switch (col) { 159 case DELETECOL: 160 case CLEARCOL: 161 return JButton.class; 162 default: 163 return String.class; 164 } 165 } 166 167 @Override 168 public boolean isCellEditable(int row, int col) { 169 switch (col) { 170 case DELETECOL: 171 case CLEARCOL: 172 return true; 173 default: 174 return false; 175 } 176 } 177 178 @Override 179 public Object getValueAt(int row, int col) { 180 RailCom t = getBySystemName(sysNameList.get(row)); 181 if (t == null) { 182 return null; 183 } 184 switch (col) { 185 case VALUECOL: 186 return t.getLocoAddress().toString(); 187 case WHERECOL: 188 Reporter r = t.getWhereLastSeen(); 189 return (r != null ? r.getSystemName() : null); 190 case WHENCOL: 191 Date d; 192 return (((d = t.getWhenLastSeen()) != null) 193 ? DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(d) : null); 194 case CLEARCOL: 195 return Bundle.getMessage("ButtonClear"); 196 case SPEEDCOL: 197 return (t.getActualSpeed()!=-1 ? t.getActualSpeed() : null); 198 case DCCADDRESSCOL: 199 return t.getDccAddress().toString()+(t.getDccAddress().isConsistAddress() ? " Consist ": ""); 200 case ORIENTATIONCOL: 201 return t.getOrientation().toString(); 202 case DIRECTIONCOL: 203 return t.getDirection().toString(); 204 case MOTIONCOL: 205 return t.getMotion().toString(); 206 case QOSCOL: 207 return t.getQoS().toString(); 208 case LOADCOL: 209 return (t.getActualLoad()!=-1 ? t.getActualLoad() : null); 210 case TEMPCOL: 211 return (t.getActualTemperature()!=-1 ? t.getActualTemperature() : null); 212 case FUELCOL: 213 return (t.getFuelLevel()!=-1 ? t.getFuelLevel() : null); 214 case WATERCOL: 215 return (t.getWaterLevel()!=-1 ? t.getWaterLevel() : null); 216 case LOCATIONCOL: 217 return (t.getLocation()!=-1 ? t.getLocation() : null); 218 case ROUTINGCOL: 219 return (t.getRoutingNo()!=-1 ? t.getRoutingNo() : null); 220 case DELETECOL: // 221 return Bundle.getMessage("ButtonDelete"); 222 default: 223 return super.getValueAt(row, col); 224 } 225 } 226 227 @Override 228 public int getPreferredWidth(int col) { 229 switch (col) { 230 case WHERECOL: 231 case WHENCOL: 232 return new JTextField(12).getPreferredSize().width; 233 case VALUECOL: 234 return new JTextField(10).getPreferredSize().width; 235 case CLEARCOL: 236 case DELETECOL: 237 return new JButton(Bundle.getMessage("ButtonClear")).getPreferredSize().width + 4; 238 default: 239 return new JTextField(5).getPreferredSize().width; 240 } 241 } 242 243 @Override 244 public void configValueColumn(JTable table) { 245 // value column isn't button, so config is null 246 } 247 248 @Override 249 protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) { 250 return true; 251 // return (e.getPropertyName().indexOf("alue")>=0); 252 } 253 254 @Override 255 public JButton configureButton() { 256 log.error("configureButton should not have been called"); 257 return null; 258 } 259 260 @Override 261 protected String getMasterClassName() { 262 return RailComTableAction.class.getName(); 263 } 264 265 @Override 266 protected String getBeanType() { 267 return "ID Tag"; 268 } 269 270 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RailComTableDataModel.class); 271 272}