Search in sources :

Example 1 with Transit

use of jmri.Transit 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 Transit

use of jmri.Transit in project JMRI by JMRI.

the class ActivateTrainFrame method initializeFreeTransitsCombo.

private void initializeFreeTransitsCombo(ArrayList<Transit> transitList) {
    ArrayList<String> allTransits = (ArrayList<String>) _TransitManager.getSystemNameList();
    transitSelectBox.removeAllItems();
    transitBoxList.clear();
    if (transitList.isEmpty()) {
        for (int i = 0; i < allTransits.size(); i++) {
            String tName = allTransits.get(i);
            Transit t = _TransitManager.getBySystemName(tName);
            transitList.add(t);
        }
    }
    for (Transit t : transitList) {
        boolean free = true;
        for (int j = 0; j < _ActiveTrainsList.size(); j++) {
            ActiveTrain at = _ActiveTrainsList.get(j);
            if (t == at.getTransit()) {
                free = false;
            }
        }
        if (free) {
            String tName = t.getSystemName();
            transitBoxList.add(t);
            String uname = t.getUserName();
            if ((uname != null) && (!uname.equals("")) && (!uname.equals(tName))) {
                tName = tName + "(" + uname + ")";
            }
            transitSelectBox.addItem(tName);
        }
    }
    if (transitBoxList.size() > 0) {
        transitSelectBox.setSelectedIndex(0);
        selectedTransit = transitBoxList.get(0);
    } else {
        selectedTransit = null;
    }
}
Also used : ArrayList(java.util.ArrayList) Transit(jmri.Transit)

Example 3 with Transit

use of jmri.Transit in project JMRI by JMRI.

the class ActivateTrainFrame method handleTransitSelectionChanged.

private void handleTransitSelectionChanged(ActionEvent e) {
    int index = transitSelectBox.getSelectedIndex();
    if (index < 0) {
        return;
    }
    Transit t = transitBoxList.get(index);
    if ((t != null) && (t != selectedTransit)) {
        selectedTransit = t;
        initializeStartingBlockCombo();
        initializeDestinationBlockCombo();
        initiateFrame.pack();
    }
}
Also used : Transit(jmri.Transit)

Example 4 with Transit

use of jmri.Transit in project JMRI by JMRI.

the class ActivateTrainFrame method initiateTrain.

/**
     * Open up a new train window for a given roster entry located in a
     * specific block.
     * 
     * @param e the action event triggering the new window
     * @param re the roster entry to open the new window for
     * @param b the block where the train is located
     */
public void initiateTrain(ActionEvent e, RosterEntry re, Block b) {
    initiateTrain(e);
    if (_TrainsFromRoster && re != null) {
        setComboBox(trainSelectBox, re.getId());
    //Add in some bits of code as some point to filter down the transits that can be used.
    }
    if (b != null && selectedTransit != null) {
        ArrayList<Transit> transitList = _TransitManager.getListUsingBlock(b);
        ArrayList<Transit> transitEntryList = _TransitManager.getListEntryBlock(b);
        for (Transit t : transitEntryList) {
            if (!transitList.contains(t)) {
                transitList.add(t);
            }
        }
        transitsFromSpecificBlock = true;
        initializeFreeTransitsCombo(transitList);
        ArrayList<Block> tmpBlkList = new ArrayList<Block>();
        if (selectedTransit.getEntryBlocksList().contains(b)) {
            tmpBlkList = selectedTransit.getEntryBlocksList();
            inTransitBox.setSelected(false);
        } else if (selectedTransit.containsBlock(b)) {
            tmpBlkList = selectedTransit.getInternalBlocksList();
            inTransitBox.setSelected(true);
        }
        ArrayList<Integer> tmpSeqList = selectedTransit.getBlockSeqList();
        for (int i = 0; i < tmpBlkList.size(); i++) {
            if (tmpBlkList.get(i) == b) {
                setComboBox(startingBlockBox, getBlockName(b) + "-" + tmpSeqList.get(i));
                break;
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Block(jmri.Block) Transit(jmri.Transit)

Example 5 with Transit

use of jmri.Transit in project JMRI by JMRI.

the class TransitCreationTool method createTransit.

public Transit createTransit() throws JmriException {
    TransitManager tm = InstanceManager.getDefault(jmri.TransitManager.class);
    String transitName = "From " + list.get(0).getDisplayName() + " to " + list.get(list.size() - 1).getDisplayName();
    Transit t = tm.createNewTransit(transitName);
    if (t == null) {
        log.error("Unable to create transit " + transitName);
        throw new JmriException(Bundle.getMessage("TCTErrorUnableToCreate", transitName));
    }
    if (list.get(0) instanceof SignalMast) {
        jmri.SignalMastLogicManager smlm = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
        for (int i = 1; i <= list.size() - 1; i++) {
            jmri.SignalMastLogic sml = smlm.getSignalMastLogic((SignalMast) list.get(i - 1));
            Section sec = sml.getAssociatedSection((SignalMast) list.get(i));
            //In theory sec being null would already have been tested when the signal was added.
            if (sec == null) {
                String error = Bundle.getMessage("TCTErrorMastPairsNoSection", list.get(i - 1).getDisplayName(), list.get(i).getDisplayName());
                log.error(error);
                tm.deregister(t);
                t.dispose();
                cancelTransitCreate();
                throw new JmriException(error);
            }
            t.addTransitSection(new jmri.TransitSection(sec, i, Section.FORWARD));
        }
    }
    //Once created clear the list for a fresh start.
    list = new ArrayList<NamedBean>();
    return t;
}
Also used : NamedBean(jmri.NamedBean) JmriException(jmri.JmriException) Transit(jmri.Transit) Section(jmri.Section) TransitManager(jmri.TransitManager) SignalMast(jmri.SignalMast)

Aggregations

Transit (jmri.Transit)14 EntryPoint (jmri.EntryPoint)5 Section (jmri.Section)4 TransitManager (jmri.TransitManager)4 ActionEvent (java.awt.event.ActionEvent)3 ArrayList (java.util.ArrayList)3 JButton (javax.swing.JButton)3 Block (jmri.Block)3 TransitSection (jmri.TransitSection)3 FlowLayout (java.awt.FlowLayout)2 ActionListener (java.awt.event.ActionListener)2 BoxLayout (javax.swing.BoxLayout)2 JLabel (javax.swing.JLabel)2 JPanel (javax.swing.JPanel)2 NamedBean (jmri.NamedBean)2 SectionManager (jmri.SectionManager)2 TransitSectionAction (jmri.TransitSectionAction)2 Element (org.jdom2.Element)2 AbstractAction (javax.swing.AbstractAction)1 JDialog (javax.swing.JDialog)1