Skip to content

Commit

Permalink
Added save and load nested Nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Dec 18, 2023
1 parent 69e8d53 commit bc5b821
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/marginallyclever/ro3/actions/LoadScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import com.marginallyclever.ro3.Registry;
import com.marginallyclever.ro3.node.Node;
import com.marginallyclever.robotoverlord.RobotOverlord;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class LoadScene extends AbstractAction {
private static final JFileChooser chooser = new JFileChooser();
Expand Down Expand Up @@ -39,14 +43,12 @@ public void actionPerformed(ActionEvent e) {

private void loadAsNewScene(File selectedFile) {
logger.info("Load scene from {}",selectedFile.getAbsolutePath());
logger.error("Load Scene not implemented yet.");

// Create an ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();

try {
// Read the JSON file and convert it into a Node object
Node loaded = mapper.readValue(selectedFile, Node.class);
String content = new String(Files.readAllBytes(Paths.get(selectedFile.getAbsolutePath())));
JSONObject json = new JSONObject(content);
Node loaded = new Node("Scene");
loaded.fromJSON(json);
Registry.setScene(loaded);
} catch (IOException e) {
logger.error("Error loading scene from JSON", e);
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/marginallyclever/ro3/actions/SaveScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class SaveScene extends AbstractAction {
Expand Down Expand Up @@ -53,12 +55,8 @@ public void actionPerformed(ActionEvent e) {
private void saveScene(String absolutePath) {
logger.info("Save scene to {}",absolutePath);

// Create an ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();

try {
// Write the Registry.scene to a JSON file
mapper.writeValue(new File(absolutePath), Registry.getScene());
try (BufferedWriter writer = new BufferedWriter(new FileWriter(absolutePath))) {
writer.write(Registry.getScene().toJSON().toString());
} catch (IOException e) {
logger.error("Error saving scene to JSON", e);
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/marginallyclever/ro3/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.marginallyclever.ro3.Registry;
import com.marginallyclever.ro3.node.nodes.Pose;
import com.marginallyclever.robotoverlord.swing.CollapsiblePanel;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -44,11 +45,11 @@ public void addChild(Node child) {
if(child.children.isEmpty()) {
fireReadyEvent(child);
child.onReady();
} else {
}/* else {
for(Node grandchild : child.children) {
child.addChild(grandchild);
}
}
}*/
}

private void fireReadyEvent(Node child) {
Expand Down Expand Up @@ -357,12 +358,16 @@ public <T extends Node> T findFirstSibling(Class<T> type) {
* @return the JSON object.
*/
public JSONObject toJSON() {
logger.info("Saving node {}.",getAbsolutePath());
logger.info("Saving {}.",getAbsolutePath());
JSONObject json = new JSONObject();
json.put("type",getClass().getSimpleName());
json.put("name",name);
json.put("nodeID",nodeID.toString());
json.put("children",children);
JSONArray childrenArray = new JSONArray();
for (Node child : this.children) {
childrenArray.put(child.toJSON());
}
json.put("children",childrenArray);
return json;
}

Expand All @@ -374,7 +379,6 @@ public JSONObject toJSON() {
*/
public void fromJSON(JSONObject from) {
name = from.getString("name");
logger.info("Loading node {}.",name);
nodeID = UUID.fromString(from.getString("nodeID"));
children.clear();
for (Object o : from.getJSONArray("children")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.security.InvalidParameterException;
import java.util.*;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -97,15 +98,20 @@ private void stopListeningTo(Node node) {

/**
* Scan the tree for existing nodes.
* @param parent the node to scan
* @param toScan the node to scan
*/
public void scanTree(Node parent) {
if(parent == null) return;
NodeTreeBranch parentBranch = findTreeNode(parent);
if(parentBranch == null) return;
public void scanTree(Node toScan) {
if(toScan == null) throw new InvalidParameterException("node is null");
logger.info("Scanning "+toScan.getAbsolutePath());

NodeTreeBranch parentBranch = findTreeNode(toScan);
if(parentBranch == null) {
logger.error("node has no branch");
return;
}

for (Node child : parent.getChildren()) {
logger.debug("scanTree "+parent.getAbsolutePath()+" has child "+child.getAbsolutePath());
for (Node child : toScan.getChildren()) {
logger.debug("node has child "+child.getAbsolutePath());
nodeAttached(child);
}
}
Expand Down Expand Up @@ -249,7 +255,7 @@ public void beforeSceneChange(Node oldScene) {
public void afterSceneChange(Node newScene) {
logger.debug("afterSceneChange");
listenTo(newScene);
treeModel.removeAllChildren();
treeModel.setUserObject(newScene);
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(treeModel.getRoot());
scanTree(newScene);
}
Expand Down

0 comments on commit bc5b821

Please sign in to comment.