001package jmri.jmrit.display.layoutEditor.blockRoutingTable;
002
003import java.beans.PropertyChangeListener;
004import jmri.jmrit.display.layoutEditor.LayoutBlock;
005import jmri.jmrit.display.layoutEditor.LayoutBlockManager;
006
007/**
008 * Table data model for display of Neighbouring layout blocks.
009 * <p>
010 * Any desired ordering, etc, is handled outside this class.
011 *
012 * @author Kevin Dickerson Copyright (C) 2011
013 */
014public class LayoutBlockNeighbourTableModel extends javax.swing.table.AbstractTableModel implements PropertyChangeListener {
015
016    public static final int NEIGHBOURCOL = 0;
017    static final int DIRECTIONCOL = 1;
018    static final int MUTUALCOL = 2;
019    static final int RELATCOL = 3;
020    static final int METRICCOL = 4;
021
022    static final int NUMCOL = 4 + 1;
023
024    boolean editable = false;
025
026    public LayoutBlockNeighbourTableModel(boolean editable, LayoutBlock lBlock) {
027        this.editable = editable;
028        this.lBlock = lBlock;
029        lBlock.addPropertyChangeListener(this);
030    }
031
032    @Override
033    public int getRowCount() {
034        return lBlock.getNumberOfNeighbours();
035    }
036
037    @Override
038    public int getColumnCount() {
039        return NUMCOL;
040    }
041
042    @Override
043    public String getColumnName(int col) {
044        switch (col) {
045            case NEIGHBOURCOL:
046                return Bundle.getMessage("Neighbour");
047            case DIRECTIONCOL:
048                return Bundle.getMessage("Direction");
049            case MUTUALCOL:
050                return Bundle.getMessage("Mutual");
051            case RELATCOL:
052                return Bundle.getMessage("TrafficFlow");
053            case METRICCOL:
054                return Bundle.getMessage("Metric");
055
056            default:
057                return "<UNKNOWN>";
058        }
059    }
060
061    @Override
062    public Class<?> getColumnClass(int col) {
063        if (col == METRICCOL) {
064            return Integer.class;
065        } else {
066            return String.class;
067        }
068    }
069
070    /**
071     * Editable state must be set in ctor.
072     */
073    @Override
074    public boolean isCellEditable(int row, int col) {
075        return false;
076    }
077
078    @Override
079    public void propertyChange(java.beans.PropertyChangeEvent e) {
080        if (e.getPropertyName().equals("length")) {
081            // a new NamedBean is available in the manager
082            //updateNameList();
083            //log.debug("Table changed length to "+sysNameList.size());
084            fireTableDataChanged();
085        } else if (matchPropertyName(e)) {
086            // a value changed.  Find it, to avoid complete redraw
087            int row;
088            row = (Integer) e.getNewValue();
089            // since we can add columns, the entire row is marked as updated
090            //int row = sysNameList.indexOf(name);
091            fireTableRowsUpdated(row, row);
092        }
093    }
094
095    protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
096        return (e.getPropertyName().contains("neighbourmetric") || e.getPropertyName().contains("neighbourpacketflow"));
097    }
098
099    /**
100     * Provides the empty String if attribute doesn't exist.
101     */
102    @Override
103    public Object getValueAt(int row, int col) {
104        // get roster entry for row
105        if (lBlock == null) {
106            log.debug("layout Block is null!");
107            return "Error";
108        }
109        switch (col) {
110            case NEIGHBOURCOL:
111                return lBlock.getNeighbourAtIndex(row).getDisplayName();
112            case MUTUALCOL:
113                Boolean mutual = lBlock.isNeighbourMutual(row);
114                if (mutual) {
115                    return Bundle.getMessage("ButtonYes");
116                }
117                return Bundle.getMessage("ButtonNo");
118            case DIRECTIONCOL:
119                return jmri.Path.decodeDirection(lBlock.getNeighbourDirection(row));
120            case METRICCOL:
121                return lBlock.getNeighbourMetric(row);
122            case RELATCOL:
123                return lBlock.getNeighbourPacketFlowAsString(row);
124            default:
125                return "<UNKNOWN>";
126        }
127    }
128
129    @Override
130    public void setValueAt(Object value, int row, int col) {
131    }
132
133    public int getPreferredWidth(int column) {
134        int retval = 20; // always take some width
135        retval = Math.max(retval, new javax.swing.JLabel(getColumnName(column)).getPreferredSize().width + 15);  // leave room for sorter arrow
136        for (int row = 0; row < getRowCount(); row++) {
137            if (getColumnClass(column).equals(String.class)) {
138                retval = Math.max(retval, new javax.swing.JLabel(getValueAt(row, column).toString()).getPreferredSize().width);
139            } else if (getColumnClass(column).equals(Integer.class)) {
140                retval = Math.max(retval, new javax.swing.JLabel(getValueAt(row, column).toString()).getPreferredSize().width);
141            }
142        }
143        return retval + 5;
144    }
145
146    // drop listeners
147    public void dispose() {
148    }
149
150    public jmri.Manager<LayoutBlock> getManager() {
151        return jmri.InstanceManager.getDefault(LayoutBlockManager.class);
152    }
153
154    private LayoutBlock lBlock = null;
155
156    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LayoutBlockNeighbourTableModel.class);
157}