001package jmri;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Represent an EntryPoint to a Section of track.
008 * Specifies a Block within the Section, and a Path of that Block.
009 * <p>
010 * An EntryPoint can be "forward" or "reverse" type, depending on if a train
011 * entering the Section at this entry point will be travelling in the forward
012 * direction or the reverse direction.
013 * <p>
014 * An EntryPoint is referenced via lists in its parent Section, and is stored on
015 * disk when its parent section is stored.
016 * <p>
017 * This module delays initialization of Blocks until first reference after an
018 * Entry Point is loaded from a configuration file.
019 *
020 * @author Dave Duchamp Copyright (C) 2008
021 */
022public class EntryPoint {
023
024    public EntryPoint(Block b, Block pb, String fbDir) {
025        mBlock = b;
026        mFromBlock = pb;
027        mFromBlockDirection = fbDir;    // direction from Path that triggered entry point
028    }
029
030    // special constructor for delayed initialization
031    public EntryPoint(String bName, String fbName, String fbDir) {
032        needsInitialize = true;
033        blockName = bName;
034        fromBlockName = fbName;
035        mFromBlockDirection = fbDir;    // direction from Path that triggered entry point
036    }
037
038    /**
039     * Constants representing the Direction of the Entry Point.
040     */
041    public static final int UNKNOWN = 0x02;
042    public static final int FORWARD = 0x04;
043    public static final int REVERSE = 0x08;
044
045    // instance variables
046    private Block mBlock = null;
047    private Block mFromBlock = null;
048    private int mDirection = UNKNOWN;
049    private boolean mFixed = false;
050    private String mFromBlockDirection = "";
051
052    // temporary instance variables
053    private boolean needsInitialize = false;
054    private String blockName = "";
055    private String fromBlockName = "";
056
057    private void initialize() {
058        mBlock = InstanceManager.getDefault(BlockManager.class).getBySystemName(blockName);
059        if (mBlock == null) {
060            log.error("Missing block - {} - when initializing entry point", blockName);
061        }
062        mFromBlock = InstanceManager.getDefault(BlockManager.class).getBySystemName(fromBlockName);
063        if (mFromBlock == null) {
064            log.error("Missing block - {} - when initializing entry point", fromBlockName);
065        }
066        needsInitialize = false;
067    }
068
069    /**
070     * Get the block.
071     *
072     * @return the block, initialized if needed
073     */
074    public Block getBlock() {
075        if (needsInitialize) {
076            initialize();
077        }
078        return mBlock;
079    }
080
081    public String getFromBlockName() {
082        if (needsInitialize) {
083            initialize();
084        }
085        String s = mFromBlock.getDisplayName();
086        if ((mFromBlockDirection != null) && (!mFromBlockDirection.isEmpty())) {
087            s = s + " ( " + mFromBlockDirection + " )";
088        }
089        return s;
090    }
091
092    public Block getFromBlock() {
093        if (needsInitialize) {
094            initialize();
095        }
096        return mFromBlock;
097    }
098
099    public void setTypeForward() {
100        mDirection = FORWARD;
101    }
102
103    public void setTypeReverse() {
104        mDirection = REVERSE;
105    }
106
107    public void setTypeUnknown() {
108        mDirection = UNKNOWN;
109    }
110
111    public boolean isForwardType() {
112        return mDirection == FORWARD;
113    }
114
115    public boolean isReverseType() {
116        return mDirection == REVERSE;
117    }
118
119    public boolean isUnknownType() {
120        return mDirection == UNKNOWN;
121    }
122
123    public int getDirection() {
124        return mDirection;
125    }
126
127    public void setDirection(int dir) {
128        mDirection = dir;
129    }
130
131    public void setFixed(boolean f) {
132        mFixed = f;
133    }
134
135    public boolean isFixed() {
136        return mFixed;
137    }
138
139    public String getFromBlockDirection() {
140        return mFromBlockDirection;
141    }
142
143    private final static Logger log = LoggerFactory.getLogger(EntryPoint.class);
144}