use of jmri.Section in project JMRI by JMRI.
the class SectionManagerXml method store.
/**
* Implementation for storing the contents of a SectionManager
*
* @param o Object to store, of type SectionManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element sections = new Element("sections");
setStoreElementClass(sections);
SectionManager tm = (SectionManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not Sections to include
if (!iter.hasNext()) {
return null;
}
// store the Section
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
} else {
log.debug("Section system name is " + sname);
Section x = tm.getBySystemName(sname);
if (x.getSectionType() != Section.DYNAMICADHOC) {
Element elem = new Element("section");
elem.addContent(new Element("systemName").addContent(sname));
// store common part
storeCommon(x, elem);
String txt = "userdefined";
if (x.getSectionType() == Section.SIGNALMASTLOGIC) {
txt = "signalmastlogic";
}
elem.setAttribute("creationtype", txt);
txt = x.getForwardStoppingSensorName();
if ((txt != null) && (!txt.equals(""))) {
elem.setAttribute("fstopsensorname", txt);
}
txt = x.getReverseStoppingSensorName();
if ((txt != null) && (!txt.equals(""))) {
elem.setAttribute("rstopsensorname", txt);
}
txt = x.getForwardBlockingSensorName();
if ((txt != null) && (!txt.equals(""))) {
elem.setAttribute("fsensorname", txt);
}
txt = x.getReverseBlockingSensorName();
if ((txt != null) && (!txt.equals(""))) {
elem.setAttribute("rsensorname", txt);
}
if (x.getSectionType() == Section.USERDEFINED) {
// save child block entries
int index = 0;
Block b = x.getBlockBySequenceNumber(index);
Element bElem = null;
while (b != null) {
bElem = new Element("blockentry");
bElem.setAttribute("sName", b.getSystemName());
bElem.setAttribute("order", Integer.toString(index));
elem.addContent(bElem);
index++;
b = x.getBlockBySequenceNumber(index);
}
// save child entry points
List<EntryPoint> epList = x.getEntryPointList();
Element epElem = null;
EntryPoint ep = null;
for (int i = 0; i < epList.size(); i++) {
ep = epList.get(i);
if (ep != null) {
epElem = new Element("entrypoint");
// add some protection against a reading problem
if (ep.getFromBlock() == null) {
log.error("Unexpected null getFromBlock while storing ep " + i + " in Section " + sname + ", skipped");
break;
}
epElem.setAttribute("fromblock", ep.getFromBlock().getSystemName());
if (ep.getBlock() == null) {
log.error("Unexpected null getBlock while storing ep " + i + " in Section " + sname + ", skipped");
break;
}
epElem.setAttribute("toblock", ep.getBlock().getSystemName());
epElem.setAttribute("direction", Integer.toString(ep.getDirection()));
epElem.setAttribute("fixed", "" + (ep.isFixed() ? "yes" : "no"));
epElem.setAttribute("fromblockdirection", "" + ep.getFromBlockDirection());
elem.addContent(epElem);
}
}
}
sections.addContent(elem);
}
}
}
}
return (sections);
}
use of jmri.Section in project JMRI by JMRI.
the class TransitTableAction method replacePrimaryForSeqPressed.
void replacePrimaryForSeqPressed(ActionEvent e) {
int seq = getSeqNum();
if (seq == 0) {
return;
}
Section sOld = null;
ArrayList<Section> altOldList = new ArrayList<>();
Section beforeSection = null;
int beforeSectionDirection = 0;
Section afterSection = null;
int afterSectionDirection = 0;
int index = -1;
for (int i = 0; i < sectionList.size(); i++) {
if ((sequence[i] == seq) && (!alternate[i])) {
sOld = sectionList.get(i);
index = i;
}
if ((sequence[i] == seq) && alternate[i]) {
altOldList.add(sectionList.get(i));
}
if ((sequence[i] == (seq - 1)) && (!alternate[i])) {
beforeSection = sectionList.get(i);
beforeSectionDirection = direction[i];
}
if ((sequence[i] == (seq + 1)) && (!alternate[i])) {
afterSection = sectionList.get(i);
afterSectionDirection = Section.FORWARD;
if (afterSectionDirection == direction[i]) {
afterSectionDirection = Section.REVERSE;
}
}
}
if (sOld == null) {
log.error("Missing primary Section for seq = " + seq);
return;
}
ArrayList<Section> possibles = new ArrayList<>();
int[] possiblesDirection = new int[150];
ArrayList<String> possibleNames = new ArrayList<>();
List<String> allSections = sectionManager.getSystemNameList();
for (int i = 0; i < allSections.size(); i++) {
Section mayBeSection = null;
String mayBeName = allSections.get(i);
int mayBeDirection = 0;
Section s = sectionManager.getBySystemName(mayBeName);
if ((s != null) && (s != sOld) && (s != beforeSection) && (s != afterSection) && (!inSectionList(s, altOldList))) {
if (beforeSection != null) {
if (forwardConnected(s, beforeSection, beforeSectionDirection)) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
mayBeName = mayBeName + "( " + uname + " )";
}
mayBeSection = s;
mayBeDirection = Section.FORWARD;
} else if (reverseConnected(s, beforeSection, beforeSectionDirection)) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
mayBeName = mayBeName + "( " + uname + " )";
}
mayBeSection = s;
mayBeDirection = Section.REVERSE;
}
if ((mayBeSection != null) && (afterSection != null)) {
if (mayBeDirection == Section.REVERSE) {
if (!forwardConnected(s, afterSection, afterSectionDirection)) {
mayBeSection = null;
}
} else {
if (!reverseConnected(s, afterSection, afterSectionDirection)) {
mayBeSection = null;
}
}
}
} else if (afterSection != null) {
if (forwardConnected(s, afterSection, afterSectionDirection)) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
mayBeName = mayBeName + "( " + uname + " )";
}
mayBeSection = s;
mayBeDirection = Section.REVERSE;
} else if (reverseConnected(s, afterSection, afterSectionDirection)) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
mayBeName = mayBeName + "( " + uname + " )";
}
mayBeSection = s;
mayBeDirection = Section.FORWARD;
}
} else {
mayBeSection = s;
mayBeDirection = Section.FORWARD;
}
if (mayBeSection != null) {
possibles.add(mayBeSection);
possiblesDirection[possibles.size() - 1] = mayBeDirection;
possibleNames.add(mayBeName);
}
}
}
if (possibles.size() == 0) {
javax.swing.JOptionPane.showMessageDialog(addFrame, java.text.MessageFormat.format(rbx.getString("Message36"), new Object[] { "" + seq }), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
return;
}
int k = 0;
if (possibles.size() > 1) {
Object[] choices = new Object[possibles.size()];
for (int j = 0; j < possibles.size(); j++) {
choices[j] = possibleNames.get(j);
}
Object selName = JOptionPane.showInputDialog(addFrame, rbx.getString("ReplacePrimaryChoice"), rbx.getString("ReplacePrimaryTitle"), JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
if (selName == null) {
return;
}
for (int j = 0; j < possibles.size(); j++) {
if (selName.equals(choices[j])) {
k = j;
}
}
}
sectionList.remove(index);
sectionList.add(index, possibles.get(k));
direction[index] = possiblesDirection[k];
if (index == (sectionList.size() - 1)) {
curSection = sectionList.get(index);
curSectionDirection = direction[index];
} else if (index == (sectionList.size() - 2)) {
prevSection = sectionList.get(index);
prevSectionDirection = direction[index];
}
initializeSectionCombos();
sectionTableModel.fireTableDataChanged();
}
use of jmri.Section in project JMRI by JMRI.
the class TransitTableAction method initializeSectionCombos.
private void initializeSectionCombos() {
List<String> allSections = sectionManager.getSystemNameList();
primarySectionBox.removeAllItems();
alternateSectionBox.removeAllItems();
insertAtBeginningBox.removeAllItems();
primarySectionBoxList.clear();
alternateSectionBoxList.clear();
insertAtBeginningBoxList.clear();
if (sectionList.size() == 0) {
// no Sections currently in Transit - all Sections and all Directions OK
for (int i = 0; i < allSections.size(); i++) {
String sName = allSections.get(i);
Section s = sectionManager.getBySystemName(sName);
if (s != null) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
sName = sName + "( " + uname + " )";
}
primarySectionBox.addItem(sName);
primarySectionBoxList.add(s);
priSectionDirection[primarySectionBoxList.size() - 1] = Section.FORWARD;
}
}
} else {
// limit to Sections that connect to the current Section and are not the previous Section
for (int i = 0; i < allSections.size(); i++) {
String sName = allSections.get(i);
Section s = sectionManager.getBySystemName(sName);
if (s != null) {
if ((s != prevSection) && (forwardConnected(s, curSection, curSectionDirection))) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
sName = sName + "( " + uname + " )";
}
primarySectionBox.addItem(sName);
primarySectionBoxList.add(s);
priSectionDirection[primarySectionBoxList.size() - 1] = Section.FORWARD;
} else if ((s != prevSection) && (reverseConnected(s, curSection, curSectionDirection))) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
sName = sName + "( " + uname + " )";
}
primarySectionBox.addItem(sName);
primarySectionBoxList.add(s);
priSectionDirection[primarySectionBoxList.size() - 1] = Section.REVERSE;
}
}
}
// check if there are any alternate Section choices
if (prevSection != null) {
for (int i = 0; i < allSections.size(); i++) {
String sName = allSections.get(i);
Section s = sectionManager.getBySystemName(sName);
if (s != null) {
if ((notIncludedWithSeq(s, curSequenceNum)) && forwardConnected(s, prevSection, prevSectionDirection)) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
sName = sName + "( " + uname + " )";
}
alternateSectionBox.addItem(sName);
alternateSectionBoxList.add(s);
altSectionDirection[alternateSectionBoxList.size() - 1] = Section.FORWARD;
} else if (notIncludedWithSeq(s, curSequenceNum) && reverseConnected(s, prevSection, prevSectionDirection)) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
sName = sName + "( " + uname + " )";
}
alternateSectionBox.addItem(sName);
alternateSectionBoxList.add(s);
altSectionDirection[alternateSectionBoxList.size() - 1] = Section.REVERSE;
}
}
}
}
// check if there are any Sections available to be inserted at beginning
Section firstSection = sectionList.get(0);
int testDirection = Section.FORWARD;
if (direction[0] == Section.FORWARD) {
testDirection = Section.REVERSE;
}
for (int i = 0; i < allSections.size(); i++) {
String sName = allSections.get(i);
Section s = sectionManager.getBySystemName(sName);
if (s != null) {
if ((s != firstSection) && (forwardConnected(s, firstSection, testDirection))) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
sName = sName + "( " + uname + " )";
}
insertAtBeginningBox.addItem(sName);
insertAtBeginningBoxList.add(s);
insertAtBeginningDirection[insertAtBeginningBoxList.size() - 1] = Section.REVERSE;
} else if ((s != firstSection) && (reverseConnected(s, firstSection, testDirection))) {
String uname = s.getUserName();
if ((uname != null) && (!uname.equals(""))) {
sName = sName + "( " + uname + " )";
}
insertAtBeginningBox.addItem(sName);
insertAtBeginningBoxList.add(s);
insertAtBeginningDirection[insertAtBeginningBoxList.size() - 1] = Section.FORWARD;
}
}
}
}
}
use of jmri.Section in project JMRI by JMRI.
the class ActiveTrain method getBlockList.
/**
* Returns list of all Blocks occupied by or allocated to this train. They
* are in order from the tail of the train to the head of the train then on
* to the forward-most allocated block. Note that unoccupied blocks can
* exist before and after the occupied blocks.
*
* TODO: doesn't handle reversing of adjacent multi-block sections well
*
* @return the list of blocks order of occupation
*/
public ArrayList<Block> getBlockList() {
ArrayList<Block> list = new ArrayList<>();
for (int i = 0; i < mAllocatedSections.size(); i++) {
// loop thru allocated sections, then all blocks for each section
Section s = mAllocatedSections.get(i).getSection();
ArrayList<Block> bl = s.getBlockList();
if (bl.size() > 1) {
//sections with multiple blocks need extra logic
boolean blocksConnected = true;
//determine if blocks should be added in forward or reverse order based on connectivity
if (i == 0) {
//for first section, compare last block to first of next section
if (mAllocatedSections.size() > 1 && //only one section, assume forward
!connected(bl.get(bl.size() - 1), mAllocatedSections.get(i + 1).getSection().getBlockList().get(0))) {
blocksConnected = false;
}
} else {
//not first section, check for connectivity between last block in list, and first block in this section
if (!connected(list.get(list.size() - 1), bl.get(0))) {
//last block is not connected to first block, add reverse
blocksConnected = false;
}
}
if (blocksConnected) {
//blocks were connected, so add to outgoing in forward order
for (int j = 0; j < bl.size(); j++) {
Block b = bl.get(j);
list.add(b);
log.trace("block {} ({}) added to list for Section {} (fwd)", b.getDisplayName(), (b.getState() == Block.OCCUPIED ? "OCCUPIED" : "UNOCCUPIED"), s.getDisplayName());
}
} else {
//not connected, add in reverse order
for (int j = bl.size() - 1; j >= 0; j--) {
Block b = bl.get(j);
list.add(b);
log.trace("block {} ({}) added to list for Section {} (rev)", b.getDisplayName(), (b.getState() == Block.OCCUPIED ? "OCCUPIED" : "UNOCCUPIED"), s.getDisplayName());
}
}
} else {
//single block sections are simply added to the outgoing list
Block b = bl.get(0);
list.add(b);
log.trace("block {} ({}) added to list for Section {} (one)", b.getDisplayName(), (b.getState() == Block.OCCUPIED ? "OCCUPIED" : "UNOCCUPIED"), s.getDisplayName());
}
}
return list;
}
use of jmri.Section in project JMRI by JMRI.
the class TransitCreationTool method createTransit.
public Transit createTransit() throws JmriException {
TransitManager tm = InstanceManager.getDefault(jmri.TransitManager.class);
String transitName = "From " + list.get(0).getDisplayName() + " to " + list.get(list.size() - 1).getDisplayName();
Transit t = tm.createNewTransit(transitName);
if (t == null) {
log.error("Unable to create transit " + transitName);
throw new JmriException(Bundle.getMessage("TCTErrorUnableToCreate", transitName));
}
if (list.get(0) instanceof SignalMast) {
jmri.SignalMastLogicManager smlm = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
for (int i = 1; i <= list.size() - 1; i++) {
jmri.SignalMastLogic sml = smlm.getSignalMastLogic((SignalMast) list.get(i - 1));
Section sec = sml.getAssociatedSection((SignalMast) list.get(i));
//In theory sec being null would already have been tested when the signal was added.
if (sec == null) {
String error = Bundle.getMessage("TCTErrorMastPairsNoSection", list.get(i - 1).getDisplayName(), list.get(i).getDisplayName());
log.error(error);
tm.deregister(t);
t.dispose();
cancelTransitCreate();
throw new JmriException(error);
}
t.addTransitSection(new jmri.TransitSection(sec, i, Section.FORWARD));
}
}
//Once created clear the list for a fresh start.
list = new ArrayList<NamedBean>();
return t;
}
Aggregations