use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class Train method getTrainWeight.
public int getTrainWeight(RouteLocation routeLocation) {
int weight = 0;
Route route = getRoute();
if (route != null) {
for (RouteLocation rl : route.getLocationsBySequenceList()) {
for (RollingStock rs : EngineManager.instance().getList(this)) {
if (rs.getRouteLocation() == rl) {
weight += rs.getAdjustedWeightTons();
}
if (rs.getRouteDestination() == rl) {
weight += -rs.getAdjustedWeightTons();
}
}
for (RollingStock rs : CarManager.instance().getList(this)) {
Car car = (Car) rs;
if (car.getRouteLocation() == rl) {
// weight depends on car load
weight += car.getAdjustedWeightTons();
}
if (car.getRouteDestination() == rl) {
weight += -car.getAdjustedWeightTons();
}
}
if (rl == routeLocation) {
break;
}
}
}
return weight;
}
use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class TrainEditFrame method checkEngineRoad.
private boolean checkEngineRoad() {
String road = (String) roadEngineBox.getSelectedItem();
String model = (String) modelEngineBox.getSelectedItem();
if (numEnginesBox.getSelectedItem().equals("0") || road.equals(NONE) || !model.equals(NONE)) {
return true;
}
for (RollingStock rs : EngineManager.instance().getList()) {
if (!_train.acceptsTypeName(rs.getTypeName())) {
continue;
}
if (rs.getRoadName().equals(road)) {
return true;
}
}
JOptionPane.showMessageDialog(this, MessageFormat.format(Bundle.getMessage("NoLocoRoad"), new Object[] { road }), MessageFormat.format(Bundle.getMessage("TrainWillNotBuild"), new Object[] { _train.getName() }), JOptionPane.WARNING_MESSAGE);
// couldn't find a loco with the selected road
return false;
}
use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class CarManager method getByTrainDestinationList.
/**
* Provides a very sorted list of cars assigned to the train. Note that this
* isn't the final sort as the cars must be sorted by each location the
* train visits.
*
* The sort priority is as follows:
* <ol>
* <li>Caboose or car with FRED to the end of the list
*
* <li>Passenger cars to the end of the list, but before cabooses or car
* with FRED. Passenger cars have blocking numbers which places them
* relative to each other.
*
* <li>Car's destination (alphabetical by location and track name or by
* track blocking order)
*
* <li>Car's current location (alphabetical by location and track name)
*
* <li>Car's final destination (alphabetical by location and track name)
*
* <li>Car is hazardous (hazardous placed after a non-hazardous car)
* </ol>
* <p>
* Cars in a kernel are placed together by their kernel blocking numbers.
* The kernel's position in the list is based on the lead car in the kernel.
* <p>
*
* If the train is to be blocked by track blocking order, all of the tracks
* at that location need a blocking number greater than 0.
* @param train The selected Train.
*
* @return Ordered list of cars assigned to the train
*/
public List<Car> getByTrainDestinationList(Train train) {
List<RollingStock> byHazard = getByList(getList(train), BY_HAZARD);
List<RollingStock> byFinal = getByList(byHazard, BY_FINAL_DEST);
List<RollingStock> byLocation = getByList(byFinal, BY_LOCATION);
List<RollingStock> byDestination = getByList(byLocation, BY_DESTINATION);
// now place cabooses, cars with FRED, and passenger cars at the rear of the train
List<Car> out = new ArrayList<Car>();
// incremented each time a car is added to the end of the list
int lastCarsIndex = 0;
for (RollingStock rs : byDestination) {
Car car = (Car) rs;
if (car.getKernel() != null && !car.getKernel().isLead(car)) {
// not the lead car, skip for now.
continue;
}
if (!car.isCaboose() && !car.hasFred() && !car.isPassenger()) {
// sort order based on train direction when serving track, low to high if West or North bound trains
if (car.getDestinationTrack() != null && car.getDestinationTrack().getBlockingOrder() > 0) {
for (int j = 0; j < out.size(); j++) {
if (car.getRouteDestination() != null && (car.getRouteDestination().getTrainDirectionString().equals(RouteLocation.WEST_DIR) || car.getRouteDestination().getTrainDirectionString().equals(RouteLocation.NORTH_DIR))) {
if (car.getDestinationTrack().getBlockingOrder() < out.get(j).getDestinationTrack().getBlockingOrder()) {
out.add(j, car);
break;
}
// Train is traveling East or South when setting out the car
} else {
if (car.getDestinationTrack().getBlockingOrder() > out.get(j).getDestinationTrack().getBlockingOrder()) {
out.add(j, car);
break;
}
}
}
}
if (!out.contains(car)) {
out.add(out.size() - lastCarsIndex, car);
}
} else if (car.isCaboose() || car.hasFred()) {
// place at end of list
out.add(car);
lastCarsIndex++;
} else if (car.isPassenger()) {
// block passenger cars at end of list, but before cabooses or car with FRED
int index;
for (index = 0; index < lastCarsIndex; index++) {
Car carTest = out.get(out.size() - 1 - index);
log.debug("Car ({}) has blocking number: {}", carTest.toString(), carTest.getBlocking());
if (carTest.isPassenger() && !carTest.isCaboose() && !carTest.hasFred() && carTest.getBlocking() < car.getBlocking()) {
break;
}
}
out.add(out.size() - index, car);
lastCarsIndex++;
}
// group the cars in the kernel together
if (car.getKernel() != null && car.getKernel().isLead(car)) {
int index = out.indexOf(car);
// already added the lead car to the list
int numberOfCars = 1;
for (Car kcar : car.getKernel().getCars()) {
if (car != kcar) {
// Block cars in kernel
for (int j = 0; j < numberOfCars; j++) {
if (kcar.getBlocking() < out.get(index + j).getBlocking()) {
out.add(index + j, kcar);
break;
}
}
if (!out.contains(kcar)) {
out.add(index + numberOfCars, kcar);
}
numberOfCars++;
if (car.hasFred() || car.isCaboose() || car.isPassenger()) {
// place entire kernel at the end of list
lastCarsIndex++;
}
}
}
}
}
return out;
}
use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class CarEditFrame method saveCar.
private void saveCar(boolean isSave) {
if (roadComboBox.getSelectedItem() == null) {
return;
}
if (_car == null || !_car.getRoadName().equals(roadComboBox.getSelectedItem()) || !_car.getNumber().equals(roadNumberTextField.getText())) {
_car = carManager.newCar((String) roadComboBox.getSelectedItem(), roadNumberTextField.getText());
_car.addPropertyChangeListener(this);
}
if (typeComboBox.getSelectedItem() != null) {
_car.setTypeName((String) typeComboBox.getSelectedItem());
}
if (lengthComboBox.getSelectedItem() != null) {
_car.setLength((String) lengthComboBox.getSelectedItem());
}
if (colorComboBox.getSelectedItem() != null) {
_car.setColor((String) colorComboBox.getSelectedItem());
}
try {
_car.setWeight(NumberFormat.getNumberInstance().parse(weightTextField.getText()).toString());
} catch (ParseException e1) {
}
_car.setWeightTons(weightTonsTextField.getText());
// ask if all cars of this type should be passenger
if (isSave && _car.isPassenger() ^ passengerCheckBox.isSelected()) {
if (JOptionPane.showConfirmDialog(this, MessageFormat.format(passengerCheckBox.isSelected() ? Bundle.getMessage("carModifyTypePassenger") : Bundle.getMessage("carRemoveTypePassenger"), new Object[] { _car.getTypeName() }), MessageFormat.format(Bundle.getMessage("carModifyAllType"), new Object[] { _car.getTypeName() }), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
// for all cars of this type
for (RollingStock rs : carManager.getList()) {
Car c = (Car) rs;
if (c.getTypeName().equals(_car.getTypeName())) {
c.setPassenger(passengerCheckBox.isSelected());
}
}
}
}
_car.setPassenger(passengerCheckBox.isSelected());
int blocking = 0;
try {
blocking = Integer.parseInt(blockingTextField.getText());
// only allow numbers between 0 and 100
if (blocking < 0 || blocking > 100) {
blocking = 0;
}
} catch (Exception e) {
log.warn("Blocking must be a number between 0 and 100");
}
// ask if blocking order should be the same
if (isSave && _car.getKernel() == null && passengerCheckBox.isSelected() && _car.getBlocking() != blocking) {
if (JOptionPane.showConfirmDialog(this, MessageFormat.format(Bundle.getMessage("carChangeBlocking"), new Object[] { blocking, _car.getTypeName() }), MessageFormat.format(Bundle.getMessage("carModifyAllType"), new Object[] { _car.getTypeName() }), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
// for all cars of this type
for (RollingStock rs : carManager.getList()) {
Car c = (Car) rs;
if (c.isPassenger() && c.getTypeName().equals(_car.getTypeName())) {
c.setBlocking(blocking);
}
}
}
}
_car.setBlocking(blocking);
// ask if all cars of this type should be caboose
if (isSave && _car.isCaboose() ^ cabooseCheckBox.isSelected()) {
if (JOptionPane.showConfirmDialog(this, MessageFormat.format(cabooseCheckBox.isSelected() ? Bundle.getMessage("carModifyTypeCaboose") : Bundle.getMessage("carRemoveTypeCaboose"), new Object[] { _car.getTypeName() }), MessageFormat.format(Bundle.getMessage("carModifyAllType"), new Object[] { _car.getTypeName() }), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
// go through the entire list and change the caboose setting for all cars of this type
for (RollingStock rs : carManager.getList()) {
Car c = (Car) rs;
if (c.getTypeName().equals(_car.getTypeName())) {
c.setCaboose(cabooseCheckBox.isSelected());
}
}
}
}
_car.setCaboose(cabooseCheckBox.isSelected());
// ask if all cars of this type should be utility
if (isSave && _car.isUtility() ^ utilityCheckBox.isSelected()) {
if (JOptionPane.showConfirmDialog(this, MessageFormat.format(utilityCheckBox.isSelected() ? Bundle.getMessage("carModifyTypeUtility") : Bundle.getMessage("carRemoveTypeUtility"), new Object[] { _car.getTypeName() }), MessageFormat.format(Bundle.getMessage("carModifyAllType"), new Object[] { _car.getTypeName() }), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
// go through the entire list and change the utility for all cars of this type
for (RollingStock rs : carManager.getList()) {
Car c = (Car) rs;
if (c.getTypeName().equals(_car.getTypeName())) {
c.setUtility(utilityCheckBox.isSelected());
}
}
}
}
_car.setUtility(utilityCheckBox.isSelected());
// ask if all cars of this type should be hazardous
if (isSave && _car.isHazardous() ^ hazardousCheckBox.isSelected()) {
if (JOptionPane.showConfirmDialog(this, MessageFormat.format(hazardousCheckBox.isSelected() ? Bundle.getMessage("carModifyTypeHazardous") : Bundle.getMessage("carRemoveTypeHazardous"), new Object[] { _car.getTypeName() }), MessageFormat.format(Bundle.getMessage("carModifyAllType"), new Object[] { _car.getTypeName() }), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
// go through the entire list and change the hazardous setting for all cars of this type
for (RollingStock rs : carManager.getList()) {
Car c = (Car) rs;
if (c.getTypeName().equals(_car.getTypeName())) {
c.setHazardous(hazardousCheckBox.isSelected());
}
}
}
}
_car.setHazardous(hazardousCheckBox.isSelected());
_car.setFred(fredCheckBox.isSelected());
_car.setBuilt(builtTextField.getText());
if (ownerComboBox.getSelectedItem() != null) {
_car.setOwner((String) ownerComboBox.getSelectedItem());
}
if (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 caboose make lead
if (_car.hasFred() || _car.isCaboose()) {
_car.getKernel().setLead(_car);
}
_car.setBlocking(_car.getKernel().getSize());
}
}
if (loadComboBox.getSelectedItem() != null && !_car.getLoadName().equals(loadComboBox.getSelectedItem())) {
_car.setLoadName((String) loadComboBox.getSelectedItem());
// check to see if car is part of kernel, and ask if all the other cars in the kernel should be changed
if (_car.getKernel() != null) {
List<Car> cars = _car.getKernel().getCars();
if (cars.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) {
// go through the entire list and change the loads for all cars
for (Car car : cars) {
if (CarLoads.instance().containsName(car.getTypeName(), _car.getLoadName())) {
car.setLoadName(_car.getLoadName());
}
}
}
}
}
}
_car.setComment(commentTextField.getText());
_car.setValue(valueTextField.getText());
// save the IdTag for this car
IdTag idTag = (IdTag) rfidComboBox.getSelectedItem();
if (idTag != null) {
_car.setRfid(idTag.toString());
}
autoTrackCheckBox.setEnabled(true);
// update blocking
pBlocking.setVisible(passengerCheckBox.isSelected() || _car.getKernel() != null);
blockingTextField.setText(Integer.toString(_car.getBlocking()));
if (locationBox.getSelectedItem() != null && trackLocationBox.getSelectedItem() == null) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("rsFullySelect"), Bundle.getMessage("rsCanNotLoc"), JOptionPane.ERROR_MESSAGE);
} else {
// update location only if it has changed
if (_car.getLocation() == null || !_car.getLocation().equals(locationBox.getSelectedItem()) || _car.getTrack() == null || !_car.getTrack().equals(trackLocationBox.getSelectedItem())) {
setLocation(_car);
// is this car part of a kernel?
if (_car.getKernel() != null) {
List<Car> cars = _car.getKernel().getCars();
if (cars.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) {
// go through the entire list and change the location for all cars
for (Car car : cars) {
if (car != _car) {
setLocation(car);
}
}
}
}
}
}
}
}
use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class CarManager method getCarsLocationUnknown.
public List<Car> getCarsLocationUnknown() {
List<Car> mias = new ArrayList<Car>();
List<RollingStock> cars = getByIdList();
for (RollingStock rs : cars) {
Car car = (Car) rs;
if (car.isLocationUnknown()) {
// return unknown location car
mias.add(car);
}
}
return mias;
}
Aggregations