Skip to content

Commit

Permalink
Merge pull request #82 from fransjacobs/76-persistence-error-when-mov…
Browse files Browse the repository at this point in the history
…ing-tiles

Fix the saving issue by storing all tile changes immediately
  • Loading branch information
fransjacobs authored Aug 21, 2024
2 parents 2695b70 + d52fa15 commit 0627e2c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 45 deletions.
66 changes: 49 additions & 17 deletions src/main/java/jcs/ui/layout/LayoutCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,7 @@ public void loadTiles() {

for (TileBean tb : tileBeans) {
Tile tile = TileFactory.createTile(tb, drawGrid, showValues);

tile.setPropertyChangeListener(this);

//TODO move this to tile factory?:
switch (tile.getTileType()) {
case SENSOR ->
JCS.getJcsCommandStation().addSensorEventListener((SensorEventListener) tile);
case SWITCH ->
JCS.getJcsCommandStation().addAccessoryEventListener((AccessoryEventListener) tile);
case SIGNAL ->
JCS.getJcsCommandStation().addAccessoryEventListener((AccessoryEventListener) tile);
default -> {
//Do nothing
}
}
tiles.put(tile.getCenter(), tile);

//Alternative point(s) to be able to find all points
Expand Down Expand Up @@ -399,6 +385,24 @@ private synchronized void saveTiles(Set<Tile> snapshot) {
PersistenceFactory.getService().persist(beans);
}

private void saveTile(final Tile tile) {
if (tile != null) {
TileBean tb = tile.getTileBean();
this.executor.execute(() -> PersistenceFactory.getService().persist(tb));
} else {
Logger.warn("Tile is null?");
}
}

private void deleteTile(final Tile tile) {
if (tile != null) {
TileBean tb = tile.getTileBean();
this.executor.execute(() -> PersistenceFactory.getService().remove(tb));
} else {
Logger.warn("Tile is null?");
}
}

private void mouseMoveAction(MouseEvent evt) {
Point sp = LayoutUtil.snapToGrid(evt.getPoint());
Tile tile = findTile(sp);
Expand Down Expand Up @@ -449,9 +453,13 @@ private void mousePressedAction(MouseEvent evt) {
case ADD -> {
if (MouseEvent.BUTTON1 == evt.getButton() && tile == null) {
//Only add a new tile when there is no tile on the selected snapPoint
Logger.debug("Adding a new tile: " + tileType + " @ (" + snapPoint.x + ", " + snapPoint.y + ")");
Logger.trace("Adding a new tile: " + tileType + " @ (" + snapPoint.x + ", " + snapPoint.y + ")");
Tile addedTile = addTile(snapPoint);
selectedTiles.addAll(addedTile.getAllPoints());

if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
this.saveTile(tile);
}
} else {
if (tile != null) {
Logger.debug("A tile exists at the selected position: " + tile.getTileType() + " @ (" + snapPoint.x + ", " + snapPoint.y + ") id: " + tile.getId());
Expand Down Expand Up @@ -567,6 +575,10 @@ private void mouseReleasedAction(MouseEvent evt) {
altTiles.put(ep, movingTile);
}
}

if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
this.saveTile(movingTile);
}
}
}
}
Expand Down Expand Up @@ -879,6 +891,11 @@ private Tile addTile(Point p) {
this.altTiles.put(ap, tile);
}
}

if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
this.saveTile(tile);
}

Logger.trace("Added Tile " + tile.getClass().getSimpleName() + " " + tile.getOrientation() + " @ " + tile.getCenter() + " Full repaint: " + fullRepaint);
Logger.trace("Added " + tile + " There are now " + this.tiles.size() + " tiles...");

Expand All @@ -896,6 +913,10 @@ private void removeTiles(Set<Point> pointsToRemove) {
for (Point p : pointsToRemove) {
Tile removed = this.tiles.remove(p);

if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
this.deleteTile(removed);
}

if (removed != null && removed.getAllPoints() != null) {
Set<Point> rps = removed.getAltPoints();
//Also remove alt points
Expand Down Expand Up @@ -942,9 +963,12 @@ public void rotateSelectedTile() {

this.selectedTiles.clear();
this.selectedTiles.addAll(t.getAllPoints());

if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
this.saveTile(t);
}
}
}
//this.executor.execute(() -> repaint());
repaint();
}

Expand All @@ -970,6 +994,10 @@ public void flipSelectedTileHorizontal() {
this.altTiles.put(ep, t);
}

if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
this.saveTile(t);
}

this.selectedTiles.clear();
this.selectedTiles.addAll(t.getAllPoints());
}
Expand Down Expand Up @@ -999,6 +1027,10 @@ public void flipSelectedTileVertical() {
this.altTiles.put(ep, t);
}

if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
this.saveTile(t);
}

this.selectedTiles.clear();
this.selectedTiles.addAll(t.getAllPoints());
}
Expand Down Expand Up @@ -1351,6 +1383,7 @@ private void removeLocMIActionPerformed(ActionEvent evt) {//GEN-FIRST:event_remo
if (this.selectedTile != null) {
Block block = (Block) selectedTile;
block.getBlockBean().setLocomotive(null);
block.setBlockState(BlockState.FREE);

this.executor.execute(() -> PersistenceFactory.getService().persist(block.getBlockBean()));
this.repaint();
Expand Down Expand Up @@ -1398,7 +1431,6 @@ private void toggleLocomotiveDirectionMIActionPerformed(ActionEvent evt) {//GEN-
}
}//GEN-LAST:event_toggleLocomotiveDirectionMIActionPerformed


private void toggleOutOfOrderMIActionPerformed(ActionEvent evt) {//GEN-FIRST:event_toggleOutOfOrderMIActionPerformed
if (this.selectedTile != null) {
Block block = (Block) selectedTile;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/jcs/ui/layout/LayoutPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import jcs.entities.TileBean;
import jcs.entities.TileBean.Direction;
import jcs.entities.TileBean.TileType;
import jcs.util.RunUtil;
import org.tinylog.Logger;

/**
Expand All @@ -61,6 +62,8 @@ public LayoutPanel(boolean readonly) {
}

private void postInit() {
RunUtil.loadProperties();

this.straightBtn.setSelected(true);
this.canvas.setTileType(TileType.STRAIGHT);
this.setMode(readonly ? LayoutCanvas.Mode.CONTROL : LayoutCanvas.Mode.SELECT);
Expand Down Expand Up @@ -152,6 +155,14 @@ private void postInit() {
this.startAllLocomotivesBtn.setEnabled(readonly && this.autoPilotBtn.isSelected());
this.startAllLocomotivesBtn.setVisible(readonly);
} else {
if ("true".equals(System.getProperty("batch.tile.persist", "true"))) {
this.saveBtn.setEnabled(true);
this.saveBtn.setVisible(true);
} else {
this.saveBtn.setEnabled(false);
this.saveBtn.setVisible(false);
this.toolBar.remove(this.saveBtn);
}

this.toolBar.remove(this.autoPilotBtn);
this.autoPilotBtn.setEnabled(readonly);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/jcs/ui/layout/tiles/TileFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import jcs.JCS;
import jcs.commandStation.events.AccessoryEventListener;
import jcs.commandStation.events.SensorEventListener;
import jcs.entities.AccessoryBean;
import jcs.entities.SensorBean;
import jcs.entities.TileBean;
Expand Down Expand Up @@ -150,6 +153,7 @@ public static Tile createTile(TileBean tileBean, boolean drawOutline, boolean sh
if (showValues && tileBean.getAccessoryBean() != null) {
((Switch) tile).setValue((tileBean.getAccessoryBean()).getAccessoryValue());
}
JCS.getJcsCommandStation().addAccessoryEventListener((AccessoryEventListener) tile);
}
case CROSS -> {
tile = new Cross(tileBean);
Expand All @@ -161,13 +165,15 @@ public static Tile createTile(TileBean tileBean, boolean drawOutline, boolean sh
if (showValues && tileBean.getAccessoryBean() != null) {
((Signal) tile).setSignalValue(((AccessoryBean) tileBean.getAccessoryBean()).getSignalValue());
}
JCS.getJcsCommandStation().addAccessoryEventListener((AccessoryEventListener) tile);
}
case SENSOR -> {
tile = new Sensor(tileBean);
sensorIdSeq = maxIdSeq(sensorIdSeq, nextIdSeq(tileBean.getId()));
if (showValues && tileBean.getSensorBean() != null) {
((Sensor) tile).setActive(((SensorBean) tileBean.getSensorBean()).isActive());
}
JCS.getJcsCommandStation().addSensorEventListener((SensorEventListener) tile);
}
case BLOCK -> {
tile = new Block(tileBean);
Expand All @@ -190,6 +196,7 @@ public static Tile createTile(TileBean tileBean, boolean drawOutline, boolean sh
}

addTileEventListener((TileEventListener) tile);

return (Tile) tile;
}

Expand Down
59 changes: 32 additions & 27 deletions src/main/java/jcs/util/RunUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,55 @@
import org.tinylog.Logger;

public class RunUtil {

public static final String DEFAULT_PATH = "~/jcs/";

private static boolean propertiesLoaded = false;

public static boolean isMacOSX() {
return System.getProperty("os.name").contains("Mac OS X");
}

public static boolean isLinux() {
String os = System.getProperty("os.name").toLowerCase();
return os.contains("nix") || os.contains("nux");
}

public static boolean isWindows() {
String os = System.getProperty("os.name").toLowerCase();
return os.contains("windows");
}

public static void loadProperties() {
Properties prop = new Properties();
InputStream inputStream = RunUtil.class.getClassLoader().getResourceAsStream("jcs.properties");
if (inputStream != null) {
try {
prop.load(inputStream);
} catch (IOException ex) {
Logger.error("Can't read jcs.properties");
if (!propertiesLoaded) {
Properties prop = new Properties();
InputStream inputStream = RunUtil.class.getClassLoader().getResourceAsStream("jcs.properties");
if (inputStream != null) {
try {
prop.load(inputStream);
} catch (IOException ex) {
Logger.error("Can't read jcs.properties");
}
}
}

for (Object pk : prop.keySet()) {
String key = (String) pk;
String value = (String) prop.get(pk);
if (System.getProperty(key) == null) {
System.setProperty(key, value);

for (Object pk : prop.keySet()) {
String key = (String) pk;
String value = (String) prop.get(pk);
if (System.getProperty(key) == null) {
System.setProperty(key, value);
}
}
propertiesLoaded = true;
Logger.trace("JCS Properties loaded.");
}
}

public static void loadExternalProperties() {
Properties prop = new Properties();
String p = DEFAULT_PATH + "jcs.properties";
p = p.replace("~", System.getProperty("user.home"));

File ep = new File(p);
if (ep.exists()) {
if (ep.exists()) {
try {
prop.load(new FileInputStream(p));
for (Object pk : prop.keySet()) {
Expand All @@ -86,12 +91,12 @@ public static void loadExternalProperties() {
Logger.trace("Optional external properties not available.");
}
}

public static void writeProperty(String filePath, String key, String value) {
Properties properties = new Properties();
String p = filePath;
p = p.replace("~", System.getProperty("user.home"));

File f = new File(p);
if (!f.exists()) {
try {
Expand All @@ -100,15 +105,15 @@ public static void writeProperty(String filePath, String key, String value) {
Logger.trace("Can't create new file: " + p);
}
}

try (OutputStream outputStream = new FileOutputStream(p)) {
properties.setProperty(key, value);
properties.store(outputStream, null);
} catch (IOException e) {
Logger.warn("Can't write property " + key + ", " + value + " to " + p);
}
}

public static String readProperty(String filePath, String key) {
String p = filePath;
p = p.replace("~", System.getProperty("user.home"));
Expand All @@ -122,5 +127,5 @@ public static String readProperty(String filePath, String key) {
}
return null;
}

}
3 changes: 2 additions & 1 deletion src/main/resources/jcs.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dispatcher=jcs.controller.DispatcherImpl
persistenceService=jcs.persistence.H2PersistenceService
jcs.plaf=com.formdev.flatlaf.FlatLightLaf
controller.cs.max.errors=15
controller.autoconnect=true
controller.autoconnect=true
batch.tile.persist=false

0 comments on commit 0627e2c

Please sign in to comment.