/
CarLoads.java
598 lines (551 loc) · 20.3 KB
/
CarLoads.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
package jmri.jmrit.operations.rollingstock.cars;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import javax.swing.JComboBox;
import org.jdom2.Attribute;
import org.jdom2.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jmri.InstanceManager;
import jmri.InstanceManagerAutoDefault;
import jmri.jmrit.operations.rollingstock.RollingStockAttribute;
import jmri.jmrit.operations.trains.TrainCommon;
/**
* Represents the loads that cars can have.
*
* @author Daniel Boudreau Copyright (C) 2008, 2014
*/
public class CarLoads extends RollingStockAttribute implements InstanceManagerAutoDefault {
protected Hashtable<String, List<CarLoad>> listCarLoads = new Hashtable<>();
protected String _emptyName = Bundle.getMessage("EmptyCar");
protected String _loadName = Bundle.getMessage("LoadedCar");
public static final String NONE = ""; // NOI18N
// for property change
public static final String LOAD_CHANGED_PROPERTY = "CarLoads_Load"; // NOI18N
public static final String LOAD_TYPE_CHANGED_PROPERTY = "CarLoads_Load_Type"; // NOI18N
public static final String LOAD_PRIORITY_CHANGED_PROPERTY = "CarLoads_Load_Priority"; // NOI18N
public static final String LOAD_NAME_CHANGED_PROPERTY = "CarLoads_Name"; // NOI18N
public static final String LOAD_COMMENT_CHANGED_PROPERTY = "CarLoads_Load_Comment"; // NOI18N
public CarLoads() {
}
/**
* Add a car type with specific loads
*
* @param type car type
*/
public void addType(String type) {
listCarLoads.put(type, new ArrayList<>());
}
/**
* Replace a car type. Transfers load priority, drop and load comments.
*
* @param oldType old car type
* @param newType new car type
*/
public void replaceType(String oldType, String newType) {
List<String> names = getNames(oldType);
addType(newType);
for (String name : names) {
addName(newType, name);
setPriority(newType, name, getPriority(oldType, name));
setDropComment(newType, name, getDropComment(oldType, name));
setPickupComment(newType, name, getPickupComment(oldType, name));
}
listCarLoads.remove(oldType);
}
/**
* Gets the appropriate car loads for the car's type.
*
* @param type Car type
*
* @return JComboBox with car loads starting with empty string.
*/
public JComboBox<String> getSelectComboBox(String type) {
JComboBox<String> box = new JComboBox<>();
box.addItem(NONE);
for (String load : getNames(type)) {
box.addItem(load);
}
return box;
}
/**
* Gets the appropriate car loads for the car's type.
*
* @param type Car type
*
* @return JComboBox with car loads.
*/
public JComboBox<String> getComboBox(String type) {
JComboBox<String> box = new JComboBox<>();
updateComboBox(type, box);
return box;
}
/**
* Gets a combobox with the available priorities
*
* @return JComboBox with car priorities.
*/
public JComboBox<String> getPriorityComboBox() {
JComboBox<String> box = new JComboBox<>();
box.addItem(CarLoad.PRIORITY_LOW);
box.addItem(CarLoad.PRIORITY_HIGH);
return box;
}
/**
* Gets a combobox with the available load types: empty and load
*
* @return JComboBox with load types: LOAD_TYPE_EMPTY and LOAD_TYPE_LOAD
*/
public JComboBox<String> getLoadTypesComboBox() {
JComboBox<String> box = new JComboBox<>();
box.addItem(CarLoad.LOAD_TYPE_EMPTY);
box.addItem(CarLoad.LOAD_TYPE_LOAD);
return box;
}
/**
* Gets a sorted list of load names for a given car type
*
* @param type car type
* @return list of load names
*/
public List<String> getNames(String type) {
List<String> names = new ArrayList<>();
if (type == null) {
names.add(getDefaultEmptyName());
names.add(getDefaultLoadName());
return names;
}
List<CarLoad> loads = listCarLoads.get(type);
if (loads == null) {
addType(type);
loads = listCarLoads.get(type);
}
if (loads.isEmpty()) {
loads.add(new CarLoad(getDefaultEmptyName()));
loads.add(new CarLoad(getDefaultLoadName()));
}
for (CarLoad carLoad : loads) {
names.add(carLoad.getName());
}
java.util.Collections.sort(names);
return names;
}
/**
* Add a load name for the car type.
*
* @param type car type.
* @param name load name.
*/
public void addName(String type, String name) {
// don't add if name already exists
if (containsName(type, name)) {
return;
}
List<CarLoad> loads = listCarLoads.get(type);
if (loads == null) {
log.debug("car type ({}) does not exist", type);
return;
}
loads.add(new CarLoad(name));
maxNameLength = 0; // reset maximum name length
setDirtyAndFirePropertyChange(LOAD_CHANGED_PROPERTY, null, name);
}
public void deleteName(String type, String name) {
List<CarLoad> loads = listCarLoads.get(type);
if (loads == null) {
log.debug("car type ({}) does not exist", type);
return;
}
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
loads.remove(cl);
break;
}
}
maxNameLength = 0; // reset maximum name length
setDirtyAndFirePropertyChange(LOAD_CHANGED_PROPERTY, name, null);
}
/**
* Determines if a car type can have a specific load name.
*
* @param type car type.
* @param name load name.
* @return true if car can have this load name.
*/
public boolean containsName(String type, String name) {
List<String> names = getNames(type);
return names.contains(name);
}
public void updateComboBox(String type, JComboBox<String> box) {
box.removeAllItems();
List<String> names = getNames(type);
for (String name : names) {
box.addItem(name);
}
}
/**
* Update a JComboBox with all load names for every type of car.
*
* @param box the combo box to update
*/
@Override
public void updateComboBox(JComboBox<String> box) {
box.removeAllItems();
List<String> names = new ArrayList<>();
for (String type : InstanceManager.getDefault(CarTypes.class).getNames()) {
for (String load : getNames(type)) {
if (!names.contains(load)) {
names.add(load);
}
}
}
java.util.Collections.sort(names);
for (String load : names) {
box.addItem(load);
}
}
public void updateRweComboBox(String type, JComboBox<String> box) {
box.removeAllItems();
List<String> loads = getNames(type);
for (String name : loads) {
if (getLoadType(type, name).equals(CarLoad.LOAD_TYPE_EMPTY)) {
box.addItem(name);
}
}
}
public void updateRwlComboBox(String type, JComboBox<String> box) {
box.removeAllItems();
List<String> loads = getNames(type);
for (String name : loads) {
if (getLoadType(type, name).equals(CarLoad.LOAD_TYPE_LOAD)) {
box.addItem(name);
}
}
}
public void replaceName(String type, String oldName, String newName) {
addName(type, newName);
deleteName(type, oldName);
setDirtyAndFirePropertyChange(LOAD_NAME_CHANGED_PROPERTY, oldName, newName);
}
public String getDefaultLoadName() {
return _loadName;
}
public void setDefaultLoadName(String name) {
String old = _loadName;
_loadName = name;
setDirtyAndFirePropertyChange(LOAD_NAME_CHANGED_PROPERTY, old, name);
}
public String getDefaultEmptyName() {
return _emptyName;
}
public void setDefaultEmptyName(String name) {
String old = _emptyName;
_emptyName = name;
setDirtyAndFirePropertyChange(LOAD_NAME_CHANGED_PROPERTY, old, name);
}
/**
* Sets the load type, empty or load.
*
* @param type car type.
* @param name load name.
* @param loadType load type: LOAD_TYPE_EMPTY or LOAD_TYPE_LOAD.
*/
public void setLoadType(String type, String name, String loadType) {
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
String oldType = cl.getLoadType();
cl.setLoadType(loadType);
if (!oldType.equals(loadType)) {
setDirtyAndFirePropertyChange(LOAD_TYPE_CHANGED_PROPERTY, oldType, loadType);
}
}
}
}
/**
* Get the load type, empty or load.
*
* @param type car type.
* @param name load name.
* @return load type, LOAD_TYPE_EMPTY or LOAD_TYPE_LOAD.
*/
public String getLoadType(String type, String name) {
if (!containsName(type, name)) {
if (name != null && name.equals(getDefaultEmptyName())) {
return CarLoad.LOAD_TYPE_EMPTY;
}
return CarLoad.LOAD_TYPE_LOAD;
}
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
return cl.getLoadType();
}
}
return "error"; // NOI18N
}
/**
* Sets a loads priority.
*
* @param type car type.
* @param name load name.
* @param priority load priority, PRIORITY_LOW or PRIORITY_HIGH.
*/
public void setPriority(String type, String name, String priority) {
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
String oldPriority = cl.getPriority();
cl.setPriority(priority);
if (!oldPriority.equals(priority)) {
setDirtyAndFirePropertyChange(LOAD_PRIORITY_CHANGED_PROPERTY, oldPriority, priority);
}
}
}
}
/**
* Get's a load's priority.
*
* @param type car type.
* @param name load name.
* @return load priority, PRIORITY_LOW or PRIORITY_HIGH.
*/
public String getPriority(String type, String name) {
if (!containsName(type, name)) {
return CarLoad.PRIORITY_LOW;
}
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
return cl.getPriority();
}
}
return "error"; // NOI18N
}
/**
* Sets the comment for a car type's load
* @param type the car type
* @param name the load name
* @param comment the comment
*/
public void setPickupComment(String type, String name, String comment) {
if (!containsName(type, name)) {
return;
}
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
String oldComment = cl.getPickupComment();
cl.setPickupComment(comment);
if (!oldComment.equals(comment)) {
setDirtyAndFirePropertyChange(LOAD_COMMENT_CHANGED_PROPERTY, oldComment, comment);
}
}
}
}
public String getPickupComment(String type, String name) {
if (!containsName(type, name)) {
return NONE;
}
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
return cl.getPickupComment();
}
}
return NONE;
}
public void setDropComment(String type, String name, String comment) {
if (!containsName(type, name)) {
return;
}
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
String oldComment = cl.getDropComment();
cl.setDropComment(comment);
if (!oldComment.equals(comment)) {
setDirtyAndFirePropertyChange(LOAD_COMMENT_CHANGED_PROPERTY, oldComment, comment);
}
}
}
}
public String getDropComment(String type, String name) {
if (!containsName(type, name)) {
return NONE;
}
List<CarLoad> loads = listCarLoads.get(type);
for (CarLoad cl : loads) {
if (cl.getName().equals(name)) {
return cl.getDropComment();
}
}
return NONE;
}
@Override
public int getMaxNameLength() {
if (maxNameLength == 0) {
maxName = "";
maxNameLength = MIN_NAME_LENGTH;
Enumeration<String> en = listCarLoads.keys();
while (en.hasMoreElements()) {
String key = en.nextElement();
List<CarLoad> loads = listCarLoads.get(key);
for (CarLoad load : loads) {
if (load.getName().split(TrainCommon.HYPHEN)[0].length() > maxNameLength) {
maxName = load.getName().split(TrainCommon.HYPHEN)[0];
maxNameLength = load.getName().split(TrainCommon.HYPHEN)[0].length();
}
}
}
log.info("Max car load name ({}) length {}", maxName, maxNameLength);
}
return maxNameLength;
}
private List<CarLoad> getSortedList(String type) {
List<CarLoad> loads = listCarLoads.get(type);
List<String> names = getNames(type);
List<CarLoad> out = new ArrayList<>();
// return a list sorted by load name
for (String name : names) {
for (CarLoad carLoad : loads) {
if (name.equals(carLoad.getName())) {
out.add(carLoad);
break;
}
}
}
return out;
}
@SuppressWarnings("unchecked")
public Hashtable<String, List<CarLoad>> getList() {
return (Hashtable<String, List<CarLoad>>) listCarLoads.clone();
}
@Override
public void dispose() {
listCarLoads.clear();
setDefaultEmptyName(Bundle.getMessage("EmptyCar"));
setDefaultLoadName(Bundle.getMessage("LoadedCar"));
super.dispose();
}
/**
* Create an XML element to represent this Entry. This member has to remain
* synchronized with the detailed DTD in operations-cars.dtd.
*
* @param root The common Element for operations-cars.dtd.
*
*/
public void store(Element root) {
Element values = new Element(Xml.LOADS);
// store default load and empty
Element defaults = new Element(Xml.DEFAULTS);
defaults.setAttribute(Xml.EMPTY, getDefaultEmptyName());
defaults.setAttribute(Xml.LOAD, getDefaultLoadName());
values.addContent(defaults);
// store loads based on car types
Enumeration<String> en = listCarLoads.keys();
while (en.hasMoreElements()) {
String carType = en.nextElement();
// check to see if car type still exists
if (!InstanceManager.getDefault(CarTypes.class).containsName(carType)) {
continue;
}
List<CarLoad> loads = getSortedList(carType);
Element xmlLoad = new Element(Xml.LOAD);
xmlLoad.setAttribute(Xml.TYPE, carType);
boolean mustStore = false; // only store loads that aren't the defaults
for (CarLoad load : loads) {
// don't store the defaults / low priority / no comment
if ((load.getName().equals(getDefaultEmptyName()) || load.getName().equals(getDefaultLoadName()))
&& load.getPriority().equals(CarLoad.PRIORITY_LOW)
&& load.getPickupComment().equals(CarLoad.NONE)
&& load.getDropComment().equals(CarLoad.NONE)) {
continue;
}
Element xmlCarLoad = new Element(Xml.CAR_LOAD);
xmlCarLoad.setAttribute(Xml.NAME, load.getName());
if (!load.getPriority().equals(CarLoad.PRIORITY_LOW)) {
xmlCarLoad.setAttribute(Xml.PRIORITY, load.getPriority());
mustStore = true; // must store
}
if (!load.getPickupComment().equals(CarLoad.NONE)) {
xmlCarLoad.setAttribute(Xml.PICKUP_COMMENT, load.getPickupComment());
mustStore = true; // must store
}
if (!load.getDropComment().equals(CarLoad.NONE)) {
xmlCarLoad.setAttribute(Xml.DROP_COMMENT, load.getDropComment());
mustStore = true; // must store
}
xmlCarLoad.setAttribute(Xml.LOAD_TYPE, load.getLoadType());
xmlLoad.addContent(xmlCarLoad);
}
if (loads.size() > 2 || mustStore) {
values.addContent(xmlLoad);
}
}
root.addContent(values);
}
public void load(Element e) {
if (e.getChild(Xml.LOADS) == null) {
return;
}
Attribute a;
Element defaults = e.getChild(Xml.LOADS).getChild(Xml.DEFAULTS);
if (defaults != null) {
if ((a = defaults.getAttribute(Xml.LOAD)) != null) {
_loadName = a.getValue();
}
if ((a = defaults.getAttribute(Xml.EMPTY)) != null) {
_emptyName = a.getValue();
}
}
List<Element> eLoads = e.getChild(Xml.LOADS).getChildren(Xml.LOAD);
log.debug("readFile sees {} car loads", eLoads.size());
for (Element eLoad : eLoads) {
if ((a = eLoad.getAttribute(Xml.TYPE)) != null) {
String type = a.getValue();
addType(type);
// old style had a list of names
if ((a = eLoad.getAttribute(Xml.NAMES)) != null) {
String names = a.getValue();
String[] loadNames = names.split("%%");// NOI18N
java.util.Arrays.sort(loadNames);
log.debug("Car load type: {} loads: {}", type, names);
// addName puts new items at the start, so reverse load
for (int j = loadNames.length; j > 0;) {
addName(type, loadNames[--j]);
}
}
// new style load and comments
List<Element> eCarLoads = eLoad.getChildren(Xml.CAR_LOAD);
log.debug("{} car loads for type: {}", eCarLoads.size(), type);
for (Element eCarLoad : eCarLoads) {
if ((a = eCarLoad.getAttribute(Xml.NAME)) != null) {
String name = a.getValue();
addName(type, name);
if ((a = eCarLoad.getAttribute(Xml.PRIORITY)) != null) {
setPriority(type, name, a.getValue());
}
if ((a = eCarLoad.getAttribute(Xml.PICKUP_COMMENT)) != null) {
setPickupComment(type, name, a.getValue());
}
if ((a = eCarLoad.getAttribute(Xml.DROP_COMMENT)) != null) {
setDropComment(type, name, a.getValue());
}
if ((a = eCarLoad.getAttribute(Xml.LOAD_TYPE)) != null) {
setLoadType(type, name, a.getValue());
}
}
}
}
}
}
protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) {
// Set dirty
InstanceManager.getDefault(CarManagerXml.class).setDirty(true);
super.firePropertyChange(p, old, n);
}
private final static Logger log = LoggerFactory.getLogger(CarLoads.class);
}