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));
}
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);
}
}
}
}
}
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;
}
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);
}
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);
}
Aggregations