Skip to content

Commit

Permalink
Fix/improve the LayoutBlock related Block creation process
Browse files Browse the repository at this point in the history
  • Loading branch information
dsand47 committed Aug 7, 2018
1 parent 336c062 commit e27afe5
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions java/src/jmri/jmrit/display/layoutEditor/LayoutBlock.java
Expand Up @@ -160,7 +160,11 @@ public LayoutBlock(String sName, String uName) {
}

/*
* Completes the creation of a LayoutBlock object by adding a Block to it
* Completes the creation of a LayoutBlock object by adding a Block to it.
*
* The block create process takes into account that the _bean register
* process considers IB1 and IB01 to be the same name which results in a
* silent failure.
*/
protected void initializeLayoutBlock() {
//get/create a Block object corresponding to this LayoutBlock
Expand All @@ -169,35 +173,50 @@ protected void initializeLayoutBlock() {
if ((userName != null) && !userName.isEmpty()) {
block = InstanceManager.getDefault(BlockManager.class).getByUserName(userName);
}

if (block == null) {
//not found, create a new Block
// Not found, create a new Block
BlockManager bm = InstanceManager.getDefault(BlockManager.class);
String s = "";

//create a unique system name
for (boolean found = true; found;) {
while (true) {
if (jmriblknum > 50000) {
throw new IndexOutOfBoundsException("Run away prevented while trying to create a block");
}
s = "IB" + jmriblknum;
jmriblknum++;
block = InstanceManager.getDefault(BlockManager.class).getBySystemName(s);

// Find an unused system name
block = bm.getBySystemName(s);
if (block != null) {
log.debug("System name is already used: {}", s);
continue;
}

// Create a new block. User name is null to prevent user name checking.
block = bm.createNewBlock(s, null);
if (block == null) {
found = false;
log.debug("Null block returned: {}", s);
continue;
}

// Verify registration
if (bm.getSystemNameList().contains(s)) {
log.debug("Block is valid: {}", s);
break;
}
log.debug("Registration failed: {}", s);
}
block = InstanceManager.getDefault(BlockManager.class).createNewBlock(s, getUserName());
if (block == null) {
log.error("Failure to get/create Block: " + s + "," + getUserName());
}
block.setUserName(getUserName());
}

if (block != null) {
//attach a listener for changes in the Block
mBlockListener = (PropertyChangeEvent e) -> {
handleBlockChange(e);
};
block.addPropertyChangeListener(mBlockListener,
getUserName(), "Layout Block:" + getUserName());
if (occupancyNamedSensor != null) {
block.setNamedSensor(occupancyNamedSensor);
}
//attach a listener for changes in the Block
mBlockListener = (PropertyChangeEvent e) -> {
handleBlockChange(e);
};
block.addPropertyChangeListener(mBlockListener,
getUserName(), "Layout Block:" + getUserName());
if (occupancyNamedSensor != null) {
block.setNamedSensor(occupancyNamedSensor);
}
}

Expand Down

0 comments on commit e27afe5

Please sign in to comment.