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>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>1.2.24</version>
|
||||
<version>1.2.25</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -319,4 +319,10 @@ polyline{
|
||||
|
||||
svg.disconnected rect{
|
||||
fill: red;
|
||||
}
|
||||
|
||||
textarea.json {
|
||||
width: 90%;
|
||||
height: 33%;
|
||||
margin-left: 5%;
|
||||
}
|
||||
@@ -824,6 +824,6 @@ public class Plan extends BaseClass{
|
||||
* @param 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.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@@ -13,8 +14,14 @@ import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
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.Select;
|
||||
import de.srsoftware.web4rail.tags.TextArea;
|
||||
|
||||
/**
|
||||
* 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>();
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||
private static final String PREFIX = Action.class.getPackageName();
|
||||
private static final String JSON = "json";
|
||||
|
||||
public Action(BaseClass parent) {
|
||||
actions.put(id(), this);
|
||||
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() {
|
||||
BaseClass context = this;
|
||||
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());
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
protected Object jsonImportExport(HashMap<String, String> params) {
|
||||
if (params.containsKey(JSON)) {
|
||||
String jString = params.get(JSON);
|
||||
JSONObject json = new JSONObject(jString);
|
||||
if (this instanceof ActionList) {
|
||||
((ActionList)this).clear();
|
||||
}
|
||||
load(json);
|
||||
return context().properties();
|
||||
}
|
||||
Window win = new Window("json-import-export-"+id(), t("JSON code of {}",this));
|
||||
Form form = new Form("json-form-"+id());
|
||||
new Input(REALM, REALM_ACTIONS).hideIn(form);
|
||||
new Input(ID, id()).hideIn(form);
|
||||
new Input(ACTION, ACTION_SAVE).hideIn(form);
|
||||
new TextArea(JSON).clazz("json").content(json().toString(4)).addTo(form).addTo(win);
|
||||
new Button(t("update"),form).addTo(form);
|
||||
return win;
|
||||
}
|
||||
|
||||
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -101,10 +130,12 @@ public abstract class Action extends BaseClass {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* @Override
|
||||
public Window properties() { // goes up to first ancestor, which is not an Action
|
||||
return parent().properties();
|
||||
}*/
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Edit json"),button(t("export"), Map.of(ACTION, ACTION_SAVE)));
|
||||
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
public static Tag selector() {
|
||||
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
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
while (!actions.isEmpty()) actions.firstElement().remove();
|
||||
}
|
||||
|
||||
public boolean drop(Action action) {
|
||||
return actions.remove(action);
|
||||
}
|
||||
@@ -199,15 +203,13 @@ public class ActionList extends Action implements Iterable<Action>{
|
||||
case ACTION_PROPS:
|
||||
return action.properties();
|
||||
case ACTION_SAVE:
|
||||
Window win = new Window("action-export", t("Export of {}",action));
|
||||
new Tag("textarea").content(action.json().toString()).addTo(win);
|
||||
return win;
|
||||
return action.jsonImportExport(params);
|
||||
case ACTION_UPDATE:
|
||||
return action.update(params);
|
||||
}
|
||||
return t("Unknown action: {}",command);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
Fieldset fieldset = new Fieldset(t("Actions"));
|
||||
|
||||
@@ -35,20 +35,22 @@ public class SetContextTrain extends Action {
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(REALM_TRAIN)) {
|
||||
new Thread() { // load asynchronously, as referred tile may not be available,yet
|
||||
public void run() {
|
||||
try {
|
||||
sleep(1000);
|
||||
Id trainId = Id.from(json,REALM_TRAIN);
|
||||
if (isSet(trainId)) train = Train.get(trainId);
|
||||
} catch (InterruptedException e) {}
|
||||
};
|
||||
}.start();
|
||||
Id trainId = Id.from(json,REALM_TRAIN);
|
||||
if (isSet(trainId)) {
|
||||
train = Train.get(trainId);
|
||||
if (isNull(train)) new Thread() { // load asynchronously, as referred tile may not be available,yet
|
||||
public void run() {
|
||||
try {
|
||||
sleep(1000);
|
||||
train = Train.get(trainId);
|
||||
} catch (InterruptedException e) {}
|
||||
};
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select train"),Train.selector(train, null));
|
||||
|
||||
Reference in New Issue
Block a user