001package jmri.jmrix.ieee802154.xbee.swing.nodeconfig; 002 003import javax.swing.table.AbstractTableModel; 004import javax.swing.table.TableColumn; 005import javax.swing.table.TableColumnModel; 006import jmri.jmrix.ieee802154.xbee.XBeeNode; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010/** 011 * Table model for the pin assignment table. 012 * 013 * @author Paul Bender Copyright (C) 2013,2016 014 */ 015public class AssignmentTableModel extends AbstractTableModel { 016 017 private XBeeNode curNode = null; 018 019 public static final int BIT_COLUMN = 0; 020 public static final int SYSNAME_COLUMN = 1; 021 public static final int USERNAME_COLUMN = 2; 022 023 private final String[] assignmentTableColumnNames = {Bundle.getMessage("HeadingBit"), 024 Bundle.getMessage("HeadingSystemName"), 025 Bundle.getMessage("HeadingUserName")}; 026 027 private final String free = Bundle.getMessage("AssignmentFree"); 028 029 public void setNode(XBeeNode node) { 030 curNode = node; 031 fireTableDataChanged(); 032 } 033 034 public void initTable(javax.swing.JTable assignmentTable) { 035 TableColumnModel assignmentColumnModel = assignmentTable.getColumnModel(); 036 TableColumn bitColumn = assignmentColumnModel.getColumn(BIT_COLUMN); 037 bitColumn.setMinWidth(20); 038 bitColumn.setMaxWidth(40); 039 bitColumn.setResizable(true); 040 TableColumn sysColumn = assignmentColumnModel.getColumn(SYSNAME_COLUMN); 041 sysColumn.setMinWidth(75); 042 sysColumn.setMaxWidth(100); 043 sysColumn.setResizable(true); 044 TableColumn userColumn = assignmentColumnModel.getColumn(USERNAME_COLUMN); 045 userColumn.setMinWidth(90); 046 userColumn.setMaxWidth(450); 047 userColumn.setResizable(true); 048 } 049 050 @Override 051 public String getColumnName(int c) { 052 return assignmentTableColumnNames[c]; 053 } 054 055 @Override 056 public Class<?> getColumnClass(int c) { 057 if (c == BIT_COLUMN) { 058 return Integer.class; 059 } else { 060 return String.class; 061 } 062 } 063 064 @Override 065 public boolean isCellEditable(int r, int c) { 066 return false; 067 } 068 069 @Override 070 public int getColumnCount() { 071 return 3; 072 } 073 074 @Override 075 public int getRowCount() { 076 return 8; 077 } 078 079 @Override 080 public Object getValueAt(int r, int c) { 081 int pin = r; 082 try { 083 switch (c) { 084 case BIT_COLUMN: 085 return pin; 086 case SYSNAME_COLUMN: 087 if(curNode==null) { 088 return free; 089 } 090 if (curNode.getPinAssigned(pin)) { 091 return curNode.getPinBean(pin).getSystemName(); 092 } else { 093 return free; 094 } 095 case USERNAME_COLUMN: 096 if(curNode==null) { 097 return ""; 098 } 099 if (curNode.getPinAssigned(pin)) { 100 return curNode.getPinBean(pin).getUserName(); 101 } else { 102 return ""; 103 } 104 default: 105 return ""; 106 } 107 } catch (java.lang.NullPointerException npe) { 108 log.debug("Caught NPE getting pin assignment for pin {}", pin); 109 return ""; 110 } 111 } 112 113 @Override 114 public void setValueAt(Object type, int r, int c) { 115 // nothing is stored here 116 } 117 118 private final static Logger log = LoggerFactory.getLogger(AssignmentTableModel.class); 119 120}