Skip to content

Commit

Permalink
Merge pull request #49 from fransjacobs/20-fix-route-display-of-a-cross
Browse files Browse the repository at this point in the history
20 fix route display of a cross
  • Loading branch information
fransjacobs authored Oct 21, 2023
2 parents 470c60f + bee69ed commit efd3d27
Show file tree
Hide file tree
Showing 47 changed files with 2,973 additions and 1,180 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/target/
.DS_Store
src/main/resources/media/Thumbs.db
src/main/resources/media/Thumbs.db
src/main/resources/media/Thumbs.db:encryptable
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>


<!--
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
44 changes: 43 additions & 1 deletion src/main/java/jcs/entities/RouteElementBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class RouteElementBean implements Serializable {
private String tileId;
private String accessoryState;
private Integer elementOrder;
private String incomingSide;

private TileBean tileBean;

public RouteElementBean() {
}
Expand Down Expand Up @@ -117,6 +120,41 @@ public void setElementOrder(Integer elementOrder) {
this.elementOrder = elementOrder;
}

@Column(name = "incoming_side", length = 255)
public String getIncomingSide() {
return incomingSide;
}

public void setIncomingSide(String incomingSide) {
this.incomingSide = incomingSide;
}

@Transient
public TileBean.Orientation getIncomingOrientation() {
if (incomingSide != null) {
return TileBean.Orientation.get(this.incomingSide);
} else {
return null;
}
}

public void setIncomingOrientation(TileBean.Orientation orientation) {
if (orientation != null) {
this.incomingSide = orientation.getOrientation();
} else {
this.incomingSide = null;
}
}

@Transient
public TileBean getTileBean() {
return tileBean;
}

public void setTileBean(TileBean tileBean) {
this.tileBean = tileBean;
}

@Override
public int hashCode() {
int hash = 3;
Expand All @@ -126,6 +164,7 @@ public int hashCode() {
hash = 23 * hash + Objects.hashCode(this.tileId);
hash = 23 * hash + Objects.hashCode(this.accessoryState);
hash = 23 * hash + Objects.hashCode(this.elementOrder);
hash = 23 * hash + Objects.hashCode(this.incomingSide);
return hash;
}

Expand All @@ -141,6 +180,9 @@ public boolean equals(Object obj) {
return false;
}
final RouteElementBean other = (RouteElementBean) obj;
if (!Objects.equals(this.incomingSide, other.incomingSide)) {
return false;
}
if (!Objects.equals(this.elementOrder, other.elementOrder)) {
return false;
}
Expand All @@ -161,7 +203,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "RouteElement{id=" + id + ", routeId=" + routeId + ", nodeId=" + nodeId + ", tileId=" + tileId + ", accessoryValue=" + accessoryState + ", elementOrder=" + elementOrder + "}";
return "RouteElement{id=" + id + ", routeId=" + routeId + ", nodeId=" + nodeId + ", tileId=" + tileId + ", accessoryValue=" + accessoryState + ", elementOrder=" + elementOrder + ", incomingSide=" + incomingSide + "}";
}

public String toLogString() {
Expand Down
38 changes: 7 additions & 31 deletions src/main/java/jcs/entities/TileBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
import javax.persistence.Transient;
import jcs.entities.enums.SignalType;

@Table(
name = "tiles",
indexes = {
@Index(name = "tile_x_y", columnList = "x, y", unique = true)})
@Table(name = "tiles", indexes = {
@Index(name = "tile_x_y", columnList = "x, y", unique = true)})
public class TileBean implements Serializable, Comparable {

protected String id;
Expand All @@ -49,45 +47,23 @@ public class TileBean implements Serializable, Comparable {
protected List<TileBean> neighbours;

protected AccessoryBean accessoryBean;

protected SensorBean sensorBean;
protected BlockBean blockBean;

public TileBean() {
this(null, TileType.STRAIGHT, Orientation.EAST, Direction.CENTER, 0, 0, null, null, null);
}

public TileBean(
String id,
TileType tileType,
Orientation orientation,
Direction direction,
Integer x,
Integer y) {
public TileBean(String id, TileType tileType, Orientation orientation, Direction direction, Integer x, Integer y) {
this(id, tileType, orientation, direction, x, y, null, null, null);
}

public TileBean(
String id,
TileType tileType,
Orientation orientation,
Direction direction,
Point center,
SignalType signalType,
String accessoryId,
String sensorId) {
public TileBean(String id, TileType tileType, Orientation orientation, Direction direction, Point center, SignalType signalType, String accessoryId, String sensorId) {
this(id, tileType, orientation, direction, center.x, center.y, signalType, null, sensorId);
}

public TileBean(
String id,
TileType tileType,
Orientation orientation,
Direction direction,
Integer x,
Integer y,
SignalType signalType,
String accessoryId,
String sensorId) {
public TileBean(String id, TileType tileType, Orientation orientation, Direction direction, Integer x, Integer y, SignalType signalType, String accessoryId, String sensorId) {
this.id = id;
this.setTileType(tileType);
this.tileOrientation = orientation.getOrientation();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jcs/persistence/H2PersistenceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ public void persist(List<TileBean> tiles) {
}

private List<RouteElementBean> getRouteElements(String routeId) {
List<RouteElementBean> routeElements
= database.where("route_id=?", routeId).orderBy("order_seq").results(RouteElementBean.class);
List<RouteElementBean> routeElements = database.where("route_id=?", routeId).orderBy("order_seq").results(RouteElementBean.class);
//is the tile needed inside the route element?
return routeElements;
}

Expand Down
109 changes: 59 additions & 50 deletions src/main/java/jcs/ui/layout/LayoutCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public enum Mode {
private boolean readonly;
private Mode mode;
private boolean drawGrid = true;
private boolean lineGrid = true;

private Orientation orientation;
private Direction direction;
Expand Down Expand Up @@ -139,65 +140,70 @@ public LayoutCanvas(boolean readonly) {

private void postInit() {
routesDialog = new RoutesDialog(getParentFrame(), false, this, this.readonly);
lineGrid = "true".equals(System.getProperty("draw.linegrid", "true"));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();

Set<Tile> snapshot;
Set<Tile> snapshot = new HashSet<>(tiles.values());
//ensure ther is no tile which is null...
Map<String, RouteElementBean> routeSnapshot;

//synchronized (tiles) {
snapshot = new HashSet<>(tiles.values());
routeSnapshot = new HashMap<>(this.selectedRouteElements);
//}
//snapshot = new HashSet<>(tiles.values());
routeSnapshot = new HashMap<>(this.selectedRouteElements);

if (this.drawGrid) {
//paintDotGrid(g);
paintLineGrid(g);
if (lineGrid) {
paintLineGrid(g);
} else {
paintDotGrid(g);
}
} else {
paintNullGrid(g);
}

for (Tile tile : snapshot) {
if (tile != null) {
tile.setDrawOutline(drawGrid);
tile.setDrawOutline(drawGrid);
tile.setTrackRouteColor(null, null);

if (selectedTiles.contains(tile.getCenter())) {
//tile.setBackgroundColor(Color.yellow);
tile.setBackgroundColor(Color.orange);
} else {
tile.setBackgroundColor(Color.white);
}

if (routeSnapshot.containsKey(tile.getId())) {
RouteElementBean re = routeSnapshot.get(tile.getId());
tile.setTrackRouteColor(Color.black, re.getIncomingOrientation());

if (selectedTiles.contains(tile.getCenter())) {
//tile.setBackgroundColor(Color.yellow);
tile.setBackgroundColor(Color.orange);
if (tile.isJunction()) {
AccessoryValue av = re.getAccessoryValue();
((Switch) tile).setRouteValue(av, Color.darkGray);
Logger.trace("Tile: " + tile.getId() + " Value: " + av + "; " + re);
} else {
tile.setBackgroundColor(Color.white);
tile.setTrackColor(Color.darkGray);
}
} else {
tile.setTrackRouteColor(null, null);

if (routeSnapshot.containsKey(tile.getId())) {
//if (TileType.CROSS.equals(tile.getTileType()) || TileType.SWITCH.equals(tile.getTileType())) {
if (tile.isJunction()) {
RouteElementBean re = routeSnapshot.get(tile.getId());
AccessoryValue av = re.getAccessoryValue();
((Switch) tile).setRouteValue(av, Color.darkGray);
Logger.trace("Tile: " + tile.getId() + " Value: " + av + "; " + re);
} else {
tile.setTrackColor(Color.darkGray);
}
if (tile.isJunction()) {
((Switch) tile).setRouteValue(AccessoryValue.OFF, Tile.DEFAULT_TRACK_COLOR);
} else {
//if (TileType.CROSS.equals(tile.getTileType()) || TileType.SWITCH.equals(tile.getTileType())) {
if (tile.isJunction()) {
((Switch) tile).setRouteValue(AccessoryValue.OFF, Tile.DEFAULT_TRACK_COLOR);
} else {
tile.setTrackColor(Tile.DEFAULT_TRACK_COLOR);
}
tile.setTrackColor(Tile.DEFAULT_TRACK_COLOR);
}
}

tile.drawTile(g2, drawGrid);
tile.drawTile(g2, drawGrid);

//debug
if (!this.readonly) {
tile.drawCenterPoint(g2, Color.magenta, 3);
}
//debug
if (!this.readonly) {
tile.drawCenterPoint(g2, Color.magenta, 3);
}
//}
}

g2.dispose();
Expand Down Expand Up @@ -250,8 +256,8 @@ private void paintNullGrid(Graphics g) {

private void paintDotGrid(Graphics g) {
if (this.grid != null) {
int pw = this.getWidth(); // getSize().width;
int ph = this.getHeight(); // getSize().height;
int pw = this.getWidth();
int ph = this.getHeight();

int gw = grid.getWidth();
int gh = grid.getHeight();
Expand Down Expand Up @@ -345,6 +351,7 @@ LayoutCanvas.Mode getMode() {

void setDrawGrid(boolean flag) {
this.drawGrid = flag;
this.repaint();
}

void setTileType(TileBean.TileType tileType) {
Expand Down Expand Up @@ -380,6 +387,7 @@ public void loadTiles() {
selectedTiles.clear();
altTiles.clear();
tiles.clear();
selectedRouteElements.clear();

for (TileBean tb : tileBeans) {
Tile tile = TileFactory.createTile(tb, drawGrid, showValues);
Expand Down Expand Up @@ -412,17 +420,13 @@ public void loadTiles() {
}

public void saveLayout() {
this.executor.execute(() -> saveTiles());
Set<Tile> snapshot = new HashSet<>(tiles.values());
this.selectedTiles.clear();
this.executor.execute(() -> saveTiles(snapshot));
}

private void saveTiles() {
Logger.debug("Saving " + this.tiles.size() + " tiles...");

Set<Tile> snapshot;
synchronized (tiles) {
snapshot = new HashSet<>(tiles.values());
}

private void saveTiles(Set<Tile> snapshot) {
Logger.debug("Saving " + snapshot.size() + " tiles...");
List<TileBean> beans = new LinkedList<>();

for (Tile tile : snapshot) {
Expand All @@ -435,8 +439,6 @@ private void saveTiles() {
}
}
PersistenceFactory.getService().persist(beans);

this.selectedTiles.clear();
}

/**
Expand Down Expand Up @@ -1112,18 +1114,23 @@ public void flipSelectedTileVertical() {
}

void routeLayout() {
routeLayoutWithAStar();
//this.executor.execute(() -> routeLayoutWithAStar());
//routeLayoutWithAStar();
this.executor.execute(() -> routeLayoutWithAStar());

}

private void routeLayoutWithAStar() {
//Make sure the layout is saved
this.saveTiles();
Set<Tile> snapshot = new HashSet<>(tiles.values());
this.saveTiles(snapshot);

AStar astar = new AStar();
astar.buildGraph(this.tiles.values().stream().collect(Collectors.toList()));
astar.routeAll();
astar.persistRoutes();
if (this.routesDialog.isVisible()) {
this.routesDialog.loadRoutes();
}
}

void showRoutesDialog() {
Expand All @@ -1136,6 +1143,8 @@ void setSelectRoute(RouteBean route) {
List<RouteElementBean> rel = route.getRouteElements();
for (RouteElementBean re : rel) {
String id = re.getTileId();
Orientation incomingSide = re.getIncomingOrientation();

String nodeId = re.getNodeId();

//if (id.startsWith("sw-") || id.startsWith("cs-")) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/jcs/ui/layout/LayoutPanel.form
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,9 @@
<Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[38, 38]"/>
</Property>
<Property name="selectedIcon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/media/new-crossing_Y.png"/>
</Property>
<Property name="verticalTextPosition" type="int" value="3"/>
</Properties>
<Events>
Expand Down
Loading

0 comments on commit efd3d27

Please sign in to comment.