001package jmri.jmrit.roster.swing.rostergroup;
002
003import javax.swing.JTable;
004import javax.swing.JTextField;
005import jmri.jmrit.roster.Roster;
006import jmri.jmrit.roster.RosterEntry;
007
008/**
009 * Table data model for display of Rosters entries to a specific Roster Group.
010 * <p>
011 * Any desired ordering, etc, is handled outside this class.
012 * <p>
013 * The initial implementation doesn't automatically update when roster entries
014 * change, it only allows the setting of a roster entry, to a roster group.
015 * Based Upon RosterTableModel
016 *
017 * @author Bob Jacobsen Copyright (C) 2009
018 * @author Kevin Dickerson Copyright (C) 2009
019 * @since 2.7.5
020 */
021public class RosterGroupTableModel extends javax.swing.table.AbstractTableModel {
022
023    static final int IDCOL = 0;
024    static final int ROADNUMBERCOL = 1;
025    static final int ROADNAMECOL = 2;
026    static final int MFGCOL = 3;
027    static final int OWNERCOL = 4;
028    static final int ADDTOGROUPCOL = 5;
029
030    String group = "RosterGroup:";
031
032    static final int NUMCOL = ADDTOGROUPCOL + 1;
033
034    @Override
035    public int getRowCount() {
036        return Roster.getDefault().numEntries();
037    }
038
039    @Override
040    public int getColumnCount() {
041        return NUMCOL;
042    }
043
044    @Override
045    public String getColumnName(int col) {
046        switch (col) {
047            case IDCOL:
048                return Bundle.getMessage("FieldID");
049            case ROADNUMBERCOL:
050                return Bundle.getMessage("FieldRoadNumber");
051            case ROADNAMECOL:
052                return Bundle.getMessage("FieldRoadName");
053            case MFGCOL:
054                return Bundle.getMessage("FieldManufacturer");
055            case ADDTOGROUPCOL:
056                return Bundle.getMessage("Include");
057            case OWNERCOL:
058                return Bundle.getMessage("FieldOwner");
059            default:
060                return "<UNKNOWN>"; // flags unforeseen case, NOI18N
061        }
062    }
063
064    public int getPreferredWidth(int col) {
065        switch (col) {
066            case IDCOL:
067                return new JTextField(10).getPreferredSize().width;
068            case ROADNUMBERCOL:
069                return 75;
070            case ROADNAMECOL:
071            case OWNERCOL:
072                return new JTextField(20).getPreferredSize().width;
073            case ADDTOGROUPCOL: // not actually used due to the configureTable, setColumnToHoldButton, configureButton
074                return 50;
075            case MFGCOL:
076                return new JTextField(5).getPreferredSize().width;
077            default:
078                //log.warn("Unexpected column in getPreferredWidth: "+col);
079                return new JTextField(8).getPreferredSize().width;
080        }
081    }
082
083    @Override
084    public Class<?> getColumnClass(int col) {
085        if (col == ADDTOGROUPCOL) {
086            return Boolean.class;
087        } else {
088            return String.class;
089        }
090    }
091
092    /**
093     * This implementation can't edit the values yet
094     */
095    @Override
096    public boolean isCellEditable(int row, int col) {
097        switch (col) {
098            case ADDTOGROUPCOL:
099                return true;
100            default:
101                return false;
102        }
103    }
104
105    /**
106     * Provides the empty String if attribute doesn't exist.
107     */
108    @Override
109    public Object getValueAt(int row, int col) {
110        // get roster entry for row
111        RosterEntry re = Roster.getDefault().getEntry(row);
112
113        switch (col) {
114            case IDCOL:
115                return re.getId();
116            case ROADNUMBERCOL:
117                return re.getRoadNumber();
118            case ROADNAMECOL:
119                return re.getRoadName();
120            case MFGCOL:
121                return re.getMfg();
122            case OWNERCOL:
123                return re.getOwner();
124            case ADDTOGROUPCOL:
125                if (group == null) {
126                    return false;
127                } else {
128                    if (re.getAttribute(group) != null) {
129                        return true;
130                    } else {
131                        return false;
132                    }
133                }
134            default:
135                return "<UNKNOWN>";
136        }
137    }
138
139    public void configureTable(JTable table) {
140        // allow reordering of the columns
141        table.getTableHeader().setReorderingAllowed(true);
142
143        // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541)
144        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
145
146        // resize columns as requested
147        for (int i = 0; i < table.getColumnCount(); i++) {
148            int width = getPreferredWidth(i);
149            table.getColumnModel().getColumn(i).setPreferredWidth(width);
150        }
151        table.sizeColumnsToFit(-1);
152        //setUpRosterIdCol(table.getColumnModel().getColumn(3));
153        //configAddToRosterColumn(table); Remarked out until the code for the add to roster has been completed
154
155    }
156
157    synchronized public void dispose() {
158        //This needs to be sorted later.
159        //getManager().removePropertyChangeListener(this);
160    }
161
162    @Override
163    public void setValueAt(Object value, int row, int col) {
164        RosterEntry re = Roster.getDefault().getEntry(row);
165        if ((col == ADDTOGROUPCOL) && (!group.equals("RosterGroup:"))) {
166            if (value.toString().equals("true")) {
167                re.putAttribute(group, "yes");
168            } else {
169                re.deleteAttribute(group);
170            }
171            re.updateFile();
172            Roster.getDefault().writeRoster();
173
174        }
175        //re.updateFile();
176        //Roster.getDefault().writeRosterFile();
177    }
178
179    public void setGroup(String grp) {
180        group = grp;
181    }
182
183    public void getGroupEnabled(RosterEntry re) {
184
185    }
186
187    // private final static Logger log = LoggerFactory.getLogger(RosterGroupTableModel.class);
188}