Skip to content

Commit 26c33fb

Browse files
author
Daniel
committed
lots of bugfixes
1 parent 34b02c9 commit 26c33fb

22 files changed

Lines changed: 181 additions & 205 deletions

src/main/java/com/daniel/jsoneditor/controller/impl/commands/CommandManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ interface UnsavedChangesCallback
6464
void onUnsavedChangesCountChanged(int unsavedChangesCount);
6565
}
6666
}
67+

src/main/java/com/daniel/jsoneditor/controller/impl/commands/CommandManagerImpl.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public List<ModelChange> executeCommand(final Command cmd)
7979
// notify UI regardless of undoability when something happened
8080
if (!changes.isEmpty())
8181
{
82-
model.sendEvent(new Event(EventEnum.COMMAND_APPLIED, cmd.getLabel(), cmd.getCategory(), "EXECUTE", changes));
82+
model.sendEvent(new Event(EventEnum.COMMAND_APPLIED, cmd, "EXECUTE", changes));
8383
}
8484
return changes;
8585
}
@@ -98,8 +98,7 @@ public List<ModelChange> undo()
9898
notifyUnsavedChangesCount(); // Notify about changes count update
9999
if (!inverted.isEmpty())
100100
{
101-
model.sendEvent(new Event(EventEnum.COMMAND_APPLIED, entry.getCommand().getLabel(), entry.getCommand().getCategory(), "UNDO",
102-
inverted));
101+
model.sendEvent(new Event(EventEnum.COMMAND_APPLIED, entry.getCommand(), "UNDO", inverted));
103102
}
104103
return inverted; // return inverse (REMOVE for previous ADD)
105104
}
@@ -117,8 +116,7 @@ public List<ModelChange> redo()
117116
notifyUnsavedChangesCount(); // Notify about changes count update
118117
if (!entry.getChanges().isEmpty())
119118
{
120-
model.sendEvent(new Event(EventEnum.COMMAND_APPLIED, entry.getCommand().getLabel(), entry.getCommand().getCategory(), "REDO",
121-
entry.getChanges()));
119+
model.sendEvent(new Event(EventEnum.COMMAND_APPLIED, entry.getCommand(), "REDO", entry.getChanges()));
122120
}
123121
return entry.getChanges();
124122
}

src/main/java/com/daniel/jsoneditor/model/WritableModel.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,4 @@ public interface WritableModel extends ReadableModel
6262
* sets a path to a node, replacing everything that was there before
6363
*/
6464
void setNode(String path, JsonNode content);
65-
66-
/**
67-
* adds a new node to the JSON for the
68-
*/
69-
void addReferenceableObjectNodeWithKey(String pathOfReferenceableObject, String key);
7065
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.daniel.jsoneditor.model.commands;
2+
3+
/**
4+
* Marker interface for commands that create referenceable objects.
5+
* Used by the UI layer to detect when special handling is needed.
6+
*/
7+
public interface ReferenceableObjectCommand extends Command
8+
{
9+
/**
10+
* @return the path of the newly created referenceable object after execution
11+
*/
12+
String getCreatedObjectPath();
13+
}

src/main/java/com/daniel/jsoneditor/model/commands/impl/CreateReferenceableObjectCommand.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import com.daniel.jsoneditor.model.WritableModelInternal;
44
import com.daniel.jsoneditor.model.changes.ModelChange;
5+
import com.daniel.jsoneditor.model.commands.ReferenceableObjectCommand;
56
import com.daniel.jsoneditor.model.json.schema.reference.ReferenceHelper;
67
import com.daniel.jsoneditor.model.json.schema.reference.ReferenceableObject;
78
import com.fasterxml.jackson.databind.JsonNode;
89
import java.util.List;
910

10-
public class CreateReferenceableObjectCommand extends BaseCommand
11+
public class CreateReferenceableObjectCommand extends BaseCommand implements ReferenceableObjectCommand
1112
{
1213
private final String referenceableObjectPath;
1314
private final String key;
15+
private String createdObjectPath;
1416

1517
public CreateReferenceableObjectCommand(final WritableModelInternal model, final String referenceableObjectPath, final String key)
1618
{
@@ -33,7 +35,6 @@ public List<ModelChange> execute()
3335
{
3436
return noChanges();
3537
}
36-
// ensure path points to an array container
3738
String path = refObj.getPath();
3839
JsonNode newItem = model.makeArrayNode(path);
3940
int addedIndex = model.addNodeToArray(path, newItem);
@@ -42,9 +43,15 @@ public List<ModelChange> execute()
4243
return noChanges();
4344
}
4445
String newNodePath = path + "/" + addedIndex;
45-
// set key of instance
46+
this.createdObjectPath = newNodePath;
4647
ReferenceHelper.setKeyOfInstance(model, refObj, newNodePath, key);
48+
4749
return List.of(ModelChange.add(newNodePath, model.getNodeForPath(newNodePath).getNode()));
4850
}
51+
52+
@Override
53+
public String getCreatedObjectPath()
54+
{
55+
return createdObjectPath;
56+
}
4957
}
50-

src/main/java/com/daniel/jsoneditor/model/commands/impl/DuplicateReferenceAndLinkCommand.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.daniel.jsoneditor.model.WritableModelInternal;
77
import com.daniel.jsoneditor.model.changes.ModelChange;
8+
import com.daniel.jsoneditor.model.commands.ReferenceableObjectCommand;
89
import com.daniel.jsoneditor.model.json.JsonNodeWithPath;
910
import com.daniel.jsoneditor.model.json.schema.paths.PathHelper;
1011
import com.fasterxml.jackson.databind.JsonNode;
@@ -13,10 +14,11 @@
1314
* Duplicates an object at pathToItemToDuplicate and updates the referencing node at referencePath to point to the new key.
1415
* Undo removes the duplicated object and restores the referencing node snapshot (via REPLACE inversion).
1516
*/
16-
public class DuplicateReferenceAndLinkCommand extends BaseCommand
17+
public class DuplicateReferenceAndLinkCommand extends BaseCommand implements ReferenceableObjectCommand
1718
{
1819
private final String referencePath;
1920
private final String pathToItemToDuplicate;
21+
private String createdObjectPath;
2022

2123
public DuplicateReferenceAndLinkCommand(final WritableModelInternal model, final String referencePath, final String pathToItemToDuplicate)
2224
{
@@ -40,11 +42,10 @@ public List<ModelChange> execute()
4042
{
4143
return noChanges();
4244
}
43-
// compute new item path (duplicate inserted after original index)
4445
final String parentPath = PathHelper.getParentPath(pathToItemToDuplicate);
4546
final int originalIndex = Integer.parseInt(PathHelper.getLastPathSegment(pathToItemToDuplicate));
4647
final String newItemPath = parentPath + "/" + (originalIndex + 1);
47-
// perform duplication and linking using existing model method
48+
this.createdObjectPath = newItemPath;
4849
model.duplicateNodeAndLink(referencePath, pathToItemToDuplicate);
4950
JsonNode newItem = model.getNodeForPath(newItemPath).getNode();
5051
if (newItem == null || newItem.isMissingNode())
@@ -56,9 +57,15 @@ public List<ModelChange> execute()
5657
out.add(ModelChange.add(newItemPath, newItem));
5758
if (!oldRefSnapshot.equals(newRefSnapshot))
5859
{
59-
out.add(ModelChange.replace(referencePath, (JsonNode) oldRefSnapshot, (JsonNode) newRefSnapshot));
60+
out.add(ModelChange.replace(referencePath, oldRefSnapshot, newRefSnapshot));
6061
}
62+
6163
return out;
6264
}
65+
66+
@Override
67+
public String getCreatedObjectPath()
68+
{
69+
return createdObjectPath;
70+
}
6371
}
64-

src/main/java/com/daniel/jsoneditor/model/impl/ModelImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,6 @@ public ReferenceableObject getReferenceableObjectByReferencingKey(String referen
658658
return ReferenceHelper.getReferenceableObject(this, referencingKey);
659659
}
660660

661-
@Override
662-
public void addReferenceableObjectNodeWithKey(String pathOfReferenceableObject, String key)
663-
{
664-
ReferenceHelper.createAndInsertReferenceableObject(this, this, pathOfReferenceableObject, key);
665-
// TODO switch state
666-
}
667-
668661
@Override
669662
public void setNode(String path, JsonNode content)
670663
{

src/main/java/com/daniel/jsoneditor/model/json/schema/SchemaHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public static boolean validateJsonWithSchema(JsonNode json, JsonSchema schema)
3333
for (ValidationMessage message : messages)
3434
{
3535
final JsonNode elementContent = json.at(convertToJSONPointer(message.getPath()));
36-
final String contentPreview = elementContent.toString().length() > 100
37-
? elementContent.toString().substring(0, 100) + "..."
36+
final String contentPreview = elementContent.toString().length() > 100
37+
? elementContent.toString().substring(0, 100) + "..."
3838
: elementContent.toString();
39-
logger.error("Validation Error: {} at path {} with element preview: {}",
39+
logger.error("Validation Error: {} at path {} with element preview: {}",
4040
message.getMessage(), message.getPath(), contentPreview);
4141
}
4242
return messages.isEmpty();

src/main/java/com/daniel/jsoneditor/model/json/schema/reference/ReferenceHelper.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import java.util.List;
66

77
import com.daniel.jsoneditor.model.ReadableModel;
8-
import com.daniel.jsoneditor.model.WritableModel;
9-
import com.daniel.jsoneditor.model.WritableModelInternal;
108
import com.daniel.jsoneditor.model.json.JsonNodeWithPath;
119
import com.daniel.jsoneditor.model.json.schema.SchemaHelper;
1210
import com.daniel.jsoneditor.model.json.schema.paths.PathHelper;
13-
import com.daniel.jsoneditor.model.statemachine.impl.Event;
14-
import com.daniel.jsoneditor.model.statemachine.impl.EventEnum;
1511
import com.fasterxml.jackson.databind.JsonNode;
1612
import com.fasterxml.jackson.databind.node.IntNode;
1713
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -63,7 +59,7 @@ public static String resolveReference(JsonNodeWithPath node, ReadableModel model
6359
}
6460
index++;
6561
}
66-
return objectNode.getPath() + "/" + 0;
62+
return null;
6763
}
6864
else
6965
{
@@ -119,35 +115,6 @@ public static List<ReferenceableObjectInstance> getReferenceableObjectInstances(
119115
}
120116
}
121117

122-
public static void createAndInsertReferenceableObject(ReadableModel readableModel, WritableModelInternal model, String referenceableObjectPath, String newKey)
123-
{
124-
// Get the ReferenceableObject using the referenceableObjectPath
125-
ReferenceableObject referenceableObject = getReferenceableObjectOfPath(readableModel, referenceableObjectPath);
126-
if (referenceableObject == null)
127-
{
128-
throw new IllegalArgumentException("ReferenceableObject not found for the given path: " + referenceableObjectPath);
129-
}
130-
131-
// Check if the ReferenceableObject is an array
132-
JsonNodeWithPath referenceableObjectNode = readableModel.getNodeForPath(referenceableObject.getPath());
133-
if (referenceableObjectNode.isArray())
134-
{
135-
String path = referenceableObject.getPath();
136-
JsonNode newItem = readableModel.makeArrayNode(path);
137-
int addedIndex = model.addNodeToArray(path, newItem);
138-
String newNodePath = referenceableObject.getPath() + "/" + addedIndex;
139-
140-
// Set the key of the new node
141-
setKeyOfInstance(readableModel, referenceableObject, newNodePath, newKey);
142-
model.sendEvent(new Event(EventEnum.ADDED_REFERENCEABLE_OBJECT, newNodePath));
143-
}
144-
else
145-
{
146-
// good point for future development
147-
throw new IllegalArgumentException("ReferenceableObject is not an array: " + referenceableObjectPath);
148-
}
149-
}
150-
151118
/**
152119
* returns the referenceable object that is at this path
153120
*/

src/main/java/com/daniel/jsoneditor/model/statemachine/impl/Event.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.daniel.jsoneditor.model.statemachine.impl;
22

33
import com.daniel.jsoneditor.model.changes.ModelChange;
4+
import com.daniel.jsoneditor.model.commands.Command;
45

56
import java.util.List;
67

@@ -20,6 +21,8 @@ public class Event
2021

2122
private final List<ModelChange> changes;
2223

24+
private final Command command; // actual command object for type-safe detection
25+
2326
public Event(EventEnum eventEnum)
2427
{
2528
this.eventEnum = eventEnum;
@@ -28,6 +31,7 @@ public Event(EventEnum eventEnum)
2831
this.commandCategory = null;
2932
this.commandPhase = null;
3033
this.changes = null;
34+
this.command = null;
3135
}
3236

3337
public Event(EventEnum eventEnum, String path)
@@ -38,6 +42,7 @@ public Event(EventEnum eventEnum, String path)
3842
this.commandCategory = null;
3943
this.commandPhase = null;
4044
this.changes = null;
45+
this.command = null;
4146
}
4247

4348
/**
@@ -51,6 +56,21 @@ public Event(EventEnum eventEnum, String commandLabel, String commandCategory, S
5156
this.commandCategory = commandCategory;
5257
this.commandPhase = commandPhase;
5358
this.changes = changes;
59+
this.command = null;
60+
}
61+
62+
/**
63+
* Enhanced constructor for COMMAND_APPLIED events with actual command object.
64+
*/
65+
public Event(EventEnum eventEnum, Command command, String commandPhase, List<ModelChange> changes)
66+
{
67+
this.eventEnum = eventEnum;
68+
this.path = null;
69+
this.commandLabel = command.getLabel();
70+
this.commandCategory = command.getCategory();
71+
this.commandPhase = commandPhase;
72+
this.changes = changes;
73+
this.command = command;
5474
}
5575

5676
public EventEnum getEvent()
@@ -82,4 +102,9 @@ public List<ModelChange> getChanges()
82102
{
83103
return changes;
84104
}
105+
106+
public Command getCommand()
107+
{
108+
return command;
109+
}
85110
}

0 commit comments

Comments
 (0)