001package jmri.jmrit.display.layoutEditor;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004
005import jmri.Block;
006import jmri.jmrit.roster.RosterEntry;
007import jmri.util.swing.JmriJOptionPane;
008
009/**
010 * An icon to display a status of a Block Object.
011 * <p>
012 * This is the same name as display.BlockContentsIcon, it follows
013 * on from the MemoryIcon
014 */
015@SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS")
016public class BlockContentsIcon extends jmri.jmrit.display.BlockContentsIcon {
017
018    //TODO: unused - dead-code strip
019    //private final String defaultText = " ";
020
021    public BlockContentsIcon(String s, LayoutEditor panel) {
022        super(s, panel);
023        this.panel = panel;
024        log.debug("BlockContentsIcon ctor= {}", BlockContentsIcon.class.getName());
025    }
026
027    private LayoutBlock lBlock = null;
028    LayoutEditor panel;
029
030    /**
031     * {@inheritDoc}
032     */
033    @Override
034    public void setBlock(jmri.NamedBeanHandle<Block> m) {
035        super.setBlock(m);
036        if (getBlock() != null) {
037            lBlock = jmri.InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(getBlock());
038        }
039    }
040
041    /**
042     * add a roster to this icon
043     * @param roster to add
044     */
045    @Override
046    protected void addRosterToIcon(RosterEntry roster) {
047        if (!jmri.InstanceManager.getDefault(LayoutBlockManager.class).isAdvancedRoutingEnabled() || lBlock == null) {
048            super.addRosterToIcon(roster);
049            return;
050        }
051
052        int paths = lBlock.getNumberOfThroughPaths();
053        jmri.Block srcBlock = null;
054        jmri.Block desBlock = null;
055        for (int i = 0; i < paths; i++) {
056            if (lBlock.isThroughPathActive(i)) {
057                srcBlock = lBlock.getThroughPathSource(i);
058                desBlock = lBlock.getThroughPathDestination(i);
059                break;
060            }
061        }
062        int dirA;
063        int dirB;
064        if (srcBlock != null && desBlock != null) {
065            dirA = lBlock.getNeighbourDirection(srcBlock);
066            dirB = lBlock.getNeighbourDirection(desBlock);
067        } else {
068            dirA = jmri.Path.EAST;
069            dirB = jmri.Path.WEST;
070        }
071
072        Object[] options = {"Facing " + jmri.Path.decodeDirection(dirB),
073            "Facing " + jmri.Path.decodeDirection(dirA),
074            "Do Not Add"};
075        int n = JmriJOptionPane.showOptionDialog(this,
076                "Would you like to assign loco "
077                + roster.titleString() + " to this location",
078                "Assign Loco",
079                JmriJOptionPane.DEFAULT_OPTION,
080                JmriJOptionPane.QUESTION_MESSAGE,
081                null,
082                options,
083                options[2]);
084        switch (n) {
085            case 2: // array position 2 do not add, or Dialog closed
086            case JmriJOptionPane.CLOSED_OPTION:
087                return;
088            case 0: // array position 0, Facing DirB
089                flipRosterIcon = true;
090                getBlock().setDirection(dirB);
091                break;
092            default: // array position 1, Facing DirA
093                flipRosterIcon = false;
094                getBlock().setDirection(dirA);
095                break;
096        }
097        if (getBlock().getValue() == roster) {
098            //No change in the loco but a change in direction facing might have occurred
099            updateIconFromRosterVal(roster);
100        } else {
101            setValue(roster);
102        }
103    }
104
105    // force a redisplay when content changes
106    @Override
107    public void propertyChange(java.beans.PropertyChangeEvent e) {
108        super.propertyChange(e);
109        panel.redrawPanel();
110    }
111
112    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BlockContentsIcon.class);
113
114}