001package jmri.jmrit.operations.locations.gui;
002
003import java.awt.Color;
004import java.beans.PropertyChangeEvent;
005
006import javax.swing.JTable;
007import javax.swing.SwingUtilities;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012import jmri.jmrit.operations.locations.*;
013import jmri.jmrit.operations.locations.schedules.Schedule;
014import jmri.jmrit.operations.setup.Control;
015
016/**
017 * Table Model for edit of spurs used by operations
018 *
019 * @author Daniel Boudreau Copyright (C) 2008
020 */
021public class SpurTableModel extends TrackTableModel {
022
023    public SpurTableModel() {
024        super();
025    }
026
027    public void initTable(JTable table, Location location) {
028        super.initTable(table);
029        super.initTable(table, location, Track.SPUR);
030    }
031
032    @Override
033    protected void editTrack(int row) {
034        log.debug("Edit spur");
035        if (tef != null) {
036            tef.dispose();
037        }
038        // use invokeLater so new window appears on top
039        SwingUtilities.invokeLater(() -> {
040            tef = new SpurEditFrame();
041            Track spur = _tracksList.get(row);
042            tef.initComponents(spur);
043        });
044    }
045    
046    @Override
047    public String getColumnName(int col) {
048        switch (col) {
049            case NAME_COLUMN:
050                return Bundle.getMessage("SpurName");
051            default:
052                return super.getColumnName(col);
053        }
054    }
055
056    @Override
057    protected Color getForegroundColor(int row) {
058        Track spur = _tracksList.get(row);
059        if (!spur.checkScheduleValid().equals(Schedule.SCHEDULE_OKAY)) {
060            return Color.red;
061        }
062        return super.getForegroundColor(row);
063    }
064
065    // this table listens for changes to a location and it's spurs
066    @Override
067    public void propertyChange(PropertyChangeEvent e) {
068        if (Control.SHOW_PROPERTY) {
069            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), e
070                    .getNewValue());
071        }
072        super.propertyChange(e);
073        if (e.getSource().getClass().equals(Track.class)) {
074            Track track = ((Track) e.getSource());
075            if (track.isSpur()) {
076                int row = _tracksList.indexOf(track);
077                if (Control.SHOW_PROPERTY) {
078                    log.debug("Update spur table row: {} track: {}", row, track.getName());
079                }
080                if (row >= 0) {
081                    fireTableRowsUpdated(row, row);
082                }
083            }
084        }
085    }
086
087    private final static Logger log = LoggerFactory.getLogger(SpurTableModel.class);
088}