Search in sources :

Example 1 with LayoutTurnout

use of jmri.jmrit.display.layoutEditor.LayoutTurnout in project JMRI by JMRI.

the class AutoTurnouts method turnoutUtil.

/**
     * Internal method implementing the above two methods Returns 'true' if
     * turnouts are set correctly, 'false' otherwise If 'set' is 'true' this
     * routine will attempt to set the turnouts, if 'false' it reports what it
     * finds.
     */
private boolean turnoutUtil(Section s, int seqNum, Section nextSection, ActiveTrain at, LayoutEditor le, boolean trustKnownTurnouts, boolean set, Section prevSection) {
    // validate input and initialize
    Transit tran = at.getTransit();
    if ((s == null) || (seqNum > tran.getMaxSequence()) || (!tran.containsSection(s)) || (le == null)) {
        log.error("Invalid argument when checking or setting turnouts in Section.");
        return false;
    }
    int direction = at.getAllocationDirectionFromSectionAndSeq(s, seqNum);
    if (direction == 0) {
        log.error("Invalid Section/sequence arguments when checking or setting turnouts");
        return false;
    }
    // check for no turnouts in this section
    if (_dispatcher.getSignalType() == DispatcherFrame.SIGNALHEAD && (s.getForwardEntryPointList().size() <= 1) && (s.getReverseEntryPointList().size() <= 1)) {
        log.debug("No entry points lists");
        // no possibility of turnouts
        return true;
    }
    // initialize connectivity utilities and beginning block pointers
    ConnectivityUtil ct = le.getConnectivityUtil();
    EntryPoint entryPt = null;
    if (prevSection != null) {
        entryPt = s.getEntryPointFromSection(prevSection, direction);
    } else if (!s.containsBlock(at.getStartBlock())) {
        entryPt = s.getEntryPointFromBlock(at.getStartBlock(), direction);
    }
    EntryPoint exitPt = null;
    if (nextSection != null) {
        exitPt = s.getExitPointToSection(nextSection, direction);
    }
    // must be in the section
    Block curBlock = null;
    // must start outside the section or be null
    Block prevBlock = null;
    // sequence number of curBlock in Section
    int curBlockSeqNum = -1;
    if (entryPt != null) {
        curBlock = entryPt.getBlock();
        prevBlock = entryPt.getFromBlock();
        curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
    } else if (!at.isAllocationReversed() && s.containsBlock(at.getStartBlock())) {
        curBlock = at.getStartBlock();
        curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
        //Get the previous block so that we can set the turnouts in the current block correctly.
        if (direction == Section.FORWARD) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
        } else if (direction == Section.REVERSE) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
        }
    } else if (at.isAllocationReversed() && s.containsBlock(at.getEndBlock())) {
        curBlock = at.getEndBlock();
        curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
        //Get the previous block so that we can set the turnouts in the current block correctly.
        if (direction == Section.REVERSE) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
        } else if (direction == Section.FORWARD) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
        }
    } else {
        if (_dispatcher.getSignalType() == DispatcherFrame.SIGNALMAST) {
            //This can be considered normal where SignalMast Logic is used.
            return true;
        }
        log.error("Error in turnout check/set request - initial Block and Section mismatch");
        return false;
    }
    Block nextBlock = null;
    // may be either in the section or the first block in the next section
    // sequence number of nextBlock in Section (-1 indicates outside Section)
    int nextBlockSeqNum = -1;
    if (exitPt != null && curBlock == exitPt.getBlock()) {
        // next Block is outside of the Section
        nextBlock = exitPt.getFromBlock();
    } else {
        // next Block is inside the Section
        if (direction == Section.FORWARD) {
            nextBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
            nextBlockSeqNum = curBlockSeqNum + 1;
        } else if (direction == Section.REVERSE) {
            nextBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
            nextBlockSeqNum = curBlockSeqNum - 1;
        }
        if ((nextBlock == null) && (curBlock != at.getEndBlock())) {
            log.error("Error in block sequence numbers when setting/checking turnouts");
            return false;
        }
    }
    ArrayList<LayoutTurnout> turnoutList = new ArrayList<LayoutTurnout>();
    ArrayList<Integer> settingsList = new ArrayList<Integer>();
    // get turnouts by Block
    boolean turnoutsOK = true;
    while (curBlock != null) {
        /*No point in getting the list if the previous block is null as it will return empty and generate an error, 
             this will only happen on the first run.  Plus working on the basis that the turnouts in the current block would have already of 
             been set correctly for the train to have arrived in the first place.
             */
        if (prevBlock != null) {
            turnoutList = ct.getTurnoutList(curBlock, prevBlock, nextBlock);
            settingsList = ct.getTurnoutSettingList();
        }
        // loop over turnouts checking and optionally setting turnouts
        for (int i = 0; i < turnoutList.size(); i++) {
            Turnout to = turnoutList.get(i).getTurnout();
            int setting = settingsList.get(i).intValue();
            if (turnoutList.get(i) instanceof LayoutSlip) {
                setting = ((LayoutSlip) turnoutList.get(i)).getTurnoutState(settingsList.get(i));
            }
            // check or ignore current setting based on flag, set in Options
            if (!trustKnownTurnouts) {
                log.debug("{}: setting turnout {} to {}", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
                to.setCommandedState(setting);
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {
                }
            //TODO: move this to separate thread
            } else {
                if (to.getKnownState() != setting) {
                    // turnout is not set correctly
                    if (set) {
                        // setting has been requested, is Section free and Block unoccupied
                        if ((s.getState() == Section.FREE) && (curBlock.getState() != Block.OCCUPIED)) {
                            // send setting command
                            log.debug("{}: turnout {} commanded to {}", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
                            to.setCommandedState(setting);
                            try {
                                Thread.sleep(100);
                            } catch (Exception ex) {
                            }
                        //TODO: move this to separate thread
                        } else {
                            turnoutsOK = false;
                        }
                    } else {
                        turnoutsOK = false;
                    }
                } else {
                    log.debug("{}: turnout {} already {}, skipping", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
                }
            }
            if (turnoutList.get(i) instanceof LayoutSlip) {
                //Look at the state of the second turnout in the slip
                setting = ((LayoutSlip) turnoutList.get(i)).getTurnoutBState(settingsList.get(i));
                to = ((LayoutSlip) turnoutList.get(i)).getTurnoutB();
                if (!trustKnownTurnouts) {
                    to.setCommandedState(setting);
                } else if (to.getKnownState() != setting) {
                    // turnout is not set correctly
                    if (set) {
                        // setting has been requested, is Section free and Block unoccupied
                        if ((s.getState() == Section.FREE) && (curBlock.getState() != Block.OCCUPIED)) {
                            // send setting command
                            to.setCommandedState(setting);
                        } else {
                            turnoutsOK = false;
                        }
                    } else {
                        turnoutsOK = false;
                    }
                }
            }
        }
        if (turnoutsOK) {
            // move to next Block if any
            if (nextBlockSeqNum >= 0) {
                prevBlock = curBlock;
                curBlock = nextBlock;
                curBlockSeqNum = nextBlockSeqNum;
                if ((exitPt != null) && (curBlock == exitPt.getBlock())) {
                    // next block is outside of the Section
                    nextBlock = exitPt.getFromBlock();
                    nextBlockSeqNum = -1;
                } else {
                    if (direction == Section.FORWARD) {
                        nextBlockSeqNum++;
                    } else {
                        nextBlockSeqNum--;
                    }
                    nextBlock = s.getBlockBySequenceNumber(nextBlockSeqNum);
                    if (nextBlock == null) {
                        // there is no next Block
                        nextBlockSeqNum = -1;
                    }
                }
            } else {
                curBlock = null;
            }
        } else {
            curBlock = null;
        }
    }
    return turnoutsOK;
}
Also used : LayoutSlip(jmri.jmrit.display.layoutEditor.LayoutSlip) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) ArrayList(java.util.ArrayList) EntryPoint(jmri.EntryPoint) Transit(jmri.Transit) EntryPoint(jmri.EntryPoint) Block(jmri.Block) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) Turnout(jmri.Turnout) ConnectivityUtil(jmri.jmrit.display.layoutEditor.ConnectivityUtil)

Example 2 with LayoutTurnout

use of jmri.jmrit.display.layoutEditor.LayoutTurnout in project JMRI by JMRI.

the class Section method getLayoutTurnoutFromTurnoutName.

private LayoutTurnout getLayoutTurnoutFromTurnoutName(String turnoutName, LayoutEditor panel) {
    Turnout t = InstanceManager.turnoutManagerInstance().getTurnout(turnoutName);
    if (t == null) {
        return null;
    }
    LayoutTurnout lt = null;
    for (int i = 0; i < panel.turnoutList.size(); i++) {
        lt = panel.turnoutList.get(i);
        if (lt.getTurnout() == t) {
            return lt;
        }
    }
    return null;
}
Also used : LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) PositionablePoint(jmri.jmrit.display.layoutEditor.PositionablePoint)

Example 3 with LayoutTurnout

use of jmri.jmrit.display.layoutEditor.LayoutTurnout in project JMRI by JMRI.

the class LayoutTurnoutXml method load.

/**
     * Load, starting with the levelxing element, then all the other data
     *
     * @param element Top level Element to unpack.
     * @param o       LayoutEditor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    LayoutEditor p = (LayoutEditor) o;
    // get center point
    String name = element.getAttribute("ident").getValue();
    double x = 0.0;
    double y = 0.0;
    int tType = LayoutTurnout.RH_TURNOUT;
    try {
        x = element.getAttribute("xcen").getFloatValue();
        y = element.getAttribute("ycen").getFloatValue();
        tType = element.getAttribute("type").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert layoutturnout attribute");
    }
    int version = 1;
    try {
        version = element.getAttribute("ver").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert layoutturnout b coords attribute");
    } catch (java.lang.NullPointerException e) {
    //can be ignored as panel file may not support method
    }
    // create the new LayoutTurnout
    LayoutTurnout l = new LayoutTurnout(name, tType, new Point2D.Double(x, y), 0.0, 1.0, 1.0, p, version);
    // get remaining attributes
    Attribute a = element.getAttribute("blockname");
    if (a != null) {
        l.tBlockName = a.getValue();
    }
    a = element.getAttribute("blockbname");
    if (a != null) {
        l.tBlockBName = a.getValue();
    }
    a = element.getAttribute("blockcname");
    if (a != null) {
        l.tBlockCName = a.getValue();
    }
    a = element.getAttribute("blockdname");
    if (a != null) {
        l.tBlockDName = a.getValue();
    }
    a = element.getAttribute("turnoutname");
    if (a != null) {
        l.tTurnoutName = a.getValue();
    }
    a = element.getAttribute("secondturnoutname");
    if (a != null) {
        l.tSecondTurnoutName = a.getValue();
    }
    a = element.getAttribute("connectaname");
    if (a != null) {
        l.connectAName = a.getValue();
    }
    a = element.getAttribute("connectbname");
    if (a != null) {
        l.connectBName = a.getValue();
    }
    a = element.getAttribute("connectcname");
    if (a != null) {
        l.connectCName = a.getValue();
    }
    a = element.getAttribute("connectdname");
    if (a != null) {
        l.connectDName = a.getValue();
    }
    a = element.getAttribute("signala1name");
    if (a != null) {
        l.setSignalA1Name(a.getValue());
    }
    a = element.getAttribute("signala2name");
    if (a != null) {
        l.setSignalA2Name(a.getValue());
    }
    a = element.getAttribute("signala3name");
    if (a != null) {
        l.setSignalA3Name(a.getValue());
    }
    a = element.getAttribute("signalb1name");
    if (a != null) {
        l.setSignalB1Name(a.getValue());
    }
    a = element.getAttribute("signalb2name");
    if (a != null) {
        l.setSignalB2Name(a.getValue());
    }
    a = element.getAttribute("signalc1name");
    if (a != null) {
        l.setSignalC1Name(a.getValue());
    }
    a = element.getAttribute("signalc2name");
    if (a != null) {
        l.setSignalC2Name(a.getValue());
    }
    a = element.getAttribute("signald1name");
    if (a != null) {
        l.setSignalD1Name(a.getValue());
    }
    a = element.getAttribute("signald2name");
    if (a != null) {
        l.setSignalD2Name(a.getValue());
    }
    a = element.getAttribute("linkedturnoutname");
    if (a != null) {
        l.linkedTurnoutName = a.getValue();
        try {
            l.linkType = element.getAttribute("linktype").getIntValue();
        } catch (org.jdom2.DataConversionException e) {
            log.error("failed to convert linked layout turnout type");
        }
    }
    a = element.getAttribute("continuing");
    if (a != null) {
        int continuing = Turnout.CLOSED;
        try {
            continuing = element.getAttribute("continuing").getIntValue();
        } catch (org.jdom2.DataConversionException e) {
            log.error("failed to convert continuingsense attribute");
        }
        l.setContinuingSense(continuing);
    }
    boolean value = false;
    if ((a = element.getAttribute("disabled")) != null && a.getValue().equals("yes")) {
        value = true;
    }
    l.setDisabled(value);
    value = false;
    if ((a = element.getAttribute("disableWhenOccupied")) != null && a.getValue().equals("yes")) {
        value = true;
    }
    l.setDisableWhenOccupied(value);
    boolean hide = false;
    if (element.getAttribute("hidden") != null) {
        if (element.getAttribute("hidden").getValue().equals("yes")) {
            hide = true;
        }
    }
    l.setHidden(hide);
    if (version == 2) {
        try {
            x = element.getAttribute("xa").getFloatValue();
            y = element.getAttribute("ya").getFloatValue();
            l.setCoordsA(new Point2D.Double(x, y));
        } catch (org.jdom2.DataConversionException e) {
            log.error("failed to convert layoutturnout b coords attribute");
        } catch (java.lang.NullPointerException e) {
        //can be ignored as panel file may not support method
        }
    }
    try {
        x = element.getAttribute("xb").getFloatValue();
        y = element.getAttribute("yb").getFloatValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert layoutturnout b coords attribute");
    }
    l.setCoordsB(new Point2D.Double(x, y));
    try {
        x = element.getAttribute("xc").getFloatValue();
        y = element.getAttribute("yc").getFloatValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert layoutturnout c coords attribute");
    }
    l.setCoordsC(new Point2D.Double(x, y));
    if (version == 2) {
        try {
            x = element.getAttribute("xd").getFloatValue();
            y = element.getAttribute("yd").getFloatValue();
            l.setCoordsD(new Point2D.Double(x, y));
        } catch (org.jdom2.DataConversionException e) {
            log.error("failed to convert layoutturnout c coords attribute");
        } catch (java.lang.NullPointerException e) {
        //can be ignored as panel file may not support method
        }
    }
    if (element.getChild("signalAMast") != null) {
        String mast = element.getChild("signalAMast").getText();
        if (mast != null && !mast.equals("")) {
            l.setSignalAMast(mast);
        }
    }
    if (element.getChild("signalBMast") != null) {
        String mast = element.getChild("signalBMast").getText();
        if (mast != null && !mast.equals("")) {
            l.setSignalBMast(mast);
        }
    }
    if (element.getChild("signalCMast") != null) {
        String mast = element.getChild("signalCMast").getText();
        if (mast != null && !mast.equals("")) {
            l.setSignalCMast(mast);
        }
    }
    if (element.getChild("signalDMast") != null) {
        String mast = element.getChild("signalDMast").getText();
        if (mast != null && !mast.equals("")) {
            l.setSignalDMast(mast);
        }
    }
    if (element.getChild("sensorA") != null) {
        String sensor = element.getChild("sensorA").getText();
        if (sensor != null && !sensor.equals("")) {
            l.setSensorA(sensor);
        }
    }
    if (element.getChild("sensorB") != null) {
        String sensor = element.getChild("sensorB").getText();
        if (sensor != null && !sensor.equals("")) {
            l.setSensorB(sensor);
        }
    }
    if (element.getChild("sensorC") != null) {
        String sensor = element.getChild("sensorC").getText();
        if (sensor != null && !sensor.equals("")) {
            l.setSensorC(sensor);
        }
    }
    if (element.getChild("sensorD") != null) {
        String sensor = element.getChild("sensorD").getText();
        if (sensor != null && !sensor.equals("")) {
            l.setSensorD(sensor);
        }
    }
    p.turnoutList.add(l);
}
Also used : LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Attribute(org.jdom2.Attribute) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) Point2D(java.awt.geom.Point2D)

Example 4 with LayoutTurnout

use of jmri.jmrit.display.layoutEditor.LayoutTurnout in project JMRI by JMRI.

the class DestinationPoints method setRoute.

//For a clear down we need to add a message, if it is a cancel, manual clear down or I didn't mean it.
void setRoute(boolean state) {
    if (log.isDebugEnabled()) {
        log.debug("Set route " + src.getPoint().getDisplayName());
    }
    if (disposed) {
        log.error("Set route called even though interlock has been disposed of");
        return;
    }
    if (routeDetails == null) {
        log.error("No route to set or clear down");
        setActiveEntryExit(false);
        setRouteTo(false);
        setRouteFrom(false);
        if ((getSignal() instanceof SignalMast) && (getEntryExitType() != EntryExitPairs.FULLINTERLOCK)) {
            SignalMast mast = (SignalMast) getSignal();
            mast.setHeld(false);
        }
        synchronized (this) {
            destination = null;
        }
        return;
    }
    if (!state) {
        switch(manager.getClearDownOption()) {
            case EntryExitPairs.PROMPTUSER:
                cancelClearOptionBox();
                break;
            case EntryExitPairs.AUTOCANCEL:
                cancelClearInterlock(EntryExitPairs.CANCELROUTE);
                break;
            case EntryExitPairs.AUTOCLEAR:
                cancelClearInterlock(EntryExitPairs.CLEARROUTE);
                break;
            default:
                cancelClearOptionBox();
                break;
        }
        if (log.isDebugEnabled()) {
            log.debug("Exit " + src.getPoint().getDisplayName());
        }
        return;
    }
    if (manager.isRouteStacked(this, false)) {
        manager.cancelStackedRoute(this, false);
    }
    /* We put the setting of the route into a seperate thread and put a glass pane in front of the layout editor.
         The swing thread for flashing the icons will carry on without interuption. */
    final ArrayList<Color> realColorStd = new ArrayList<Color>();
    final ArrayList<Color> realColorXtra = new ArrayList<Color>();
    final ArrayList<LayoutBlock> routeBlocks = new ArrayList<LayoutBlock>();
    if (manager.useDifferentColorWhenSetting()) {
        for (LayoutBlock lbk : routeDetails) {
            routeBlocks.add(lbk);
            realColorXtra.add(lbk.getBlockExtraColor());
            realColorStd.add(lbk.getBlockTrackColor());
            lbk.setBlockExtraColor(manager.getSettingRouteColor());
            lbk.setBlockTrackColor(manager.getSettingRouteColor());
        }
        //Force a redraw, to reflect color change
        src.getPoint().getPanel().redrawPanel();
    }
    ActiveTrain tmpat = null;
    if (manager.getDispatcherIntegration() && jmri.InstanceManager.getNullableDefault(jmri.jmrit.dispatcher.DispatcherFrame.class) != null) {
        jmri.jmrit.dispatcher.DispatcherFrame df = jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class);
        for (ActiveTrain atl : df.getActiveTrainsList()) {
            if (atl.getEndBlock() == src.getStart().getBlock()) {
                if (atl.getLastAllocatedSection() == atl.getEndBlockSection()) {
                    if (!atl.getReverseAtEnd() && !atl.getResetWhenDone()) {
                        tmpat = atl;
                        break;
                    }
                    log.warn("Interlock will not be added to existing Active Train as it is set for back and forth operation");
                }
            }
        }
    }
    final ActiveTrain at = tmpat;
    Runnable setRouteRun = new Runnable() {

        @Override
        public void run() {
            src.getPoint().getPanel().getGlassPane().setVisible(true);
            try {
                Hashtable<Turnout, Integer> turnoutSettings = new Hashtable<Turnout, Integer>();
                ConnectivityUtil connection = new ConnectivityUtil(point.getPanel());
                // Last block in the route is the one that we are protecting at the last sensor/signalmast
                for (int i = 0; i < routeDetails.size(); i++) {
                    //if we are not using the dispatcher and the signal logic is dynamic, then set the turnouts
                    if (at == null && isSignalLogicDynamic()) {
                        if (i > 0) {
                            ArrayList<LayoutTurnout> turnoutlist;
                            int nxtBlk = i + 1;
                            int preBlk = i - 1;
                            if (i < routeDetails.size() - 1) {
                                turnoutlist = connection.getTurnoutList(routeDetails.get(i).getBlock(), routeDetails.get(preBlk).getBlock(), routeDetails.get(nxtBlk).getBlock());
                                ArrayList<Integer> throwlist = connection.getTurnoutSettingList();
                                for (int x = 0; x < turnoutlist.size(); x++) {
                                    if (turnoutlist.get(x) instanceof LayoutSlip) {
                                        int slipState = throwlist.get(x);
                                        LayoutSlip ls = (LayoutSlip) turnoutlist.get(x);
                                        int taState = ls.getTurnoutState(slipState);
                                        turnoutSettings.put(ls.getTurnout(), taState);
                                        int tbState = ls.getTurnoutBState(slipState);
                                        ls.getTurnoutB().setCommandedState(tbState);
                                        turnoutSettings.put(ls.getTurnoutB(), tbState);
                                    } else {
                                        String t = turnoutlist.get(x).getTurnoutName();
                                        Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(t);
                                        turnoutSettings.put(turnout, throwlist.get(x));
                                        if (turnoutlist.get(x).getSecondTurnout() != null) {
                                            turnoutSettings.put(turnoutlist.get(x).getSecondTurnout(), throwlist.get(x));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ((getEntryExitType() == EntryExitPairs.FULLINTERLOCK)) {
                        // was set against occupancy sensor
                        routeDetails.get(i).getBlock().addPropertyChangeListener(propertyBlockListener);
                        if (i > 0) {
                            routeDetails.get(i).setUseExtraColor(true);
                        }
                    } else {
                        // was set against occupancy sensor
                        routeDetails.get(i).getBlock().removePropertyChangeListener(propertyBlockListener);
                    }
                }
                if (at == null) {
                    if (!isSignalLogicDynamic()) {
                        jmri.SignalMastLogic tmSml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).getSignalMastLogic((SignalMast) src.sourceSignal);
                        for (Turnout t : tmSml.getAutoTurnouts((SignalMast) getSignal())) {
                            turnoutSettings.put(t, tmSml.getAutoTurnoutState(t, (SignalMast) getSignal()));
                        }
                    }
                    for (Map.Entry<Turnout, Integer> entry : turnoutSettings.entrySet()) {
                        entry.getKey().setCommandedState(entry.getValue());
                        Runnable r = new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(250 + manager.turnoutSetDelay);
                                } catch (InterruptedException ex) {
                                    Thread.currentThread().interrupt();
                                }
                            }
                        };
                        Thread thr = new Thread(r, "Entry Exit Route, turnout setting");
                        thr.start();
                        try {
                            thr.join();
                        } catch (InterruptedException ex) {
                        //            log.info("interrupted at join " + ex);
                        }
                    }
                }
                src.getPoint().getPanel().redrawPanel();
                if (getEntryExitType() != EntryExitPairs.SETUPTURNOUTSONLY) {
                    if (getEntryExitType() == EntryExitPairs.FULLINTERLOCK) {
                        //If our start block is already active we will set it as our lastSeenActiveBlock.
                        if (src.getStart().getState() == Block.OCCUPIED) {
                            src.getStart().removePropertyChangeListener(propertyBlockListener);
                            lastSeenActiveBlockObject = src.getStart().getBlock().getValue();
                            log.debug("Last seen value " + lastSeenActiveBlockObject);
                        }
                    }
                    if ((src.sourceSignal instanceof SignalMast) && (getSignal() instanceof SignalMast)) {
                        SignalMast smSource = (SignalMast) src.sourceSignal;
                        SignalMast smDest = (SignalMast) getSignal();
                        synchronized (this) {
                            sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).newSignalMastLogic(smSource);
                            if (!sml.isDestinationValid(smDest)) {
                                //if no signalmastlogic existed then created it, but set it not to be stored.
                                sml.setDestinationMast(smDest);
                                sml.setStore(jmri.SignalMastLogic.STORENONE, smDest);
                            }
                        }
                        //Remove the first block as it is our start block
                        routeDetails.remove(0);
                        synchronized (this) {
                            smSource.setHeld(false);
                            //Only change the block and turnout details if this a temp signalmast logic
                            if (sml.getStoreState(smDest) == jmri.SignalMastLogic.STORENONE) {
                                LinkedHashMap<Block, Integer> blks = new LinkedHashMap<Block, Integer>();
                                for (int i = 0; i < routeDetails.size(); i++) {
                                    if (routeDetails.get(i).getBlock().getState() == Block.UNKNOWN) {
                                        routeDetails.get(i).getBlock().setState(Block.UNOCCUPIED);
                                    }
                                    blks.put(routeDetails.get(i).getBlock(), Block.UNOCCUPIED);
                                }
                                sml.setAutoBlocks(blks, smDest);
                                sml.setAutoTurnouts(turnoutSettings, smDest);
                                sml.initialise(smDest);
                            }
                        }
                        smSource.addPropertyChangeListener(new PropertyChangeListener() {

                            @Override
                            public void propertyChange(PropertyChangeEvent e) {
                                SignalMast source = (SignalMast) e.getSource();
                                source.removePropertyChangeListener(this);
                                setRouteFrom(true);
                                setRouteTo(true);
                            }
                        });
                        src.pd.extendedtime = true;
                        point.extendedtime = true;
                    } else {
                        if (src.sourceSignal instanceof SignalMast) {
                            SignalMast mast = (SignalMast) src.sourceSignal;
                            mast.setHeld(false);
                        } else if (src.sourceSignal instanceof SignalHead) {
                            SignalHead head = (SignalHead) src.sourceSignal;
                            head.setHeld(false);
                        }
                        setRouteFrom(true);
                        setRouteTo(true);
                    }
                }
                if (manager.useDifferentColorWhenSetting()) {
                    //final ArrayList<Color> realColorXtra = realColorXtra;
                    javax.swing.Timer resetColorBack = new javax.swing.Timer(manager.getSettingTimer(), new java.awt.event.ActionListener() {

                        @Override
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                            for (int i = 0; i < routeBlocks.size(); i++) {
                                LayoutBlock lbk = routeBlocks.get(i);
                                lbk.setBlockExtraColor(realColorXtra.get(i));
                                lbk.setBlockTrackColor(realColorStd.get(i));
                            }
                            src.getPoint().getPanel().redrawPanel();
                        }
                    });
                    resetColorBack.setRepeats(false);
                    resetColorBack.start();
                }
                if (at != null) {
                    jmri.Section sec = null;
                    if (sml != null && sml.getAssociatedSection((SignalMast) getSignal()) != null) {
                        sec = sml.getAssociatedSection((SignalMast) getSignal());
                    } else {
                        sec = InstanceManager.getDefault(jmri.SectionManager.class).createNewSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
                        if (sec == null) {
                            //A Section already exists, lets grab it and check that it is one used with the Interlocking, if so carry on using that.
                            sec = InstanceManager.getDefault(jmri.SectionManager.class).getSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
                        } else {
                            sec.setSectionType(jmri.Section.DYNAMICADHOC);
                        }
                        if (sec.getSectionType() == jmri.Section.DYNAMICADHOC) {
                            sec.removeAllBlocksFromSection();
                            for (LayoutBlock key : routeDetails) {
                                if (key != src.getStart()) {
                                    sec.addBlock(key.getBlock());
                                }
                            }
                            String dir = jmri.Path.decodeDirection(src.getStart().getNeighbourDirection(routeDetails.get(0).getBlock()));
                            jmri.EntryPoint ep = new jmri.EntryPoint(routeDetails.get(0).getBlock(), src.getStart().getBlock(), dir);
                            ep.setTypeForward();
                            sec.addToForwardList(ep);
                            LayoutBlock proDestLBlock = point.getProtecting().get(0);
                            if (proDestLBlock != null) {
                                dir = jmri.Path.decodeDirection(proDestLBlock.getNeighbourDirection(point.getFacing()));
                                ep = new jmri.EntryPoint(point.getFacing().getBlock(), proDestLBlock.getBlock(), dir);
                                ep.setTypeReverse();
                                sec.addToReverseList(ep);
                            }
                        }
                    }
                    jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class).extendActiveTrainsPath(sec, at, src.getPoint().getPanel());
                }
                src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
                point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
            } catch (RuntimeException ex) {
                log.error("An error occured while setting the route");
                ex.printStackTrace();
                src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
                point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
                if (manager.useDifferentColorWhenSetting()) {
                    for (int i = 0; i < routeBlocks.size(); i++) {
                        LayoutBlock lbk = routeBlocks.get(i);
                        lbk.setBlockExtraColor(realColorXtra.get(i));
                        lbk.setBlockTrackColor(realColorStd.get(i));
                    }
                }
                src.getPoint().getPanel().redrawPanel();
            }
            src.getPoint().getPanel().getGlassPane().setVisible(false);
        //src.setMenuEnabled(true);
        }
    };
    Thread thrMain = new Thread(setRouteRun, "Entry Exit Set Route");
    thrMain.start();
    try {
        thrMain.join();
    } catch (InterruptedException e) {
        log.error("Interuption exception " + e.toString());
    }
    if (log.isDebugEnabled()) {
        log.debug("finish route " + src.getPoint().getDisplayName());
    }
}
Also used : LayoutSlip(jmri.jmrit.display.layoutEditor.LayoutSlip) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ActiveTrain(jmri.jmrit.dispatcher.ActiveTrain) PropertyChangeEvent(java.beans.PropertyChangeEvent) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) Color(java.awt.Color) Block(jmri.Block) LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) Turnout(jmri.Turnout) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConnectivityUtil(jmri.jmrit.display.layoutEditor.ConnectivityUtil) ActionListener(java.awt.event.ActionListener) PropertyChangeListener(java.beans.PropertyChangeListener) SignalHead(jmri.SignalHead) LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) SignalMast(jmri.SignalMast) Hashtable(java.util.Hashtable) ActionEvent(java.awt.event.ActionEvent)

Example 5 with LayoutTurnout

use of jmri.jmrit.display.layoutEditor.LayoutTurnout in project JMRI by JMRI.

the class PointDetails method getSignal.

NamedBean getSignal() {
    if ((getPanel() != null) && (!getPanel().isEditable()) && (getSignalMast() != null)) {
        return getSignalMast();
    }
    if ((getPanel() != null) && (!getPanel().isEditable()) && (getSignalHead() != null)) {
        return getSignalHead();
    }
    jmri.SignalHeadManager sh = InstanceManager.getDefault(jmri.SignalHeadManager.class);
    NamedBean signal = null;
    if (getRefObject() == null) {
        log.error("Signal not found at point");
        return null;
    } else if (getRefObject() instanceof SignalMast) {
        signal = getRefObject();
        setSignalMast(((SignalMast) getRefObject()));
        return signal;
    } else if (getRefObject() instanceof SignalHead) {
        signal = getRefObject();
        setSignalHead(((SignalHead) getRefObject()));
        return signal;
    }
    Sensor sen = (Sensor) getRefObject();
    log.debug("looking at Sensor " + sen.getDisplayName());
    if (getRefLocation() instanceof PositionablePoint) {
        PositionablePoint p = (PositionablePoint) getRefLocation();
        if (p.getEastBoundSensor() == sen) {
            if (p.getEastBoundSignalMast() != null) {
                signal = p.getEastBoundSignalMast();
            } else if (!p.getEastBoundSignal().equals("")) {
                signal = sh.getSignalHead(p.getEastBoundSignal());
            }
        } else if (p.getWestBoundSensor() == sen) {
            if (p.getWestBoundSignalMast() != null) {
                signal = p.getWestBoundSignalMast();
            } else if (!p.getWestBoundSignal().equals("")) {
                signal = sh.getSignalHead(p.getWestBoundSignal());
            }
        }
    } else if (getRefLocation() instanceof LayoutTurnout) {
        LayoutTurnout t = (LayoutTurnout) getRefLocation();
        if (t.getSensorA() == sen) {
            if (t.getSignalAMast() != null) {
                signal = t.getSignalAMast();
            } else if (!t.getSignalA1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalA1Name());
            }
        } else if (t.getSensorB() == sen) {
            if (t.getSignalBMast() != null) {
                signal = t.getSignalBMast();
            } else if (!t.getSignalB1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalB1Name());
            }
        } else if (t.getSensorC() == sen) {
            if (t.getSignalCMast() != null) {
                signal = t.getSignalCMast();
            } else if (!t.getSignalC1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalC1Name());
            }
        } else if (t.getSensorD() == sen) {
            if (t.getSignalDMast() != null) {
                signal = t.getSignalDMast();
            } else if (!t.getSignalD1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalD1Name());
            }
        }
    } else if (getRefLocation() instanceof LevelXing) {
        LevelXing x = (LevelXing) getRefLocation();
        if (x.getSensorA() == sen) {
            if (x.getSignalAMast() != null) {
                signal = x.getSignalAMast();
            } else if (!x.getSignalAName().equals("")) {
                signal = sh.getSignalHead(x.getSignalAName());
            }
        } else if (x.getSensorB() == sen) {
            if (x.getSignalBMast() != null) {
                signal = x.getSignalBMast();
            } else if (!x.getSignalBName().equals("")) {
                signal = sh.getSignalHead(x.getSignalBName());
            }
        } else if (x.getSensorC() == sen) {
            if (x.getSignalCMast() != null) {
                signal = x.getSignalCMast();
            } else if (!x.getSignalCName().equals("")) {
                signal = sh.getSignalHead(x.getSignalCName());
            }
        } else if (x.getSensorD() == sen) {
            if (x.getSignalDMast() != null) {
                signal = x.getSignalDMast();
            } else if (!x.getSignalDName().equals("")) {
                signal = sh.getSignalHead(x.getSignalDName());
            }
        }
    } else if (getRefLocation() instanceof LayoutSlip) {
        LayoutSlip t = (LayoutSlip) getRefLocation();
        if (t.getSensorA() == sen) {
            if (t.getSignalAMast() != null) {
                signal = t.getSignalAMast();
            } else if (!t.getSignalA1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalA1Name());
            }
        } else if (t.getSensorB() == sen) {
            if (t.getSignalBMast() != null) {
                signal = t.getSignalBMast();
            } else if (!t.getSignalB1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalB1Name());
            }
        } else if (t.getSensorC() == sen) {
            if (t.getSignalCMast() != null) {
                signal = t.getSignalCMast();
            } else if (!t.getSignalC1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalC1Name());
            }
        } else if (t.getSensorD() == sen) {
            if (t.getSignalDMast() != null) {
                signal = t.getSignalDMast();
            } else if (!t.getSignalD1Name().equals("")) {
                signal = sh.getSignalHead(t.getSignalD1Name());
            }
        }
    }
    if (signal instanceof SignalMast) {
        setSignalMast(((SignalMast) signal));
    } else if (signal instanceof SignalHead) {
        setSignalHead(((SignalHead) signal));
    }
    return signal;
}
Also used : NamedBean(jmri.NamedBean) LayoutSlip(jmri.jmrit.display.layoutEditor.LayoutSlip) LevelXing(jmri.jmrit.display.layoutEditor.LevelXing) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) SignalMast(jmri.SignalMast) SignalHead(jmri.SignalHead) PositionablePoint(jmri.jmrit.display.layoutEditor.PositionablePoint) Sensor(jmri.Sensor)

Aggregations

LayoutTurnout (jmri.jmrit.display.layoutEditor.LayoutTurnout)9 LayoutSlip (jmri.jmrit.display.layoutEditor.LayoutSlip)6 PositionablePoint (jmri.jmrit.display.layoutEditor.PositionablePoint)5 LevelXing (jmri.jmrit.display.layoutEditor.LevelXing)4 SignalHead (jmri.SignalHead)3 SignalMast (jmri.SignalMast)3 ConnectivityUtil (jmri.jmrit.display.layoutEditor.ConnectivityUtil)3 Point2D (java.awt.geom.Point2D)2 ArrayList (java.util.ArrayList)2 Block (jmri.Block)2 Sensor (jmri.Sensor)2 Turnout (jmri.Turnout)2 LayoutBlock (jmri.jmrit.display.layoutEditor.LayoutBlock)2 Color (java.awt.Color)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 Hashtable (java.util.Hashtable)1 LinkedHashMap (java.util.LinkedHashMap)1