/
TrainBuilder.java
5693 lines (5490 loc) · 304 KB
/
TrainBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jmri.jmrit.operations.trains;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.*;
import javax.swing.JOptionPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jmri.InstanceManager;
import jmri.Version;
import jmri.jmrit.operations.locations.Location;
import jmri.jmrit.operations.locations.LocationManager;
import jmri.jmrit.operations.locations.Track;
import jmri.jmrit.operations.locations.schedules.ScheduleItem;
import jmri.jmrit.operations.rollingstock.RollingStock;
import jmri.jmrit.operations.rollingstock.cars.*;
import jmri.jmrit.operations.rollingstock.engines.Engine;
import jmri.jmrit.operations.rollingstock.engines.EngineManager;
import jmri.jmrit.operations.router.Router;
import jmri.jmrit.operations.routes.Route;
import jmri.jmrit.operations.routes.RouteLocation;
import jmri.jmrit.operations.setup.Control;
import jmri.jmrit.operations.setup.Setup;
import jmri.jmrit.operations.trains.schedules.TrainSchedule;
import jmri.jmrit.operations.trains.schedules.TrainScheduleManager;
/**
* Builds a train and then creates the train's manifest.
*
* @author Daniel Boudreau Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013,
* 2014, 2015, 2021
*/
public class TrainBuilder extends TrainCommon {
// report levels
protected static final String ONE = Setup.BUILD_REPORT_MINIMAL;
protected static final String THREE = Setup.BUILD_REPORT_NORMAL;
protected static final String FIVE = Setup.BUILD_REPORT_DETAILED;
protected static final String SEVEN = Setup.BUILD_REPORT_VERY_DETAILED;
protected static final int DISPLAY_CAR_LIMIT_20 = 20; // build exception out of staging
protected static final int DISPLAY_CAR_LIMIT_50 = 50;
protected static final int DISPLAY_CAR_LIMIT_100 = 100;
protected static final int PERCENT_100 = 100;
// build variables shared between local routines
Date _startTime; // when the build report started
Train _train; // the train being built
int _numberCars = 0; // number of cars moved by this train
int _reqNumEngines = 0; // the number of engines required for this train
List<Engine> _engineList; // list of engines available for this train, modified during the build
Engine _lastEngine; // last engine found from getEngine
Engine _secondLeadEngine; // the lead engine in the second half of the train's route
Engine _thirdLeadEngine; // the lead engine in the third part of the train's route
int _carIndex; // index for carList
List<Car> _carList; // list of cars available for this train, modified during the build
List<RouteLocation> _routeList; // list of locations from departure to termination served by this train
Hashtable<String, Integer> _numOfBlocks; // Number of blocks of cars departing staging.
int _completedMoves; // the number of pick up car moves for a location
int _reqNumOfMoves; // the requested number of car moves for a location
Location _departLocation; // train departs this location
Track _departStageTrack; // departure staging track (null if not staging)
Location _terminateLocation; // train terminates at this location
Track _terminateStageTrack; // terminate staging track (null if not staging)
boolean _success; // true when enough cars have been picked up from a location
PrintWriter _buildReport; // build report for this train
List<Car> _notRoutable = new ArrayList<>(); // list of cars that couldn't be routed
List<Location> _modifiedLocations = new ArrayList<>(); // list of locations that have been modified
boolean _warnings = false; // when true there are warning in the build report
// managers
CarManager carManager = InstanceManager.getDefault(CarManager.class);
LocationManager locationManager = InstanceManager.getDefault(LocationManager.class);
EngineManager engineManager = InstanceManager.getDefault(EngineManager.class);
TrainManager trainManager = InstanceManager.getDefault(TrainManager.class);
TrainScheduleManager trainScheduleManager = InstanceManager.getDefault(TrainScheduleManager.class);
CarLoads carLoads = InstanceManager.getDefault(CarLoads.class);
Router router = InstanceManager.getDefault(Router.class);
/**
* Build rules:
* <ol>
* <li>Need at least one location in route to build train
* <li>Select only locos and cars that the train can service
* <li>Optional TODO, train must depart with the required number of moves (cars)
* <li>If required, add caboose or car with FRED to train
* <li>When departing staging find a track matching train requirements
* <li>All cars and locos on one track must leave staging
* <li>Service locations based on train direction, location car types, roads and
* loads.
* <li>Ignore track direction when train is a local (serves one location)
* </ol>
*
* @param train the train that is to be built
* @return True if successful.
*
*/
public boolean build(Train train) {
this._train = train;
try {
build();
return true;
} catch (BuildFailedException e) {
buildFailed(e);
return false;
}
}
private void build() throws BuildFailedException {
_startTime = new Date();
log.debug("Building train ({})", _train.getName());
_train.setStatusCode(Train.CODE_BUILDING);
_train.setBuilt(false);
_train.setLeadEngine(null);
createBuildReportFile(); // backup previous build report and create new build report file
addBuildReportInfo(); // add the build report header information
setUpRoute(); // load route, departure and terminate locations
showTrainBuildOptions(); // show the build options
showSpecificTrainBuildOptions(); // show the build options for this train
showAndInitializeTrainRoute(); // show the train's route and initialize it
showIfLocalSwitcher(); // show if this train a switcher, a train that works only one location
determineEngineRequirements(); // determine how many engines need to be assigned to train
showTrainRequirements(); // show how many engines, caboose, car with FRED and changes in the route
showTrainServices(); // show which roads, owners, built dates, and engine types
getAndRemoveEnginesFromList(); // get a list of available engines
determineIfTrainTerminatesIntoStaging(); // find a terminus track in staging for this train
determineIfTrainDepartsStagingAndAddEngines(); // assign engines to train if departing staging
addEngines(); // 2nd and 3rd engine swaps in a train's route
showTrainCarTypes(); // show car types that this train will service
showTrainLoadNames(); // show load names that this train will service
loadRemoveAndListCars(); // remove unwanted cars and list available cars by location
sortCarsOnFifoLifoTracks(); // sort cars on FIFO or LIFO tracks
addCabooseOrFredToTrain(); // do all caboose and FRED changes in the train's route
removeCaboosesAndCarsWithFred(); // done assigning cabooses and cars with FRED, remove the rest
saveCarFinalDestinations(); // save car's final destination and schedule id in case of train reset
blockCarsFromStaging(); // optionally block cars from staging by setting destinations
addCarsToTrain(); // finds and adds cars to the train, throws BuildFailedException
checkStuckCarsInStaging(); // determine if cars are stuck in staging, throws BuildFailedException
showTrainBuildStatus(); // show how well the build went with regards to cars requested and actual
checkEngineHP(); // check that engine assigned to the train has the appropriate HP
checkNumnberOfEnginesNeededHPT(); // check to see if additional engines are needed for this train
showCarsNotRoutable(); // list cars that couldn't be routed
// done building
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTime"),
new Object[] { _train.getName(), new Date().getTime() - _startTime.getTime() }));
_buildReport.flush();
_buildReport.close();
createManifests(); // now make Manifests
// notify locations have been modified by this train's build
for (Location location : _modifiedLocations) {
location.setStatus(Location.MODIFIED);
}
// automations use wait for train built to create custom manifests and switch
// lists
_train.setCurrentLocation(_train.getTrainDepartsRouteLocation());
_train.setBuilt(true);
_train.moveTrainIcon(_train.getTrainDepartsRouteLocation()); // create and place train icon
log.debug("Done building train ({})", _train.getName());
showWarningMessage();
}
private void createBuildReportFile() {
// backup the train's previous build report file
InstanceManager.getDefault(TrainManagerXml.class).savePreviousBuildStatusFile(_train.getName());
// create build report file
File file = InstanceManager.getDefault(TrainManagerXml.class).createTrainBuildReportFile(_train.getName());
try {
_buildReport = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)),
true);
} catch (IOException e) {
log.error("Can not open build report file: {}", file.getName());
return;
}
}
/**
* Creates the build report header information lines. Build report date, JMRI
* version, train schedule, build report display levels, setup comment.
*/
private void addBuildReportInfo() {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("BuildReportMsg"),
new Object[] { _train.getName(), _startTime }));
addLine(_buildReport, ONE,
MessageFormat.format(Bundle.getMessage("BuildReportVersion"), new Object[] { Version.name() }));
if (!trainScheduleManager.getTrainScheduleActiveId().equals(TrainScheduleManager.NONE)) {
if (trainScheduleManager.getTrainScheduleActiveId().equals(TrainSchedule.ANY)) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildActiveSchedule"),
new Object[] { Bundle.getMessage("Any") }));
} else {
TrainSchedule sch = trainScheduleManager.getActiveSchedule();
if (sch != null) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildActiveSchedule"),
new Object[] { sch.getName() }));
}
}
}
// show the various build detail levels
addLine(_buildReport, THREE, Bundle.getMessage("buildReportLevelThree"));
addLine(_buildReport, FIVE, Bundle.getMessage("buildReportLevelFive"));
addLine(_buildReport, SEVEN, Bundle.getMessage("buildReportLevelSeven"));
if (Setup.getRouterBuildReportLevel().equals(Setup.BUILD_REPORT_DETAILED)) {
addLine(_buildReport, SEVEN, Bundle.getMessage("buildRouterReportLevelDetailed"));
} else if (Setup.getRouterBuildReportLevel().equals(Setup.BUILD_REPORT_VERY_DETAILED)) {
addLine(_buildReport, SEVEN, Bundle.getMessage("buildRouterReportLevelVeryDetailed"));
}
if (!Setup.getComment().trim().isEmpty()) {
addLine(_buildReport, ONE, BLANK_LINE);
addLine(_buildReport, ONE, Setup.getComment());
}
addLine(_buildReport, ONE, BLANK_LINE);
}
private void setUpRoute() throws BuildFailedException {
if (_train.getRoute() == null) {
throw new BuildFailedException(
MessageFormat.format(Bundle.getMessage("buildErrorRoute"), new Object[] { _train.getName() }));
}
// get the train's route
_routeList = _train.getRoute().getLocationsBySequenceList();
if (_routeList.size() < 1) {
throw new BuildFailedException(
MessageFormat.format(Bundle.getMessage("buildErrorNeedRoute"), new Object[] { _train.getName() }));
}
// train departs
_departLocation = locationManager.getLocationByName(_train.getTrainDepartsName());
if (_departLocation == null) {
throw new BuildFailedException(
MessageFormat.format(Bundle.getMessage("buildErrorNeedDepLoc"), new Object[] { _train.getName() }));
}
// train terminates
_terminateLocation = locationManager.getLocationByName(_train.getTrainTerminatesName());
if (_terminateLocation == null) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorNeedTermLoc"),
new Object[] { _train.getName() }));
}
}
/**
* show train build options when in detailed mode
*/
private void showTrainBuildOptions() {
ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrit.operations.setup.JmritOperationsSetupBundle");
addLine(_buildReport, FIVE, Bundle.getMessage("MenuItemBuildOptions") + ":");
if (Setup.isBuildAggressive()) {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildModeAggressive"));
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("BuildNumberPasses"),
new Object[] { Setup.getNumberPasses() }));
if (Setup.isStagingTrackImmediatelyAvail() && _departLocation.isStaging()) {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildStagingTrackAvail"));
}
} else {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildModeNormal"));
}
// show switcher options
if (_train.isLocalSwitcher()) {
addLine(_buildReport, FIVE, BLANK_LINE);
addLine(_buildReport, FIVE, rb.getString("BorderLayoutSwitcherService") + ":");
if (Setup.isLocalInterchangeMovesEnabled()) {
addLine(_buildReport, FIVE, rb.getString("AllowLocalInterchange"));
} else {
addLine(_buildReport, FIVE, rb.getString("NoAllowLocalInterchange"));
}
if (Setup.isLocalSpurMovesEnabled()) {
addLine(_buildReport, FIVE, rb.getString("AllowLocalSpur"));
} else {
addLine(_buildReport, FIVE, rb.getString("NoAllowLocalSpur"));
}
if (Setup.isLocalYardMovesEnabled()) {
addLine(_buildReport, FIVE, rb.getString("AllowLocalYard"));
} else {
addLine(_buildReport, FIVE, rb.getString("NoAllowLocalYard"));
}
}
// show staging options
if (_departLocation.isStaging() || _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, BLANK_LINE);
addLine(_buildReport, FIVE, Bundle.getMessage("buildStagingOptions"));
if (Setup.isStagingTrainCheckEnabled() && _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, Bundle.getMessage("buildOptionRestrictStaging"));
}
if (Setup.isStagingTrackImmediatelyAvail() && _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, rb.getString("StagingAvailable"));
}
if (Setup.isStagingAllowReturnEnabled() &&
_departLocation.isStaging() &&
_terminateLocation.isStaging() &&
_departLocation == _terminateLocation) {
addLine(_buildReport, FIVE, rb.getString("AllowCarsToReturn"));
}
if (Setup.isStagingPromptFromEnabled() && _departLocation.isStaging()) {
addLine(_buildReport, FIVE, rb.getString("PromptFromStaging"));
}
if (Setup.isStagingPromptToEnabled() && _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, rb.getString("PromptToStaging"));
}
if (Setup.isStagingTryNormalBuildEnabled() && _departLocation.isStaging()) {
addLine(_buildReport, FIVE, rb.getString("TryNormalStaging"));
}
}
// Car routing options
addLine(_buildReport, FIVE, BLANK_LINE);
addLine(_buildReport, FIVE, Bundle.getMessage("buildCarRoutingOptions"));
// warn if car routing is disabled
if (!Setup.isCarRoutingEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("RoutingDisabled"));
_warnings = true;
} else {
if (Setup.isCarRoutingViaYardsEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("RoutingViaYardsEnabled"));
}
if (Setup.isCarRoutingViaStagingEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("RoutingViaStagingEnabled"));
}
if (Setup.isOnlyActiveTrainsEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("OnlySelectedTrains"));
_warnings = true;
// list the selected trains
for (Train train : trainManager.getTrainsByNameList()) {
if (train.isBuildEnabled()) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildTrainNameAndDesc"),
new Object[] { train.getName(), train.getDescription() }));
}
}
if (!_train.isBuildEnabled()) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainNotSelected"),
new Object[] { _train.getName() }));
}
} else {
addLine(_buildReport, FIVE, rb.getString("AllTrains"));
}
if (Setup.isCheckCarDestinationEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("CheckCarDestination"));
}
}
addLine(_buildReport, FIVE, BLANK_LINE);
}
/*
* Show the enabled and disabled build options for this train.
*/
private void showSpecificTrainBuildOptions() {
addLine(_buildReport, FIVE,
MessageFormat.format(Bundle.getMessage("buildOptionsForTrain"), new Object[] { _train.getName() }));
showSpecificTrainBuildOptions(true);
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildDisabledOptionsForTrain"),
new Object[] { _train.getName() }));
showSpecificTrainBuildOptions(false);
}
/*
* Enabled when true lists selected build options for this train. Enabled when
* false list disabled build options for this train.
*/
private void showSpecificTrainBuildOptions(boolean enabled) {
if (_train.isBuildTrainNormalEnabled() ^ !enabled) {
addLine(_buildReport, FIVE, Bundle.getMessage("NormalModeWhenBuilding"));
}
if (_train.isSendCarsToTerminalEnabled() ^ !enabled) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("SendToTerminal"),
new Object[] { _terminateLocation.getName() }));
}
if ((_train.isAllowReturnToStagingEnabled() || Setup.isStagingAllowReturnEnabled()) ^ !enabled &&
_departLocation.isStaging() &&
_departLocation == _terminateLocation) {
addLine(_buildReport, FIVE, Bundle.getMessage("AllowCarsToReturn"));
}
if (_train.isAllowLocalMovesEnabled() ^ !enabled) {
addLine(_buildReport, FIVE, Bundle.getMessage("AllowLocalMoves"));
}
if (_train.isAllowThroughCarsEnabled() ^ !enabled && _departLocation != _terminateLocation) {
addLine(_buildReport, FIVE, Bundle.getMessage("AllowThroughCars"));
}
if (_train.isServiceAllCarsWithFinalDestinationsEnabled() ^ !enabled) {
addLine(_buildReport, FIVE, Bundle.getMessage("ServiceAllCars"));
}
if (_train.isSendCarsWithCustomLoadsToStagingEnabled() ^ !enabled) {
addLine(_buildReport, FIVE, Bundle.getMessage("SendCustomToStaging"));
}
if (_train.isBuildConsistEnabled() ^ !enabled) {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildConsist"));
if (enabled) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("BuildConsistHPT"),
new Object[] { Setup.getHorsePowerPerTon() }));
}
}
addLine(_buildReport, FIVE, BLANK_LINE);
}
/**
* Adds to the build report what the train will service. Road and owner names,
* built dates, and engine types.
*/
private void showTrainServices() {
// show road names that this train will service
if (!_train.getRoadOption().equals(Train.ALL_ROADS)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainRoads"), new Object[] {
_train.getName(), _train.getRoadOption(), formatStringToCommaSeparated(_train.getRoadNames()) }));
}
// show owner names that this train will service
if (!_train.getOwnerOption().equals(Train.ALL_OWNERS)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainOwners"), new Object[] {
_train.getName(), _train.getOwnerOption(), formatStringToCommaSeparated(_train.getOwnerNames()) }));
}
// show built dates serviced
if (!_train.getBuiltStartYear().equals(Train.NONE)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainBuiltAfter"),
new Object[] { _train.getName(), _train.getBuiltStartYear() }));
}
if (!_train.getBuiltEndYear().equals(Train.NONE)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainBuiltBefore"),
new Object[] { _train.getName(), _train.getBuiltEndYear() }));
}
// show engine types that this train will service
if (_reqNumEngines > 0) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainServicesEngineTypes"),
new Object[] { _train.getName() }));
addLine(_buildReport, FIVE, formatStringToCommaSeparated(_train.getLocoTypeNames()));
}
}
/**
* Show and initialize the train's route. Determines the number of car moves
* requested for this train. Also adjust the number of car moves if the random
* car moves option was selected.
*
* @throws BuildFailedException
*/
private void showAndInitializeTrainRoute() throws BuildFailedException {
int requestedCarMoves = 0; // how many cars were asked to be moved
// TODO: DAB control minimal build by each train
if (_train.getTrainDepartsRouteLocation().getMaxCarMoves() > _departLocation.getNumberRS() &&
Control.fullTrainOnly) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorCars"), new Object[] {
Integer.toString(_departLocation.getNumberRS()), _train.getTrainDepartsName(), _train.getName() }));
}
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildTrainRoute"),
new Object[] { _train.getName(), _train.getRoute().getName() }));
// get the number of requested car moves for this train
for (RouteLocation rl : _routeList) {
// check to see if there's a location for each stop in the route
Location location = locationManager.getLocationByName(rl.getName()); // this checks for a deleted location
if (location == null || rl.getLocation() == null) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorLocMissing"),
new Object[] { _train.getRoute().getName() }));
}
// train doesn't drop or pick up cars from staging locations found in middle of
// a route
if (location.isStaging() &&
rl != _train.getTrainDepartsRouteLocation() &&
rl != _train.getTrainTerminatesRouteLocation()) {
addLine(_buildReport, ONE,
MessageFormat.format(Bundle.getMessage("buildLocStaging"), new Object[] { rl.getName() }));
rl.setCarMoves(rl.getMaxCarMoves()); // don't allow car moves for this location
// if a location is skipped, no car drops or pick ups
} else if (_train.isLocationSkipped(rl.getId())) {
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildLocSkippedMaxTrain"),
new Object[] { rl.getId(), rl.getName(), _train.getName(), rl.getMaxTrainLength(),
Setup.getLengthUnit().toLowerCase() }));
rl.setCarMoves(rl.getMaxCarMoves()); // don't allow car moves for this location
} else if (!rl.isDropAllowed() && !rl.isPickUpAllowed()) {
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildLocNoDropsOrPickups"), new Object[] { rl.getId(),
rl.getName(), rl.getMaxTrainLength(), Setup.getLengthUnit().toLowerCase() }));
rl.setCarMoves(rl.getMaxCarMoves()); // don't allow car moves for this location
} else {
// we're going to use this location, so initialize the route location
rl.setCarMoves(0); // clear the number of moves
requestedCarMoves += rl.getMaxCarMoves(); // add up the total number of car moves requested
// show the type of moves allowed at this location
if (location.isStaging() && rl.isPickUpAllowed() && rl == _train.getTrainDepartsRouteLocation()) {
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildStagingDeparts"),
new Object[] { rl.getId(), rl.getName(), rl.getMaxCarMoves(),
rl.getMaxTrainLength(), Setup.getLengthUnit().toLowerCase() }));
} else if (location.isStaging() &&
rl.isDropAllowed() &&
rl == _train.getTrainTerminatesRouteLocation()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildStagingTerminates"),
new Object[] { rl.getId(), rl.getName(), rl.getMaxCarMoves() }));
} else if (rl == _train.getTrainTerminatesRouteLocation() &&
rl.isDropAllowed() &&
rl.isPickUpAllowed()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocTerminatesMoves"),
new Object[] { rl.getId(), rl.getName(), rl.getMaxCarMoves() }));
} else if (rl.isDropAllowed() && rl.isPickUpAllowed()) {
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildLocRequestMoves"),
new Object[] { rl.getId(), rl.getName(), rl.getMaxCarMoves(),
rl.getMaxTrainLength(), Setup.getLengthUnit().toLowerCase() }));
} else if (!rl.isDropAllowed()) {
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildLocRequestPickups"),
new Object[] { rl.getId(), rl.getName(), rl.getMaxCarMoves(),
rl.getMaxTrainLength(), Setup.getLengthUnit().toLowerCase() }));
} else if (rl == _train.getTrainTerminatesRouteLocation()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocTerminates"),
new Object[] { rl.getId(), rl.getName(), rl.getMaxCarMoves() }));
} else {
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildLocRequestDrops"),
new Object[] { rl.getId(), rl.getName(), rl.getMaxCarMoves(),
rl.getMaxTrainLength(), Setup.getLengthUnit().toLowerCase() }));
}
}
rl.setTrainWeight(0); // clear the total train weight
rl.setTrainLength(0); // and length
}
// check for random moves in the train's route
for (RouteLocation rl : _routeList) {
if (rl.getRandomControl().equals(RouteLocation.DISABLED)) {
continue;
}
if (rl.getCarMoves() == 0 && rl.getMaxCarMoves() > 0) {
log.debug("Location ({}) has random control value {} and maximum moves {}", rl.getName(),
rl.getRandomControl(), rl.getMaxCarMoves());
try {
int value = Integer.parseInt(rl.getRandomControl());
// now adjust the number of available moves for this location
double random = Math.random();
log.debug("random {}", random);
int moves = (int) (random * ((rl.getMaxCarMoves() * value / 100) + 1));
log.debug("Reducing number of moves for location ({}) by {}", rl.getName(), moves);
rl.setCarMoves(moves);
requestedCarMoves = requestedCarMoves - moves;
addLine(_buildReport, FIVE,
MessageFormat.format(Bundle.getMessage("buildRouteRandomControl"),
new Object[] { rl.getName(), rl.getId(), rl.getRandomControl(), rl.getMaxCarMoves(),
rl.getMaxCarMoves() - moves }));
} catch (NumberFormatException e) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorRandomControl"),
new Object[] { _train.getRoute().getName(), rl.getName(), rl.getRandomControl() }));
}
}
}
int numMoves = requestedCarMoves; // number of car moves
if (!_train.isLocalSwitcher()) {
requestedCarMoves = requestedCarMoves / 2; // only need half as many cars to meet requests
}
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildRouteRequest"), new Object[] {
_train.getRoute().getName(), Integer.toString(requestedCarMoves), Integer.toString(numMoves) }));
_train.setNumberCarsRequested(requestedCarMoves); // save number of car moves requested
addLine(_buildReport, ONE, BLANK_LINE);
}
/**
* reports if local switcher
*/
private void showIfLocalSwitcher() {
if (_train.isLocalSwitcher()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildTrainIsSwitcher"),
new Object[] { _train.getName(), TrainCommon.splitString(_train.getTrainDepartsName()) }));
addLine(_buildReport, THREE, BLANK_LINE);
}
}
/**
* set the engine requirements for this train
*/
private void determineEngineRequirements() {
if (_train.getNumberEngines().equals(Train.AUTO)) {
_reqNumEngines = getAutoEngines();
} else if (_train.getNumberEngines().equals(Train.AUTO_HPT)) {
_reqNumEngines = 1; // get one loco for now, check HP requirements after train is built
} else {
_reqNumEngines = Integer.parseInt(_train.getNumberEngines());
}
}
/**
* Show how many engines are required for this train, and if a certain road name
* for the engine is requested. Show if there are any engine changes in the
* route, or if helper engines are needed. There can be up to 2 engine changes
* or helper requests. Show if caboose or FRED is needed for train, and if
* there's a road name requested. There can be up to 2 caboose changes in the
* route.
*/
private void showTrainRequirements() {
addLine(_buildReport, ONE, Bundle.getMessage("TrainRequirements"));
if (_train.getNumberEngines().equals("0")) {
addLine(_buildReport, ONE, Bundle.getMessage("buildTrainReq0Engine"));
} else if (_train.getNumberEngines().equals("1")) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainReq1Engine"),
new Object[] { _train.getTrainDepartsName(), _train.getEngineModel(), _train.getEngineRoad() }));
} else {
addLine(_buildReport, ONE,
MessageFormat.format(Bundle.getMessage("buildTrainReqEngine"),
new Object[] { _train.getTrainDepartsName(), _train.getNumberEngines(),
_train.getEngineModel(), _train.getEngineRoad() }));
}
// show any required loco changes
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, ONE,
MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"),
new Object[] { _train.getSecondLegStartLocationName(), _train.getSecondLegNumberEngines(),
_train.getSecondLegEngineModel(), _train.getSecondLegEngineRoad() }));
}
if ((_train.getSecondLegOptions() & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) {
addLine(_buildReport, ONE,
MessageFormat.format(Bundle.getMessage("buildTrainHelperEngines"),
new Object[] { _train.getSecondLegNumberEngines(), _train.getSecondLegStartLocationName(),
_train.getSecondLegEndLocationName(), _train.getSecondLegEngineModel(),
_train.getSecondLegEngineRoad() }));
}
if ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, ONE,
MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"),
new Object[] { _train.getThirdLegStartLocationName(), _train.getThirdLegNumberEngines(),
_train.getThirdLegEngineModel(), _train.getThirdLegEngineRoad() }));
}
if ((_train.getThirdLegOptions() & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) {
addLine(_buildReport, ONE,
MessageFormat.format(Bundle.getMessage("buildTrainHelperEngines"),
new Object[] { _train.getThirdLegNumberEngines(), _train.getThirdLegStartLocationName(),
_train.getThirdLegEndLocationName(), _train.getThirdLegEngineModel(),
_train.getThirdLegEngineRoad() }));
}
// show caboose or FRED requirements
if (_train.isCabooseNeeded()) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainRequiresCaboose"),
new Object[] { _train.getTrainDepartsName(), _train.getCabooseRoad() }));
}
// show any caboose changes in the train's route
if ((_train.getSecondLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getSecondLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildCabooseChange"),
new Object[] { _train.getSecondLegStartRouteLocation() }));
}
if ((_train.getThirdLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildCabooseChange"),
new Object[] { _train.getThirdLegStartRouteLocation() }));
}
if (_train.isFredNeeded()) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainRequiresFRED"),
new Object[] { _train.getTrainDepartsName(), _train.getCabooseRoad() }));
}
addLine(_buildReport, ONE, BLANK_LINE);
}
/**
* Figures out if the train terminates into staging, and if true, sets the
* termination track. Note if the train is returning back to the same track in
* staging _terminateStageTrack is null, and is loaded later when the departure
* track is determined.
*
* @throws BuildFailedException
*/
private void determineIfTrainTerminatesIntoStaging() throws BuildFailedException {
// does train terminate into staging?
_terminateStageTrack = null;
List<Track> stagingTracksTerminate = _terminateLocation.getTracksByMovesList(Track.STAGING);
if (stagingTracksTerminate.size() > 0) {
addLine(_buildReport, THREE, BLANK_LINE);
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTerminateStaging"),
new Object[] { _terminateLocation.getName(), Integer.toString(stagingTracksTerminate.size()) }));
if (stagingTracksTerminate.size() > 1 && Setup.isStagingPromptToEnabled()) {
_terminateStageTrack = promptToStagingDialog();
_startTime = new Date(); // reset build time since user can take awhile to pick
} else {
// is this train returning to the same staging in aggressive mode?
if (_departLocation == _terminateLocation &&
Setup.isBuildAggressive() &&
Setup.isStagingTrackImmediatelyAvail()) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildStagingReturn"),
new Object[] { _terminateLocation.getName() }));
} else {
for (Track track : stagingTracksTerminate) {
_terminateStageTrack = track;
if (checkTerminateStagingTrack(_terminateStageTrack)) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildStagingAvail"),
new Object[] { _terminateStageTrack.getName(), _terminateLocation.getName() }));
break;
}
_terminateStageTrack = null;
}
}
}
if (_terminateStageTrack == null) {
// is this train returning to the same staging in aggressive mode?
if (_departLocation == _terminateLocation &&
Setup.isBuildAggressive() &&
Setup.isStagingTrackImmediatelyAvail()) {
log.debug("Train is returning to same track in staging");
} else {
addLine(_buildReport, ONE, Bundle.getMessage("buildErrorStagingFullNote"));
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorStagingFull"),
new Object[] { _terminateLocation.getName() }));
}
}
}
}
/**
* Builds a list of possible engines for this train.
*/
private void getAndRemoveEnginesFromList() {
_engineList = engineManager.getAvailableTrainList(_train);
// remove any locos that the train can't use
for (int indexEng = 0; indexEng < _engineList.size(); indexEng++) {
Engine engine = _engineList.get(indexEng);
// remove engines types that train does not service
if (!_train.isTypeNameAccepted(engine.getTypeName())) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeEngineType"),
new Object[] { engine.toString(), engine.getTypeName() }));
_engineList.remove(indexEng--);
continue;
}
// remove engines with owners that train does not service
if (!_train.isOwnerNameAccepted(engine.getOwner())) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeEngineOwner"),
new Object[] { engine.toString(), engine.getOwner() }));
_engineList.remove(indexEng--);
continue;
}
// remove engines with built dates that train does not service
if (!_train.isBuiltDateAccepted(engine.getBuilt())) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeEngineBuilt"),
new Object[] { engine.toString(), engine.getBuilt() }));
_engineList.remove(indexEng--);
continue;
}
// remove engines that are out of service
if (engine.isOutOfService()) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeEngineOutOfService"),
new Object[] { engine.toString(), engine.getLocationName(), engine.getTrackName() }));
_engineList.remove(indexEng--);
continue;
}
}
}
/**
* Figures out if the train is departing staging, and if true, sets the
* departure track. Also sets the arrival track if the train is returning to the
* same departure track in staging.
*
* @throws NumberFormatException
* @throws BuildFailedException
*/
private void determineIfTrainDepartsStagingAndAddEngines() throws NumberFormatException, BuildFailedException {
// allow up to two engine and caboose swaps in the train's route
RouteLocation engineTerminatesFirstLeg = _train.getTrainTerminatesRouteLocation();
// Adjust where the locos will terminate
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES &&
_train.getSecondLegStartRouteLocation() != null) {
engineTerminatesFirstLeg = _train.getSecondLegStartRouteLocation();
}
if ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES &&
_train.getThirdLegStartRouteLocation() != null) {
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) != Train.CHANGE_ENGINES) {
engineTerminatesFirstLeg = _train.getThirdLegStartRouteLocation();
}
}
// determine if train is departing staging
List<Track> stagingTracks = _departLocation.getTracksByMovesList(Track.STAGING);
if (stagingTracks.size() > 0) {
addLine(_buildReport, THREE, BLANK_LINE);
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildDepartStaging"),
new Object[] { _departLocation.getName(), Integer.toString(stagingTracks.size()) }));
if (stagingTracks.size() > 1 && Setup.isStagingPromptFromEnabled()) {
_departStageTrack = promptFromStagingDialog();
_startTime = new Date(); // restart build timer
if (_departStageTrack == null) {
showTrainRequirements();
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorStagingEmpty"),
new Object[] { _departLocation.getName() }));
}
} else {
for (Track track : stagingTracks) {
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildStagingHas"),
new Object[] { track.getName(), Integer.toString(track.getNumberEngines()),
Integer.toString(track.getNumberCars()) }));
// is the departure track available?
if (!checkDepartureStagingTrack(track)) {
addLine(_buildReport, SEVEN,
MessageFormat.format(Bundle.getMessage("buildStagingTrackRestriction"),
new Object[] { track.getName(), _train.getName() }));
continue;
}
_departStageTrack = track;
// try each departure track for the required engines
if (getEngines(_reqNumEngines, _train.getEngineModel(), _train.getEngineRoad(),
_train.getTrainDepartsRouteLocation(), engineTerminatesFirstLeg)) {
addLine(_buildReport, SEVEN, Bundle.getMessage("buildDoneAssignEnginesStaging"));
break; // done!
}
_departStageTrack = null;
}
}
if (_departStageTrack == null) {
showTrainRequirements();
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorStagingEmpty"),
new Object[] { _departLocation.getName() }));
// departing staging and returning to same track?
} else if (_terminateStageTrack == null &&
_departLocation == _terminateLocation &&
Setup.isBuildAggressive() &&
Setup.isStagingTrackImmediatelyAvail()) {
_terminateStageTrack = _departStageTrack; // use the same track
}
}
_train.setTerminationTrack(_terminateStageTrack);
_train.setDepartureTrack(_departStageTrack);
}
private void addEngines() throws NumberFormatException, BuildFailedException {
// allow up to two engine and caboose swaps in the train's route
RouteLocation engineTerminatesFirstLeg = _train.getTrainTerminatesRouteLocation();
RouteLocation engineTerminatesSecondLeg = _train.getTrainTerminatesRouteLocation();
// Adjust where the locos will terminate
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES &&
_train.getSecondLegStartRouteLocation() != null) {
engineTerminatesFirstLeg = _train.getSecondLegStartRouteLocation();
}
if ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES &&
_train.getThirdLegStartRouteLocation() != null) {
engineTerminatesSecondLeg = _train.getThirdLegStartRouteLocation();
// No engine or caboose change at first leg?
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) != Train.CHANGE_ENGINES) {
engineTerminatesFirstLeg = _train.getThirdLegStartRouteLocation();
}
}
// load engines for this train
if (_train.getLeadEngine() == null) {
addLine(_buildReport, THREE, BLANK_LINE);
if (getEngines(_reqNumEngines, _train.getEngineModel(), _train.getEngineRoad(),
_train.getTrainDepartsRouteLocation(), engineTerminatesFirstLeg)) {
_secondLeadEngine = _lastEngine; // when adding a caboose later in the route, no engine change
_thirdLeadEngine = _lastEngine;
} else {
throw new BuildFailedException(
MessageFormat.format(Bundle.getMessage("buildErrorEngines"), new Object[] { _reqNumEngines,
_train.getTrainDepartsName(), engineTerminatesFirstLeg.getName() }));
}
}
// First engine change in route?
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, THREE, BLANK_LINE);
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"),
new Object[] { _train.getSecondLegStartLocationName(), _train.getSecondLegNumberEngines(),
_train.getSecondLegEngineModel(), _train.getSecondLegEngineRoad() }));
if (getEngines(Integer.parseInt(_train.getSecondLegNumberEngines()), _train.getSecondLegEngineModel(),
_train.getSecondLegEngineRoad(), _train.getSecondLegStartRouteLocation(),
engineTerminatesSecondLeg)) {
_secondLeadEngine = _lastEngine;
_thirdLeadEngine = _lastEngine;
} else {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorEngines"),
new Object[] { Integer.parseInt(_train.getSecondLegNumberEngines()),
_train.getSecondLegStartRouteLocation(), engineTerminatesSecondLeg }));
}
}
// Second engine change in route?
if ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, THREE, BLANK_LINE);
addLine(_buildReport, THREE,
MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"),
new Object[] { _train.getThirdLegStartLocationName(), _train.getThirdLegNumberEngines(),
_train.getThirdLegEngineModel(), _train.getThirdLegEngineRoad() }));
if (getEngines(Integer.parseInt(_train.getThirdLegNumberEngines()), _train.getThirdLegEngineModel(),
_train.getThirdLegEngineRoad(), _train.getThirdLegStartRouteLocation(),
_train.getTrainTerminatesRouteLocation())) {
_thirdLeadEngine = _lastEngine;
} else {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorEngines"),
new Object[] { Integer.parseInt(_train.getThirdLegNumberEngines()),
_train.getThirdLegStartRouteLocation(), _train.getTrainTerminatesRouteLocation() }));
}
}
if (_reqNumEngines > 0 && (!_train.isBuildConsistEnabled() || Setup.getHorsePowerPerTon() == 0)) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildDoneAssingEnginesTrain"),
new Object[] { _train.getName() }));
}
}
private void showTrainCarTypes() {
addLine(_buildReport, FIVE, BLANK_LINE);
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainServicesCarTypes"),
new Object[] { _train.getName() }));
addLine(_buildReport, FIVE, formatStringToCommaSeparated(_train.getCarTypeNames()));
}
private void showTrainLoadNames() {
if (!_train.getLoadOption().equals(Train.ALL_LOADS)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainLoads"), new Object[] {
_train.getName(), _train.getLoadOption(), formatStringToCommaSeparated(_train.getLoadNames()) }));
}
}
/**
* Adds and removes cabooses or car with FRED in the train's route. Up to 2
* caboose changes.
*
* @throws BuildFailedException
*/
private void addCabooseOrFredToTrain() throws BuildFailedException {
// allow up to two caboose swaps in the train's route
RouteLocation cabooseOrFredTerminatesFirstLeg = _train.getTrainTerminatesRouteLocation();
RouteLocation cabooseOrFredTerminatesSecondLeg = _train.getTrainTerminatesRouteLocation();
// determine if there are any caboose changes
if ((_train.getSecondLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getSecondLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
cabooseOrFredTerminatesFirstLeg = _train.getSecondLegStartRouteLocation();
} else if ((_train.getThirdLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
cabooseOrFredTerminatesFirstLeg = _train.getThirdLegStartRouteLocation();
}
if ((_train.getThirdLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
cabooseOrFredTerminatesSecondLeg = _train.getThirdLegStartRouteLocation();
}
// Do caboose changes in reverse order in case there isn't enough track space
// second caboose change?
if ((_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE &&
_train.getThirdLegStartRouteLocation() != null &&
_train.getTrainTerminatesRouteLocation() != null) {
getCaboose(_train.getThirdLegCabooseRoad(), _thirdLeadEngine, _train.getThirdLegStartRouteLocation(),
_train.getTrainTerminatesRouteLocation(), true);
}
// first caboose change?
if ((_train.getSecondLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE &&
_train.getSecondLegStartRouteLocation() != null &&
cabooseOrFredTerminatesSecondLeg != null) {
getCaboose(_train.getSecondLegCabooseRoad(), _secondLeadEngine, _train.getSecondLegStartRouteLocation(),
cabooseOrFredTerminatesSecondLeg, true);
}
// departure caboose or car with FRED
getCaboose(_train.getCabooseRoad(), _train.getLeadEngine(), _train.getTrainDepartsRouteLocation(),
cabooseOrFredTerminatesFirstLeg, _train.isCabooseNeeded());
getCarWithFred(_train.getCabooseRoad(), _train.getTrainDepartsRouteLocation(), cabooseOrFredTerminatesFirstLeg);
}
/**
* Ask which staging track the train is to depart on.
*
* @return The departure track the user selected.
*/
private Track promptFromStagingDialog() {
List<Track> tracksIn = _departLocation.getTracksByNameList(null);
List<Track> validTracks = new ArrayList<>();
// only show valid tracks
for (Track track : tracksIn) {
if (checkDepartureStagingTrack(track)) {
validTracks.add(track);
}
}
if (validTracks.size() > 1) {
// need an object array for dialog window
Object[] tracks = new Object[validTracks.size()];
for (int i = 0; i < validTracks.size(); i++) {
tracks[i] = validTracks.get(i);
}
Track selected = (Track) JOptionPane.showInputDialog(null,
MessageFormat.format(Bundle.getMessage("TrainDepartingStaging"),
new Object[] { _train.getName(), _departLocation.getName() }),
Bundle.getMessage("SelectDepartureTrack"), JOptionPane.QUESTION_MESSAGE, null, tracks, null);
if (selected != null) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildUserSelectedDeparture"),