26 changed files with 72414 additions and 144 deletions
@ -1,10 +1,27 @@
@@ -1,10 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<classpath> |
||||
<classpathentry kind="src" path="src/main/java"/> |
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> |
||||
<classpathentry kind="src" output="target/classes" path="src/main/java"> |
||||
<attributes> |
||||
<attribute name="module" value="true"/> |
||||
<attribute name="optional" value="true"/> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="output" path="bin"/> |
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> |
||||
<attributes> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> |
||||
<attributes> |
||||
<attribute name="optional" value="true"/> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
<attribute name="test" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> |
||||
<attributes> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="output" path="target/classes"/> |
||||
</classpath> |
||||
|
@ -1,3 +1,4 @@
@@ -1,3 +1,4 @@
|
||||
/Debug/ |
||||
*.pyc |
||||
/bin/ |
||||
/target/ |
||||
|
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
eclipse.preferences.version=1 |
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 |
||||
org.eclipse.jdt.core.compiler.compliance=11 |
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled |
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning |
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore |
||||
org.eclipse.jdt.core.compiler.release=enabled |
||||
org.eclipse.jdt.core.compiler.source=11 |
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
activeProfiles= |
||||
eclipse.preferences.version=1 |
||||
resolveWorkspaceProjects=true |
||||
version=1 |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
.tile{ |
||||
border: 1px solid black; |
||||
width: 30px; |
||||
height: 30px; |
||||
position: absolute; |
||||
} |
||||
|
||||
svg polygon, |
||||
svg rect{ |
||||
fill:rgb(0,0,255); |
||||
stroke-width:3; |
||||
stroke:rgb(0,0,0); |
||||
} |
||||
|
||||
svg text{ |
||||
font-size: 50px; |
||||
} |
@ -0,0 +1,93 @@
@@ -0,0 +1,93 @@
|
||||
package de.srsoftware.web4rail; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.net.InetSocketAddress; |
||||
import java.net.URI; |
||||
import java.nio.charset.StandardCharsets; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.sun.net.httpserver.HttpExchange; |
||||
import com.sun.net.httpserver.HttpServer; |
||||
|
||||
import de.keawe.localconfig.Configuration; |
||||
import de.keawe.tools.translations.Translation; |
||||
import de.srsoftware.web4rail.tiles.DiagNE; |
||||
import de.srsoftware.web4rail.tiles.DiagSW; |
||||
import de.srsoftware.web4rail.tiles.DiagWN; |
||||
import de.srsoftware.web4rail.tiles.EndE; |
||||
import de.srsoftware.web4rail.tiles.StraightH; |
||||
import de.srsoftware.web4rail.tiles.StraightV; |
||||
import de.srsoftware.web4rail.tiles.TurnoutSE; |
||||
import de.srsoftware.web4rail.tiles.TurnoutSW; |
||||
import de.srsoftware.web4rail.tiles.TurnoutWS; |
||||
|
||||
public class Application { |
||||
private static Plan plan; |
||||
private static final Logger LOG = LoggerFactory.getLogger(Application.class); |
||||
|
||||
public static void main(String[] args) throws IOException { |
||||
Configuration config = new Configuration(Configuration.dir("Web4Rail")+"/app.config"); |
||||
plan = new Plan(); |
||||
plan.set(0, 0, new StraightH()); |
||||
plan.set(1, 0, new DiagSW()); |
||||
plan.set(1, 1, new StraightV()); |
||||
plan.set(1, 2, new DiagNE()); |
||||
plan.set(2, 2, new TurnoutWS()); |
||||
plan.set(3, 2, new DiagWN()); |
||||
plan.set(3, 1, new TurnoutSE()); |
||||
plan.set(3, 0, new TurnoutSW()); |
||||
plan.set(2, 0, new EndE()); |
||||
InetSocketAddress addr = new InetSocketAddress(config.getOrAdd("port", 8080)); |
||||
HttpServer server = HttpServer.create(addr, 0); |
||||
server.createContext("/plan", client -> sendPlan(client)); |
||||
server.createContext("/css" , client -> sendFile(client)); |
||||
server.createContext("/svg" , client -> sendFile(client)); |
||||
server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool()); |
||||
server.start(); |
||||
} |
||||
|
||||
private static void sendFile(HttpExchange client) throws IOException { |
||||
URI uri = client.getRequestURI(); |
||||
File file = new File(System.getProperty("user.dir")+uri); |
||||
if (file.exists()) { |
||||
client.sendResponseHeaders(200, file.length()); |
||||
OutputStream out = client.getResponseBody(); |
||||
FileInputStream in = new FileInputStream(file); |
||||
in.transferTo(out); |
||||
in.close(); |
||||
out.close(); |
||||
return; |
||||
} |
||||
sendError(client,404,t("Could not find \"{}\"",uri)); |
||||
} |
||||
|
||||
private static void sendError(HttpExchange client, int code, String msg) throws IOException { |
||||
client.sendResponseHeaders(code, msg.length()); |
||||
OutputStream out = client.getResponseBody(); |
||||
out.write(msg.getBytes(StandardCharsets.UTF_8)); |
||||
out.close(); |
||||
} |
||||
|
||||
private static String t(String text, Object...fills) { |
||||
return Translation.get(Application.class, text, fills); |
||||
} |
||||
|
||||
private static void sendPlan(HttpExchange client) throws IOException { |
||||
send(client,plan.html().style("css/style.css")); |
||||
} |
||||
|
||||
private static void send(HttpExchange client, Page response) throws IOException { |
||||
client.getResponseHeaders().set("content-type", "text/plain"); |
||||
StringBuffer html = response.html(); |
||||
client.getResponseHeaders().add("content-type", "text/html"); |
||||
client.sendResponseHeaders(200, html.length()); |
||||
OutputStream os = client.getResponseBody(); |
||||
os.write(html.toString().getBytes(StandardCharsets.UTF_8)); |
||||
os.close(); |
||||
} |
||||
} |
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
package de.srsoftware.web4rail; |
||||
|
||||
import java.util.Vector; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
|
||||
|
||||
public class Page { |
||||
private StringBuffer buf; |
||||
private Vector<String> cssFiles = new Vector<String>(); |
||||
|
||||
public Page() { |
||||
buf = new StringBuffer(); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return head().append(body(buf)).toString(); |
||||
} |
||||
|
||||
private StringBuffer head() { |
||||
StringBuffer sb = new StringBuffer() |
||||
.append("<html>\n") |
||||
.append("\t<head>\n"); |
||||
for (String cssFile : cssFiles) { |
||||
sb.append("\t\t"+new Tag("link").attr("rel", "stylesheet").attr("type", "text/css").attr("href", cssFile)+"\n"); |
||||
} |
||||
return sb.append("\t</head>\n"); |
||||
} |
||||
|
||||
private StringBuffer body(StringBuffer content) { |
||||
return new StringBuffer() |
||||
.append("\t<body>\n") |
||||
.append(content) |
||||
.append("\t</body>\n") |
||||
.append("</html>\n"); |
||||
} |
||||
|
||||
public StringBuffer html() { |
||||
return head().append(body(buf)); |
||||
} |
||||
|
||||
public Page append(Object code) { |
||||
buf.append(code); |
||||
return this; |
||||
} |
||||
|
||||
public Page style(String cssPath) { |
||||
cssFiles.add(cssPath); |
||||
return this; |
||||
} |
||||
} |
@ -1,16 +1,20 @@
@@ -1,16 +1,20 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class DiagNE extends Tile{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
switch (direction) { |
||||
case NORTH: |
||||
case EAST: |
||||
return true; |
||||
default: |
||||
return false; |
||||
} |
||||
} |
||||
new Tag("polygon") |
||||
.attr("points","35,0 65,0 100,35 100,65") |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
@ -1,16 +1,20 @@
@@ -1,16 +1,20 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class DiagSW extends Tile{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
switch (direction) { |
||||
case SOUTH: |
||||
case WEST: |
||||
return true; |
||||
default: |
||||
return false; |
||||
} |
||||
} |
||||
new Tag("polygon") |
||||
.attr("points","0,35 65,100 35,100 0,65") |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
@ -1,16 +1,20 @@
@@ -1,16 +1,20 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class DiagWN extends Tile{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
switch (direction) { |
||||
case NORTH: |
||||
case WEST: |
||||
return true; |
||||
default: |
||||
return false; |
||||
} |
||||
} |
||||
new Tag("polygon") |
||||
.attr("points","35,0 65,0 0,65 0,35") |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class EndE extends Tile{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
new Tag("rect") |
||||
.size(60,30) |
||||
.pos(40,35) |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
|
||||
} |
@ -1,14 +1,21 @@
@@ -1,14 +1,21 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class StraightH extends StretchableTile{ |
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
switch (direction) { |
||||
case EAST: |
||||
case WEST: |
||||
return true; |
||||
default: |
||||
return false; |
||||
} |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
new Tag("rect") |
||||
.size(100,30) |
||||
.pos(0,35) |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
@ -1,15 +1,21 @@
@@ -1,15 +1,21 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class StraightV extends StretchableTile{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
switch (direction) { |
||||
case NORTH: |
||||
case SOUTH: |
||||
return true; |
||||
default: |
||||
return false; |
||||
} |
||||
new Tag("rect") |
||||
.size(30,100) |
||||
.pos(35,0) |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
@ -1,55 +1,46 @@
@@ -1,55 +1,46 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
|
||||
import de.keawe.tools.translations.Translation; |
||||
import de.srsoftware.tools.Tag; |
||||
import de.srsoftware.web4rail.Application; |
||||
|
||||
public abstract class Tile { |
||||
|
||||
public enum Direction{ |
||||
NORTH,SOUTH,EAST,WEST; |
||||
} |
||||
private class Position { |
||||
int x,y; |
||||
} |
||||
protected int x,y; |
||||
protected HashSet<String> classes = new HashSet<String>(); |
||||
|
||||
Position position; |
||||
private HashMap<Direction,Tile> neighbours = new HashMap(); |
||||
|
||||
public abstract boolean hasConnector(Direction direction); |
||||
|
||||
public boolean connect(Direction direction, Tile neighbour) { |
||||
if (hasConnector(direction)) { |
||||
switch (direction) { |
||||
case NORTH: |
||||
neighbour.neighbours.put(Direction.SOUTH, this); |
||||
neighbour.position.x = position.x; |
||||
neighbour.position.y = position.y-1; |
||||
case SOUTH: |
||||
neighbour.neighbours.put(Direction.NORTH, this); |
||||
neighbour.position.x = position.x; |
||||
neighbour.position.y = position.y+1; |
||||
case EAST: |
||||
neighbour.neighbours.put(Direction.WEST, this); |
||||
neighbour.position.x = position.x+1; |
||||
neighbour.position.y = position.y; |
||||
case WEST: |
||||
neighbour.neighbours.put(Direction.EAST, this); |
||||
neighbour.position.x = position.x-1; |
||||
neighbour.position.y = position.y; |
||||
public Tile() { |
||||
classes.add("tile"); |
||||
} |
||||
neighbours.put(direction, neighbour); |
||||
return true; |
||||
} |
||||
return false; |
||||
|
||||
public Tile position(int x, int y) { |
||||
this.x = x; |
||||
this.y = y; |
||||
return this; |
||||
} |
||||
|
||||
public Tile neighbour(Direction direction) { |
||||
return neighbours.get(direction); |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
new Tag("title").content(t("No display defined for this tile ({})",getClass().getSimpleName())).addTo(svg); |
||||
|
||||
new Tag("text") |
||||
.pos(35,70) |
||||
.content("?") |
||||
.addTo(svg); |
||||
|
||||
|
||||
return svg.toString(); |
||||
} |
||||
|
||||
public Tile position(int x, int y) { |
||||
position.x = x; |
||||
position.y = y; |
||||
return this; |
||||
private String t(String txt, Object...fills) { |
||||
return Translation.get(Application.class, txt, fills); |
||||
} |
||||
|
||||
} |
||||
|
@ -1,10 +1,25 @@
@@ -1,10 +1,25 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class TurnoutSE extends Turnout{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
return direction != Direction.WEST; |
||||
} |
||||
new Tag("rect") |
||||
.size(30,100) |
||||
.pos(35,0) |
||||
.addTo(svg); |
||||
|
||||
new Tag("polygon") |
||||
.attr("points","35,100 100,35 100,65 65,100") |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
@ -1,10 +1,25 @@
@@ -1,10 +1,25 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class TurnoutSW extends Turnout{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
return direction != Direction.EAST; |
||||
} |
||||
new Tag("rect") |
||||
.size(30,100) |
||||
.pos(35,0) |
||||
.addTo(svg); |
||||
|
||||
new Tag("polygon") |
||||
.attr("points","0,35 65,100 35,100 0,65") |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
@ -1,10 +1,25 @@
@@ -1,10 +1,25 @@
|
||||
package de.srsoftware.web4rail.tiles; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class TurnoutWS extends Turnout{ |
||||
public String html() { |
||||
Tag svg = new Tag("svg") |
||||
.id("tile-"+x+"-"+y) |
||||
.clazz(classes) |
||||
.size(100,100) |
||||
.attr("viewbox", "0 0 100 100") |
||||
.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); |
||||
|
||||
@Override |
||||
public boolean hasConnector(Direction direction) { |
||||
return direction != Direction.NORTH; |
||||
} |
||||
new Tag("rect") |
||||
.size(100,30) |
||||
.pos(0,35) |
||||
.addTo(svg); |
||||
|
||||
new Tag("polygon") |
||||
.attr("points","0,35 65,100 35,100 0,65") |
||||
.addTo(svg); |
||||
|
||||
return svg.toString(); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue