implemented json import/export of actions
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.srsoftware</groupId>
|
<groupId>de.srsoftware</groupId>
|
||||||
<artifactId>web4rail</artifactId>
|
<artifactId>web4rail</artifactId>
|
||||||
<version>1.2.24</version>
|
<version>1.2.25</version>
|
||||||
<name>Web4Rail</name>
|
<name>Web4Rail</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<description>Java Model Railway Control</description>
|
<description>Java Model Railway Control</description>
|
||||||
|
|||||||
@@ -319,4 +319,10 @@ polyline{
|
|||||||
|
|
||||||
svg.disconnected rect{
|
svg.disconnected rect{
|
||||||
fill: red;
|
fill: red;
|
||||||
|
}
|
||||||
|
|
||||||
|
textarea.json {
|
||||||
|
width: 90%;
|
||||||
|
height: 33%;
|
||||||
|
margin-left: 5%;
|
||||||
}
|
}
|
||||||
@@ -824,6 +824,6 @@ public class Plan extends BaseClass{
|
|||||||
* @param contact
|
* @param contact
|
||||||
*/
|
*/
|
||||||
public void warn(Contact contact) {
|
public void warn(Contact contact) {
|
||||||
stream(t("Warning: {}",t("Ghost train @ {}",contact)));
|
//stream(t("Warning: {}",t("Ghost train @ {}",contact)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
@@ -13,8 +14,14 @@ import de.keawe.tools.translations.Translation;
|
|||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Application;
|
import de.srsoftware.web4rail.Application;
|
||||||
import de.srsoftware.web4rail.BaseClass;
|
import de.srsoftware.web4rail.BaseClass;
|
||||||
|
import de.srsoftware.web4rail.Window;
|
||||||
|
import de.srsoftware.web4rail.tags.Button;
|
||||||
|
import de.srsoftware.web4rail.tags.Fieldset;
|
||||||
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
import de.srsoftware.web4rail.tags.Label;
|
import de.srsoftware.web4rail.tags.Label;
|
||||||
import de.srsoftware.web4rail.tags.Select;
|
import de.srsoftware.web4rail.tags.Select;
|
||||||
|
import de.srsoftware.web4rail.tags.TextArea;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base Class for all other actions
|
* Base Class for all other actions
|
||||||
@@ -25,12 +32,39 @@ public abstract class Action extends BaseClass {
|
|||||||
private static final HashMap<Id,Action> actions = new HashMap<Id, Action>();
|
private static final HashMap<Id,Action> actions = new HashMap<Id, Action>();
|
||||||
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||||
private static final String PREFIX = Action.class.getPackageName();
|
private static final String PREFIX = Action.class.getPackageName();
|
||||||
|
private static final String JSON = "json";
|
||||||
|
|
||||||
public Action(BaseClass parent) {
|
public Action(BaseClass parent) {
|
||||||
actions.put(id(), this);
|
actions.put(id(), this);
|
||||||
parent(parent);
|
parent(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<Class<? extends Action>> classes() {
|
||||||
|
return List.of(
|
||||||
|
BrakeStart.class,
|
||||||
|
BrakeStop.class,
|
||||||
|
BrakeCancel.class,
|
||||||
|
ConditionalAction.class,
|
||||||
|
DelayedAction.class,
|
||||||
|
DetermineTrainInBlock.class,
|
||||||
|
FinishRoute.class,
|
||||||
|
PreserveRoute.class,
|
||||||
|
SendCommand.class,
|
||||||
|
SetContextTrain.class,
|
||||||
|
SetDisplayText.class,
|
||||||
|
SetPower.class,
|
||||||
|
SetRelay.class,
|
||||||
|
SetSignal.class,
|
||||||
|
SetSpeed.class,
|
||||||
|
SetTurnout.class,
|
||||||
|
ShowText.class,
|
||||||
|
StopAllTrains.class,
|
||||||
|
StartStopAuto.class,
|
||||||
|
TriggerContact.class,
|
||||||
|
TurnTrain.class
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public BaseClass context() {
|
public BaseClass context() {
|
||||||
BaseClass context = this;
|
BaseClass context = this;
|
||||||
while (context instanceof Action && isSet(context.parent())) context = context.parent();
|
while (context instanceof Action && isSet(context.parent())) context = context.parent();
|
||||||
@@ -61,33 +95,28 @@ public abstract class Action extends BaseClass {
|
|||||||
return new JSONObject().put(TYPE, getClass().getSimpleName());
|
return new JSONObject().put(TYPE, getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Class<? extends Action>> classes() {
|
protected Object jsonImportExport(HashMap<String, String> params) {
|
||||||
return List.of(
|
if (params.containsKey(JSON)) {
|
||||||
BrakeStart.class,
|
String jString = params.get(JSON);
|
||||||
BrakeStop.class,
|
JSONObject json = new JSONObject(jString);
|
||||||
BrakeCancel.class,
|
if (this instanceof ActionList) {
|
||||||
ConditionalAction.class,
|
((ActionList)this).clear();
|
||||||
DelayedAction.class,
|
}
|
||||||
DetermineTrainInBlock.class,
|
load(json);
|
||||||
FinishRoute.class,
|
return context().properties();
|
||||||
PreserveRoute.class,
|
}
|
||||||
SendCommand.class,
|
Window win = new Window("json-import-export-"+id(), t("JSON code of {}",this));
|
||||||
SetContextTrain.class,
|
Form form = new Form("json-form-"+id());
|
||||||
SetDisplayText.class,
|
new Input(REALM, REALM_ACTIONS).hideIn(form);
|
||||||
SetPower.class,
|
new Input(ID, id()).hideIn(form);
|
||||||
SetRelay.class,
|
new Input(ACTION, ACTION_SAVE).hideIn(form);
|
||||||
SetSignal.class,
|
new TextArea(JSON).clazz("json").content(json().toString(4)).addTo(form).addTo(win);
|
||||||
SetSpeed.class,
|
new Button(t("update"),form).addTo(form);
|
||||||
SetTurnout.class,
|
return win;
|
||||||
ShowText.class,
|
|
||||||
StopAllTrains.class,
|
|
||||||
StartStopAuto.class,
|
|
||||||
TriggerContact.class,
|
|
||||||
TurnTrain.class
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Action load(JSONObject json) {
|
public Action load(JSONObject json) {
|
||||||
|
super.load(json);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,10 +130,12 @@ public abstract class Action extends BaseClass {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @Override
|
@Override
|
||||||
public Window properties() { // goes up to first ancestor, which is not an Action
|
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||||
return parent().properties();
|
formInputs.add(t("Edit json"),button(t("export"), Map.of(ACTION, ACTION_SAVE)));
|
||||||
}*/
|
|
||||||
|
return super.properties(preForm, formInputs, postForm);
|
||||||
|
}
|
||||||
|
|
||||||
public static Tag selector() {
|
public static Tag selector() {
|
||||||
Select select = new Select(TYPE);
|
Select select = new Select(TYPE);
|
||||||
|
|||||||
@@ -82,6 +82,10 @@ public class ActionList extends Action implements Iterable<Action>{
|
|||||||
actions.forEach(action -> other.removeChild(action)); // zugewiesene Aktionen von alter Liste löschen
|
actions.forEach(action -> other.removeChild(action)); // zugewiesene Aktionen von alter Liste löschen
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
while (!actions.isEmpty()) actions.firstElement().remove();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean drop(Action action) {
|
public boolean drop(Action action) {
|
||||||
return actions.remove(action);
|
return actions.remove(action);
|
||||||
}
|
}
|
||||||
@@ -199,15 +203,13 @@ public class ActionList extends Action implements Iterable<Action>{
|
|||||||
case ACTION_PROPS:
|
case ACTION_PROPS:
|
||||||
return action.properties();
|
return action.properties();
|
||||||
case ACTION_SAVE:
|
case ACTION_SAVE:
|
||||||
Window win = new Window("action-export", t("Export of {}",action));
|
return action.jsonImportExport(params);
|
||||||
new Tag("textarea").content(action.json().toString()).addTo(win);
|
|
||||||
return win;
|
|
||||||
case ACTION_UPDATE:
|
case ACTION_UPDATE:
|
||||||
return action.update(params);
|
return action.update(params);
|
||||||
}
|
}
|
||||||
return t("Unknown action: {}",command);
|
return t("Unknown action: {}",command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||||
Fieldset fieldset = new Fieldset(t("Actions"));
|
Fieldset fieldset = new Fieldset(t("Actions"));
|
||||||
|
|||||||
@@ -35,20 +35,22 @@ public class SetContextTrain extends Action {
|
|||||||
public Action load(JSONObject json) {
|
public Action load(JSONObject json) {
|
||||||
super.load(json);
|
super.load(json);
|
||||||
if (json.has(REALM_TRAIN)) {
|
if (json.has(REALM_TRAIN)) {
|
||||||
new Thread() { // load asynchronously, as referred tile may not be available,yet
|
Id trainId = Id.from(json,REALM_TRAIN);
|
||||||
public void run() {
|
if (isSet(trainId)) {
|
||||||
try {
|
train = Train.get(trainId);
|
||||||
sleep(1000);
|
if (isNull(train)) new Thread() { // load asynchronously, as referred tile may not be available,yet
|
||||||
Id trainId = Id.from(json,REALM_TRAIN);
|
public void run() {
|
||||||
if (isSet(trainId)) train = Train.get(trainId);
|
try {
|
||||||
} catch (InterruptedException e) {}
|
sleep(1000);
|
||||||
};
|
train = Train.get(trainId);
|
||||||
}.start();
|
} catch (InterruptedException e) {}
|
||||||
|
};
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||||
formInputs.add(t("Select train"),Train.selector(train, null));
|
formInputs.add(t("Select train"),Train.selector(train, null));
|
||||||
|
|||||||
Reference in New Issue
Block a user