use of jmri.DccLocoAddress in project JMRI by JMRI.
the class ConsistFile method consistToXml.
/**
* convert a Consist to XML.
*
* @param consist a Consist object to write to the file
* @return an Element representing the consist.
*/
private Element consistToXml(Consist consist) {
Element e = new Element("consist");
e.setAttribute("id", consist.getConsistID());
e.setAttribute("consistNumber", "" + consist.getConsistAddress().getNumber());
e.setAttribute("longAddress", consist.getConsistAddress().isLongAddress() ? "yes" : "no");
e.setAttribute("type", consist.getConsistType() == Consist.ADVANCED_CONSIST ? "DAC" : "CSAC");
ArrayList<DccLocoAddress> addressList = consist.getConsistList();
for (int i = 0; i < addressList.size(); i++) {
DccLocoAddress locoaddress = addressList.get(i);
Element eng = new Element("loco");
eng.setAttribute("dccLocoAddress", "" + locoaddress.getNumber());
eng.setAttribute("longAddress", locoaddress.isLongAddress() ? "yes" : "no");
eng.setAttribute("locoDir", consist.getLocoDirection(locoaddress) ? "normal" : "reverse");
int position = consist.getPosition(locoaddress);
switch(position) {
case Consist.POSITION_LEAD:
eng.setAttribute("locoName", "lead");
break;
case Consist.POSITION_TRAIL:
eng.setAttribute("locoName", "rear");
break;
default:
eng.setAttribute("locoName", "mid");
eng.setAttribute("locoMidNumber", "" + position);
break;
}
String rosterId = consist.getRosterId(locoaddress);
if (rosterId != null) {
eng.setAttribute("locoRosterId", rosterId);
}
e.addContent(eng);
}
return (e);
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class ConsistFile method consistFromXml.
/**
* Load a Consist from the consist elements in the file.
*
* @param consist a JDOM element containing a consist
*/
@SuppressWarnings("unchecked")
private void consistFromXml(Element consist) {
Attribute type, cnumber, isCLong, cID;
Consist newConsist;
// Read the consist address from the file and create the
// consisit in memory if it doesn't exist already.
cnumber = consist.getAttribute("consistNumber");
isCLong = consist.getAttribute("longAddress");
DccLocoAddress consistAddress;
if (isCLong != null) {
log.debug("adding consist {} with longAddress set to {}.", cnumber, isCLong.getValue());
try {
int number = Integer.parseInt(cnumber.getValue());
consistAddress = new DccLocoAddress(number, isCLong.getValue().equals("yes"));
} catch (NumberFormatException e) {
log.debug("Consist number not an integer");
return;
}
} else {
log.debug("adding consist {} with default long address setting.", cnumber);
consistAddress = new DccLocoAddress(Integer.parseInt(cnumber.getValue()), false);
}
newConsist = consistMan.getConsist(consistAddress);
if (!(newConsist.getConsistList().isEmpty())) {
log.debug("Consist {} is not empty. Using version in memory.", consistAddress.toString());
return;
}
// read and set the consist type
type = consist.getAttribute("type");
if (type != null) {
// use the value read from the file
newConsist.setConsistType((type.getValue().equals("CSAC")) ? Consist.CS_CONSIST : Consist.ADVANCED_CONSIST);
} else {
// use the default (DAC)
newConsist.setConsistType(Consist.ADVANCED_CONSIST);
}
// Read the consist ID from the file;
cID = consist.getAttribute("id");
if (cID != null) {
// use the value read from the file
newConsist.setConsistID(cID.getValue());
}
// read each child of locomotive in the consist from the file
// and restore it's information to memory.
Iterator<Element> childIterator = consist.getDescendants(new ElementFilter("loco"));
try {
Element e;
do {
e = childIterator.next();
Attribute number, isLong, direction, position, rosterId;
number = e.getAttribute("dccLocoAddress");
isLong = e.getAttribute("longAddress");
direction = e.getAttribute("locoDir");
position = e.getAttribute("locoName");
rosterId = e.getAttribute("locoRosterId");
log.debug("adding Loco {}", number);
// Use restore so we DO NOT cause send any commands
// to the command station as we recreate the consist.
DccLocoAddress address;
if (isLong != null && direction != null) {
// use the values from the file
log.debug("using direction from file {}", direction.getValue());
address = new DccLocoAddress(Integer.parseInt(number.getValue()), isLong.getValue().equals("yes"));
newConsist.restore(address, direction.getValue().equals("normal"));
} else if (isLong == null && direction != null) {
// use the direction from the file
// but set as long address
log.debug("using direction from file {}", direction.getValue());
address = new DccLocoAddress(Integer.parseInt(number.getValue()), true);
newConsist.restore(address, direction.getValue().equals("normal"));
} else if (isLong != null && direction == null) {
// use the default direction
// but the long/short value from the file
address = new DccLocoAddress(Integer.parseInt(number.getValue()), isLong.getValue().equals("yes"));
newConsist.restore(address, true);
} else {
// use the default values long address
// and normal direction
address = new DccLocoAddress(Integer.parseInt(number.getValue()), true);
newConsist.restore(address, true);
}
if (position != null && !position.getValue().equals("mid")) {
if (position.getValue().equals("lead")) {
newConsist.setPosition(address, Consist.POSITION_LEAD);
} else if (position.getValue().equals("rear")) {
newConsist.setPosition(address, Consist.POSITION_TRAIL);
}
} else {
Attribute midNumber = e.getAttribute("locoMidNumber");
if (midNumber != null) {
int pos = Integer.parseInt(midNumber.getValue());
newConsist.setPosition(address, pos);
}
}
if (rosterId != null) {
newConsist.setRosterId(address, rosterId.getValue());
}
} while (true);
} catch (NoSuchElementException nse) {
log.debug("end of loco list");
}
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class ConsistToolFrame method deleteButtonActionPerformed.
public void deleteButtonActionPerformed(ActionEvent e) {
if (adrSelector.getAddress() == null) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("NoConsistSelectedError"));
return;
}
DccLocoAddress address = adrSelector.getAddress();
ConsistMan.getConsist(address);
/*ArrayList<DccLocoAddress> addressList = tempConsist.getConsistList();
addressList.forEach((locoaddress)->{
if (log.isDebugEnabled()) {
log.debug("Deleting Locomotive: " + locoaddress.toString());
}
try {
tempConsist.remove(locoaddress);
} catch (Exception ex) {
log.error("Error removing address "
+ locoaddress.toString()
+ " from consist "
+ address.toString());
}
});*/
try {
ConsistMan.delConsist(address);
} catch (Exception ex) {
log.error("Error delting consist " + address);
}
adrSelector.reset();
adrSelector.setEnabled(true);
initializeConsistBox();
try {
consistFile.writeFile(ConsistMan.getConsistList());
} catch (Exception ex) {
log.warn("error writing consist file: " + ex);
}
resetLocoButtonActionPerformed(e);
canAdd();
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class ConsistToolFrame method canAdd.
// Check to see if a consist address is selected, and if it
// is, dissable the "add button" if the maximum consist size is reached
public void canAdd() {
// if the maximum size is reached
if (adrSelector.getAddress() != null) {
DccLocoAddress address = adrSelector.getAddress();
if (consistModel.getRowCount() == ConsistMan.getConsist(address).sizeLimit()) {
locoSelector.setEnabled(false);
locoRosterBox.setEnabled(false);
addLocoButton.setEnabled(false);
resetLocoButton.setEnabled(false);
locoDirectionNormal.setEnabled(false);
} else {
locoSelector.setEnabled(true);
locoRosterBox.setEnabled(true);
addLocoButton.setEnabled(true);
resetLocoButton.setEnabled(true);
locoDirectionNormal.setEnabled(false);
// don't let the user change the direction
if (consistModel.getRowCount() == 0) {
locoDirectionNormal.setEnabled(false);
} else {
locoDirectionNormal.setEnabled(true);
}
}
} else {
locoSelector.setEnabled(true);
locoRosterBox.setEnabled(true);
addLocoButton.setEnabled(true);
resetLocoButton.setEnabled(true);
locoDirectionNormal.setEnabled(false);
// the user change the direction
if (consistModel.getRowCount() == 0) {
locoDirectionNormal.setEnabled(false);
} else {
locoDirectionNormal.setEnabled(true);
}
}
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class ConsistToolFrame method reverseButtonActionPerformed.
public void reverseButtonActionPerformed(ActionEvent e) {
if (adrSelector.getAddress() == null) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("NoConsistSelectedError"));
return;
}
// make sure any new locomotives are added to the consist.
addLocoButtonActionPerformed(e);
/*
* get the array list of the locomotives in the consist
*/
DccLocoAddress address = adrSelector.getAddress();
Consist tempConsist = ConsistMan.getConsist(address);
tempConsist.reverse();
}
Aggregations