Search in sources :

Example 21 with RollingStock

use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.

the class EngineManagerTest method testListEnginesByTrain.

public void testListEnginesByTrain() {
    resetEngineManager();
    EngineManager manager = EngineManager.instance();
    Route r = new Route("id", "Test");
    r.addLocation(l1);
    r.addLocation(l2);
    r.addLocation(l3);
    Train t1 = new Train("id1", "F");
    t1.setRoute(r);
    Train t3 = new Train("id3", "E");
    t3.setRoute(r);
    //setup the engines
    e1.setTrain(t1);
    e2.setTrain(t3);
    e3.setTrain(t3);
    e4.setTrain(new Train("id4", "B"));
    e5.setTrain(t3);
    e6.setTrain(new Train("id6", "A"));
    // now get engines by train
    List<RollingStock> engineList = manager.getByTrainList();
    Assert.assertEquals("Number of Engines by train", 6, engineList.size());
    Assert.assertEquals("1st engine in list by train", e6, engineList.get(0));
    Assert.assertEquals("2nd engine in list by train", e4, engineList.get(1));
    Assert.assertEquals("3rd engine in list by train", e5, engineList.get(2));
    Assert.assertEquals("4th engine in list by train", e2, engineList.get(3));
    Assert.assertEquals("5th engine in list by train", e3, engineList.get(4));
    Assert.assertEquals("6th engine in list by train", e1, engineList.get(5));
}
Also used : Train(jmri.jmrit.operations.trains.Train) Route(jmri.jmrit.operations.routes.Route) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock)

Example 22 with RollingStock

use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.

the class PrintLocationsAction method printAnalysisSelected.

private void printAnalysisSelected() throws IOException {
    CarManager carManager = CarManager.instance();
    List<Location> locations = manager.getLocationsByNameList();
    List<RollingStock> cars = carManager.getByLocationList();
    String[] carTypes = CarTypes.instance().getNames();
    String s = Bundle.getMessage("TrackAnalysis") + NEW_LINE;
    writer.write(s);
    // print the car type being analyzed
    for (String type : carTypes) {
        // get the total length for a given car type
        int numberOfCars = 0;
        int totalTrackLength = 0;
        for (RollingStock car : cars) {
            if (car.getTypeName().equals(type) && car.getLocation() != null) {
                numberOfCars++;
                totalTrackLength += car.getTotalLength();
            }
        }
        writer.write(MessageFormat.format(Bundle.getMessage("NumberTypeLength"), new Object[] { numberOfCars, type, totalTrackLength, Setup.getLengthUnit().toLowerCase() }) + NEW_LINE);
        // don't bother reporting when the number of cars for a given type is zero
        if (numberOfCars > 0) {
            // spurs
            writer.write(SPACE + MessageFormat.format(Bundle.getMessage("SpurTrackThatAccept"), new Object[] { type }) + NEW_LINE);
            int trackLength = getTrackLengthAcceptType(locations, type, Track.SPUR);
            if (trackLength > 0) {
                writer.write(SPACE + MessageFormat.format(Bundle.getMessage("TotalLengthSpur"), new Object[] { type, trackLength, Setup.getLengthUnit().toLowerCase(), 100 * totalTrackLength / trackLength }) + NEW_LINE);
            } else {
                writer.write(SPACE + Bundle.getMessage("None") + NEW_LINE);
            }
            // yards
            writer.write(SPACE + MessageFormat.format(Bundle.getMessage("YardTrackThatAccept"), new Object[] { type }) + NEW_LINE);
            trackLength = getTrackLengthAcceptType(locations, type, Track.YARD);
            if (trackLength > 0) {
                writer.write(SPACE + MessageFormat.format(Bundle.getMessage("TotalLengthYard"), new Object[] { type, trackLength, Setup.getLengthUnit().toLowerCase(), 100 * totalTrackLength / trackLength }) + NEW_LINE);
            } else {
                writer.write(SPACE + Bundle.getMessage("None") + NEW_LINE);
            }
            // interchanges
            writer.write(SPACE + MessageFormat.format(Bundle.getMessage("InterchangesThatAccept"), new Object[] { type }) + NEW_LINE);
            trackLength = getTrackLengthAcceptType(locations, type, Track.INTERCHANGE);
            if (trackLength > 0) {
                writer.write(SPACE + MessageFormat.format(Bundle.getMessage("TotalLengthInterchange"), new Object[] { type, trackLength, Setup.getLengthUnit().toLowerCase(), 100 * totalTrackLength / trackLength }) + NEW_LINE);
            } else {
                writer.write(SPACE + Bundle.getMessage("None") + NEW_LINE);
            }
            // staging
            if (showStaging) {
                writer.write(SPACE + MessageFormat.format(Bundle.getMessage("StageTrackThatAccept"), new Object[] { type }) + NEW_LINE);
                trackLength = getTrackLengthAcceptType(locations, type, Track.STAGING);
                if (trackLength > 0) {
                    writer.write(SPACE + MessageFormat.format(Bundle.getMessage("TotalLengthStage"), new Object[] { type, trackLength, Setup.getLengthUnit().toLowerCase(), 100 * totalTrackLength / trackLength }) + NEW_LINE);
                } else {
                    writer.write(SPACE + Bundle.getMessage("None") + NEW_LINE);
                }
            }
        }
    }
}
Also used : CarManager(jmri.jmrit.operations.rollingstock.cars.CarManager) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock) Location(jmri.jmrit.operations.locations.Location)

Example 23 with RollingStock

use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.

the class CarSetFrame method change.

@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "GUI ease of use")
protected boolean change(Car car) {
    // save the auto button
    autoReturnWhenEmptyTrackCheckBoxSelected = autoReturnWhenEmptyTrackCheckBox.isSelected();
    // car load
    if (!ignoreLoadCheckBox.isSelected() && loadComboBox.getSelectedItem() != null) {
        String load = (String) loadComboBox.getSelectedItem();
        if (!car.getLoadName().equals(load)) {
            if (CarLoads.instance().containsName(car.getTypeName(), load)) {
                car.setLoadName(load);
                updateComboBoxesLoadChange();
            } else {
                JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("carLoadNotValid"), new Object[] { load, car.getTypeName() }), Bundle.getMessage("carCanNotChangeLoad"), JOptionPane.WARNING_MESSAGE);
            }
        }
    }
    // set final destination fields before destination in case there's a schedule at destination
    if (!ignoreFinalDestinationCheckBox.isSelected()) {
        if (finalDestinationBox.getSelectedItem() == null) {
            car.setFinalDestination(null);
            car.setFinalDestinationTrack(null);
        } else {
            Track finalDestTrack = null;
            if (finalDestTrackBox.getSelectedItem() != null) {
                finalDestTrack = (Track) finalDestTrackBox.getSelectedItem();
            }
            if (finalDestTrack != null && car.getFinalDestinationTrack() != finalDestTrack && finalDestTrack.getTrackType().equals(Track.STAGING)) {
                log.debug("Destination track ({}) is staging", finalDestTrack.getName());
                JOptionPane.showMessageDialog(this, Bundle.getMessage("rsDoNotSelectStaging"), Bundle.getMessage("rsCanNotFinal"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
            car.setFinalDestination((Location) finalDestinationBox.getSelectedItem());
            car.setFinalDestinationTrack(finalDestTrack);
            String status = car.testDestination((Location) finalDestinationBox.getSelectedItem(), finalDestTrack);
            if (!status.equals(Track.OKAY)) {
                JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("rsCanNotFinalMsg"), new Object[] { car.toString(), status }), Bundle.getMessage("rsCanNotFinal"), JOptionPane.WARNING_MESSAGE);
            }
        }
    }
    // kernel
    if (!ignoreKernelCheckBox.isSelected() && kernelComboBox.getSelectedItem() != null) {
        if (kernelComboBox.getSelectedItem().equals(CarManager.NONE)) {
            car.setKernel(null);
        } else if (!car.getKernelName().equals(kernelComboBox.getSelectedItem())) {
            car.setKernel(carManager.getKernelByName((String) kernelComboBox.getSelectedItem()));
            // if car has FRED or is caboose make lead
            if (car.hasFred() || car.isCaboose()) {
                car.getKernel().setLead(car);
            }
            car.setBlocking(car.getKernel().getSize());
        }
    }
    // save car's track
    Track saveTrack = car.getTrack();
    if (!super.change(car)) {
        return false;
    }
    // return when empty fields
    if (!ignoreRWECheckBox.isSelected()) {
        // check that RWE load is valid for this car's type
        if (CarLoads.instance().getNames(car.getTypeName()).contains(loadReturnWhenEmptyBox.getSelectedItem())) {
            car.setReturnWhenEmptyLoadName((String) loadReturnWhenEmptyBox.getSelectedItem());
        } else {
            log.debug("Car ({}) type ({}) doesn't support RWE load ({})", car.toString(), car.getTypeName(), loadReturnWhenEmptyBox.getSelectedItem());
            JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("carLoadNotValid"), new Object[] { loadReturnWhenEmptyBox.getSelectedItem(), car.getTypeName() }), Bundle.getMessage("carCanNotChangeRweLoad"), JOptionPane.WARNING_MESSAGE);
        }
        if (destReturnWhenEmptyBox.getSelectedItem() == null) {
            car.setReturnWhenEmptyDestination(null);
            car.setReturnWhenEmptyDestTrack(null);
        } else {
            Location locationRWE = (Location) destReturnWhenEmptyBox.getSelectedItem();
            if (trackReturnWhenEmptyBox.getSelectedItem() != null) {
                Track trackRWE = (Track) trackReturnWhenEmptyBox.getSelectedItem();
                // warn user if they selected a staging track
                if (trackRWE != null && trackRWE.getTrackType().equals(Track.STAGING)) {
                    log.debug("Return when empty track ({}) is staging", trackRWE.getName());
                    JOptionPane.showMessageDialog(this, Bundle.getMessage("rsDoNotSelectStaging"), Bundle.getMessage("rsCanNotRWE"), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                // use a test car with a load of "E" and no length
                String status = getTestCar(car).testDestination(locationRWE, trackRWE);
                if (!status.equals(Track.OKAY)) {
                    JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("rsCanNotRWEMsg"), new Object[] { car.toString(), status }), Bundle.getMessage("rsCanNotRWE"), JOptionPane.WARNING_MESSAGE);
                }
                car.setReturnWhenEmptyDestTrack(trackRWE);
            } else {
                car.setReturnWhenEmptyDestTrack(null);
            }
            car.setReturnWhenEmptyDestination(locationRWE);
        }
    }
    // check to see if there's a schedule when placing the car at a spur
    if (!ignoreLocationCheckBox.isSelected() && trackLocationBox.getSelectedItem() != null && saveTrack != trackLocationBox.getSelectedItem()) {
        Track track = (Track) trackLocationBox.getSelectedItem();
        if (track.getSchedule() != null) {
            if (JOptionPane.showConfirmDialog(this, MessageFormat.format(Bundle.getMessage("rsDoYouWantSchedule"), new Object[] { car.toString() }), MessageFormat.format(Bundle.getMessage("rsSpurHasSchedule"), new Object[] { track.getName(), track.getScheduleName() }), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                String results = track.checkSchedule(car);
                if (!results.equals(Track.OKAY)) {
                    JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("rsNotAbleToApplySchedule"), new Object[] { results }), Bundle.getMessage("rsApplyingScheduleFailed"), JOptionPane.ERROR_MESSAGE);
                    // restore previous location and track so we'll ask to test schedule again
                    if (saveTrack != null) {
                        car.setLocation(saveTrack.getLocation(), saveTrack);
                    } else {
                        car.setLocation(null, null);
                    }
                    return false;
                }
                // now apply schedule to car
                track.scheduleNext(car);
                car.loadNext(track);
            }
        }
    }
    // determine if train services this car's load
    if (car.getTrain() != null) {
        Train train = car.getTrain();
        if (!train.acceptsLoad(car.getLoadName(), car.getTypeName())) {
            JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("carTrainNotServLoad"), new Object[] { car.getLoadName(), train.getName() }), Bundle.getMessage("rsNotMove"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (car.getLocation() != null && car.getDestination() != null && !train.services(car)) {
            JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("carTrainNotService"), new Object[] { car.toString(), train.getName() }), Bundle.getMessage("rsNotMove"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    checkTrain(car);
    // is this car part of a kernel?
    if (askKernelChange && car.getKernel() != null) {
        List<RollingStock> list = car.getKernel().getGroup();
        if (list.size() > 1) {
            if (JOptionPane.showConfirmDialog(this, MessageFormat.format(Bundle.getMessage("carInKernel"), new Object[] { car.toString() }), MessageFormat.format(Bundle.getMessage("carPartKernel"), new Object[] { car.getKernelName() }), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                if (!updateGroup(list)) {
                    return false;
                }
            } else if (outOfServiceCheckBox.isSelected()) {
                // don't leave car in kernel if out of service
                car.setKernel(null);
            }
        }
    }
    return true;
}
Also used : Train(jmri.jmrit.operations.trains.Train) Track(jmri.jmrit.operations.locations.Track) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock) Location(jmri.jmrit.operations.locations.Location) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 24 with RollingStock

use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.

the class CarSetFrame method updateGroup.

@Override
protected boolean updateGroup(List<RollingStock> list) {
    for (RollingStock rs : list) {
        Car car = (Car) rs;
        if (car == _car) {
            continue;
        }
        // make all cars in kernel the same
        if (!ignoreRWECheckBox.isSelected()) {
            car.setReturnWhenEmptyDestination(_car.getReturnWhenEmptyDestination());
            car.setReturnWhenEmptyDestTrack(_car.getReturnWhenEmptyDestTrack());
        }
        if (!ignoreFinalDestinationCheckBox.isSelected()) {
            car.setFinalDestination(_car.getFinalDestination());
            car.setFinalDestinationTrack(_car.getFinalDestinationTrack());
        }
        // update car load
        if (!ignoreLoadCheckBox.isSelected() && CarLoads.instance().containsName(car.getTypeName(), _car.getLoadName())) {
            car.setLoadName(_car.getLoadName());
        }
        // update kernel
        if (!ignoreKernelCheckBox.isSelected()) {
            car.setKernel(_car.getKernel());
        }
    }
    return super.updateGroup(list);
}
Also used : RollingStock(jmri.jmrit.operations.rollingstock.RollingStock)

Example 25 with RollingStock

use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.

the class ExportCars method writeFile.

@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "CarManager only provides Car Objects")
public void writeFile(String name) {
    log.debug("writeFile {}", name);
    // This is taken in large part from "Java and XML" page 368
    File file = findFile(name);
    if (file == null) {
        file = new File(name);
    }
    PrintWriter fileOut = null;
    try {
        fileOut = new // NOI18N
        PrintWriter(// NOI18N
        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")), // NOI18N
        true);
    } catch (IOException e) {
        log.error("Can not open export cars CSV file: {}", file.getName());
        return;
    }
    // create header
    String header = Bundle.getMessage("Number") + del + Bundle.getMessage("Road") + del + Bundle.getMessage("Type") + del + Bundle.getMessage("Length") + del + Bundle.getMessage("Weight") + del + Bundle.getMessage("Color") + del + Bundle.getMessage("Owner") + del + Bundle.getMessage("Built") + del + Bundle.getMessage("Location") + del + "-" + del + Bundle.getMessage("Track") + del + Bundle.getMessage("Load") + del + Bundle.getMessage("Kernel") + del + Bundle.getMessage("Moves") + del + Setup.getValueLabel() + del + Bundle.getMessage("Comment") + del + Bundle.getMessage("Miscellaneous") + del + Bundle.getMessage("Extensions");
    fileOut.println(header);
    String line = "";
    String carType;
    String carLoad;
    String carKernel;
    String carLocationName;
    String carTrackName;
    String value;
    String comment;
    String miscellaneous;
    String extensions;
    // store car number, road, type, length, weight, color, owner, built date, location and track
    for (RollingStock rs : _carList) {
        Car car = (Car) rs;
        carType = car.getTypeName();
        if (carType.contains(del)) {
            carType = ESC + car.getTypeName() + ESC;
        }
        carLoad = car.getLoadName();
        if (carLoad.contains(del)) {
            carLoad = ESC + car.getLoadName() + ESC;
        }
        carKernel = car.getKernelName();
        if (carKernel.contains(del)) {
            carKernel = ESC + car.getKernelName() + ESC;
        }
        carLocationName = car.getLocationName();
        if (carLocationName.contains(del)) {
            carLocationName = ESC + car.getLocationName() + ESC;
        }
        carTrackName = car.getTrackName();
        if (carTrackName.contains(del)) {
            carTrackName = ESC + car.getTrackName() + ESC;
        }
        value = car.getValue();
        if (value.contains(del)) {
            value = ESC + car.getValue() + ESC;
        }
        comment = car.getComment();
        if (comment.contains(del)) {
            comment = ESC + car.getComment() + ESC;
        }
        miscellaneous = "";
        if (car.isOutOfService()) {
            miscellaneous = Bundle.getMessage("OutOfService");
        }
        extensions = car.getTypeExtensions();
        line = car.getNumber() + del + car.getRoadName() + del + carType + del + car.getLength() + del + car.getWeight() + del + car.getColor() + del + car.getOwner() + del + car.getBuilt() + del + carLocationName + ",-," + carTrackName + del + carLoad + del + carKernel + del + car.getMoves() + del + value + del + comment + del + miscellaneous + del + extensions;
        fileOut.println(line);
    }
    fileOut.flush();
    fileOut.close();
    log.info("Exported " + _carList.size() + " cars to file " + defaultOperationsFilename());
    JOptionPane.showMessageDialog(null, MessageFormat.format(Bundle.getMessage("ExportedCarsToFile"), new Object[] { _carList.size(), defaultOperationsFilename() }), Bundle.getMessage("ExportComplete"), JOptionPane.INFORMATION_MESSAGE);
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

RollingStock (jmri.jmrit.operations.rollingstock.RollingStock)43 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)13 Car (jmri.jmrit.operations.rollingstock.cars.Car)13 Route (jmri.jmrit.operations.routes.Route)11 RouteLocation (jmri.jmrit.operations.routes.RouteLocation)11 Track (jmri.jmrit.operations.locations.Track)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 Location (jmri.jmrit.operations.locations.Location)5 Engine (jmri.jmrit.operations.rollingstock.engines.Engine)5 Train (jmri.jmrit.operations.trains.Train)5 BufferedWriter (java.io.BufferedWriter)4 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 PrintWriter (java.io.PrintWriter)4 Dimension (java.awt.Dimension)2 JPanel (javax.swing.JPanel)2 XmlFile (jmri.jmrit.XmlFile)2 HardcopyWriter (jmri.util.davidflanagan.HardcopyWriter)2