use of jmri.BlockManager in project JMRI by JMRI.
the class BlockManagerXml method store.
/**
* Store the contents of a BlockManager.
*
* @param o Object to store, of type BlockManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element blocks = new Element("blocks");
setStoreElementClass(blocks);
BlockManager tm = (BlockManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not blocks to include
if (!iter.hasNext()) {
return null;
}
blocks.addContent(new Element("defaultspeed").addContent(tm.getDefaultSpeed()));
// write out first set of blocks without contents
while (iter.hasNext()) {
try {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
} else {
Block b = tm.getBySystemName(sname);
// the following null check is to catch a null pointer exception that sometimes was found to happen
if (b == null) {
log.error("Null block during store - sname = " + sname);
} else {
Element elem = new Element("block");
elem.addContent(new Element("systemName").addContent(sname));
// the following null check is to catch a null pointer exception that sometimes was found to happen
String uname = b.getUserName();
if ((uname != null) && (!uname.equals(""))) {
elem.addContent(new Element("userName").addContent(b.getUserName()));
}
if (log.isDebugEnabled()) {
log.debug("initial store Block " + sname);
}
// and put this element out
blocks.addContent(elem);
}
}
} catch (Exception e) {
log.error(e.toString());
}
}
// write out again with contents
iter = tm.getSystemNameList().iterator();
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store skipped for this block");
} else {
Block b = tm.getBySystemName(sname);
// the following null check is to catch a null pointer exception that sometimes was found to happen
if (b == null) {
log.error("Null Block during store - second store skipped for this block - " + sname);
} else {
String uname = b.getUserName();
if (uname == null) {
uname = "";
}
Element elem = new Element("block");
elem.addContent(new Element("systemName").addContent(sname));
if (log.isDebugEnabled()) {
log.debug("second store Block " + sname + ":" + uname);
}
// store length and curvature attributes
elem.setAttribute("length", Float.toString(b.getLengthMm()));
elem.setAttribute("curve", Integer.toString(b.getCurvature()));
// store common parts
storeCommon(b, elem);
if ((!b.getBlockSpeed().equals("")) && !b.getBlockSpeed().contains("Global")) {
elem.addContent(new Element("speed").addContent(b.getBlockSpeed()));
}
String perm = "no";
if (b.getPermissiveWorking()) {
perm = "yes";
}
elem.addContent(new Element("permissive").addContent(perm));
// Add content. First, the sensor.
if (b.getNamedSensor() != null) {
elem.addContent(new Element("occupancysensor").addContent(b.getNamedSensor().getName()));
}
if (b.getDeniedBlocks().size() > 0) {
Element denied = new Element("deniedBlocks");
b.getDeniedBlocks().forEach((deniedBlock) -> {
denied.addContent(new Element("block").addContent(deniedBlock));
});
elem.addContent(denied);
}
// Now the Reporter
Reporter r = b.getReporter();
if (r != null) {
Element re = new Element("reporter");
re.setAttribute("systemName", r.getSystemName());
re.setAttribute("useCurrent", b.isReportingCurrent() ? "yes" : "no");
elem.addContent(re);
}
if (tm.isSavedPathInfo()) {
// then the paths
List<Path> paths = b.getPaths();
for (int i = 0; i < paths.size(); i++) {
addPath(elem, paths.get(i));
}
// and put this element out
}
blocks.addContent(elem);
}
}
}
}
return blocks;
}
use of jmri.BlockManager in project JMRI by JMRI.
the class ManageLocationsAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if (f == null || !f.isVisible()) {
// Handle the Listener
listenerLoc = VSDecoderManager.instance().getVSDecoderPreferences().getListenerPosition();
// Handle Reporters
ReporterManager rmgr = jmri.InstanceManager.getDefault(jmri.ReporterManager.class);
String[] reporterNameArray = rmgr.getSystemNameArray();
Object[][] reporterTable = new Object[reporterNameArray.length][6];
reporterMap = new HashMap<String, PhysicalLocation>();
int i = 0;
for (String s : reporterNameArray) {
Reporter r = rmgr.getByDisplayName(s);
if (r instanceof PhysicalLocationReporter) {
reporterMap.put(s, ((PhysicalLocationReporter) r).getPhysicalLocation());
PhysicalLocation p = ((PhysicalLocationReporter) r).getPhysicalLocation();
reporterTable[i][0] = s;
reporterTable[i][1] = true;
reporterTable[i][2] = p.getX();
reporterTable[i][3] = p.getY();
reporterTable[i][4] = p.getZ();
reporterTable[i][5] = p.isTunnel();
} else {
reporterTable[i][0] = s;
reporterTable[i][1] = false;
reporterTable[i][2] = Float.valueOf(0.0f);
reporterTable[i][3] = Float.valueOf(0.0f);
reporterTable[i][4] = Float.valueOf(0.0f);
reporterTable[i][5] = false;
}
i++;
}
// Handle Blocks
BlockManager bmgr = jmri.InstanceManager.getDefault(jmri.BlockManager.class);
String[] blockNameArray = bmgr.getSystemNameArray();
Object[][] blockTable = new Object[blockNameArray.length][6];
blockMap = new HashMap<String, PhysicalLocation>();
i = 0;
for (String s : blockNameArray) {
Block b = bmgr.getByDisplayName(s);
// NOTE: Unlike Reporters, all Blocks are (now) PhysicalLocationReporters, so no need to do a check here.
// We'll keep the explicit cast for now, but it's not actually necessary.
blockMap.put(s, ((PhysicalLocationReporter) b).getPhysicalLocation());
PhysicalLocation p = ((PhysicalLocationReporter) b).getPhysicalLocation();
blockTable[i][0] = s;
blockTable[i][1] = true;
blockTable[i][2] = p.getX();
blockTable[i][3] = p.getY();
blockTable[i][4] = p.getZ();
blockTable[i][5] = p.isTunnel();
i++;
}
// Handle Ops Locations
LocationManager lmgr = LocationManager.instance();
List<Location> locations = lmgr.getLocationsByIdList();
opsMap = new HashMap<String, PhysicalLocation>();
log.debug("TableSize : " + locations.size());
Object[][] opsTable = new Object[locations.size()][6];
i = 0;
for (Location l : locations) {
if (log.isDebugEnabled()) {
log.debug("i = " + i + "MLA " + l.getId() + " Name: " + l.getName() + " table " + java.util.Arrays.toString(opsTable[i]));
}
PhysicalLocation p = l.getPhysicalLocation();
Boolean use = false;
if (p == PhysicalLocation.Origin) {
use = false;
} else {
use = true;
}
opsTable[i][0] = l.getName();
opsTable[i][1] = use;
opsTable[i][2] = p.getX();
opsTable[i][3] = p.getY();
opsTable[i][4] = p.getZ();
opsTable[i][5] = p.isTunnel();
opsMap.put(l.getName(), l.getPhysicalLocation());
i++;
}
f = new ManageLocationsFrame(listenerLoc, reporterTable, opsTable, blockTable);
}
f.setExtendedState(Frame.NORMAL);
}
use of jmri.BlockManager in project JMRI by JMRI.
the class LEConnectivityTest method testShowAndClose.
@SuppressWarnings("unchecked")
public void testShowAndClose() throws Exception {
if (GraphicsEnvironment.isHeadless()) {
// Can't Assume in TestCase
return;
}
jmri.configurexml.ConfigXmlManager cm = new jmri.configurexml.ConfigXmlManager() {
};
// load and display test panel file
java.io.File f = new java.io.File("java/test/jmri/jmrit/display/layoutEditor/valid/LEConnectTest.xml");
cm.load(f);
// time for internal listeners to calm down
sleep(100);
// Find new window by name (should be more distinctive, comes from sample file)
LayoutEditor le = (LayoutEditor) jmri.util.JmriJFrame.getFrame("Connectivity Test");
Assert.assertNotNull(le);
// Panel is up, continue set up for tests.
ConnectivityUtil cu = new ConnectivityUtil(le);
Assert.assertNotNull(cu);
BlockManager bm = jmri.InstanceManager.getDefault(jmri.BlockManager.class);
Assert.assertNotNull(bm);
// Test right-handed crossover connectivity turnout settings
Block cBlock = bm.getBlock("4");
Block pBlock = bm.getBlock("6");
Block nBlock = bm.getBlock("5");
cu.getTurnoutList(cBlock, pBlock, nBlock);
ArrayList<Integer> tsList = cu.getTurnoutSettingList();
int setting = tsList.get(0).intValue();
Assert.assertEquals("6_4_5Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("5");
nBlock = bm.getBlock("6");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("5_4_6Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("5");
nBlock = bm.getBlock("2");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("5_4_2Connect", setting, Turnout.THROWN);
cBlock = bm.getBlock("2");
pBlock = bm.getBlock("1");
nBlock = bm.getBlock("3");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("1_2_3Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("3");
nBlock = bm.getBlock("1");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("3_2_1Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("1");
nBlock = bm.getBlock("4");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("1_2_4Connect", setting, Turnout.THROWN);
// Test left-handed crossover connectivity turnout settings
cBlock = bm.getBlock("14");
pBlock = bm.getBlock("13");
nBlock = bm.getBlock("17");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("13_14_17Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("17");
nBlock = bm.getBlock("13");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("17_14_13Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("17");
nBlock = bm.getBlock("12");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("17_14_12Connect", setting, Turnout.THROWN);
cBlock = bm.getBlock("12");
pBlock = bm.getBlock("11");
nBlock = bm.getBlock("15");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("11_12_15Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("15");
nBlock = bm.getBlock("11");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("15_12_11Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("15");
nBlock = bm.getBlock("14");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("15_12_14Connect", setting, Turnout.THROWN);
// Test double crossover connectivity turnout settings
cBlock = bm.getBlock("21");
pBlock = bm.getBlock("20");
nBlock = bm.getBlock("22");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("20_21_22Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("22");
nBlock = bm.getBlock("20");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("22_21_20Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("20");
nBlock = bm.getBlock("26");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("20_21_26Connect", setting, Turnout.THROWN);
cBlock = bm.getBlock("22");
pBlock = bm.getBlock("23");
nBlock = bm.getBlock("21");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("23_22_21Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("21");
nBlock = bm.getBlock("23");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("21_22_23Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("23");
nBlock = bm.getBlock("25");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("23_22_25Connect", setting, Turnout.THROWN);
cBlock = bm.getBlock("26");
pBlock = bm.getBlock("27");
nBlock = bm.getBlock("25");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("27_26_25Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("25");
nBlock = bm.getBlock("27");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("25_26_27Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("27");
nBlock = bm.getBlock("21");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("27_26_21Connect", setting, Turnout.THROWN);
cBlock = bm.getBlock("25");
pBlock = bm.getBlock("24");
nBlock = bm.getBlock("26");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("24_25_26Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("26");
nBlock = bm.getBlock("24");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("26_25_24Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("24");
nBlock = bm.getBlock("22");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("24_25_22Connect", setting, Turnout.THROWN);
// Test right handed turnout (with "wings" in same block) connectivity turnout settings
cBlock = bm.getBlock("62");
pBlock = bm.getBlock("64");
nBlock = bm.getBlock("61");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("64_62_61Connect", setting, Turnout.THROWN);
pBlock = bm.getBlock("61");
nBlock = bm.getBlock("64");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("61_62_64Connect", setting, Turnout.THROWN);
pBlock = bm.getBlock("63");
nBlock = bm.getBlock("61");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("63_62_61Connect", setting, Turnout.CLOSED);
pBlock = bm.getBlock("61");
nBlock = bm.getBlock("63");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("61_62_63Connect", setting, Turnout.CLOSED);
// Test extended track following connectivity turnout settings
// Each path must go through two turnouts, whose settings are tested in order
cBlock = bm.getBlock("32");
pBlock = bm.getBlock("31");
nBlock = bm.getBlock("33");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("31_32_33ConnectA", setting, Turnout.CLOSED);
setting = tsList.get(1).intValue();
Assert.assertEquals("31_32_33ConnectB", setting, Turnout.THROWN);
pBlock = bm.getBlock("33");
nBlock = bm.getBlock("31");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("33_32_31ConnectA", setting, Turnout.THROWN);
setting = tsList.get(1).intValue();
Assert.assertEquals("33_32_31ConnectB", setting, Turnout.CLOSED);
pBlock = bm.getBlock("31");
nBlock = bm.getBlock("34");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("31_32_34ConnectA", setting, Turnout.CLOSED);
setting = tsList.get(1).intValue();
Assert.assertEquals("31_32_34ConnectB", setting, Turnout.CLOSED);
pBlock = bm.getBlock("34");
nBlock = bm.getBlock("31");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("34_32_31ConnectA", setting, Turnout.CLOSED);
setting = tsList.get(1).intValue();
Assert.assertEquals("34_32_31ConnectB", setting, Turnout.CLOSED);
pBlock = bm.getBlock("31");
nBlock = bm.getBlock("35");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("31_32_35ConnectA", setting, Turnout.THROWN);
setting = tsList.get(1).intValue();
Assert.assertEquals("31_32_35ConnectB", setting, Turnout.CLOSED);
pBlock = bm.getBlock("35");
nBlock = bm.getBlock("31");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("35_32_31ConnectA", setting, Turnout.CLOSED);
setting = tsList.get(1).intValue();
Assert.assertEquals("35_32_31ConnectB", setting, Turnout.THROWN);
pBlock = bm.getBlock("31");
nBlock = bm.getBlock("36");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("31_32_36ConnectA", setting, Turnout.THROWN);
setting = tsList.get(1).intValue();
Assert.assertEquals("31_32_36ConnectB", setting, Turnout.THROWN);
pBlock = bm.getBlock("36");
nBlock = bm.getBlock("31");
cu.getTurnoutList(cBlock, pBlock, nBlock);
tsList = cu.getTurnoutSettingList();
setting = tsList.get(0).intValue();
Assert.assertEquals("36_32_31ConnectA", setting, Turnout.THROWN);
setting = tsList.get(1).intValue();
Assert.assertEquals("36_32_31ConnectB", setting, Turnout.THROWN);
// Ask to close window
TestHelper.disposeWindow(le, this);
// Dialog has popped up, so handle that. First, locate it.
List<JDialog> dialogList = new DialogFinder(null).findAll(le);
JDialog d = dialogList.get(0);
// Find the button that deletes the panel
AbstractButtonFinder finder = new AbstractButtonFinder("Delete Panel");
JButton button = (JButton) finder.find(d, 0);
Assert.assertNotNull(button);
// Click button to delete panel and close window
getHelper().enterClickAndLeave(new MouseEventData(this, button));
// another dialog has popped up, so handle that by finding the "Yes - Delete" button.
dialogList = new DialogFinder(null).findAll(le);
d = dialogList.get(0);
finder = new AbstractButtonFinder("Yes - Delete");
button = (JButton) finder.find(d, 0);
Assert.assertNotNull(button);
// Click to say yes, I really mean it.
getHelper().enterClickAndLeave(new MouseEventData(this, button));
le.dispose();
}
use of jmri.BlockManager in project JMRI by JMRI.
the class LayoutBlockManager method getLayoutBlocksOccupiedByRosterEntry.
/**
* Returns a list of layout blocks which this roster entry appears to
* be occupying. A layout block is assumed to contain this roster entry if the value
* of the underlying block is the RosterEntry itself, or a string with the entry's
* id or dcc address.
*
* @param re the roster entry
* @return list of layout block user names
*/
public List<LayoutBlock> getLayoutBlocksOccupiedByRosterEntry(RosterEntry re) {
BlockManager bm = jmri.InstanceManager.getDefault(jmri.BlockManager.class);
List<Block> blockList = bm.getBlocksOccupiedByRosterEntry(re);
List<LayoutBlock> layoutBlockList = new ArrayList<>();
for (Block block : blockList) {
if (block.getUserName() != null) {
LayoutBlock lb = getByUserName(block.getUserName());
if (lb != null) {
layoutBlockList.add(lb);
}
}
}
return layoutBlockList;
}
use of jmri.BlockManager in project JMRI by JMRI.
the class BlockManagerXml method load.
/**
* Load Blocks into the existing BlockManager.
* <p>
* The BlockManager in the InstanceManager is created automatically.
*
* @param sharedBlocks Element containing the block elements to load
* @param perNodeBlocks Per-node block elements to load
* @return true if successful
* @throws jmri.configurexml.JmriConfigureXmlException if error during load
*/
@Override
public boolean load(Element sharedBlocks, Element perNodeBlocks) throws JmriConfigureXmlException {
boolean result = true;
try {
if (sharedBlocks.getChild("defaultspeed") != null) {
String speed = sharedBlocks.getChild("defaultspeed").getText();
if (speed != null && !speed.equals("")) {
InstanceManager.getDefault(jmri.BlockManager.class).setDefaultSpeed(speed);
}
}
} catch (IllegalArgumentException ex) {
log.error(ex.toString());
}
List<Element> list = sharedBlocks.getChildren("block");
if (log.isDebugEnabled()) {
log.debug("Found " + list.size() + " objects");
}
for (int i = 0; i < list.size(); i++) {
Element block = list.get(i);
loadBlock(block);
}
return result;
}
Aggregations