From 5499d5a82b0035e58bb52eb00b225956113e4211 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Thu, 5 Nov 2020 21:19:28 +0100 Subject: [PATCH] sorting methods --- .../java/de/srsoftware/web4rail/Route.java | 94 ++++++++++--------- .../srsoftware/web4rail/actions/Action.java | 31 +++--- .../web4rail/actions/ActionList.java | 6 +- .../de/srsoftware/web4rail/tiles/Block.java | 10 +- .../de/srsoftware/web4rail/tiles/Tile.java | 47 +++++----- .../de/srsoftware/web4rail/tiles/Turnout.java | 28 +++--- 6 files changed, 117 insertions(+), 99 deletions(-) diff --git a/src/main/java/de/srsoftware/web4rail/Route.java b/src/main/java/de/srsoftware/web4rail/Route.java index f357196..8787ca6 100644 --- a/src/main/java/de/srsoftware/web4rail/Route.java +++ b/src/main/java/de/srsoftware/web4rail/Route.java @@ -48,35 +48,37 @@ import de.srsoftware.web4rail.tiles.Turnout.State; * */ public class Route extends BaseClass{ - private static final Logger LOG = LoggerFactory.getLogger(Route.class); - static final String NAME = "name"; - static final String PATH = "path"; - static final String SIGNALS = "signals"; - static final String TURNOUTS = "turnouts"; - private Vector path; - private Vector signals; - private Vector contacts; - private HashMap turnouts; - private HashMap triggers = new HashMap(); - private int id; - private static HashMap names = new HashMap(); // maps id to name. needed to keep names during plan.analyze() - public Train train; - private Block startBlock = null,endBlock; - private static final String START_DIRECTION = "direction_start"; - private static final String END_DIRECTION = "direction_end"; - private Vector conditions = new Vector(); - private ActionList setupActions = new ActionList(); - - public Direction startDirection; - private Direction endDirection; - private boolean disabled = false; + private static final Logger LOG = LoggerFactory.getLogger(Route.class); - private static final String TRIGGER = "trigger"; - private static final String ACTIONS = "actions"; private static final String ACTION_LISTS = "action_lists"; - private static final String ROUTES = "routes"; + private static final String ACTIONS = "actions"; private static final String CONDITIONS = "conditions"; private static final String DROP_CONDITION = "drop_condition"; + private static final String END_DIRECTION = "direction_end"; + private static final String ROUTES = "routes"; + private static final String START_DIRECTION = "direction_start"; + private static final String TRIGGER = "trigger"; + static final String NAME = "name"; + static final String PATH = "path"; + static final String SIGNALS = "signals"; + static final String TURNOUTS = "turnouts"; + + private static HashMap names = new HashMap(); // maps id to name. needed to keep names during plan.analyze() + + private Vector conditions = new Vector(); + private Vector contacts; + private boolean disabled = false; + private Block endBlock = null; + private Direction endDirection; + private int id; + private Vector path; + private Vector signals; + public Train train; + private HashMap triggers = new HashMap(); + private HashMap turnouts; + private ActionList setupActions = new ActionList(); + private Block startBlock = null; + public Direction startDirection; /** * process commands from the client @@ -109,20 +111,6 @@ public class Route extends BaseClass{ return t("Unknown action: {}",params.get(ACTION)); } - private Object dropCodition(HashMap params) { - String condId = params.get(REALM_CONDITION); - if (isSet(condId)) { - int cid = Integer.parseInt(condId); - for (Condition condition : conditions) { - if (condition.id() == cid) { - conditions.remove(condition); - break; - } - } - } - return properties(params); - } - /** * adds a tile to the route * @param tile @@ -323,6 +311,20 @@ public class Route extends BaseClass{ public String context() { return REALM_ROUTE+":"+id(); } + + private Object dropCodition(HashMap params) { + String condId = params.get(REALM_CONDITION); + if (isSet(condId)) { + int cid = Integer.parseInt(condId); + for (Condition condition : conditions) { + if (condition.id() == cid) { + conditions.remove(condition); + break; + } + } + } + return properties(params); + } public Block endBlock() { return endBlock; @@ -344,13 +346,6 @@ public class Route extends BaseClass{ return setupActions.fire(context); } - public boolean isFree() { - for (int i=1; i actions = new HashMap(); public static final Logger LOG = LoggerFactory.getLogger(Action.class); @@ -115,6 +120,19 @@ public abstract class Action extends BaseClass { return this; } + public static Tag selector() { + Select select = new Select(TYPE); + TreeMap names = new TreeMap(String.CASE_INSENSITIVE_ORDER); + + for (Class clazz : Action.list()) { + String s = t(clazz.getSimpleName()); + names.put(s, clazz.getSimpleName()); + } + + for (Entry entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey()); + return select.addTo(new Label(t("Action type:")+NBSP)); + } + public Window properties(HashMap params) { return new Window("action-props-"+id, t("Properties of {}",this.getClass().getSimpleName())); } @@ -131,17 +149,4 @@ public abstract class Action extends BaseClass { protected Object update(HashMap params) { return t("Nothing changed"); } - - public static Tag selector() { - Select select = new Select(TYPE); - TreeMap names = new TreeMap(String.CASE_INSENSITIVE_ORDER); - - for (Class clazz : Action.list()) { - String s = t(clazz.getSimpleName()); - names.put(s, clazz.getSimpleName()); - } - - for (Entry entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey()); - return select.addTo(new Label(t("Action type:")+NBSP)); - } } diff --git a/src/main/java/de/srsoftware/web4rail/actions/ActionList.java b/src/main/java/de/srsoftware/web4rail/actions/ActionList.java index bbf0dfc..a4fcac6 100644 --- a/src/main/java/de/srsoftware/web4rail/actions/ActionList.java +++ b/src/main/java/de/srsoftware/web4rail/actions/ActionList.java @@ -23,11 +23,11 @@ import de.srsoftware.web4rail.tags.Form; import de.srsoftware.web4rail.tags.Input; public class ActionList extends Vector implements Constants{ + private static final Logger LOG = LoggerFactory.getLogger(ActionList.class); private static final long serialVersionUID = 4862000041987682112L; - private static final Logger LOG = LoggerFactory.getLogger(ActionList.class); - private int id; private static final HashMap actionLists = new HashMap(); + private int id; public ActionList() { id = Application.createId(); @@ -210,4 +210,4 @@ public class ActionList extends Vector implements Constants{ } return t("No action with id {} found.",actionId); } -} +} \ No newline at end of file diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Block.java b/src/main/java/de/srsoftware/web4rail/tiles/Block.java index fb04e51..06307cb 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Block.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Block.java @@ -16,11 +16,15 @@ import de.srsoftware.web4rail.tags.Input; import de.srsoftware.web4rail.tags.Label; import de.srsoftware.web4rail.tags.Select; +/** + * Base class for all kinds of Blocks + * @author Stephan Richter, SRSoftware + * + */ public abstract class Block extends StretchableTile{ - private static final String NAME = "name"; - public String name = "Block"; - private static final String ALLOW_TURN = "allowTurn"; + private static final String NAME = "name"; + public String name = "Block"; public boolean turnAllowed = false; @Override diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Tile.java b/src/main/java/de/srsoftware/web4rail/tiles/Tile.java index 9c77148..4a5f026 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Tile.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Tile.java @@ -36,6 +36,11 @@ import de.srsoftware.web4rail.tags.Form; import de.srsoftware.web4rail.tags.Input; import de.srsoftware.web4rail.tags.Radio; +/** + * Base class for all tiles + * @author Stephan Richter, SRSoftware + * + */ public abstract class Tile extends BaseClass{ protected static Logger LOG = LoggerFactory.getLogger(Tile.class); private static int DEFAUT_LENGTH = 5; @@ -50,8 +55,6 @@ public abstract class Tile extends BaseClass{ private static final String TYPE = "type"; private static final String X = "x"; private static final String Y = "y"; - - private boolean disabled = false; private int length = DEFAUT_LENGTH; @@ -64,6 +67,14 @@ public abstract class Tile extends BaseClass{ public Integer x = null; public Integer y = null; + public void add(Route route) { + this.routes.add(route); + } + + public void addShadow(Shadow shadow) { + shadows.add(shadow); + } + protected Vector classes(){ Vector classes = new Vector(); classes.add("tile"); @@ -74,14 +85,6 @@ public abstract class Tile extends BaseClass{ return classes; } - public void add(Route route) { - this.routes.add(route); - } - - public void addShadow(Shadow shadow) { - shadows.add(shadow); - } - public Object click() throws IOException { LOG.debug("{}.click()",getClass().getSimpleName()); return propMenu(); @@ -95,14 +98,10 @@ public abstract class Tile extends BaseClass{ return new HashMap<>(); } - public boolean isFree() { - return !(disabled || isSet(route) || isSet(train)); - } - public int height() { return 1; } - + public String id() { return Tile.id(x, y); } @@ -119,6 +118,10 @@ public abstract class Tile extends BaseClass{ plan.set(tile.x, tile.y, tile); } + public boolean isFree() { + return !(disabled || isSet(route) || isSet(train)); + } + public JSONObject json() { JSONObject json = new JSONObject(); json.put(TYPE, getClass().getSimpleName()); @@ -274,6 +277,12 @@ public abstract class Tile extends BaseClass{ file.close(); } + public Tile set(Train newTrain) { + if (newTrain == train) return this; // nothing to update + this.train = newTrain; + return plan.place(this); + } + public Tile setRoute(Route lockingRoute) { if (route == lockingRoute) return this; // nothing changed if (isSet(route) && isSet(lockingRoute)) throw new IllegalStateException(this.toString()); // tile already locked by other route @@ -360,12 +369,6 @@ public abstract class Tile extends BaseClass{ return train; } - public Tile set(Train newTrain) { - if (newTrain == train) return this; // nothing to update - this.train = newTrain; - return plan.place(this); - } - public void unlock() { route = null; train = null; @@ -392,4 +395,4 @@ public abstract class Tile extends BaseClass{ public int width() { return 1; } -} +} \ No newline at end of file diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java b/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java index a4b33e3..be792dd 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java @@ -17,25 +17,29 @@ import de.srsoftware.web4rail.tags.Input; import de.srsoftware.web4rail.tags.Label; import de.srsoftware.web4rail.tags.Radio; +/** + * Base class for Turnouts + * @author Stephan Richter, SRSoftware + * + */ public abstract class Turnout extends Tile implements Device{ - public static final String STATE = "state"; - private static final String PORT_A = "port_a"; - private static final String PORT_B = "port_b"; + private static final String PORT_A = "port_a"; + private static final String PORT_B = "port_b"; + public static final String STATE = "state"; protected static final String STRAIGHT = "straight"; - private Protocol protocol = Protocol.DCC128; - protected int address = 0; - protected int portA = 0, portB = 1; - protected int delay = 400; - protected boolean initialized = false; - protected boolean error = false; + protected int address = 0; + protected int delay = 400; + protected boolean error = false; + protected boolean initialized = false; + private Protocol protocol = Protocol.DCC128; + protected int portA = 0, portB = 1; + protected State state = State.STRAIGHT; public enum State{ LEFT,STRAIGHT,RIGHT,UNDEF; } - protected State state = State.STRAIGHT; - @Override public Object click() throws IOException { LOG.debug(getClass().getSimpleName()+".click()"); @@ -197,4 +201,4 @@ public abstract class Turnout extends Tile implements Device{ if (params.containsKey(PORT_B)) portB = Integer.parseInt(params.get(PORT_B)); return super.update(params); } -} +} \ No newline at end of file