implementing lookup and storing result in variable
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>1.5.21</version>
|
||||
<version>1.5.22</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -140,6 +140,7 @@ delete route : Route löschen
|
||||
depart : abfahren
|
||||
Destination : Ziel
|
||||
Destination\: {} from {} : Ziel: {} von {}
|
||||
DetermineCarAtPosition : Fahrzeug im Zug bestimmen
|
||||
DetermineTrainInBlock : Zug im Block bestimmen
|
||||
Determine, which train is in {} : Bestimmen, welcher Zug sich in {} befindet
|
||||
Direction : Richtung
|
||||
|
||||
@@ -98,6 +98,11 @@ public abstract class BaseClass implements Constants{
|
||||
return car;
|
||||
}
|
||||
|
||||
public Context car(Car newCar) {
|
||||
car = newCar;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
action = null;
|
||||
block = null;
|
||||
@@ -200,6 +205,7 @@ public abstract class BaseClass implements Constants{
|
||||
if (isSet(train)) sb.append(", "+t("Train: {}",train));
|
||||
if (isSet(direction)) sb.append(", "+t("Direction: {}",direction));
|
||||
if (isSet(block)) sb.append(", "+t("Block: {}",block));
|
||||
if (isSet(car)) sb.append(", "+t("Fahrzeug: {}",car));
|
||||
if (isSet(route)) sb.append(", "+t("Route: {}",route));
|
||||
if (isSet(contact)) sb.append(", "+t("Contact: {}",contact));
|
||||
if (isSet(waitTime)) sb.append(", "+t("Wait time: {} ms",waitTime));
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.srsoftware.web4rail;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -117,6 +119,52 @@ public class LookupTable extends BaseClass{
|
||||
return properties(div.toString());
|
||||
}
|
||||
|
||||
public int getValue(Context context) {
|
||||
Train train = context.train();
|
||||
Car car = context.car();
|
||||
|
||||
Object rowKey = null;
|
||||
switch (rowType) {
|
||||
case REALM_TRAIN:
|
||||
if (isSet(train)) rowKey = train.id();
|
||||
break;
|
||||
case REALM_CAR:
|
||||
if (isSet(car )) rowKey = car.id();
|
||||
break;
|
||||
case LENGTH:
|
||||
if (isSet(train)) {
|
||||
int len = train.length();
|
||||
Vector<Object> rows = new Vector<>(this.rows);
|
||||
Collections.reverse(rows);
|
||||
for (Object row : rows) {
|
||||
try {
|
||||
int val = Integer.parseInt(row.toString());
|
||||
if (len < val) rowKey = row;
|
||||
} catch (NumberFormatException nfe) {}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Object colKey = null;
|
||||
switch (colType) {
|
||||
case REALM_TRAIN:
|
||||
if (isSet(train))colKey = train.id();
|
||||
break;
|
||||
case REALM_CAR:
|
||||
if (isSet(car ))colKey = car.id();
|
||||
break;
|
||||
case LENGTH:
|
||||
if (isSet(train)) colKey = train.length();
|
||||
break;
|
||||
}
|
||||
|
||||
if (!isSet(colKey,rowKey)) throw new NullPointerException();
|
||||
TreeMap<Object, Integer> dummy = values.get(rowKey.toString());
|
||||
|
||||
return dummy.get(colKey.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
@@ -199,6 +247,21 @@ public class LookupTable extends BaseClass{
|
||||
return selector;
|
||||
}
|
||||
|
||||
public static Select selector(LookupTable preselected,Collection<LookupTable> exclude) {
|
||||
if (isNull(exclude)) exclude = new Vector<LookupTable>();
|
||||
Select select = new Select(LookupTable.class.getSimpleName());
|
||||
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
||||
|
||||
List<LookupTable> tables = BaseClass.listElements(LookupTable.class);
|
||||
tables.sort((t1,t2)->t1.name().compareTo(t2.name()));
|
||||
for (LookupTable table : tables) {
|
||||
if (exclude.contains(table)) continue;
|
||||
Tag opt = select.addOption(table.id, table);
|
||||
if (table == preselected) opt.attr("selected", "selected");
|
||||
}
|
||||
return select;
|
||||
}
|
||||
|
||||
private void setValue(String key, Object value) {
|
||||
try {
|
||||
Integer intVal = Integer.parseInt(value.toString());
|
||||
|
||||
39
src/main/java/de/srsoftware/web4rail/Store.java
Normal file
39
src/main/java/de/srsoftware/web4rail/Store.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package de.srsoftware.web4rail;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Store {
|
||||
|
||||
private static HashMap<String,Store> stores = new HashMap<>();
|
||||
private String name;
|
||||
private Integer value = null;
|
||||
|
||||
public Store(String name) {
|
||||
this.name = name;
|
||||
stores.put(name, this);
|
||||
}
|
||||
|
||||
public static Store get(String name) {
|
||||
Store store = stores.get(name);
|
||||
if (BaseClass.isNull(store)) store = new Store(name);
|
||||
return store;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Integer value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int newVal) {
|
||||
value = newVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name+"("+(BaseClass.isNull(value) ? BaseClass.t("no value") : value)+")";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,10 +48,12 @@ public abstract class Action extends BaseClass {
|
||||
ConditionalAction.class,
|
||||
CoupleTrain.class,
|
||||
DelayedAction.class,
|
||||
DetermineCarAtPosition.class,
|
||||
DetermineTrainInBlock.class,
|
||||
DisableEnableTile.class,
|
||||
EngageDecoupler.class,
|
||||
FinishRoute.class,
|
||||
LookupValue.class,
|
||||
Loop.class,
|
||||
PreserveRoute.class,
|
||||
ReactivateContact.class,
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Params;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
|
||||
public class DetermineCarAtPosition extends Action {
|
||||
|
||||
private static final String POSITION = "position";
|
||||
|
||||
public DetermineCarAtPosition(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
private int position = 1;
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (isNull(context)) return false;
|
||||
Train train = context.train();
|
||||
if (isNull(train)) return false;
|
||||
List<Car> cars = train.cars();
|
||||
Car car = null;
|
||||
if (position > 0 && position <= cars.size()) {
|
||||
car = cars.get(position-1);
|
||||
}
|
||||
if (position < 0 && -position <= cars.size()) {
|
||||
car = cars.get(cars.size()+position);
|
||||
}
|
||||
if (isNull(car)) return false;
|
||||
context.car(car);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(POSITION, position);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(POSITION)) position = json.getInt(POSITION);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm,String...errors) {
|
||||
formInputs.add(t("Position in train"),new Input(POSITION).numeric());
|
||||
return super.properties(preForm, formInputs, postForm,errors);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return t("Determine, which car is at position {} of the current train",position);
|
||||
};
|
||||
|
||||
@Override
|
||||
protected Object update(Params params) {
|
||||
LOG.debug("update: {}",params);
|
||||
if (params.containsKey(POSITION)) {
|
||||
position = Integer.parseInt(POSITION);
|
||||
}
|
||||
return context().properties();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.LoadCallback;
|
||||
import de.srsoftware.web4rail.LookupTable;
|
||||
import de.srsoftware.web4rail.Params;
|
||||
import de.srsoftware.web4rail.Store;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
|
||||
public class LookupValue extends Action {
|
||||
|
||||
private static final String TABLE = "table";
|
||||
private static final String STORE = "store";
|
||||
private LookupTable lookupTable = null;
|
||||
private Store store = null;
|
||||
|
||||
public LookupValue(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
try {
|
||||
if (!isSet(store,lookupTable)) return false;
|
||||
int value = lookupTable.getValue(context);
|
||||
store.setValue(value);
|
||||
} catch (NullPointerException npe) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (isSet(lookupTable)) json.put(TABLE, lookupTable.id());
|
||||
if (isSet(store)) json.put(STORE, store.name());
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(TABLE)) new LoadCallback() {
|
||||
@Override
|
||||
public void afterLoad() {
|
||||
lookupTable = LookupTable.get(new Id(json.getString(TABLE)));
|
||||
}
|
||||
};
|
||||
|
||||
if (json.has(STORE)) store = Store.get(json.getString(STORE));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm,String...errors) {
|
||||
formInputs.add(t("Lookup table"),LookupTable.selector(lookupTable, null));
|
||||
formInputs.add(t("Store"), new Input(STORE,isSet(store)?store.name():""));
|
||||
return super.properties(preForm, formInputs, postForm,errors);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (isSet(store,lookupTable)) return t("Set {} to value from {}",store.name(),lookupTable);
|
||||
return t("[Click here to setup look-up]");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(Params params) {
|
||||
LOG.debug("update: {}",params);
|
||||
if (params.containsKey(LookupTable.class.getSimpleName())) {
|
||||
Id tableId = Id.from(params, LookupTable.class.getSimpleName());
|
||||
if (tableId.equals(0)) {
|
||||
lookupTable = null;
|
||||
} else {
|
||||
LookupTable newTable = LookupTable.get(tableId);
|
||||
if (isSet(newTable)) lookupTable = newTable;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
String storeName = params.getString(STORE);
|
||||
if (isSet(storeName) && !storeName.isEmpty()) store = Store.get(storeName);
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user