contact within block now working

This commit is contained in:
Stephan Richter
2020-12-13 20:05:41 +01:00
parent cfb8909107
commit d004fbfd6d
7 changed files with 66 additions and 12 deletions

View File

@@ -159,6 +159,10 @@ public abstract class Block extends StretchableTile{
return fieldset;
}
public Collection<? extends Contact> contacts() {
return internalContacts;
}
public abstract Direction directionA();
public abstract Direction directionB();
@@ -194,6 +198,10 @@ public abstract class Block extends StretchableTile{
}
return getWaitTime(NO_TAG).get(dir);
}
public int indexOf(BlockContact contact) {
return 1+internalContacts.indexOf(contact);
}
@Override
public JSONObject json() {
@@ -203,6 +211,15 @@ public abstract class Block extends StretchableTile{
JSONArray jWaitTimes = new JSONArray();
for (WaitTime wt : waitTimes) jWaitTimes.put(wt.json());
json.put(WAIT_TIMES, jWaitTimes);
JSONObject jContacts = null;
for (BlockContact contact : internalContacts) {
int addr = contact.addr();
if (addr != 0) {
if (isNull(jContacts)) jContacts = new JSONObject();
jContacts.put(contact.id().toString(), contact.addr());
}
}
if (isSet(jContacts)) json.put(CONTACT, jContacts);
return json;
}
@@ -230,6 +247,10 @@ public abstract class Block extends StretchableTile{
if (object instanceof JSONObject) waitTimes.add(new WaitTime(null).load((JSONObject) object));
});
}
if (json.has(CONTACT)) {
JSONObject jContact = json.getJSONObject(CONTACT);
for (String key : jContact.keySet()) new BlockContact(this).addr(jContact.getInt(key));
}
return super.load(json);
}
@@ -260,6 +281,16 @@ public abstract class Block extends StretchableTile{
return contact;
}
@Override
public void removeChild(BaseClass child) {
super.removeChild(child);
internalContacts.remove(child);
}
public void removeContact(BlockContact blockContact) {
internalContacts.remove(blockContact);
}
public static Select selector(Block preselected,Collection<Block> exclude) {
if (isNull(exclude)) exclude = new Vector<Block>();
Select select = new Select(Block.class.getSimpleName());

View File

@@ -1,5 +1,10 @@
package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import java.util.Map;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
public class BlockContact extends Contact {
@@ -11,14 +16,16 @@ public class BlockContact extends Contact {
@Override
public Contact addr(int address) {
super.addr(address);
Block block = (Block) parent();
return block.register(this);
Block block = (Block) parent();
block.removeContact(this);
if (address != 0) block.register(this);
return this;
}
@Override
public Id id() {
if (id == null) id = new Id();
return id;
Block block = ((Block)parent());
return new Id(block.name+":"+block.indexOf(this));
}
@Override
@@ -26,8 +33,18 @@ public class BlockContact extends Contact {
return parent().properties();
}
@Override
public Route route() {
return ((Block)parent()).route();
}
@Override
public Tag tag(Map<String, Object> replacements) throws IOException {
return ((Block)parent()).tag(replacements);
}
@Override
public String toString() {
return getClass().getSimpleName()+"("+addr+")";
return id().toString()+" ("+addr+")";
}
}

View File

@@ -12,6 +12,7 @@ import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.actions.Action;
import de.srsoftware.web4rail.actions.ActionList;
@@ -74,6 +75,7 @@ public class Contact extends Tile{
state = true;
if (isSet(timer)) timer.abort();
Context context = null;
Route route = route();
if (isSet(route)) {
context = route.context();
actions.fire(context);
@@ -95,7 +97,7 @@ public class Contact extends Tile{
}
public Contact addr(int address) {
contactsByAddr.remove(addr); // alte ID aus der Map löschen
Contact oldContact = contactsByAddr.remove(addr); // alte ID aus der Map löschen
addr = address;
if (addr != 0) contactsByAddr.put(addr, this); // neue ID setzen
return this;
@@ -204,7 +206,7 @@ public class Contact extends Tile{
public void stream() {
try {
Tag tag = super.tag(null);
Tag tag = tag(null);
if (state) tag.clazz(tag.get("class")+" active");
plan.stream("place "+tag);
} catch (IOException e) {