Search in sources :

Example 56 with RouteLocation

use of jmri.jmrit.operations.routes.RouteLocation in project JMRI by JMRI.

the class TrainSwitchLists method buildSwitchList.

/**
     * Builds a switch list for a location showing the work by train arrival
     * time. If not running in real time, new train work is appended to the end
     * of the file. User has the ability to modify the text of the messages
     * which can cause an IllegalArgumentException. Some messages have more
     * arguments than the default message allowing the user to customize the
     * message to their liking.
     * 
     * There also an option to list all of the car work by track name. This option
     * is only available in real time and is shown after the switch list by
     * train.
     *
     * @param location The Location needing a switch list
     */
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", // NOI18N
justification = "CarManager only provides Car Objects")
public void buildSwitchList(Location location) {
    // Append switch list data if not operating in real time
    boolean newTrainsOnly = !Setup.isSwitchListRealTime();
    // add text to end of file when true
    boolean append = false;
    // used to determine if FF needed between trains
    boolean checkFormFeed = true;
    if (newTrainsOnly) {
        if (!location.getStatus().equals(Location.MODIFIED) && !Setup.isSwitchListAllTrainsEnabled()) {
            // nothing to add
            return;
        }
        append = location.getSwitchListState() == Location.SW_APPEND;
        if (location.getSwitchListState() != Location.SW_APPEND) {
            location.setSwitchListState(Location.SW_APPEND);
        }
        location.setStatus(Location.UPDATED);
    }
    log.debug("Append: {} for location ({})", append, location.getName());
    // create switch list file
    File file = TrainManagerXml.instance().createSwitchListFile(location.getName());
    PrintWriter fileOut = null;
    try {
        fileOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), "UTF-8")), // NOI18N
        true);
    } catch (IOException e) {
        log.error("Can not open switchlist file: {}", file.getName());
        return;
    }
    try {
        // build header
        if (!append) {
            newLine(fileOut, Setup.getRailroadName());
            newLine(fileOut);
            newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringSwitchListFor(), new Object[] { splitString(location.getName()) }));
            if (!location.getSwitchListComment().equals(Location.NONE)) {
                newLine(fileOut, location.getSwitchListComment());
            }
        }
        String valid = MessageFormat.format(messageFormatText = TrainManifestText.getStringValid(), new Object[] { getDate(true) });
        if (Setup.isPrintTimetableNameEnabled()) {
            TrainSchedule sch = TrainScheduleManager.instance().getScheduleById(trainManager.getTrainScheduleActiveId());
            if (sch != null) {
                valid = valid + " (" + sch.getName() + ")";
            }
        }
        // get a list of trains sorted by arrival time
        List<Train> trains = trainManager.getTrainsArrivingThisLocationList(location);
        for (Train train : trains) {
            if (!train.isBuilt()) {
                // train wasn't built so skip
                continue;
            }
            if (newTrainsOnly && train.getSwitchListStatus().equals(Train.PRINTED)) {
                // already printed this train
                continue;
            }
            Route route = train.getRoute();
            if (route == null) {
                // no route for this train
                continue;
            }
            // determine if train works this location
            boolean works = isThereWorkAtLocation(train, location);
            if (!works && !Setup.isSwitchListAllTrainsEnabled()) {
                log.debug("No work for train ({}) at location ({})", train.getName(), location.getName());
                continue;
            }
            // we're now going to add to the switch list
            if (checkFormFeed) {
                if (append && !Setup.getSwitchListPageFormat().equals(Setup.PAGE_NORMAL)) {
                    fileOut.write(FORM_FEED);
                }
                if (Setup.isPrintValidEnabled()) {
                    newLine(fileOut, valid);
                }
            } else if (!Setup.getSwitchListPageFormat().equals(Setup.PAGE_NORMAL)) {
                fileOut.write(FORM_FEED);
            }
            // done with FF for this train
            checkFormFeed = false;
            // some cars booleans and the number of times this location get's serviced
            // when true there was a car pick up
            pickupCars = false;
            // when true there was a car set out
            dropCars = false;
            int stops = 1;
            boolean trainDone = false;
            // get engine and car lists
            List<Engine> engineList = engineManager.getByTrainBlockingList(train);
            List<Car> carList = carManager.getByTrainDestinationList(train);
            List<RouteLocation> routeList = route.getLocationsBySequenceList();
            RouteLocation rlPrevious = null;
            // does the train stop once or more at this location?
            for (RouteLocation rl : routeList) {
                if (!splitString(rl.getName()).equals(splitString(location.getName()))) {
                    rlPrevious = rl;
                    continue;
                }
                String expectedArrivalTime = train.getExpectedArrivalTime(rl);
                if (expectedArrivalTime.equals(Train.ALREADY_SERVICED)) {
                    trainDone = true;
                }
                // first time at this location?
                if (stops == 1) {
                    newLine(fileOut);
                    newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringScheduledWork(), new Object[] { train.getName(), train.getDescription() }));
                    if (train.isTrainEnRoute()) {
                        if (!trainDone) {
                            newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringDepartedExpected(), new Object[] { splitString(train.getTrainDepartsName()), expectedArrivalTime, rl.getTrainDirectionString() }));
                        }
                    } else if (!train.isLocalSwitcher()) {
                        if (rl == train.getRoute().getDepartsRouteLocation()) {
                            newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringDepartsAt(), new Object[] { splitString(train.getTrainDepartsName()), rl.getTrainDirectionString(), train.getFormatedDepartureTime() }));
                        } else {
                            newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringDepartsAtExpectedArrival(), new Object[] { splitString(train.getTrainDepartsName()), train.getFormatedDepartureTime(), expectedArrivalTime, rl.getTrainDirectionString() }));
                        }
                    }
                } else {
                    // Print visit number only if previous location wasn't the same
                    if (rlPrevious == null || !splitString(rl.getName()).equals(splitString(rlPrevious.getName()))) {
                        if (Setup.getSwitchListPageFormat().equals(Setup.PAGE_PER_VISIT)) {
                            fileOut.write(FORM_FEED);
                        }
                        newLine(fileOut);
                        if (train.isTrainEnRoute()) {
                            if (expectedArrivalTime.equals(Train.ALREADY_SERVICED)) {
                                newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringVisitNumberDone(), new Object[] { stops, train.getName(), train.getDescription() }));
                            } else if (rl != train.getRoute().getTerminatesRouteLocation()) {
                                newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringVisitNumberDeparted(), new Object[] { stops, train.getName(), expectedArrivalTime, rl.getTrainDirectionString(), train.getDescription() }));
                            } else {
                                // message: Visit number {0} for train ({1}) expect to arrive in {2}, terminates {3}
                                newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringVisitNumberTerminatesDeparted(), new Object[] { stops, train.getName(), expectedArrivalTime, splitString(rl.getName()), train.getDescription() }));
                            }
                        } else {
                            // train hasn't departed
                            if (rl != train.getRoute().getTerminatesRouteLocation()) {
                                newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringVisitNumber(), new Object[] { stops, train.getName(), expectedArrivalTime, rl.getTrainDirectionString(), train.getDescription() }));
                            } else {
                                newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringVisitNumberTerminates(), new Object[] { stops, train.getName(), expectedArrivalTime, splitString(rl.getName()), train.getDescription() }));
                            }
                        }
                    } else {
                        // don't bump stop count, same location
                        stops--;
                        // Does the train reverse direction?
                        if (rl.getTrainDirection() != rlPrevious.getTrainDirection() && !TrainSwitchListText.getStringTrainDirectionChange().equals("")) {
                            newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringTrainDirectionChange(), new Object[] { train.getName(), rl.getTrainDirectionString(), train.getDescription(), train.getTrainTerminatesName() }));
                        }
                    }
                }
                // save current location in case there's back to back location with the same name
                rlPrevious = rl;
                // add route comment
                if (Setup.isSwitchListRouteLocationCommentEnabled() && !rl.getComment().trim().equals("")) {
                    newLine(fileOut, rl.getComment());
                }
                // now print out the work for this location
                if (Setup.getManifestFormat().equals(Setup.STANDARD_FORMAT)) {
                    pickupEngines(fileOut, engineList, rl, !IS_MANIFEST);
                    // if switcher show loco drop at end of list
                    if (train.isLocalSwitcher()) {
                        blockCarsByTrack(fileOut, train, carList, routeList, rl, IS_PRINT_HEADER, !IS_MANIFEST);
                        dropEngines(fileOut, engineList, rl, !IS_MANIFEST);
                    } else {
                        dropEngines(fileOut, engineList, rl, !IS_MANIFEST);
                        blockCarsByTrack(fileOut, train, carList, routeList, rl, IS_PRINT_HEADER, !IS_MANIFEST);
                    }
                } else if (Setup.getManifestFormat().equals(Setup.TWO_COLUMN_FORMAT)) {
                    blockLocosTwoColumn(fileOut, engineList, rl, !IS_MANIFEST);
                    blockCarsByTrackTwoColumn(fileOut, train, carList, routeList, rl, IS_PRINT_HEADER, !IS_MANIFEST);
                } else {
                    blockLocosTwoColumn(fileOut, engineList, rl, !IS_MANIFEST);
                    blockCarsByTrackNameTwoColumn(fileOut, train, carList, routeList, rl, IS_PRINT_HEADER, !IS_MANIFEST);
                }
                if (Setup.isPrintHeadersEnabled() || !Setup.getManifestFormat().equals(Setup.STANDARD_FORMAT)) {
                    printHorizontalLine(fileOut, !IS_MANIFEST);
                }
                stops++;
                // done with work, now print summary for this location if we're done
                if (rl != train.getRoute().getTerminatesRouteLocation()) {
                    RouteLocation nextRl = train.getRoute().getNextRouteLocation(rl);
                    if (splitString(rl.getName()).equals(splitString(nextRl.getName()))) {
                        // the current location name is the "same" as the next
                        continue;
                    }
                    // print departure text if not a switcher
                    if (!train.isLocalSwitcher()) {
                        String trainDeparts = "";
                        if (Setup.isPrintLoadsAndEmptiesEnabled()) {
                            int emptyCars = train.getNumberEmptyCarsInTrain(rl);
                            // Message format: Train departs Boston Westbound with 4 loads, 8 empties, 450 feet,
                            // 3000 tons
                            trainDeparts = MessageFormat.format(TrainSwitchListText.getStringTrainDepartsLoads(), new Object[] { TrainCommon.splitString(rl.getName()), rl.getTrainDirectionString(), train.getNumberCarsInTrain(rl) - emptyCars, emptyCars, train.getTrainLength(rl), Setup.getLengthUnit().toLowerCase(), train.getTrainWeight(rl), train.getTrainTerminatesName(), train.getName() });
                        } else {
                            // Message format: Train departs Boston Westbound with 12 cars, 450 feet, 3000 tons
                            trainDeparts = MessageFormat.format(TrainSwitchListText.getStringTrainDepartsCars(), new Object[] { TrainCommon.splitString(rl.getName()), rl.getTrainDirectionString(), train.getNumberCarsInTrain(rl), train.getTrainLength(rl), Setup.getLengthUnit().toLowerCase(), train.getTrainWeight(rl), train.getTrainTerminatesName(), train.getName() });
                        }
                        newLine(fileOut, trainDeparts);
                    }
                }
            }
            if (trainDone && !pickupCars && !dropCars) {
                // Default message: Train ({0}) has serviced this location
                newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringTrainDone(), new Object[] { train.getName(), train.getDescription(), splitString(location.getName()) }));
            } else {
                if (stops > 1 && !pickupCars) {
                    // Default message: No car pick ups for train ({0}) at this location
                    newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringNoCarPickUps(), new Object[] { train.getName(), train.getDescription(), splitString(location.getName()) }));
                }
                if (stops > 1 && !dropCars) {
                    // Default message: No car set outs for train ({0}) at this location
                    newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringNoCarDrops(), new Object[] { train.getName(), train.getDescription(), splitString(location.getName()) }));
                }
            }
        }
        // now report car movement by tracks at location
        if (Setup.isTrackSummaryEnabled() && Setup.isSwitchListRealTime()) {
            // list utility cars by quantity
            clearUtilityCarTypes();
            if (Setup.getSwitchListPageFormat().equals(Setup.PAGE_NORMAL)) {
                newLine(fileOut);
                newLine(fileOut);
            } else {
                fileOut.write(FORM_FEED);
            }
            newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringSwitchListByTrack(), new Object[] { splitString(location.getName()) }));
            // we only need the cars delivered to or at this location
            List<RollingStock> rsList = carManager.getByTrainList();
            List<Car> carList = new ArrayList<Car>();
            for (RollingStock rs : rsList) {
                if ((rs.getLocation() != null && splitString(rs.getLocation().getName()).equals(splitString(location.getName()))) || (rs.getDestination() != null && splitString(rs.getDestination().getName()).equals(splitString(location.getName()))))
                    carList.add((Car) rs);
            }
            // locations and tracks can have "similar" names, only list track names once
            List<String> trackNames = new ArrayList<String>();
            for (Location loc : locationManager.getLocationsByNameList()) {
                if (!splitString(loc.getName()).equals(splitString(location.getName())))
                    continue;
                for (Track track : loc.getTrackByNameList(null)) {
                    String trackName = splitString(track.getName());
                    if (trackNames.contains(trackName))
                        continue;
                    trackNames.add(trackName);
                    // for printing train message once                     
                    String trainName = "";
                    newLine(fileOut);
                    // print out just the track name
                    newLine(fileOut, trackName);
                    // now show the cars pickup and holds for this track
                    for (Car car : carList) {
                        if (!splitString(car.getTrackName()).equals(trackName)) {
                            continue;
                        }
                        // is the car scheduled for pickup?
                        if (car.getRouteLocation() != null) {
                            if (splitString(car.getRouteLocation().getLocation().getName()).equals(splitString(location.getName()))) {
                                // cars are sorted by train name, print train message once
                                if (!trainName.equals(car.getTrainName())) {
                                    trainName = car.getTrainName();
                                    newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringScheduledWork(), new Object[] { car.getTrainName(), car.getTrain().getDescription() }));
                                    printPickupCarHeader(fileOut, !IS_MANIFEST, !IS_TWO_COLUMN_TRACK);
                                }
                                if (car.isUtility()) {
                                    pickupUtilityCars(fileOut, carList, car, !IS_MANIFEST);
                                } else {
                                    pickUpCar(fileOut, car, !IS_MANIFEST);
                                }
                            }
                        // car holds
                        } else if (car.isUtility()) {
                            String s = pickupUtilityCars(carList, car, !IS_MANIFEST, !IS_TWO_COLUMN_TRACK);
                            if (s != null) {
                                newLine(fileOut, // NOI18N
                                TrainSwitchListText.getStringHoldCar().split("\\{")[0] + s.trim());
                            }
                        } else {
                            newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringHoldCar(), new Object[] { padAndTruncateString(car.getRoadName(), CarRoads.instance().getMaxNameLength()), padAndTruncateString(TrainCommon.splitString(car.getNumber()), Control.max_len_string_print_road_number), padAndTruncateString(car.getTypeName().split("-")[0], CarTypes.instance().getMaxNameLength()), padAndTruncateString(car.getLength() + LENGTHABV, Control.max_len_string_length_name), padAndTruncateString(car.getLoadName(), CarLoads.instance().getMaxNameLength()), padAndTruncateString(trackName, locationManager.getMaxTrackNameLength()), padAndTruncateString(car.getColor(), CarColors.instance().getMaxNameLength()) }));
                        }
                    }
                    // now do set outs at this location
                    for (Car car : carList) {
                        if (!splitString(car.getDestinationTrackName()).equals(trackName)) {
                            continue;
                        }
                        if (car.getRouteDestination() != null && splitString(car.getRouteDestination().getLocation().getName()).equals(splitString(location.getName()))) {
                            // cars are sorted by train name, print train message once
                            if (!trainName.equals(car.getTrainName())) {
                                trainName = car.getTrainName();
                                newLine(fileOut, MessageFormat.format(messageFormatText = TrainSwitchListText.getStringScheduledWork(), new Object[] { car.getTrainName(), car.getTrain().getDescription() }));
                                printDropCarHeader(fileOut, !IS_MANIFEST, !IS_TWO_COLUMN_TRACK);
                            }
                            if (car.isUtility()) {
                                setoutUtilityCars(fileOut, carList, car, !IS_MANIFEST);
                            } else {
                                dropCar(fileOut, car, !IS_MANIFEST);
                            }
                        }
                    }
                }
            }
        }
    } catch (IllegalArgumentException e) {
        newLine(fileOut, MessageFormat.format(Bundle.getMessage("ErrorIllegalArgument"), new Object[] { Bundle.getMessage("TitleSwitchListText"), e.getLocalizedMessage() }));
        newLine(fileOut, messageFormatText);
        e.printStackTrace();
    }
    // Are there any cars that need to be found?
    addCarsLocationUnknown(fileOut, !IS_MANIFEST);
    fileOut.flush();
    fileOut.close();
}
Also used : ArrayList(java.util.ArrayList) RouteLocation(jmri.jmrit.operations.routes.RouteLocation) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock) BufferedWriter(java.io.BufferedWriter) TrainSchedule(jmri.jmrit.operations.trains.timetable.TrainSchedule) Route(jmri.jmrit.operations.routes.Route) Engine(jmri.jmrit.operations.rollingstock.engines.Engine) PrintWriter(java.io.PrintWriter) IOException(java.io.IOException) Car(jmri.jmrit.operations.rollingstock.cars.Car) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Track(jmri.jmrit.operations.locations.Track) RouteLocation(jmri.jmrit.operations.routes.RouteLocation) Location(jmri.jmrit.operations.locations.Location) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 57 with RouteLocation

use of jmri.jmrit.operations.routes.RouteLocation in project JMRI by JMRI.

the class TrainManager method getTrainsArrivingThisLocationList.

/**
     * Provides a list of trains ordered by arrival time to a location
     *
     * @param location The location
     * @return A list of trains ordered by arrival time.
     */
public List<Train> getTrainsArrivingThisLocationList(Location location) {
    // get a list of trains
    List<Train> out = new ArrayList<Train>();
    List<Integer> arrivalTimes = new ArrayList<Integer>();
    for (Train train : getTrainsByTimeList()) {
        if (!train.isBuilt()) {
            // train wasn't built so skip
            continue;
        }
        Route route = train.getRoute();
        if (route == null) {
            // no route for this train
            continue;
        }
        for (RouteLocation rl : route.getLocationsBySequenceList()) {
            if (TrainCommon.splitString(rl.getName()).equals(TrainCommon.splitString(location.getName()))) {
                int expectedArrivalTime = train.getExpectedTravelTimeInMinutes(rl);
                // is already serviced then "-1"
                if (expectedArrivalTime == -1) {
                    // place all trains that have already been serviced at the start
                    out.add(0, train);
                    arrivalTimes.add(0, expectedArrivalTime);
                } else // if the train is in route, then expected arrival time is in minutes
                if (train.isTrainEnRoute()) {
                    for (int j = 0; j < out.size(); j++) {
                        Train t = out.get(j);
                        int time = arrivalTimes.get(j);
                        if (t.isTrainEnRoute() && expectedArrivalTime < time) {
                            out.add(j, train);
                            arrivalTimes.add(j, expectedArrivalTime);
                            break;
                        }
                        if (!t.isTrainEnRoute()) {
                            out.add(j, train);
                            arrivalTimes.add(j, expectedArrivalTime);
                            break;
                        }
                    }
                // Train has not departed
                } else {
                    for (int j = 0; j < out.size(); j++) {
                        Train t = out.get(j);
                        int time = arrivalTimes.get(j);
                        if (!t.isTrainEnRoute() && expectedArrivalTime < time) {
                            out.add(j, train);
                            arrivalTimes.add(j, expectedArrivalTime);
                            break;
                        }
                    }
                }
                if (!out.contains(train)) {
                    out.add(train);
                    arrivalTimes.add(expectedArrivalTime);
                }
                // done
                break;
            }
        }
    }
    return out;
}
Also used : ArrayList(java.util.ArrayList) RouteLocation(jmri.jmrit.operations.routes.RouteLocation) Route(jmri.jmrit.operations.routes.Route)

Example 58 with RouteLocation

use of jmri.jmrit.operations.routes.RouteLocation in project JMRI by JMRI.

the class JsonUtil method getRouteLocationsForTrain.

private static ArrayNode getRouteLocationsForTrain(Locale locale, Train train) throws JsonException {
    ArrayNode rlan = mapper.createArrayNode();
    List<RouteLocation> routeList = train.getRoute().getLocationsBySequenceList();
    for (RouteLocation route : routeList) {
        ObjectNode rln = mapper.createObjectNode();
        RouteLocation rl = route;
        rln.put(ID, rl.getId());
        rln.put(NAME, rl.getName());
        rln.put(DIRECTION, rl.getTrainDirectionString());
        rln.put(COMMENT, rl.getComment());
        rln.put(SEQUENCE, rl.getSequenceId());
        rln.put(EXPECTED_ARRIVAL, train.getExpectedArrivalTime(rl));
        rln.put(EXPECTED_DEPARTURE, train.getExpectedDepartureTime(rl));
        rln.put(LOCATION, getLocation(locale, rl.getLocation().getId()).get(DATA));
        //add this routeLocation to the routeLocation array
        rlan.add(rln);
    }
    //return array of routeLocations
    return rlan;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RouteLocation(jmri.jmrit.operations.routes.RouteLocation)

Example 59 with RouteLocation

use of jmri.jmrit.operations.routes.RouteLocation in project JMRI by JMRI.

the class RollingStockSetFrame method change.

@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "GUI ease of use")
protected boolean change(RollingStock rs) {
    log.debug("Change button action for rs ({})", rs.toString());
    // save the auto buttons
    autoTrackCheckBoxSelected = autoTrackCheckBox.isSelected();
    autoDestinationTrackCheckBoxSelected = autoDestinationTrackCheckBox.isSelected();
    autoFinalDestTrackCheckBoxSelected = autoFinalDestTrackCheckBox.isSelected();
    autoTrainCheckBoxSelected = autoTrainCheckBox.isSelected();
    // save the statuses
    if (!ignoreStatusCheckBox.isSelected()) {
        rs.setLocationUnknown(locationUnknownCheckBox.isSelected());
        rs.setOutOfService(outOfServiceCheckBox.isSelected());
    }
    // update location
    if (!changeLocation(rs)) {
        return false;
    }
    // check to see if rolling stock is in staging and out of service (also location unknown)
    if (outOfServiceCheckBox.isSelected() && rs.getTrack() != null && rs.getTrack().getTrackType().equals(Track.STAGING)) {
        JOptionPane.showMessageDialog(this, getRb().getString("rsNeedToRemoveStaging"), getRb().getString("rsInStaging"), JOptionPane.WARNING_MESSAGE);
        // clear the rolling stock's location
        rs.setLocation(null, null);
    }
    loadTrain(rs);
    // update destination
    if (!changeDestination(rs)) {
        return false;
    }
    if (!ignoreTrainCheckBox.isSelected()) {
        Train train = rs.getTrain();
        if (train != null) {
            // determine if train services this rs's type
            if (!train.acceptsTypeName(rs.getTypeName())) {
                JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsTrainNotServType"), new Object[] { rs.getTypeName(), train.getName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
            // determine if train services this rs's road
            if (!train.acceptsRoadName(rs.getRoadName())) {
                JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsTrainNotServRoad"), new Object[] { rs.getRoadName(), train.getName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
            // determine if train services this rs's built date
            if (!train.acceptsBuiltDate(rs.getBuilt())) {
                JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsTrainNotServBuilt"), new Object[] { rs.getBuilt(), train.getName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
            // determine if train services this rs's built date
            if (!train.acceptsOwnerName(rs.getOwner())) {
                JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsTrainNotServOwner"), new Object[] { rs.getOwner(), train.getName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
            // determine if train services the location and destination selected by user
            rl = null;
            rd = null;
            if (rs.getLocation() != null) {
                Route route = train.getRoute();
                if (route != null) {
                    // this is a quick check, the actual rl and rd are set later in this routine.
                    rl = route.getLastLocationByName(rs.getLocationName());
                    rd = route.getLastLocationByName(rs.getDestinationName());
                }
                if (rl == null) {
                    JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsLocNotServ"), new Object[] { rs.getLocationName(), train.getName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                if (rd == null && !rs.getDestinationName().equals(RollingStock.NONE)) {
                    JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsDestNotServ"), new Object[] { rs.getDestinationName(), train.getName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                if (rd != null && route != null) {
                    // now determine if destination is after location
                    List<RouteLocation> routeSequence = route.getLocationsBySequenceList();
                    // when true, found the train's location
                    boolean foundTrainLoc = false;
                    // when true, found the rs's location in the route
                    boolean foundLoc = false;
                    boolean foundDes = false;
                    for (RouteLocation rlocation : routeSequence) {
                        if (train.isTrainEnRoute() && !foundTrainLoc) {
                            if (train.getCurrentLocation() == rlocation) {
                                foundTrainLoc = true;
                            } else {
                                continue;
                            }
                        }
                        if (rs.getLocationName().equals(rlocation.getName())) {
                            rl = rlocation;
                            foundLoc = true;
                        }
                        if (rs.getDestinationName().equals(rlocation.getName()) && foundLoc) {
                            rd = rlocation;
                            foundDes = true;
                            if (rs.getDestinationTrack() != null && (rlocation.getTrainDirection() & rs.getDestinationTrack().getTrainDirections()) == 0) {
                                // destination track isn't serviced by the train's direction
                                continue;
                            }
                            break;
                        }
                    }
                    if (!foundLoc) {
                        JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsTrainEnRoute"), new Object[] { rs.toString(), train.getName(), rs.getLocationName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                        return false;
                    }
                    if (!foundDes) {
                        JOptionPane.showMessageDialog(this, MessageFormat.format(getRb().getString("rsLocOrder"), new Object[] { rs.getDestinationName(), rs.getLocationName(), train.getName() }), getRb().getString("rsNotMove"), JOptionPane.ERROR_MESSAGE);
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : RouteLocation(jmri.jmrit.operations.routes.RouteLocation) Train(jmri.jmrit.operations.trains.Train) Route(jmri.jmrit.operations.routes.Route) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 60 with RouteLocation

use of jmri.jmrit.operations.routes.RouteLocation in project JMRI by JMRI.

the class RollingStock method setRouteDestination.

/**
     * Set where in a train's route this rolling stock will be set out.
     *
     * @param routeDestination the location where the rolling stock is to leave
     *            the train.
     */
public void setRouteDestination(RouteLocation routeDestination) {
    if (routeDestination != null && _destination != null && !routeDestination.getName().equals(_destination.getName())) {
        log.debug("WARNING route destination name ({}) not equal to destination name ({}) for rolling stock ({})", routeDestination.getName(), _destination.getName(), // NOI18N
        toString());
    }
    RouteLocation old = _routeDestination;
    _routeDestination = routeDestination;
    if (old != routeDestination) {
        setDirtyAndFirePropertyChange(ROUTE_DESTINATION_CHANGED_PROPERTY, old, routeDestination);
    }
}
Also used : RouteLocation(jmri.jmrit.operations.routes.RouteLocation)

Aggregations

RouteLocation (jmri.jmrit.operations.routes.RouteLocation)118 Route (jmri.jmrit.operations.routes.Route)64 Location (jmri.jmrit.operations.locations.Location)55 Track (jmri.jmrit.operations.locations.Track)52 Car (jmri.jmrit.operations.rollingstock.cars.Car)48 Engine (jmri.jmrit.operations.rollingstock.engines.Engine)33 LocationManager (jmri.jmrit.operations.locations.LocationManager)21 RouteManager (jmri.jmrit.operations.routes.RouteManager)20 Consist (jmri.jmrit.operations.rollingstock.engines.Consist)17 Train (jmri.jmrit.operations.trains.Train)16 CarManager (jmri.jmrit.operations.rollingstock.cars.CarManager)13 ArrayList (java.util.ArrayList)12 CarTypes (jmri.jmrit.operations.rollingstock.cars.CarTypes)12 EngineManager (jmri.jmrit.operations.rollingstock.engines.EngineManager)12 RollingStock (jmri.jmrit.operations.rollingstock.RollingStock)11 EngineTypes (jmri.jmrit.operations.rollingstock.engines.EngineTypes)8 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)6 IOException (java.io.IOException)6 TrainManager (jmri.jmrit.operations.trains.TrainManager)6 JCheckBox (javax.swing.JCheckBox)5