Skip to content

Commit 6784419

Browse files
committed
Add support for DefKey and Keys to GraphCollection
1 parent fbca35f commit 6784419

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

StructuredXmlEditor.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ Global
4343
GlobalSection(Performance) = preSolution
4444
HasPerformanceSessions = true
4545
EndGlobalSection
46+
GlobalSection(Performance) = preSolution
47+
HasPerformanceSessions = true
48+
EndGlobalSection
4649
EndGlobal

StructuredXmlEditor/Core.xmldef

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@
327327
<Boolean Name="AllowCircularLinks" Default="false" ToolTip="Whether circular links are allowed." VisibleIf="AllowReferenceLinks==true" />
328328
<Boolean Name="FlattenData" Default="false" ToolTip="If true each graph node will be placed in a flat list within this element." VisibleIf="AllowReferenceLinks==true" />
329329
<String Name="NodeStoreName" Default="Nodes" ToolTip="The name of the node added to this to store all the graph nodes." VisibleIf="FlattenData==true" />
330+
<String Name="Keys" ToolTip="The names of the objects to use in this collection." />
331+
<String Name="DefKey" ToolTip="The name of the ReferenceDef to use in this collection." />
330332
<Number Name="MinCount" Min="0" ToolTip="The minimum number of child elements this can have." />
331333
<Number Name="MaxCount" Min="1" Default="99999999" ToolTip="The maximum number of child elements this can have." />
332334
<Colour Name="Background" Default="0,0,0,0" />
@@ -346,6 +348,8 @@
346348
<Attributes>
347349
<String Name="Name" Default="GraphCollection" ToolTip="The name to be written for this element. Place a path to an element inside a '{}' to use that elements value. This will be used as the key when referencing it." />
348350
<Boolean Name="ChildrenAreUnique" ToolTip="If set then each child can only be added once" />
351+
<String Name="Keys" ToolTip="The names of the objects to use in this collection." />
352+
<String Name="DefKey" ToolTip="The name of the ReferenceDef to use in this collection." />
349353
<Number Name="MinCount" Min="0" ToolTip="The minimum number of child elements this can have." />
350354
<Number Name="MaxCount" Min="1" Default="99999999" ToolTip="The maximum number of child elements this can have." />
351355
<Colour Name="Background" Default="0,0,0,0" />

StructuredXmlEditor/Definition/GraphCollectionDefinition.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using StructuredXmlEditor.Data;
2+
using StructuredXmlEditor.View;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -17,6 +18,8 @@ public class GraphCollectionDefinition : GraphNodeDefinition
1718
public List<CollectionChildDefinition> ChildDefinitions { get; } = new List<CollectionChildDefinition>();
1819
public int MinCount { get; set; } = 0;
1920
public int MaxCount { get; set; } = int.MaxValue;
21+
public List<Tuple<string, string>> DefKeys { get; set; } = new List<Tuple<string, string>>();
22+
public string DefKey { get; set; }
2023

2124
public GraphCollectionDefinition()
2225
{
@@ -129,6 +132,27 @@ public override void Parse(XElement definition)
129132

130133
var currentGroup = "Items";
131134

135+
DefKey = definition.Attribute("DefKey")?.Value?.ToString();
136+
var keyString = definition.Attribute("Keys")?.Value?.ToString();
137+
if (!string.IsNullOrWhiteSpace(keyString))
138+
{
139+
if (!keyString.Contains('('))
140+
{
141+
DefKeys.AddRange(keyString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => new Tuple<string, string>(e.Trim(), "Type")));
142+
}
143+
else
144+
{
145+
var categories = keyString.Split(new char[] { ')' }, StringSplitOptions.RemoveEmptyEntries);
146+
foreach (var categoryString in categories)
147+
{
148+
var split = categoryString.Split('(');
149+
var category = split[0].Trim();
150+
if (category.StartsWith(",")) category = category.Substring(1);
151+
DefKeys.AddRange(split[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => new Tuple<string, string>(e.Trim(), category)));
152+
}
153+
}
154+
}
155+
132156
var childDefs = definition.Nodes();
133157
foreach (var childDef in childDefs)
134158
{
@@ -152,7 +176,7 @@ public override void Parse(XElement definition)
152176
}
153177
}
154178

155-
if (ChildDefinitions.Count == 0)
179+
if (ChildDefinitions.Count == 0 && DefKey == null && DefKeys.Count == 0)
156180
{
157181
throw new Exception("No child definitions in collection '" + Name + "'!");
158182
}
@@ -224,6 +248,55 @@ public override void DoSaveData(XElement parent, DataItem item)
224248

225249
public override void RecursivelyResolve(Dictionary<string, DataDefinition> local, Dictionary<string, DataDefinition> global, Dictionary<string, Dictionary<string, DataDefinition>> referenceableDefinitions)
226250
{
251+
if (DefKey != null)
252+
{
253+
var key = DefKey.ToLower();
254+
255+
Dictionary<string, DataDefinition> defs = null;
256+
if (local.ContainsKey(key)) defs = local;
257+
else if (global.ContainsKey(key)) defs = global;
258+
259+
if (defs != null)
260+
{
261+
var def = defs[key] as ReferenceDefinition;
262+
263+
foreach (var keydef in def.Keys)
264+
{
265+
var childDef = def.Definitions[keydef.Item1];
266+
var childWrapperDef = new CollectionChildDefinition();
267+
childWrapperDef.WrappedDefinition = childDef;
268+
269+
ChildDefinitions.Add(childWrapperDef);
270+
Keys.Add(new Tuple<CollectionChildDefinition, string>(childWrapperDef, keydef.Item2));
271+
}
272+
}
273+
else
274+
{
275+
Message.Show("Failed to find key " + DefKey + "!", "Collection Resolve Failed", "Ok");
276+
}
277+
}
278+
279+
foreach (var key in DefKeys)
280+
{
281+
Dictionary<string, DataDefinition> defs = null;
282+
if (local.ContainsKey(key.Item1.ToLower())) defs = local;
283+
else if (global.ContainsKey(key.Item1.ToLower())) defs = global;
284+
285+
if (defs != null)
286+
{
287+
var childDef = defs[key.Item1.ToLower()];
288+
var childWrapperDef = new CollectionChildDefinition();
289+
childWrapperDef.WrappedDefinition = childDef;
290+
291+
ChildDefinitions.Add(childWrapperDef);
292+
Keys.Add(new Tuple<CollectionChildDefinition, string>(childWrapperDef, key.Item2));
293+
}
294+
else if (key.Item1 != "---")
295+
{
296+
Message.Show("Failed to find key " + key.Item1 + "!", "Collection Resolve Failed", "Ok");
297+
}
298+
}
299+
227300
foreach (var def in ChildDefinitions) def.WrappedDefinition.RecursivelyResolve(local, global, referenceableDefinitions);
228301
}
229302
}

0 commit comments

Comments
 (0)