8 changed files with 284 additions and 1 deletions
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
package de.srsoftware.web4rail.conditions; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
|
||||
import org.json.JSONObject; |
||||
|
||||
import de.srsoftware.web4rail.BaseClass; |
||||
import de.srsoftware.web4rail.tags.Fieldset; |
||||
import de.srsoftware.web4rail.tags.Window; |
||||
import de.srsoftware.web4rail.tiles.Switch; |
||||
|
||||
public class SwitchIsOn extends Condition { |
||||
|
||||
private Switch swtch = null; |
||||
private static final String SWITCH = "switch"; |
||||
|
||||
|
||||
|
||||
@Override |
||||
public boolean fulfilledBy(Context context) { |
||||
if (isSet(swtch)) return swtch.isOn() != inverted; |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public JSONObject json() { |
||||
JSONObject json = super.json(); |
||||
if (isSet(swtch)) json.put(SWITCH, swtch.id().toString()); |
||||
return json; |
||||
} |
||||
|
||||
public Condition load(JSONObject json) { |
||||
super.load(json); |
||||
if (json.has(SWITCH)) swtch = BaseClass.get(new Id(json.getString(SWITCH))); |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) { |
||||
formInputs.add(t("Select switch"),Switch.selector(swtch)); |
||||
|
||||
return super.properties(preForm, formInputs, postForm); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
if (isNull(SWITCH)) return "["+t("Click here to select switch!")+"]"; |
||||
return t(inverted ? "{} is off" : "{} is on",swtch) ; |
||||
} |
||||
|
||||
@Override |
||||
protected Object update(HashMap<String, String> params) { |
||||
String switchId = params.get(Switch.class.getSimpleName()); |
||||
if (isSet(switchId)) swtch = BaseClass.get(new Id(switchId)); |
||||
return super.update(params); |
||||
} |
||||
} |
@ -0,0 +1,194 @@
@@ -0,0 +1,194 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Map.Entry; |
||||
import java.util.TreeMap; |
||||
|
||||
import org.json.JSONArray; |
||||
import org.json.JSONObject; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
import de.srsoftware.web4rail.Application; |
||||
import de.srsoftware.web4rail.BaseClass; |
||||
import de.srsoftware.web4rail.actions.Action; |
||||
import de.srsoftware.web4rail.actions.ActionList; |
||||
import de.srsoftware.web4rail.tags.Fieldset; |
||||
import de.srsoftware.web4rail.tags.Select; |
||||
import de.srsoftware.web4rail.tags.Window; |
||||
|
||||
public class Switch extends Tile{ |
||||
|
||||
private boolean state = false; |
||||
private ActionList actionsOn,actionsOff; |
||||
private static final String ON = "ON"; |
||||
private static final String OFF = "OFF"; |
||||
|
||||
public Switch() { |
||||
actionsOn = new ActionList(this); |
||||
actionsOff = new ActionList(this); |
||||
} |
||||
|
||||
@Override |
||||
protected HashSet<String> classes() { |
||||
HashSet<String> classes = super.classes(); |
||||
classes.add(state?"on":"off"); |
||||
return classes; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public Object click() throws IOException { |
||||
state = !state; |
||||
Application.threadPool.execute(new Runnable() { |
||||
|
||||
@Override |
||||
public void run() { |
||||
Context context = new Context(Switch.this); |
||||
if (state) { |
||||
actionsOn.fire(context); |
||||
} else actionsOff.fire(context); |
||||
} |
||||
}); |
||||
stream(); |
||||
return super.click(); |
||||
} |
||||
|
||||
@Override |
||||
public JSONObject json() { |
||||
JSONObject json = super.json(); |
||||
if (!actionsOn.isEmpty()) json.put(ON, actionsOn.json()); |
||||
if (!actionsOff.isEmpty()) json.put(OFF, actionsOff.json()); |
||||
json.put(STATE, state); |
||||
return json; |
||||
} |
||||
|
||||
@Override |
||||
public Tile load(JSONObject json) { |
||||
if (json.has(ON)) { |
||||
Object dummy = json.get(ON); |
||||
if (dummy instanceof JSONArray) { |
||||
JSONArray jarr = (JSONArray) dummy; |
||||
for (Object o : jarr) { |
||||
if (o instanceof JSONObject) { |
||||
JSONObject jo = (JSONObject) o; |
||||
String type = jo.getString("type"); |
||||
Action action = Action.create(type, actionsOn); |
||||
if (isSet(action)) { |
||||
action.load(jo); |
||||
actionsOn.add(action); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
if (dummy instanceof JSONObject) { |
||||
actionsOn.load((JSONObject) dummy); |
||||
} |
||||
} |
||||
if (json.has(OFF)) { |
||||
Object dummy = json.get(OFF); |
||||
if (dummy instanceof JSONArray) { |
||||
JSONArray jarr = (JSONArray) dummy; |
||||
for (Object o : jarr) { |
||||
if (o instanceof JSONObject) { |
||||
JSONObject jo = (JSONObject) o; |
||||
String type = jo.getString("type"); |
||||
Action action = Action.create(type, actionsOff); |
||||
if (isSet(action)) { |
||||
action.load(jo); |
||||
actionsOff.add(action); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
if (dummy instanceof JSONObject) { |
||||
actionsOff.load((JSONObject) dummy); |
||||
} |
||||
} |
||||
if (json.has(STATE)) state =json.getBoolean(STATE); |
||||
return super.load(json); |
||||
} |
||||
|
||||
@Override |
||||
public Tile position(int x, int y) { |
||||
super.position(x, y); |
||||
return this; |
||||
} |
||||
|
||||
public static Object process(HashMap<String, String> params) { |
||||
String action = params.get(ACTION); |
||||
Id id = Id.from(params); |
||||
if (action == null) return t("Missing ACTION on call to {}.process()",Switch.class.getSimpleName()); |
||||
Switch swtch = isSet(id) ? BaseClass.get(id) : null; |
||||
switch (action) { |
||||
case ACTION_DROP: |
||||
if (isNull(id)) return t("Missing ID on call to {}.process()",Switch.class.getSimpleName()); |
||||
if (isNull(swtch)) return t("No contact with id {} found!",id); |
||||
swtch.remove(); |
||||
return t("Removed {}.",id); |
||||
case ACTION_PROPS: |
||||
return swtch.properties(); |
||||
case ACTION_UPDATE: |
||||
return plan.update(params); |
||||
} |
||||
return t("Unknown action: {}",action); |
||||
} |
||||
|
||||
@Override |
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) { |
||||
Fieldset fieldset = new Fieldset(t("Actions (On)")); |
||||
fieldset.id("actionsOn"); |
||||
actionsOn.list().addTo(fieldset); |
||||
postForm.add(fieldset); |
||||
fieldset = new Fieldset(t("Actions (Off)")); |
||||
fieldset.id("actionsOff"); |
||||
actionsOff.list().addTo(fieldset); |
||||
postForm.add(fieldset); |
||||
return super.properties(preForm, formInputs, postForm); |
||||
} |
||||
|
||||
@Override |
||||
public void removeChild(BaseClass child) { |
||||
if (child == actionsOn) actionsOn = null; |
||||
if (child == actionsOff) actionsOff = null; |
||||
super.removeChild(child); |
||||
} |
||||
|
||||
public static Select selector(Switch preselect) { |
||||
TreeMap<String,Switch> sortedSet = new TreeMap<String, Switch>(); // Map from Name to Contact
|
||||
for (Switch contact : BaseClass.listElements(Switch.class)) sortedSet.put(contact.toString(), contact); |
||||
Select select = new Select("Switch"); |
||||
for (Entry<String, Switch> entry : sortedSet.entrySet()) { |
||||
Switch contact = entry.getValue(); |
||||
Tag option = select.addOption(contact.id(),contact); |
||||
if (contact == preselect) option.attr("selected", "selected"); |
||||
} |
||||
return select; |
||||
} |
||||
|
||||
public boolean isOn() { |
||||
return state; |
||||
} |
||||
|
||||
public void stream() { |
||||
try { |
||||
Tag tag = tag(null); |
||||
if (state) tag.clazz(tag.get("class")+" active"); |
||||
plan.stream("place "+tag); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String title() { |
||||
return t("Switch @ ({}, {})",x,y); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return t("Switch")+"("+x+","+y+")"; |
||||
} |
||||
} |
Loading…
Reference in new issue