001package jmri.jmrit.operations.rollingstock.cars;
002
003import java.beans.PropertyChangeEvent;
004import java.util.ArrayList;
005import java.util.List;
006
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010import jmri.InstanceManager;
011import jmri.jmrit.operations.locations.*;
012import jmri.jmrit.operations.locations.schedules.Schedule;
013import jmri.jmrit.operations.locations.schedules.ScheduleItem;
014import jmri.jmrit.operations.rollingstock.RollingStock;
015import jmri.jmrit.operations.routes.RouteLocation;
016import jmri.jmrit.operations.trains.schedules.TrainSchedule;
017import jmri.jmrit.operations.trains.schedules.TrainScheduleManager;
018import jmri.jmrit.operations.trains.trainbuilder.TrainCommon;
019
020/**
021 * Represents a car on the layout
022 *
023 * @author Daniel Boudreau Copyright (C) 2008, 2009, 2010, 2012, 2013, 2014,
024 *         2015, 2023, 2025
025 */
026public class Car extends RollingStock {
027
028    CarLoads carLoads = InstanceManager.getDefault(CarLoads.class);
029
030    protected boolean _passenger = false;
031    protected boolean _hazardous = false;
032    protected boolean _caboose = false;
033    protected boolean _fred = false;
034    protected boolean _utility = false;
035    protected boolean _loadGeneratedByStaging = false;
036    protected Kernel _kernel = null;
037    protected String _loadName = carLoads.getDefaultEmptyName();
038    protected int _wait = 0;
039
040    protected Location _rweDestination = null; // return when empty destination
041    protected Track _rweDestTrack = null; // return when empty track
042    protected String _rweLoadName = carLoads.getDefaultEmptyName();
043
044    protected Location _rwlDestination = null; // return when loaded destination
045    protected Track _rwlDestTrack = null; // return when loaded track
046    protected String _rwlLoadName = carLoads.getDefaultLoadName();
047
048    // schedule items
049    protected String _scheduleId = NONE; // the schedule id assigned to this car
050    protected String _nextLoadName = NONE; // next load by schedule
051    protected Location _finalDestination = null; 
052    protected Track _finalDestTrack = null; // final track by schedule or router
053    protected Location _previousFinalDestination = null;
054    protected Track _previousFinalDestTrack = null;
055    protected String _previousScheduleId = NONE;
056    protected String _pickupScheduleId = NONE;
057
058    protected String _routePath = NONE;
059
060    public static final String EXTENSION_REGEX = " ";
061    public static final String CABOOSE_EXTENSION = Bundle.getMessage("(C)");
062    public static final String FRED_EXTENSION = Bundle.getMessage("(F)");
063    public static final String PASSENGER_EXTENSION = Bundle.getMessage("(P)");
064    public static final String UTILITY_EXTENSION = Bundle.getMessage("(U)");
065    public static final String HAZARDOUS_EXTENSION = Bundle.getMessage("(H)");
066    public static final String CLONE = TrainCommon.HYPHEN + "(Clone)"; // NOI18N
067    // parentheses are special chars
068    public static final String CLONE_REGEX = TrainCommon.HYPHEN + "\\(Clone\\)"; // NOI18N
069
070    public static final String LOAD_CHANGED_PROPERTY = "Car load changed"; // NOI18N
071    public static final String RWE_LOAD_CHANGED_PROPERTY = "Car RWE load changed"; // NOI18N
072    public static final String RWL_LOAD_CHANGED_PROPERTY = "Car RWL load changed"; // NOI18N
073    public static final String WAIT_CHANGED_PROPERTY = "Car wait changed"; // NOI18N
074    public static final String FINAL_DESTINATION_CHANGED_PROPERTY = "Car final destination changed"; // NOI18N
075    public static final String FINAL_DESTINATION_TRACK_CHANGED_PROPERTY = "Car final destination track changed"; // NOI18N
076    public static final String RETURN_WHEN_EMPTY_CHANGED_PROPERTY = "Car return when empty changed"; // NOI18N
077    public static final String RETURN_WHEN_LOADED_CHANGED_PROPERTY = "Car return when loaded changed"; // NOI18N
078    public static final String SCHEDULE_ID_CHANGED_PROPERTY = "car schedule id changed"; // NOI18N
079    public static final String KERNEL_NAME_CHANGED_PROPERTY = "kernel name changed"; // NOI18N
080
081    public Car() {
082        super();
083        loaded = true;
084    }
085
086    public Car(String road, String number) {
087        super(road, number);
088        loaded = true;
089        log.debug("New car ({} {})", road, number);
090        addPropertyChangeListeners();
091    }
092
093    public Car copy() {
094        Car car = new Car();
095        car.setBuilt(getBuilt());
096        car.setColor(getColor());
097        car.setLength(getLength());
098        car.setLoadName(getLoadName());
099        car.setWeightTons(getWeightTons());
100        car.setReturnWhenEmptyLoadName(getReturnWhenEmptyLoadName());
101        car.setReturnWhenLoadedLoadName(getReturnWhenLoadedLoadName());
102        car.setNumber(getNumber());
103        car.setOwnerName(getOwnerName());
104        car.setRoadName(getRoadName());
105        car.setTypeName(getTypeName());
106        car.setComment(getComment());
107        car.setCarHazardous(isCarHazardous());
108        car.setCaboose(isCaboose());
109        car.setFred(hasFred());
110        car.setPassenger(isPassenger());
111        car.setBlocking(getBlocking());
112        car.setLastTrain(getLastTrain());
113        car.setLastDate(getLastDate());
114        car.setLastLocationId(getLastLocationId());
115        car.setLastTrackId(getLastTrackId());
116        car.setLoadGeneratedFromStaging(isLoadGeneratedFromStaging());
117        car.setDivision(getDivision());
118        car.loaded = true;
119        return car;
120    }
121
122    public void setCarHazardous(boolean hazardous) {
123        boolean old = _hazardous;
124        _hazardous = hazardous;
125        if (!old == hazardous) {
126            setDirtyAndFirePropertyChange("car hazardous", old ? "true" : "false", hazardous ? "true" : "false"); // NOI18N
127        }
128    }
129
130    public boolean isCarHazardous() {
131        return _hazardous;
132    }
133
134    public boolean isCarLoadHazardous() {
135        return carLoads.isHazardous(getTypeName(), getLoadName());
136    }
137
138    /**
139     * Used to determine if the car is hazardous or the car's load is hazardous.
140     * 
141     * @return true if the car or car's load is hazardous.
142     */
143    public boolean isHazardous() {
144        return isCarHazardous() || isCarLoadHazardous();
145    }
146
147    public void setPassenger(boolean passenger) {
148        boolean old = _passenger;
149        _passenger = passenger;
150        if (!old == passenger) {
151            setDirtyAndFirePropertyChange("car passenger", old ? "true" : "false", passenger ? "true" : "false"); // NOI18N
152        }
153    }
154
155    public boolean isPassenger() {
156        return _passenger;
157    }
158
159    public void setFred(boolean fred) {
160        boolean old = _fred;
161        _fred = fred;
162        if (!old == fred) {
163            setDirtyAndFirePropertyChange("car has fred", old ? "true" : "false", fred ? "true" : "false"); // NOI18N
164        }
165    }
166
167    /**
168     * Used to determine if car has FRED (Flashing Rear End Device).
169     *
170     * @return true if car has FRED.
171     */
172    public boolean hasFred() {
173        return _fred;
174    }
175
176    public void setLoadName(String load) {
177        String old = _loadName;
178        _loadName = load;
179        if (!old.equals(load)) {
180            setDirtyAndFirePropertyChange(LOAD_CHANGED_PROPERTY, old, load);
181        }
182    }
183
184    /**
185     * The load name assigned to this car.
186     *
187     * @return The load name assigned to this car.
188     */
189    public String getLoadName() {
190        return _loadName;
191    }
192
193    public void setReturnWhenEmptyLoadName(String load) {
194        String old = _rweLoadName;
195        _rweLoadName = load;
196        if (!old.equals(load)) {
197            setDirtyAndFirePropertyChange(RWE_LOAD_CHANGED_PROPERTY, old, load);
198        }
199    }
200
201    public String getReturnWhenEmptyLoadName() {
202        return _rweLoadName;
203    }
204
205    public void setReturnWhenLoadedLoadName(String load) {
206        String old = _rwlLoadName;
207        _rwlLoadName = load;
208        if (!old.equals(load)) {
209            setDirtyAndFirePropertyChange(RWL_LOAD_CHANGED_PROPERTY, old, load);
210        }
211    }
212
213    public String getReturnWhenLoadedLoadName() {
214        return _rwlLoadName;
215    }
216
217    /**
218     * Gets the car's load's priority.
219     * 
220     * @return The car's load priority.
221     */
222    public String getLoadPriority() {
223        return (carLoads.getPriority(getTypeName(), getLoadName()));
224    }
225
226    /**
227     * Gets the car load's type, empty or load.
228     *
229     * @return type empty or type load
230     */
231    public String getLoadType() {
232        return (carLoads.getLoadType(getTypeName(), getLoadName()));
233    }
234
235    public String getPickupComment() {
236        return carLoads.getPickupComment(getTypeName(), getLoadName());
237    }
238
239    public String getDropComment() {
240        return carLoads.getDropComment(getTypeName(), getLoadName());
241    }
242
243    public void setLoadGeneratedFromStaging(boolean fromStaging) {
244        _loadGeneratedByStaging = fromStaging;
245    }
246
247    public boolean isLoadGeneratedFromStaging() {
248        return _loadGeneratedByStaging;
249    }
250
251    /**
252     * Used to keep track of which item in a schedule was used for this car.
253     * 
254     * @param id The ScheduleItem id for this car.
255     */
256    public void setScheduleItemId(String id) {
257        log.debug("Set schedule item id ({}) for car ({})", id, toString());
258        String old = _scheduleId;
259        _scheduleId = id;
260        if (!old.equals(id)) {
261            setDirtyAndFirePropertyChange(SCHEDULE_ID_CHANGED_PROPERTY, old, id);
262        }
263    }
264
265    public String getScheduleItemId() {
266        return _scheduleId;
267    }
268
269    public ScheduleItem getScheduleItem(Track track) {
270        ScheduleItem si = null;
271        // arrived at spur?
272        if (track != null && track.isSpur() && !getScheduleItemId().equals(NONE)) {
273            Schedule sch = track.getSchedule();
274            if (sch == null) {
275                log.error("Schedule null for car ({}) at spur ({})", toString(), track.getName());
276            } else {
277                si = sch.getItemById(getScheduleItemId());
278            }
279        }
280        return si;
281    }
282
283    /**
284     * Only here for backwards compatibility before version 5.1.4. The next load
285     * name for this car. Normally set by a schedule.
286     * 
287     * @param load the next load name.
288     */
289    public void setNextLoadName(String load) {
290        String old = _nextLoadName;
291        _nextLoadName = load;
292        if (!old.equals(load)) {
293            setDirtyAndFirePropertyChange(LOAD_CHANGED_PROPERTY, old, load);
294        }
295    }
296
297    public String getNextLoadName() {
298        return _nextLoadName;
299    }
300
301    @Override
302    public String getWeightTons() {
303        String weight = super.getWeightTons();
304        if (!_weightTons.equals(DEFAULT_WEIGHT)) {
305            return weight;
306        }
307        if (!isCaboose() && !isPassenger()) {
308            return weight;
309        }
310        // .9 tons/foot for caboose and passenger cars
311        try {
312            weight = Integer.toString((int) (Double.parseDouble(getLength()) * .9));
313        } catch (Exception e) {
314            log.debug("Car ({}) length not set for caboose or passenger car", toString());
315        }
316        return weight;
317    }
318
319    /**
320     * Returns a car's weight adjusted for load. An empty car's weight is 1/3
321     * the car's loaded weight.
322     */
323    @Override
324    public int getAdjustedWeightTons() {
325        int weightTons = 0;
326        try {
327            // get loaded weight
328            weightTons = Integer.parseInt(getWeightTons());
329            // adjust for empty weight if car is empty, 1/3 of loaded weight
330            if (!isCaboose() && !isPassenger() && getLoadType().equals(CarLoad.LOAD_TYPE_EMPTY)) {
331                weightTons = weightTons / 3;
332            }
333        } catch (NumberFormatException e) {
334            log.debug("Car ({}) weight not set", toString());
335        }
336        return weightTons;
337    }
338
339    public void setWait(int count) {
340        int old = _wait;
341        _wait = count;
342        if (old != count) {
343            setDirtyAndFirePropertyChange(WAIT_CHANGED_PROPERTY, old, count);
344        }
345    }
346
347    public int getWait() {
348        return _wait;
349    }
350
351    /**
352     * Sets when this car will be picked up (day of the week)
353     *
354     * @param id See TrainSchedule.java
355     */
356    public void setPickupScheduleId(String id) {
357        String old = _pickupScheduleId;
358        _pickupScheduleId = id;
359        if (!old.equals(id)) {
360            setDirtyAndFirePropertyChange("car pickup schedule changes", old, id); // NOI18N
361        }
362    }
363
364    public String getPickupScheduleId() {
365        return _pickupScheduleId;
366    }
367
368    public String getPickupScheduleName() {
369        if (getTrain() != null) {
370            return getPickupTime();
371        }
372        TrainSchedule sch = InstanceManager.getDefault(TrainScheduleManager.class)
373                .getScheduleById(getPickupScheduleId());
374        if (sch != null) {
375            return sch.getName();
376        }
377        return NONE;
378    }
379
380    /**
381     * Sets the final destination for a car.
382     *
383     * @param destination The final destination for this car.
384     */
385    public void setFinalDestination(Location destination) {
386        Location old = _finalDestination;
387        if (old != null) {
388            old.removePropertyChangeListener(this);
389        }
390        _finalDestination = destination;
391        if (_finalDestination != null) {
392            _finalDestination.addPropertyChangeListener(this);
393        }
394        if ((old != null && !old.equals(destination)) || (destination != null && !destination.equals(old))) {
395            setRoutePath(NONE);
396            setDirtyAndFirePropertyChange(FINAL_DESTINATION_CHANGED_PROPERTY, old, destination);
397        }
398    }
399
400    public Location getFinalDestination() {
401        return _finalDestination;
402    }
403    
404    public String getFinalDestinationName() {
405        if (getFinalDestination() != null) {
406            return getFinalDestination().getName();
407        }
408        return NONE;
409    }
410    
411    public String getSplitFinalDestinationName() {
412        return TrainCommon.splitString(getFinalDestinationName());
413    }
414
415    public void setFinalDestinationTrack(Track track) {
416        Track old = _finalDestTrack;
417        _finalDestTrack = track;
418        if ((old != null && !old.equals(track)) || (track != null && !track.equals(old))) {
419            if (old != null) {
420                old.removePropertyChangeListener(this);
421                old.deleteReservedInRoute(this);
422            }
423            if (_finalDestTrack != null) {
424                _finalDestTrack.addReservedInRoute(this);
425                _finalDestTrack.addPropertyChangeListener(this);
426            }
427            setDirtyAndFirePropertyChange(FINAL_DESTINATION_TRACK_CHANGED_PROPERTY, old, track);
428        }
429    }
430
431    public Track getFinalDestinationTrack() {
432        return _finalDestTrack;
433    }
434
435    public String getFinalDestinationTrackName() {
436        if (getFinalDestinationTrack() != null) {
437            return getFinalDestinationTrack().getName();
438        }
439        return NONE;
440    }
441    
442    public String getSplitFinalDestinationTrackName() {
443        return TrainCommon.splitString(getFinalDestinationTrackName());
444    }
445
446    public void setPreviousFinalDestination(Location location) {
447        _previousFinalDestination = location;
448    }
449
450    public Location getPreviousFinalDestination() {
451        return _previousFinalDestination;
452    }
453
454    public String getPreviousFinalDestinationName() {
455        if (getPreviousFinalDestination() != null) {
456            return getPreviousFinalDestination().getName();
457        }
458        return NONE;
459    }
460
461    public void setPreviousFinalDestinationTrack(Track track) {
462        _previousFinalDestTrack = track;
463    }
464
465    public Track getPreviousFinalDestinationTrack() {
466        return _previousFinalDestTrack;
467    }
468
469    public String getPreviousFinalDestinationTrackName() {
470        if (getPreviousFinalDestinationTrack() != null) {
471            return getPreviousFinalDestinationTrack().getName();
472        }
473        return NONE;
474    }
475
476    public void setPreviousScheduleId(String id) {
477        _previousScheduleId = id;
478    }
479
480    public String getPreviousScheduleId() {
481        return _previousScheduleId;
482    }
483
484    public void setReturnWhenEmptyDestination(Location destination) {
485        Location old = _rweDestination;
486        _rweDestination = destination;
487        if ((old != null && !old.equals(destination)) || (destination != null && !destination.equals(old))) {
488            setDirtyAndFirePropertyChange(RETURN_WHEN_EMPTY_CHANGED_PROPERTY, null, null);
489        }
490    }
491
492    public Location getReturnWhenEmptyDestination() {
493        return _rweDestination;
494    }
495
496    public String getReturnWhenEmptyDestinationName() {
497        if (getReturnWhenEmptyDestination() != null) {
498            return getReturnWhenEmptyDestination().getName();
499        }
500        return NONE;
501    }
502    
503    public String getSplitReturnWhenEmptyDestinationName() {
504        return TrainCommon.splitString(getReturnWhenEmptyDestinationName());
505    }
506    
507    public void setReturnWhenEmptyDestTrack(Track track) {
508        Track old = _rweDestTrack;
509        _rweDestTrack = track;
510        if ((old != null && !old.equals(track)) || (track != null && !track.equals(old))) {
511            setDirtyAndFirePropertyChange(RETURN_WHEN_EMPTY_CHANGED_PROPERTY, null, null);
512        }
513    }
514
515    public Track getReturnWhenEmptyDestTrack() {
516        return _rweDestTrack;
517    }
518
519    public String getReturnWhenEmptyDestTrackName() {
520        if (getReturnWhenEmptyDestTrack() != null) {
521            return getReturnWhenEmptyDestTrack().getName();
522        }
523        return NONE;
524    }
525    
526    public String getSplitReturnWhenEmptyDestinationTrackName() {
527        return TrainCommon.splitString(getReturnWhenEmptyDestTrackName());
528    }
529
530    public void setReturnWhenLoadedDestination(Location destination) {
531        Location old = _rwlDestination;
532        _rwlDestination = destination;
533        if ((old != null && !old.equals(destination)) || (destination != null && !destination.equals(old))) {
534            setDirtyAndFirePropertyChange(RETURN_WHEN_LOADED_CHANGED_PROPERTY, null, null);
535        }
536    }
537
538    public Location getReturnWhenLoadedDestination() {
539        return _rwlDestination;
540    }
541
542    public String getReturnWhenLoadedDestinationName() {
543        if (getReturnWhenLoadedDestination() != null) {
544            return getReturnWhenLoadedDestination().getName();
545        }
546        return NONE;
547    }
548
549    public void setReturnWhenLoadedDestTrack(Track track) {
550        Track old = _rwlDestTrack;
551        _rwlDestTrack = track;
552        if ((old != null && !old.equals(track)) || (track != null && !track.equals(old))) {
553            setDirtyAndFirePropertyChange(RETURN_WHEN_LOADED_CHANGED_PROPERTY, null, null);
554        }
555    }
556
557    public Track getReturnWhenLoadedDestTrack() {
558        return _rwlDestTrack;
559    }
560
561    public String getReturnWhenLoadedDestTrackName() {
562        if (getReturnWhenLoadedDestTrack() != null) {
563            return getReturnWhenLoadedDestTrack().getName();
564        }
565        return NONE;
566    }
567
568    /**
569     * Used to determine is car has been given a Return When Loaded (RWL)
570     * address or custom load
571     * 
572     * @return true if car has RWL
573     */
574    protected boolean isRwlEnabled() {
575        if (!getReturnWhenLoadedLoadName().equals(carLoads.getDefaultLoadName()) ||
576                getReturnWhenLoadedDestination() != null) {
577            return true;
578        }
579        return false;
580    }
581
582    public void setRoutePath(String routePath) {
583        String old = _routePath;
584        _routePath = routePath;
585        if (!old.equals(routePath)) {
586            setDirtyAndFirePropertyChange("Route path change", old, routePath);
587        }
588    }
589
590    public String getRoutePath() {
591        return _routePath;
592    }
593
594    public void setCaboose(boolean caboose) {
595        boolean old = _caboose;
596        _caboose = caboose;
597        if (!old == caboose) {
598            setDirtyAndFirePropertyChange("car is caboose", old ? "true" : "false", caboose ? "true" : "false"); // NOI18N
599        }
600    }
601
602    public boolean isCaboose() {
603        return _caboose;
604    }
605
606    public void setUtility(boolean utility) {
607        boolean old = _utility;
608        _utility = utility;
609        if (!old == utility) {
610            setDirtyAndFirePropertyChange("car is utility", old ? "true" : "false", utility ? "true" : "false"); // NOI18N
611        }
612    }
613
614    public boolean isUtility() {
615        return _utility;
616    }
617
618    /**
619     * Used to determine if car is performing a local move. A local move is when
620     * a car is moved to a different track at the same location.
621     * 
622     * @return true if local move
623     */
624    public boolean isLocalMove() {
625        if (getTrain() == null && getLocation() != null) {
626            return getSplitLocationName().equals(getSplitDestinationName());
627        }
628        if (getRouteLocation() == null || getRouteDestination() == null) {
629            return false;
630        }
631        if (getRouteLocation().equals(getRouteDestination()) && getTrack() != null) {
632            return true;
633        }
634        if (getTrain().isLocalSwitcher() &&
635                getRouteLocation().getSplitName()
636                        .equals(getRouteDestination().getSplitName()) &&
637                getTrack() != null) {
638            return true;
639        }
640        // look for sequential locations with the "same" name
641        if (getRouteLocation().getSplitName().equals(
642                getRouteDestination().getSplitName()) && getTrain().getRoute() != null) {
643            boolean foundRl = false;
644            for (RouteLocation rl : getTrain().getRoute().getLocationsBySequenceList()) {
645                if (foundRl) {
646                    if (getRouteDestination().getSplitName()
647                            .equals(rl.getSplitName())) {
648                        // user can specify the "same" location two more more
649                        // times in a row
650                        if (getRouteDestination() != rl) {
651                            continue;
652                        } else {
653                            return true;
654                        }
655                    } else {
656                        return false;
657                    }
658                }
659                if (getRouteLocation().equals(rl)) {
660                    foundRl = true;
661                }
662            }
663        }
664        return false;
665    }
666
667    /**
668     * A kernel is a group of cars that are switched as a unit.
669     * 
670     * @param kernel The assigned Kernel for this car.
671     */
672    public void setKernel(Kernel kernel) {
673        if (_kernel == kernel) {
674            return;
675        }
676        String old = "";
677        if (_kernel != null) {
678            old = _kernel.getName();
679            _kernel.delete(this);
680        }
681        _kernel = kernel;
682        String newName = "";
683        if (_kernel != null) {
684            _kernel.add(this);
685            newName = _kernel.getName();
686        }
687        if (!old.equals(newName)) {
688            setDirtyAndFirePropertyChange(KERNEL_NAME_CHANGED_PROPERTY, old, newName); // NOI18N
689        }
690    }
691
692    public Kernel getKernel() {
693        return _kernel;
694    }
695
696    public String getKernelName() {
697        if (_kernel != null) {
698            return _kernel.getName();
699        }
700        return NONE;
701    }
702
703    /**
704     * Used to determine if car is lead car in a kernel
705     * 
706     * @return true if lead car in a kernel
707     */
708    public boolean isLead() {
709        if (getKernel() != null) {
710            return getKernel().isLead(this);
711        }
712        return false;
713    }
714
715    /**
716     * Updates all cars in a kernel. After the update, the cars will all have
717     * the same final destination, load, and route path.
718     */
719    public void updateKernel() {
720        if (isLead()) {
721            for (Car car : getKernel().getCars()) {
722                if (car != this) {
723                    car.setScheduleItemId(getScheduleItemId());
724                    car.setFinalDestination(getFinalDestination());
725                    car.setFinalDestinationTrack(getFinalDestinationTrack());
726                    car.setLoadGeneratedFromStaging(isLoadGeneratedFromStaging());
727                    car.setRoutePath(getRoutePath());
728                    car.setWait(getWait());
729                    if (carLoads.containsName(car.getTypeName(), getLoadName())) {
730                        car.setLoadName(getLoadName());
731                    } else {
732                        updateKernelCarCustomLoad(car);
733                    }
734                }
735            }
736        }
737    }
738
739    /**
740     * The non-lead car in a kernel can't use the custom load of the lead car.
741     * Determine if car has custom loads, and if the departure and arrival
742     * tracks allows one of the custom loads.
743     * 
744     * @param car the non-lead car in a kernel
745     */
746    private void updateKernelCarCustomLoad(Car car) {
747        // only update car's load if departing staging or spur
748        if (getTrack() != null) {
749            if (getTrack().isStaging() || getTrack().isSpur()) {
750                List<String> carLoadNames = carLoads.getNames(car.getTypeName());
751                List<String> okLoadNames = new ArrayList<>();
752                for (String name : carLoadNames) {
753                    if (getTrack().isLoadNameAndCarTypeShipped(name, car.getTypeName())) {
754                        if (getTrain() != null && !getTrain().isLoadNameAccepted(name, car.getTypeName())) {
755                            continue; // load not carried by train
756                        }
757                        if (getFinalDestinationTrack() != null &&
758                                getDestinationTrack() != null &&
759                                !getDestinationTrack().isSpur()) {
760                            if (getFinalDestinationTrack().isLoadNameAndCarTypeAccepted(name, car.getTypeName())) {
761                                okLoadNames.add(name);
762                            }
763                        } else if (getDestinationTrack() != null &&
764                                getDestinationTrack().isLoadNameAndCarTypeAccepted(name, car.getTypeName())) {
765                            okLoadNames.add(name);
766                        }
767                    }
768                }
769                // remove default names leaving only custom
770                okLoadNames.remove(carLoads.getDefaultEmptyName());
771                okLoadNames.remove(carLoads.getDefaultLoadName());
772                // randomly pick one of the available car loads
773                if (okLoadNames.size() > 0) {
774                    int rnd = (int) (Math.random() * okLoadNames.size());
775                    car.setLoadName(okLoadNames.get(rnd));
776                } else {
777                    log.debug("Car ({}) in kernel ({}) leaving staging ({}, {}) with load ({})", car.toString(),
778                            getKernelName(), getLocationName(), getTrackName(), car.getLoadName());
779                }
780            }
781        }
782    }
783
784    /**
785     * Returns the car length or the length of the car's kernel including
786     * couplers.
787     * 
788     * @return length of car or kernel
789     */
790    public int getTotalKernelLength() {
791        if (getKernel() != null) {
792            return getKernel().getTotalLength();
793        }
794        return getTotalLength();
795    }
796
797    /**
798     * Used to determine if a car can be set out at a destination (location).
799     * Track is optional. In addition to all of the tests that checkDestination
800     * performs, spurs with schedules are also checked.
801     *
802     * @return status OKAY, TYPE, ROAD, LENGTH, ERROR_TRACK, CAPACITY, SCHEDULE,
803     *         CUSTOM
804     */
805    @Override
806    public String checkDestination(Location destination, Track track) {
807        String status = super.checkDestination(destination, track);
808        if (!status.equals(Track.OKAY) && !status.startsWith(Track.LENGTH)) {
809            return status;
810        }
811        // now check to see if the track has a schedule
812        if (track == null) {
813            return status;
814        }
815        String statusSchedule = track.checkSchedule(this);
816        if (status.startsWith(Track.LENGTH) && statusSchedule.equals(Track.OKAY)) {
817            return status;
818        }
819        return statusSchedule;
820    }
821
822    /**
823     * Sets the car's destination on the layout
824     *
825     * @param track (yard, spur, staging, or interchange track)
826     * @return "okay" if successful, "type" if the rolling stock's type isn't
827     *         acceptable, or "length" if the rolling stock length didn't fit,
828     *         or Schedule if the destination will not accept the car because
829     *         the spur has a schedule and the car doesn't meet the schedule
830     *         requirements. Also changes the car load status when the car
831     *         reaches its destination.
832     */
833    @Override
834    public String setDestination(Location destination, Track track) {
835        return setDestination(destination, track, !Car.FORCE);
836    }
837
838    /**
839     * Sets the car's destination on the layout
840     *
841     * @param track (yard, spur, staging, or interchange track)
842     * @param force when true ignore track length, type, and road when setting
843     *              destination
844     * @return "okay" if successful, "type" if the rolling stock's type isn't
845     *         acceptable, or "length" if the rolling stock length didn't fit,
846     *         or Schedule if the destination will not accept the car because
847     *         the spur has a schedule and the car doesn't meet the schedule
848     *         requirements. Also changes the car load status when the car
849     *         reaches its destination. Removes car if clone.
850     */
851    @Override
852    public String setDestination(Location destination, Track track, boolean force) {
853        // save destination name and track in case car has reached its
854        // destination
855        String destinationName = getDestinationName();
856        Track destinationTrack = getDestinationTrack();
857        String status = super.setDestination(destination, track, force);
858        // return if not Okay
859        if (!status.equals(Track.OKAY)) {
860            return status;
861        }
862        // now check to see if the track has a schedule
863        if (track != null && destinationTrack != track && loaded) {
864            status = track.scheduleNext(this);
865            if (!status.equals(Track.OKAY)) {
866                return status;
867            }
868        }
869        // done?
870        if (destinationName.equals(NONE) || (destination != null) || getTrain() == null) {
871            return status;
872        }
873        // car was in a train and has been dropped off, update load, RWE could
874        // set a new final destination
875        if (isClone()) {
876            // destroy clone
877            InstanceManager.getDefault(KernelManager.class).deleteKernel(getKernelName());
878            InstanceManager.getDefault(CarManager.class).deregister(this);
879        } else {
880            loadNext(destinationTrack);
881        }
882        return status;
883    }
884
885    /**
886     * Called when setting a car's destination to this spur. Loads the car with
887     * a final destination which is the ship address for the schedule item.
888     * 
889     * @param scheduleItem The schedule item to be applied this this car
890     */
891    public void loadCarFinalDestination(ScheduleItem scheduleItem) {
892        if (scheduleItem != null) {
893            // set the car's final destination and track
894            setFinalDestination(scheduleItem.getDestination());
895            setFinalDestinationTrack(scheduleItem.getDestinationTrack());
896            // set all cars in kernel same final destination
897            updateKernel();
898        } 
899    }
900
901    /**
902     * Called when car is delivered to track. Updates the car's wait, pickup
903     * day, and load if spur. If staging, can swap default loads, force load to
904     * default empty, or replace custom loads with the default empty load. Can
905     * trigger RWE or RWL.
906     * 
907     * @param track the destination track for this car
908     */
909    public void loadNext(Track track) {
910        setLoadGeneratedFromStaging(false);
911        if (track != null) {
912            if (track.isSpur()) {
913                ScheduleItem si = getScheduleItem(track);
914                if (si == null) {
915                    log.debug("Schedule item ({}) is null for car ({}) at spur ({})", getScheduleItemId(), toString(),
916                            track.getName());
917                } else {
918                    setWait(si.getWait());
919                    setPickupScheduleId(si.getPickupTrainScheduleId());
920                }
921                updateLoad(track);
922            }
923            // update load optionally when car reaches staging
924            else if (track.isStaging()) {
925                if (track.isLoadSwapEnabled() && getLoadName().equals(carLoads.getDefaultEmptyName())) {
926                    setLoadLoaded();
927                } else if ((track.isLoadSwapEnabled() || track.isLoadEmptyEnabled()) &&
928                        getLoadName().equals(carLoads.getDefaultLoadName())) {
929                    setLoadEmpty();
930                } else if (track.isRemoveCustomLoadsEnabled() &&
931                        !getLoadName().equals(carLoads.getDefaultEmptyName()) &&
932                        !getLoadName().equals(carLoads.getDefaultLoadName())) {
933                    // remove this car's final destination if it has one
934                    setFinalDestination(null);
935                    setFinalDestinationTrack(null);
936                    if (getLoadType().equals(CarLoad.LOAD_TYPE_EMPTY) && isRwlEnabled()) {
937                        setLoadLoaded();
938                        // car arriving into staging with the RWE load?
939                    } else if (getLoadName().equals(getReturnWhenEmptyLoadName())) {
940                        setLoadName(carLoads.getDefaultEmptyName());
941                    } else {
942                        setLoadEmpty(); // note that RWE sets the car's final
943                                        // destination
944                    }
945                }
946            }
947        }
948    }
949
950    /**
951     * Updates a car's load when placed at a spur. Load change delayed if wait
952     * count is greater than zero. 
953     * 
954     * @param track The spur the car is sitting on
955     */
956    public void updateLoad(Track track) {
957        if (track.isDisableLoadChangeEnabled()) {
958            return;
959        }
960        if (getWait() > 0) {
961            return; // change load name when wait count reaches 0
962        }
963        // arriving at spur with a schedule?
964        String loadName = NONE;
965        ScheduleItem si = getScheduleItem(track);
966        if (si != null) {
967            loadName = si.getShipLoadName(); // can be NONE
968        } else {
969            // for backwards compatibility before version 5.1.4
970            log.debug("Schedule item ({}) is null for car ({}) at spur ({}), using next load name", getScheduleItemId(),
971                    toString(), track.getName());
972            loadName = getNextLoadName();
973        }
974        setNextLoadName(NONE); // never used again
975        // car could be part of a kernel
976        if (getKernel() != null && !carLoads.containsName(getTypeName(), loadName)) {
977            loadName = NONE;
978        }
979        if (!loadName.equals(NONE)) {
980            setLoadName(loadName);
981            if (getLoadName().equals(getReturnWhenEmptyLoadName())) {
982                setReturnWhenEmpty();
983            } else if (getLoadName().equals(getReturnWhenLoadedLoadName())) {
984                setReturnWhenLoaded();
985            }
986        } else {
987            // flip load names
988            if (getLoadType().equals(CarLoad.LOAD_TYPE_EMPTY)) {
989                setLoadLoaded();
990            } else {
991                setLoadEmpty();
992            }
993        }
994        loadCarFinalDestination(si);
995        setScheduleItemId(Car.NONE);
996    }
997
998    /**
999     * Sets the car's load to empty, triggers RWE load and destination if
1000     * enabled.
1001     */
1002    private void setLoadEmpty() {
1003        if (!getLoadName().equals(getReturnWhenEmptyLoadName())) {
1004            setLoadName(getReturnWhenEmptyLoadName()); // default RWE load is
1005                                                       // the "E" load
1006            setReturnWhenEmpty();
1007        }
1008    }
1009
1010    /*
1011     * Don't set return address if in staging with the same RWE address and
1012     * don't set return address if at the RWE address
1013     */
1014    private void setReturnWhenEmpty() {
1015        if (getFinalDestination() == null &&
1016                getReturnWhenEmptyDestination() != null &&
1017                (getLocation() != getReturnWhenEmptyDestination() ||
1018                        (!getReturnWhenEmptyDestination().isStaging() &&
1019                                getTrack() != getReturnWhenEmptyDestTrack()))) {
1020            setFinalDestination(getReturnWhenEmptyDestination());
1021            setFinalDestinationTrack(getReturnWhenEmptyDestTrack());
1022            log.debug("Car ({}) has return when empty destination ({}, {}) load {}", toString(),
1023                    getFinalDestinationName(), getFinalDestinationTrackName(), getLoadName());
1024        }
1025    }
1026
1027    /**
1028     * Sets the car's load to loaded, triggers RWL load and destination if
1029     * enabled.
1030     */
1031    private void setLoadLoaded() {
1032        if (!getLoadName().equals(getReturnWhenLoadedLoadName())) {
1033            setLoadName(getReturnWhenLoadedLoadName()); // default RWL load is
1034                                                        // the "L" load
1035            setReturnWhenLoaded();
1036        }
1037    }
1038
1039    /*
1040     * Don't set return address if in staging with the same RWL address and
1041     * don't set return address if at the RWL address
1042     */
1043    private void setReturnWhenLoaded() {
1044        if (getFinalDestination() == null &&
1045                getReturnWhenLoadedDestination() != null &&
1046                (getLocation() != getReturnWhenLoadedDestination() ||
1047                        (!getReturnWhenLoadedDestination().isStaging() &&
1048                                getTrack() != getReturnWhenLoadedDestTrack()))) {
1049            setFinalDestination(getReturnWhenLoadedDestination());
1050            setFinalDestinationTrack(getReturnWhenLoadedDestTrack());
1051            log.debug("Car ({}) has return when loaded destination ({}, {}) load {}", toString(),
1052                    getFinalDestinationName(), getFinalDestinationTrackName(), getLoadName());
1053        }
1054    }
1055
1056    public String getTypeExtensions() {
1057        StringBuffer buf = new StringBuffer();
1058        if (isCaboose()) {
1059            buf.append(EXTENSION_REGEX + CABOOSE_EXTENSION);
1060        }
1061        if (hasFred()) {
1062            buf.append(EXTENSION_REGEX + FRED_EXTENSION);
1063        }
1064        if (isPassenger()) {
1065            buf.append(EXTENSION_REGEX + PASSENGER_EXTENSION + EXTENSION_REGEX + getBlocking());
1066        }
1067        if (isUtility()) {
1068            buf.append(EXTENSION_REGEX + UTILITY_EXTENSION);
1069        }
1070        if (isCarHazardous()) {
1071            buf.append(EXTENSION_REGEX + HAZARDOUS_EXTENSION);
1072        }
1073        return buf.toString();
1074    }
1075
1076    @Override
1077    public void reset() {
1078        setScheduleItemId(getPreviousScheduleId()); // revert to previous
1079        setNextLoadName(NONE);
1080        setFinalDestination(getPreviousFinalDestination());
1081        setFinalDestinationTrack(getPreviousFinalDestinationTrack());
1082        if (isLoadGeneratedFromStaging()) {
1083            setLoadGeneratedFromStaging(false);
1084            setLoadName(carLoads.getDefaultEmptyName());
1085        }
1086        super.reset();
1087        destroyClone();
1088    }
1089
1090    /*
1091     * This routine destroys the clone and restores the cloned car to its
1092     * original location and load. Note there can be multiple clones for a car.
1093     * Only the first clone created has the right info. A clone has creation
1094     * order number appended to the road number.
1095     */
1096    private void destroyClone() {
1097        if (isClone()) {
1098            // move cloned car back to original location
1099            CarManager carManager = InstanceManager.getDefault(CarManager.class);
1100            String[] number = getNumber().split(Car.CLONE_REGEX);
1101            Car car = carManager.getByRoadAndNumber(getRoadName(), number[0]);
1102            int cloneCreationNumber = Integer.parseInt(number[1]);
1103            if (cloneCreationNumber <= car.getCloneOrder()) {
1104                car.setLocation(getLocation(), getTrack(), Car.FORCE);
1105                car.setLoadName(getLoadName());
1106                car.setLastTrain(getLastTrain());
1107                car.setLastRouteId(getLastRouteId());
1108                car.setLastDate(getLastDate());
1109                car.setFinalDestination(getPreviousFinalDestination());
1110                car.setFinalDestinationTrack(getPreviousFinalDestinationTrack());
1111                car.setPreviousFinalDestination(getPreviousFinalDestination());
1112                car.setPreviousFinalDestinationTrack(getPreviousFinalDestinationTrack());
1113                car.setScheduleItemId(getPreviousScheduleId());
1114                car.setWait(0);
1115                car.setMoves(getMoves());
1116                // remember the last clone destroyed
1117                car.setCloneOrder(cloneCreationNumber);
1118            }
1119            InstanceManager.getDefault(KernelManager.class).deleteKernel(getKernelName());
1120            carManager.deregister(this);
1121        }
1122    }
1123
1124    @Override
1125    public void dispose() {
1126        setKernel(null);
1127        setFinalDestination(null); // removes property change listener
1128        setFinalDestinationTrack(null); // removes property change listener
1129        InstanceManager.getDefault(CarTypes.class).removePropertyChangeListener(this);
1130        InstanceManager.getDefault(CarLengths.class).removePropertyChangeListener(this);
1131        super.dispose();
1132    }
1133
1134    // used to stop a track's schedule from bumping when loading car database
1135    private boolean loaded = false;
1136
1137    /**
1138     * Construct this Entry from XML. This member has to remain synchronized
1139     * with the detailed DTD in operations-cars.dtd
1140     *
1141     * @param e Car XML element
1142     */
1143    public Car(org.jdom2.Element e) {
1144        super(e);
1145        loaded = true;
1146        org.jdom2.Attribute a;
1147        if ((a = e.getAttribute(Xml.PASSENGER)) != null) {
1148            _passenger = a.getValue().equals(Xml.TRUE);
1149        }
1150        if ((a = e.getAttribute(Xml.HAZARDOUS)) != null) {
1151            _hazardous = a.getValue().equals(Xml.TRUE);
1152        }
1153        if ((a = e.getAttribute(Xml.CABOOSE)) != null) {
1154            _caboose = a.getValue().equals(Xml.TRUE);
1155        }
1156        if ((a = e.getAttribute(Xml.FRED)) != null) {
1157            _fred = a.getValue().equals(Xml.TRUE);
1158        }
1159        if ((a = e.getAttribute(Xml.UTILITY)) != null) {
1160            _utility = a.getValue().equals(Xml.TRUE);
1161        }
1162        if ((a = e.getAttribute(Xml.KERNEL)) != null) {
1163            Kernel k = InstanceManager.getDefault(KernelManager.class).getKernelByName(a.getValue());
1164            if (k != null) {
1165                setKernel(k);
1166                if ((a = e.getAttribute(Xml.LEAD_KERNEL)) != null && a.getValue().equals(Xml.TRUE)) {
1167                    _kernel.setLead(this);
1168                }
1169            } else {
1170                log.error("Kernel {} does not exist", a.getValue());
1171            }
1172        }
1173        if ((a = e.getAttribute(Xml.LOAD)) != null) {
1174            _loadName = a.getValue();
1175        }
1176        if ((a = e.getAttribute(Xml.LOAD_FROM_STAGING)) != null && a.getValue().equals(Xml.TRUE)) {
1177            setLoadGeneratedFromStaging(true);
1178        }
1179        if ((a = e.getAttribute(Xml.WAIT)) != null) {
1180            try {
1181                _wait = Integer.parseInt(a.getValue());
1182            } catch (NumberFormatException nfe) {
1183                log.error("Wait count ({}) for car ({}) isn't a valid number!", a.getValue(), toString());
1184            }
1185        }
1186        if ((a = e.getAttribute(Xml.PICKUP_SCHEDULE_ID)) != null) {
1187            _pickupScheduleId = a.getValue();
1188        }
1189        if ((a = e.getAttribute(Xml.SCHEDULE_ID)) != null) {
1190            _scheduleId = a.getValue();
1191        }
1192        // for backwards compatibility before version 5.1.4
1193        if ((a = e.getAttribute(Xml.NEXT_LOAD)) != null) {
1194            _nextLoadName = a.getValue();
1195        }
1196        if ((a = e.getAttribute(Xml.NEXT_DEST_ID)) != null) {
1197            setFinalDestination(InstanceManager.getDefault(LocationManager.class).getLocationById(a.getValue()));
1198        }
1199        if (getFinalDestination() != null && (a = e.getAttribute(Xml.NEXT_DEST_TRACK_ID)) != null) {
1200            setFinalDestinationTrack(getFinalDestination().getTrackById(a.getValue()));
1201        }
1202        if ((a = e.getAttribute(Xml.PREVIOUS_NEXT_DEST_ID)) != null) {
1203            setPreviousFinalDestination(
1204                    InstanceManager.getDefault(LocationManager.class).getLocationById(a.getValue()));
1205        }
1206        if (getPreviousFinalDestination() != null && (a = e.getAttribute(Xml.PREVIOUS_NEXT_DEST_TRACK_ID)) != null) {
1207            setPreviousFinalDestinationTrack(getPreviousFinalDestination().getTrackById(a.getValue()));
1208        }
1209        if ((a = e.getAttribute(Xml.PREVIOUS_SCHEDULE_ID)) != null) {
1210            setPreviousScheduleId(a.getValue());
1211        }
1212        if ((a = e.getAttribute(Xml.RWE_DEST_ID)) != null) {
1213            _rweDestination = InstanceManager.getDefault(LocationManager.class).getLocationById(a.getValue());
1214        }
1215        if (_rweDestination != null && (a = e.getAttribute(Xml.RWE_DEST_TRACK_ID)) != null) {
1216            _rweDestTrack = _rweDestination.getTrackById(a.getValue());
1217        }
1218        if ((a = e.getAttribute(Xml.RWE_LOAD)) != null) {
1219            _rweLoadName = a.getValue();
1220        }
1221        if ((a = e.getAttribute(Xml.RWL_DEST_ID)) != null) {
1222            _rwlDestination = InstanceManager.getDefault(LocationManager.class).getLocationById(a.getValue());
1223        }
1224        if (_rwlDestination != null && (a = e.getAttribute(Xml.RWL_DEST_TRACK_ID)) != null) {
1225            _rwlDestTrack = _rwlDestination.getTrackById(a.getValue());
1226        }
1227        if ((a = e.getAttribute(Xml.RWL_LOAD)) != null) {
1228            _rwlLoadName = a.getValue();
1229        }
1230        if ((a = e.getAttribute(Xml.ROUTE_PATH)) != null) {
1231            _routePath = a.getValue();
1232        }
1233        addPropertyChangeListeners();
1234    }
1235
1236    /**
1237     * Create an XML element to represent this Entry. This member has to remain
1238     * synchronized with the detailed DTD in operations-cars.dtd.
1239     *
1240     * @return Contents in a JDOM Element
1241     */
1242    public org.jdom2.Element store() {
1243        org.jdom2.Element e = new org.jdom2.Element(Xml.CAR);
1244        super.store(e);
1245        if (isPassenger()) {
1246            e.setAttribute(Xml.PASSENGER, isPassenger() ? Xml.TRUE : Xml.FALSE);
1247        }
1248        if (isCarHazardous()) {
1249            e.setAttribute(Xml.HAZARDOUS, isCarHazardous() ? Xml.TRUE : Xml.FALSE);
1250        }
1251        if (isCaboose()) {
1252            e.setAttribute(Xml.CABOOSE, isCaboose() ? Xml.TRUE : Xml.FALSE);
1253        }
1254        if (hasFred()) {
1255            e.setAttribute(Xml.FRED, hasFred() ? Xml.TRUE : Xml.FALSE);
1256        }
1257        if (isUtility()) {
1258            e.setAttribute(Xml.UTILITY, isUtility() ? Xml.TRUE : Xml.FALSE);
1259        }
1260        if (getKernel() != null) {
1261            e.setAttribute(Xml.KERNEL, getKernelName());
1262            if (isLead()) {
1263                e.setAttribute(Xml.LEAD_KERNEL, Xml.TRUE);
1264            }
1265        }
1266
1267        e.setAttribute(Xml.LOAD, getLoadName());
1268
1269        if (isLoadGeneratedFromStaging()) {
1270            e.setAttribute(Xml.LOAD_FROM_STAGING, Xml.TRUE);
1271        }
1272
1273        if (getWait() != 0) {
1274            e.setAttribute(Xml.WAIT, Integer.toString(getWait()));
1275        }
1276
1277        if (!getPickupScheduleId().equals(NONE)) {
1278            e.setAttribute(Xml.PICKUP_SCHEDULE_ID, getPickupScheduleId());
1279        }
1280
1281        if (!getScheduleItemId().equals(NONE)) {
1282            e.setAttribute(Xml.SCHEDULE_ID, getScheduleItemId());
1283        }
1284
1285        // for backwards compatibility before version 5.1.4
1286        if (!getNextLoadName().equals(NONE)) {
1287            e.setAttribute(Xml.NEXT_LOAD, getNextLoadName());
1288        }
1289
1290        if (getFinalDestination() != null) {
1291            e.setAttribute(Xml.NEXT_DEST_ID, getFinalDestination().getId());
1292            if (getFinalDestinationTrack() != null) {
1293                e.setAttribute(Xml.NEXT_DEST_TRACK_ID, getFinalDestinationTrack().getId());
1294            }
1295        }
1296
1297        if (getPreviousFinalDestination() != null) {
1298            e.setAttribute(Xml.PREVIOUS_NEXT_DEST_ID, getPreviousFinalDestination().getId());
1299            if (getPreviousFinalDestinationTrack() != null) {
1300                e.setAttribute(Xml.PREVIOUS_NEXT_DEST_TRACK_ID, getPreviousFinalDestinationTrack().getId());
1301            }
1302        }
1303
1304        if (!getPreviousScheduleId().equals(NONE)) {
1305            e.setAttribute(Xml.PREVIOUS_SCHEDULE_ID, getPreviousScheduleId());
1306        }
1307
1308        if (getReturnWhenEmptyDestination() != null) {
1309            e.setAttribute(Xml.RWE_DEST_ID, getReturnWhenEmptyDestination().getId());
1310            if (getReturnWhenEmptyDestTrack() != null) {
1311                e.setAttribute(Xml.RWE_DEST_TRACK_ID, getReturnWhenEmptyDestTrack().getId());
1312            }
1313        }
1314        if (!getReturnWhenEmptyLoadName().equals(carLoads.getDefaultEmptyName())) {
1315            e.setAttribute(Xml.RWE_LOAD, getReturnWhenEmptyLoadName());
1316        }
1317
1318        if (getReturnWhenLoadedDestination() != null) {
1319            e.setAttribute(Xml.RWL_DEST_ID, getReturnWhenLoadedDestination().getId());
1320            if (getReturnWhenLoadedDestTrack() != null) {
1321                e.setAttribute(Xml.RWL_DEST_TRACK_ID, getReturnWhenLoadedDestTrack().getId());
1322            }
1323        }
1324        if (!getReturnWhenLoadedLoadName().equals(carLoads.getDefaultLoadName())) {
1325            e.setAttribute(Xml.RWL_LOAD, getReturnWhenLoadedLoadName());
1326        }
1327
1328        if (!getRoutePath().isEmpty()) {
1329            e.setAttribute(Xml.ROUTE_PATH, getRoutePath());
1330        }
1331
1332        return e;
1333    }
1334
1335    @Override
1336    protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) {
1337        // Set dirty
1338        InstanceManager.getDefault(CarManagerXml.class).setDirty(true);
1339        super.setDirtyAndFirePropertyChange(p, old, n);
1340    }
1341
1342    private void addPropertyChangeListeners() {
1343        InstanceManager.getDefault(CarTypes.class).addPropertyChangeListener(this);
1344        InstanceManager.getDefault(CarLengths.class).addPropertyChangeListener(this);
1345    }
1346
1347    @Override
1348    public void propertyChange(PropertyChangeEvent e) {
1349        super.propertyChange(e);
1350        if (e.getPropertyName().equals(CarTypes.CARTYPES_NAME_CHANGED_PROPERTY)) {
1351            if (e.getOldValue().equals(getTypeName())) {
1352                log.debug("Car ({}) sees type name change old: ({}) new: ({})", toString(), e.getOldValue(),
1353                        e.getNewValue()); // NOI18N
1354                setTypeName((String) e.getNewValue());
1355            }
1356        }
1357        if (e.getPropertyName().equals(CarLengths.CARLENGTHS_NAME_CHANGED_PROPERTY)) {
1358            if (e.getOldValue().equals(getLength())) {
1359                log.debug("Car ({}) sees length name change old: ({}) new: ({})", toString(), e.getOldValue(),
1360                        e.getNewValue()); // NOI18N
1361                setLength((String) e.getNewValue());
1362            }
1363        }
1364        if (e.getPropertyName().equals(Location.DISPOSE_CHANGED_PROPERTY)) {
1365            if (e.getSource() == getFinalDestination()) {
1366                log.debug("delete final destination for car: ({})", toString());
1367                setFinalDestination(null);
1368            }
1369        }
1370        if (e.getPropertyName().equals(Track.DISPOSE_CHANGED_PROPERTY)) {
1371            if (e.getSource() == getFinalDestinationTrack()) {
1372                log.debug("delete final destination for car: ({})", toString());
1373                setFinalDestinationTrack(null);
1374            }
1375        }
1376    }
1377
1378    private final static Logger log = LoggerFactory.getLogger(Car.class);
1379
1380}