implemented adding and removal of action to contact on route.
This commit is contained in:
@@ -11,6 +11,7 @@ public interface Constants {
|
||||
public static final String ACTION_AUTO = "auto";
|
||||
public static final String ACTION_CLICK = "click";
|
||||
public static final String ACTION_CONNECT = "connect";
|
||||
public static final String ACTION_DROP = "drop";
|
||||
public static final String ACTION_EMERGENCY = "emergency";
|
||||
public static final String ACTION_FASTER10 = "faster10";
|
||||
public static final String ACTION_MOVE = "move";
|
||||
@@ -40,5 +41,7 @@ public interface Constants {
|
||||
public static final String ID = "id";
|
||||
public static final String PORT = "port";
|
||||
public static final Charset UTF8 = StandardCharsets.UTF_8;
|
||||
public static final String CONTACT = "contact";
|
||||
public static final String TYPE = "type";
|
||||
|
||||
}
|
||||
|
||||
@@ -409,6 +409,8 @@ public class Plan implements Constants{
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD_ACTION:
|
||||
return route.addActionForm(params);
|
||||
case ACTION_DROP:
|
||||
return route.dropAction(params);
|
||||
case ACTION_PROPS:
|
||||
return route.properties();
|
||||
case ACTION_UPDATE:
|
||||
|
||||
@@ -66,12 +66,11 @@ public class Route implements Constants{
|
||||
|
||||
private static final String TRIGGER = "trigger";
|
||||
private static final String ACTIONS = "actions";
|
||||
private static final String CONTACT = "contact";
|
||||
private static final String TYPE = "type";
|
||||
private static final String ACTION_ID = "action_id";
|
||||
|
||||
private Tag actionTypeForm(Contact contact) {
|
||||
String formId ="add-action-to-contact-"+contact.id();
|
||||
Tag typeForm = new Form().id(formId);
|
||||
Tag typeForm = new Form(formId);
|
||||
new Input(REALM, REALM_ROUTE).hideIn(typeForm);
|
||||
new Input(ID,id()).hideIn(typeForm);
|
||||
new Input(ACTION,ACTION_ADD_ACTION).hideIn(typeForm);
|
||||
@@ -108,7 +107,7 @@ public class Route implements Constants{
|
||||
return tile;
|
||||
}
|
||||
|
||||
private void addAction(String trigger, Action action) {
|
||||
public void addAction(String trigger, Action action) {
|
||||
Vector<Action> actions = triggers.get(trigger);
|
||||
if (actions == null) {
|
||||
actions = new Vector<Action>();
|
||||
@@ -117,6 +116,24 @@ public class Route implements Constants{
|
||||
actions.add(action);
|
||||
}
|
||||
|
||||
public Object dropAction(HashMap<String, String> params) {
|
||||
String action_id = params.get(ACTION_ID);
|
||||
if (action_id == null) return t("No action id passed to request!");
|
||||
String contactId = params.get(CONTACT);
|
||||
Tile tag = plan.get(contactId, false);
|
||||
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
||||
Contact contact = (Contact) tag;
|
||||
Vector<Action> actions = triggers.get(contact.trigger());
|
||||
|
||||
for (int i=0; i<actions.size(); i++) {
|
||||
if (actions.elementAt(i).toString().equals(action_id)) {
|
||||
actions.remove(i);
|
||||
return t("removed {} from contact {}.",action_id,contactId);
|
||||
}
|
||||
}
|
||||
return t("No action \"{}\" assigned with {}!",action_id,contact);
|
||||
}
|
||||
|
||||
public Object addActionForm(HashMap<String, String> params) {
|
||||
String contactId = params.get(CONTACT);
|
||||
Tile tag = plan.get(contactId, false);
|
||||
@@ -130,7 +147,7 @@ public class Route implements Constants{
|
||||
if (type == null) return (actionTypeForm(contact).addTo(win));
|
||||
switch (type) {
|
||||
case "SpeedReduction":
|
||||
return SpeedReduction.propForm(params);
|
||||
return SpeedReduction.propForm(params,this,contact);
|
||||
}
|
||||
|
||||
return win;
|
||||
@@ -170,7 +187,9 @@ public class Route implements Constants{
|
||||
Tag act = new Tag("li").content(action.toString());
|
||||
new Button("↑").addTo(act);
|
||||
new Button("↓").addTo(act);
|
||||
new Button("-").addTo(act);
|
||||
json.put(ACTION, ACTION_DROP);
|
||||
json.put(ACTION_ID, action.toString());
|
||||
new Button("-",json).addTo(act);
|
||||
act.addTo(ul);
|
||||
}
|
||||
ul.addTo(link);
|
||||
@@ -262,7 +281,6 @@ public class Route implements Constants{
|
||||
public Vector<Contact> contacts() {
|
||||
return new Vector<>(contacts);
|
||||
}
|
||||
|
||||
|
||||
public void finish() throws IOException {
|
||||
startBlock.train(null);
|
||||
@@ -404,8 +422,7 @@ public class Route implements Constants{
|
||||
file.close();
|
||||
}
|
||||
|
||||
public boolean lock() {
|
||||
|
||||
public boolean lock() {
|
||||
ArrayList<Tile> lockedTiles = new ArrayList<Tile>();
|
||||
try {
|
||||
for (Tile tile : path) lockedTiles.add(tile.lock(this));
|
||||
|
||||
@@ -42,11 +42,11 @@ public abstract class Action {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object propForm(HashMap<String, String> params) {
|
||||
public static Window propForm(HashMap<String, String> params) {
|
||||
return new Window("action-props", "Action properties");
|
||||
}
|
||||
|
||||
protected String t(String tex,Object...fills) {
|
||||
protected static String t(String tex,Object...fills) {
|
||||
return Translation.get(Application.class, tex, fills);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
|
||||
public class SpeedReduction extends RouteAction {
|
||||
public class SpeedReduction extends RouteAction implements Constants{
|
||||
|
||||
static final String MAX_SPEED = "max_speed";
|
||||
public static final String MAX_SPEED = "max_speed";
|
||||
private int maxSpeed;
|
||||
|
||||
public SpeedReduction(int routeId, int kmh) {
|
||||
@@ -28,6 +38,39 @@ public class SpeedReduction extends RouteAction {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static Window propForm(HashMap<String, String> params, Route route, Contact contact) {
|
||||
String error = null;
|
||||
String ms = params.get(MAX_SPEED);
|
||||
if (ms == null) {
|
||||
ms = ""+128;
|
||||
} else {
|
||||
try {
|
||||
int s = Integer.parseInt(ms);
|
||||
if (s<0) error = t("Speed must not be less than zero!");
|
||||
if (error == null) {
|
||||
route.addAction(contact.trigger(),new SpeedReduction(route.id(), s));
|
||||
contact.plan().stream("Action added!");
|
||||
return null;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
error = t("Not a valid number!");
|
||||
}
|
||||
}
|
||||
Window win = Action.propForm(params);
|
||||
String formId = "add-action-to-contact-"+contact.id();
|
||||
Tag form = new Form(formId);
|
||||
new Tag("div").content(t("Add Action {} to contact {} on route {}:",SpeedReduction.class.getSimpleName(),contact,route)).addTo(win);
|
||||
new Input(REALM, REALM_ROUTE).hideIn(form);
|
||||
new Input(ID,route.id()).hideIn(form);
|
||||
new Input(ACTION,ACTION_ADD_ACTION).hideIn(form);
|
||||
new Input(CONTACT,contact.id()).hideIn(form);
|
||||
new Input(TYPE,SpeedReduction.class.getSimpleName()).hideIn(form);
|
||||
new Input(MAX_SPEED, ms).addTo(new Label("new speed")).addTo(form);
|
||||
if (error != null) new Tag("div").content(error).addTo(form);
|
||||
new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("Reduce speed to {} km/h",maxSpeed);
|
||||
|
||||
@@ -5,10 +5,16 @@ import de.srsoftware.tools.Tag;
|
||||
public class Form extends Tag {
|
||||
|
||||
private static final long serialVersionUID = 3518580733330482303L;
|
||||
|
||||
|
||||
public Form() {
|
||||
super("form");
|
||||
attr("method","POST");
|
||||
}
|
||||
|
||||
public Form(String id) {
|
||||
super("form");
|
||||
attr("method","POST");
|
||||
id(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user