refactoring properties framework
This commit is contained in:
@@ -2,6 +2,8 @@ package de.srsoftware.web4rail;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -21,7 +23,7 @@ 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.Table;
|
||||
import de.srsoftware.web4rail.tags.TextArea;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
@@ -225,6 +227,23 @@ public abstract class BaseClass implements Constants{
|
||||
return Map.of(ACTION,action,CONTEXT,realm()+":"+id());
|
||||
}
|
||||
|
||||
public Form form(String id,List<Map.Entry<String, Tag>> elements) {
|
||||
Form form = new Form(id);
|
||||
|
||||
Table table = new Table();
|
||||
for (Map.Entry<String, Tag>entry : elements) {
|
||||
String key = entry.getKey();
|
||||
Tag val = entry.getValue();
|
||||
if (isNull(key) && val instanceof Input) ((Input)val).hideIn(form);
|
||||
table.addRow(isSet(key)?key:"",entry.getValue());
|
||||
}
|
||||
|
||||
table.addTo(form);
|
||||
|
||||
new Button(t("Apply"),form).addTo(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
public Id id() {
|
||||
if (isNull(id)) id = new Id();
|
||||
return id;
|
||||
@@ -276,26 +295,24 @@ public abstract class BaseClass implements Constants{
|
||||
return merged;
|
||||
}
|
||||
|
||||
public Window properties(List<Fieldset> preForm,List<Tag> formInputs,List<Fieldset> postForm) {
|
||||
public Window properties() {
|
||||
return properties(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
|
||||
}
|
||||
|
||||
|
||||
protected Window properties(List<Fieldset> preForm,List<Map.Entry<String, Tag>> formInputs,List<Fieldset> postForm) {
|
||||
Window win = new Window(getClass().getSimpleName()+"-properties", t("Properties of {}",this));
|
||||
|
||||
preForm.forEach(fieldset -> fieldset.addTo(win));
|
||||
|
||||
|
||||
Form form = new Form(getClass().getSimpleName()+"-prop-form");
|
||||
new Input(ACTION, ACTION_UPDATE).hideIn(form);
|
||||
new Input(REALM,realm()).hideIn(form);
|
||||
new Input(ID,id()).hideIn(form);
|
||||
|
||||
formInputs.forEach(tag -> tag.addTo(form));
|
||||
|
||||
new TextArea(NOTES,notes)
|
||||
.addTo(new Label(t("Notes")+NBSP))
|
||||
.addTo(form);
|
||||
|
||||
new Button(t("Apply"),form)
|
||||
.addTo(form)
|
||||
.addTo(new Fieldset("Basic properties"))
|
||||
formInputs.add(new AbstractMap.SimpleEntry<String, Tag>(null,new Input(ACTION, ACTION_UPDATE)));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<String, Tag>(null,new Input(REALM,realm())));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<String, Tag>(null,new Input(ID,id())));
|
||||
|
||||
formInputs.add(new AbstractMap.SimpleEntry<String, Tag>(t("Notes"),new TextArea(NOTES,notes)));
|
||||
|
||||
form(getClass().getSimpleName()+"-prop-form",formInputs)
|
||||
.addTo(new Fieldset(t("Basic properties")))
|
||||
.addTo(win);
|
||||
|
||||
postForm.forEach(fieldset -> fieldset.addTo(win));
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -198,20 +199,20 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
return this;
|
||||
}
|
||||
|
||||
public Window properties() {
|
||||
|
||||
List<Tag> formInputs = List.of(
|
||||
new Input(NAME,name).addTo(new Label(t("Name")+NBSP)),
|
||||
new Input(STOCK_ID,stockId).addTo(new Label(t("Stock ID")+NBSP)),
|
||||
new Input(LENGTH,length).attr("type", "number").addTo(new Label(t("Length")+NBSP)).content(NBSP+lengthUnit),
|
||||
new Input(TAGS,String.join(", ", tags)).addTo(new Label(t("Tags")+NBSP)),
|
||||
new Input(MAX_SPEED, maxSpeed).numeric().addTo(new Label(t("Maximum speed")+":"+NBSP)).content(NBSP+speedUnit)
|
||||
);
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, List<Entry<String, Tag>> formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Name"),new Input(NAME,name)));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Stock ID"),new Input(STOCK_ID,stockId)));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Length"),new Input(LENGTH,length).attr("type", "number").addTo(new Tag("span")).content(NBSP+lengthUnit)));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Tag"), new Input(TAGS,String.join(", ", tags))));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Maximum Speed"),new Input(MAX_SPEED, maxSpeed).numeric().addTo(new Tag("span")).content(NBSP+speedUnit)));
|
||||
|
||||
Fieldset fieldset = new Fieldset(t("Train"));
|
||||
if (train != null) train.link().addTo(fieldset);
|
||||
|
||||
return super.properties(List.of(),formInputs,List.of(fieldset));
|
||||
postForm.add(fieldset);
|
||||
|
||||
return super.properties(preForm,formInputs,postForm);
|
||||
}
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package de.srsoftware.web4rail.moving;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -260,33 +263,16 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Window properties() {
|
||||
Window win = super.properties();
|
||||
Tag cockpit = cockpit(this);
|
||||
win.children().insertElementAt(cockpit, 2);
|
||||
return win;
|
||||
}
|
||||
|
||||
/* @Override
|
||||
public Form propertyForm() {
|
||||
Form form = super.propertyForm();
|
||||
|
||||
for (Tag tag : form.children()) {
|
||||
if (REALM.equals(tag.get(Input.NAME)) && REALM_CAR.equals(tag.get(Input.VALUE))) {
|
||||
tag.attr("value", REALM_LOCO);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Fieldset fieldset = new Fieldset("Decoder settings");
|
||||
Label protocol = new Label(t("Protocol:"));
|
||||
protected Window properties(List<Fieldset> preForm, List<Entry<String, Tag>> formInputs, List<Fieldset> postForm) {
|
||||
preForm.add(cockpit(this));
|
||||
Tag div = new Tag("div");
|
||||
for (Protocol proto : Protocol.values()) {
|
||||
new Radio(PROTOCOL, proto.toString(), t(proto.toString()), proto == this.proto).addTo(protocol);
|
||||
new Radio(PROTOCOL, proto.toString(), t(proto.toString()), proto == this.proto).addTo(div);
|
||||
}
|
||||
protocol.addTo(fieldset);
|
||||
new Input(ADDRESS, address).attr("type", "number").addTo(new Label(t("Address:"))).addTo(fieldset);
|
||||
fieldset.addTo(form);
|
||||
return form;
|
||||
}*/
|
||||
formInputs.add(new AbstractMap.SimpleEntry<String,Tag>(t("Protocol"),div));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<String,Tag>(t("Address"),new Input(ADDRESS, address).numeric()));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
private void queue() {
|
||||
int step = proto.steps * speed / (maxSpeed == 0 ? 100 : maxSpeed);
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -502,9 +503,9 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
this.plan = plan;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Window properties() {
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, List<Entry<String, Tag>> formInputs, List<Fieldset> postForm) {
|
||||
Fieldset otherTrainProsps = new Fieldset(t("other train properties"));
|
||||
|
||||
Tag propList = new Tag("ul").clazz("proplist");
|
||||
@@ -534,7 +535,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
for (String tag : allTags) new Tag("li").content(tag).addTo(tagList);
|
||||
tagList.addTo(new Tag("li").content(t("Tags"))).addTo(propList);
|
||||
}
|
||||
new Tag("li").content(t("length: {}",length())).addTo(propList);
|
||||
new Tag("li").content(t("length: {}",length())+NBSP+lengthUnit).addTo(propList);
|
||||
|
||||
if (!trace.isEmpty()) {
|
||||
Tag li = new Tag("li").content(t("Occupied area:"));
|
||||
@@ -545,13 +546,14 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
propList.addTo(otherTrainProsps);
|
||||
|
||||
List<Tag> formInputs = List.of(
|
||||
new Input(NAME,name),
|
||||
new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull),
|
||||
new Input(TAGS,String.join(", ", tags)).addTo(new Label(t("Tags")+NBSP))
|
||||
);
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Name"), new Input(NAME,name)));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Push-pull train"),new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull)));
|
||||
formInputs.add(new AbstractMap.SimpleEntry<>(t("Tags"), new Input(TAGS,String.join(", ", tags))));
|
||||
|
||||
return super.properties(List.of(Locomotive.cockpit(this)), formInputs, List.of(otherTrainProsps));
|
||||
preForm.add(Locomotive.cockpit(this));
|
||||
postForm.add(otherTrainProsps);
|
||||
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
public Object quitAutopilot() {
|
||||
|
||||
Reference in New Issue
Block a user