improved window tiling
This commit is contained in:
parent
3dc11c18e1
commit
8e747cb2a0
2
pom.xml
2
pom.xml
@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.6.1</version>
|
||||
<version>0.6.3</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@ -7,12 +7,21 @@ html{
|
||||
}
|
||||
body{
|
||||
min-height: 100%;
|
||||
background: #c6dbd2;
|
||||
}
|
||||
|
||||
#plan{
|
||||
background: #c6dbd2;
|
||||
#plan,
|
||||
.window{
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
overflow: auto;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
.window{
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
background: white;
|
||||
}
|
||||
|
||||
.tile{
|
||||
@ -69,7 +78,7 @@ svg circle{
|
||||
position: fixed;
|
||||
width: 100%;
|
||||
height: 30px;
|
||||
bottom: 0px;
|
||||
bottom: 20px;
|
||||
}
|
||||
|
||||
.menu > div{
|
||||
@ -95,8 +104,9 @@ svg circle{
|
||||
.menu > div > .list{
|
||||
position: fixed;
|
||||
height: 30px;
|
||||
bottom: 30px;
|
||||
bottom: 50px;
|
||||
width: 100%;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.menu > div > .list > *{
|
||||
@ -119,32 +129,29 @@ svg circle{
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.window{
|
||||
position: fixed;
|
||||
top: 50%;
|
||||
bottom: 10px;
|
||||
left: 10px;
|
||||
right: 10px;
|
||||
background: #ecffa2;
|
||||
padding: 5px;
|
||||
overflow: scroll;
|
||||
}
|
||||
|
||||
h2{
|
||||
margin: 0 0 10px;
|
||||
text-align: center;
|
||||
background: #627fda;
|
||||
}
|
||||
|
||||
.swapbtn,
|
||||
.closebtn{
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
top: 10px;
|
||||
font-size: 30px;
|
||||
background: red;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.closebtn{
|
||||
right: 10px;
|
||||
}
|
||||
|
||||
.swapbtn{
|
||||
right: 50px;
|
||||
}
|
||||
|
||||
#Eraser polygon{
|
||||
stroke: red;
|
||||
}
|
||||
|
||||
@ -51,6 +51,7 @@ function closeMenu(ev){
|
||||
|
||||
function closeWindows(){
|
||||
$('.window').remove();
|
||||
$('#plan').css('height','').css('width','');
|
||||
}
|
||||
|
||||
function connectCu(){
|
||||
@ -158,8 +159,13 @@ function request(data){
|
||||
if (resp.startsWith('<svg')){
|
||||
$(PLAN).append($(resp));
|
||||
} else if (resp.startsWith('<')) {
|
||||
// console.log("appending to body: "+resp.substring(0,10));
|
||||
var isWindow = $(resp).attr('class') == 'window';
|
||||
if (isWindow){
|
||||
$('.window').remove();
|
||||
$('#plan').css('height','50%');
|
||||
}
|
||||
$(BODY).append($(resp));
|
||||
$('.window').css('height','50%');
|
||||
} else {
|
||||
addMessage(resp);
|
||||
}
|
||||
@ -185,6 +191,18 @@ function stream(ev){
|
||||
if (data.startsWith("dropclass")) return dropClass(data.substring(10));
|
||||
}
|
||||
|
||||
function swapTiling(ev){
|
||||
if ($('.swapbtn').text() == '◧'){
|
||||
$('.swapbtn').text('⬒');
|
||||
$('.window').css('height','').css('width','50%');
|
||||
$(PLAN).css('height','').css('width','50%');
|
||||
} else {
|
||||
$('.swapbtn').text('◧');
|
||||
$('.window').css('height','50%').css('width','');
|
||||
$(PLAN).css('height','50%').css('width','');
|
||||
}
|
||||
}
|
||||
|
||||
function train(id,action){
|
||||
return request({realm:'train',action:action,id:id});
|
||||
}
|
||||
|
||||
@ -11,8 +11,6 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@ -25,16 +23,18 @@ import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import de.keawe.localconfig.Configuration;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
import de.srsoftware.web4rail.moving.Locomotive;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
|
||||
public class Application {
|
||||
public class Application implements Constants{
|
||||
private static Plan plan;
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
|
||||
private static final String PORT = "port";
|
||||
private static final Charset UTF8 = StandardCharsets.UTF_8;
|
||||
|
||||
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
Configuration config = new Configuration(Configuration.dir("Web4Rail")+"/app.config");
|
||||
LOG.debug("Config: {}",config);
|
||||
LOG.debug("config: {}",config);
|
||||
InetSocketAddress addr = new InetSocketAddress(config.getOrAdd(PORT, 8080));
|
||||
HttpServer server = HttpServer.create(addr, 0);
|
||||
server.createContext("/plan", client -> sendPlan(client));
|
||||
@ -51,6 +51,32 @@ public class Application {
|
||||
Desktop.getDesktop().browse(URI.create("http://localhost:"+config.getInt(PORT)+"/plan"));
|
||||
}
|
||||
|
||||
private static Object handle(HashMap<String, String> params) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
|
||||
String realm = params.get(REALM);
|
||||
if (realm == null) throw new NullPointerException(REALM+" should not be null!");
|
||||
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
|
||||
|
||||
switch (realm) {
|
||||
case REALM_CAR:
|
||||
return Car.action(params);
|
||||
case REALM_CU:
|
||||
return plan.controlUnit().process(params);
|
||||
case REALM_LOCO:
|
||||
return Locomotive.action(params);
|
||||
case REALM_PLAN:
|
||||
return plan.action(params);
|
||||
case REALM_ROUTE:
|
||||
return plan.routeAction(params);
|
||||
case REALM_TRAIN:
|
||||
return Train.action(params);
|
||||
}
|
||||
|
||||
return t("Unknown realm: {}",params.get(REALM));
|
||||
}
|
||||
|
||||
private static HashMap<String, String> inflate(String data) {
|
||||
//LOG.debug("inflate({})",data);
|
||||
HashMap<String, String> params = new HashMap<String, String>();
|
||||
@ -130,13 +156,22 @@ public class Application {
|
||||
private static void sendPlan(HttpExchange client) throws IOException {
|
||||
try {
|
||||
HashMap<String, String> params = inflate(client.getRequestBody().readAllBytes());
|
||||
send(client,params.isEmpty() ? plan.html() : plan.action(params));
|
||||
LOG.debug("sendPlan({})",params);
|
||||
|
||||
if (params.isEmpty()) {
|
||||
send(client,plan.html());
|
||||
return;
|
||||
}
|
||||
|
||||
Object response = handle(params);
|
||||
LOG.debug("response ({}): {}",response.getClass().getSimpleName(),response);
|
||||
send(client,response instanceof String || response instanceof Tag ? response : plan.html());
|
||||
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error during sendPlan(): {}",e);
|
||||
send(client,new Page().append(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void stream(HttpExchange client) throws IOException {
|
||||
client.getResponseHeaders().set("content-type", "text/event-stream");
|
||||
client.sendResponseHeaders(200, 0);
|
||||
|
||||
@ -1,5 +1,11 @@
|
||||
package de.srsoftware.web4rail;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public interface Constants {
|
||||
public static final String ACTION = "action";
|
||||
public static final String ACTION_ADD = "add";
|
||||
@ -30,4 +36,7 @@ public interface Constants {
|
||||
public static final String REALM_TRAIN = "train";
|
||||
|
||||
public static final String ID = "id";
|
||||
public static final String PORT = "port";
|
||||
public static final Charset UTF8 = StandardCharsets.UTF_8;
|
||||
|
||||
}
|
||||
|
||||
@ -21,7 +21,6 @@ import org.slf4j.LoggerFactory;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
import de.srsoftware.web4rail.moving.Locomotive;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Div;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
@ -106,49 +105,8 @@ public class Plan implements Constants{
|
||||
new Heartbeat().start();
|
||||
}
|
||||
|
||||
public Object action(HashMap<String, String> params) {
|
||||
try {
|
||||
LOG.debug("Plan.action: {}",params);
|
||||
String realm = params.get(REALM);
|
||||
if (realm == null) throw new NullPointerException(REALM+" should not be null!");
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
|
||||
switch (realm) {
|
||||
case REALM_CAR:
|
||||
return carAction(params);
|
||||
case REALM_CU:
|
||||
return controlUnit.process(params);
|
||||
case REALM_LOCO:
|
||||
return locoAction(params);
|
||||
case REALM_PLAN:
|
||||
return planAction(params);
|
||||
case REALM_ROUTE:
|
||||
return routeAction(params);
|
||||
case REALM_TRAIN:
|
||||
Object result = Train.action(params);
|
||||
if (result instanceof Train) return html();
|
||||
return result;
|
||||
}
|
||||
return t("Unknown realm: {}",realm);
|
||||
} catch (Exception e) {
|
||||
String msg = e.getMessage();
|
||||
if (msg == null || msg.isEmpty()) msg = t("An unknown error occured!");
|
||||
LOG.debug(msg,e);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
private Object routeAction(HashMap<String, String> params) throws IOException {
|
||||
Route route = route(Integer.parseInt(params.get(ID)));
|
||||
if (route == null) return t("Unknown route: {}",params.get(ID));
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_PROPS:
|
||||
return route.properties();
|
||||
case ACTION_UPDATE:
|
||||
route.update(params);
|
||||
return html();
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
public ControlUnit controlUnit() {
|
||||
return controlUnit;
|
||||
}
|
||||
|
||||
private Tag actionMenu() throws IOException {
|
||||
@ -204,19 +162,6 @@ public class Plan implements Constants{
|
||||
return blocks;
|
||||
}
|
||||
|
||||
private Object carAction(HashMap<String, String> params) throws IOException {
|
||||
Car car = Car.get(params.get(Car.ID));
|
||||
if (car == null) return t("No car with id {} found!",params.get(Car.ID));
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_UPDATE:
|
||||
car.update(params);
|
||||
return html();
|
||||
case ACTION_PROPS:
|
||||
return car.properties();
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private Object click(Tile tile) throws IOException {
|
||||
if (tile == null) return null;
|
||||
return tile.click();
|
||||
@ -325,28 +270,6 @@ public class Plan implements Constants{
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
private Object locoAction(HashMap<String, String> params) throws IOException {
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD:
|
||||
new Locomotive(params.get(Locomotive.NAME));
|
||||
return html();
|
||||
case ACTION_FASTER10:
|
||||
return Locomotive.get(params.get(ID)).faster(10);
|
||||
case ACTION_PROPS:
|
||||
String id = params.get(ID);
|
||||
if (id == null) return Locomotive.manager();
|
||||
return Locomotive.get(params.get(ID)).properties();
|
||||
case ACTION_SLOWER10:
|
||||
return Locomotive.get(params.get(ID)).faster(-10);
|
||||
case ACTION_STOP:
|
||||
return Locomotive.get(params.get(ID)).stop();
|
||||
case ACTION_TURN:
|
||||
return Locomotive.get(params.get(ID)).turn();
|
||||
}
|
||||
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private Tag menu() throws IOException {
|
||||
Tag menu = new Tag("div").clazz("menu");
|
||||
@ -432,7 +355,7 @@ public class Plan implements Constants{
|
||||
return tile;
|
||||
}
|
||||
|
||||
private Object planAction(HashMap<String, String> params) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException {
|
||||
public Object action(HashMap<String, String> params) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD:
|
||||
return addTile(params.get(TILE),params.get(X),params.get(Y),null);
|
||||
@ -473,6 +396,19 @@ public class Plan implements Constants{
|
||||
LOG.debug("removed {} from tile list",tiles.remove(Tile.id(x, y)));
|
||||
}
|
||||
|
||||
Object routeAction(HashMap<String, String> params) throws IOException {
|
||||
Route route = route(Integer.parseInt(params.get(ID)));
|
||||
if (route == null) return t("Unknown route: {}",params.get(ID));
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_PROPS:
|
||||
return route.properties();
|
||||
case ACTION_UPDATE:
|
||||
route.update(params);
|
||||
return html();
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private String saveTo(String name) throws IOException {
|
||||
if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");
|
||||
Car.saveAll(name+".cars");
|
||||
|
||||
@ -14,7 +14,11 @@ public class Window extends Tag{
|
||||
.content(title).addTo(this);
|
||||
new Tag("div")
|
||||
.clazz("closebtn")
|
||||
.attr("onclick", "$('#"+id+"').remove(); return false")
|
||||
.attr("onclick", "return closeWindows();")
|
||||
.content("×").addTo(this);
|
||||
new Tag("div")
|
||||
.clazz("swapbtn")
|
||||
.attr("onclick", "return swapTiling();")
|
||||
.content("◧").addTo(this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,6 +54,18 @@ public class Car implements Constants {
|
||||
}
|
||||
this.id = id;
|
||||
cars.put(id, this);
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params) throws IOException {
|
||||
Car car = Car.get(params.get(Car.ID));
|
||||
if (car == null) return t("No car with id {} found!",params.get(Car.ID));
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_UPDATE:
|
||||
return car.update(params);
|
||||
case ACTION_PROPS:
|
||||
return car.properties();
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
protected Tag cockpit(String realm) {
|
||||
@ -177,10 +189,10 @@ public class Car implements Constants {
|
||||
this.train = train;
|
||||
}
|
||||
|
||||
public Object update(HashMap<String, String> params) {
|
||||
public Car update(HashMap<String, String> params) {
|
||||
if (params.containsKey(NAME)) name = params.get(NAME);
|
||||
if (params.containsKey(STOCK_ID)) stockId = params.get(STOCK_ID);
|
||||
if (params.containsKey(LENGTH)) length = Integer.parseInt(params.get(LENGTH));
|
||||
return null;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.srsoftware.web4rail.moving;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Vector;
|
||||
|
||||
@ -35,6 +36,27 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
super(name,id);
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params) throws IOException {
|
||||
String id = params.get(ID);
|
||||
Locomotive loco = id == null ? null : Locomotive.get(id);
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD:
|
||||
return new Locomotive(params.get(Locomotive.NAME));
|
||||
case ACTION_FASTER10:
|
||||
return loco.faster(10);
|
||||
case ACTION_PROPS:
|
||||
return loco == null ? Locomotive.manager() : loco.properties();
|
||||
case ACTION_SLOWER10:
|
||||
return loco.faster(-10);
|
||||
case ACTION_STOP:
|
||||
return loco.stop();
|
||||
case ACTION_TURN:
|
||||
return loco.turn();
|
||||
}
|
||||
|
||||
return Car.action(params);
|
||||
}
|
||||
|
||||
protected Tag cockpit(String realm) {
|
||||
Fieldset fieldset = new Fieldset(t("Control"));
|
||||
String request = "return request({realm:'"+realm+"',id:"+id()+",action:'{}'})";
|
||||
@ -171,10 +193,10 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object update(HashMap<String, String> params) {
|
||||
public Car update(HashMap<String, String> params) {
|
||||
super.update(params);
|
||||
if (params.containsKey(PROTOCOL)) proto = Protocol.valueOf(params.get(PROTOCOL));
|
||||
if (params.containsKey(ADDRESS)) address = Integer.parseInt(params.get(ADDRESS));
|
||||
return t("Updated locomotive.");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user