66using System . Windows . Data ;
77using System . Xml . Linq ;
88using StructuredXmlEditor . Data ;
9+ using StructuredXmlEditor . View ;
910
1011namespace StructuredXmlEditor . Definition
1112{
@@ -19,6 +20,8 @@ public class CollectionDefinition : ComplexDataDefinition
1920 public List < DataDefinition > AdditionalDefs { get ; } = new List < DataDefinition > ( ) ;
2021 public int MinCount { get ; set ; } = 0 ;
2122 public int MaxCount { get ; set ; } = int . MaxValue ;
23+ public List < Tuple < string , string > > DefKeys { get ; set ; } = new List < Tuple < string , string > > ( ) ;
24+ public string DefKey { get ; set ; }
2225
2326 public CollectionDefinition ( )
2427 {
@@ -155,6 +158,27 @@ public override void Parse(XElement definition)
155158 MinCount = TryParseInt ( definition , "MinCount" , 0 ) ;
156159 MaxCount = TryParseInt ( definition , "MaxCount" , int . MaxValue ) ;
157160
161+ DefKey = definition . Attribute ( "DefKey" ) ? . Value ? . ToString ( ) ;
162+ var keyString = definition . Attribute ( "Keys" ) ? . Value ? . ToString ( ) ;
163+ if ( ! string . IsNullOrWhiteSpace ( keyString ) )
164+ {
165+ if ( ! keyString . Contains ( '(' ) )
166+ {
167+ DefKeys . AddRange ( keyString . Split ( new char [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) . Select ( e => new Tuple < string , string > ( e . Trim ( ) , "Type" ) ) ) ;
168+ }
169+ else
170+ {
171+ var categories = keyString . Split ( new char [ ] { ')' } , StringSplitOptions . RemoveEmptyEntries ) ;
172+ foreach ( var categoryString in categories )
173+ {
174+ var split = categoryString . Split ( '(' ) ;
175+ var category = split [ 0 ] . Trim ( ) ;
176+ if ( category . StartsWith ( "," ) ) category = category . Substring ( 1 ) ;
177+ DefKeys . AddRange ( split [ 1 ] . Split ( new char [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) . Select ( e => new Tuple < string , string > ( e . Trim ( ) , category ) ) ) ;
178+ }
179+ }
180+ }
181+
158182 var currentGroup = "Items" ;
159183
160184 var childDefs = definition . Nodes ( ) ;
@@ -180,7 +204,7 @@ public override void Parse(XElement definition)
180204 }
181205 }
182206
183- if ( ChildDefinitions . Count == 0 )
207+ if ( ChildDefinitions . Count == 0 && DefKey == null && DefKeys . Count == 0 )
184208 {
185209 throw new Exception ( "No child definitions in collection '" + Name + "'!" ) ;
186210 }
@@ -289,6 +313,46 @@ public override void DoSaveData(XElement parent, DataItem item)
289313
290314 public override void RecursivelyResolve ( Dictionary < string , DataDefinition > local , Dictionary < string , DataDefinition > global , Dictionary < string , Dictionary < string , DataDefinition > > referenceableDefinitions )
291315 {
316+ if ( DefKey != null )
317+ {
318+ var key = DefKey . ToLower ( ) ;
319+
320+ Dictionary < string , DataDefinition > defs = null ;
321+ if ( local . ContainsKey ( key ) ) defs = local ;
322+ else if ( global . ContainsKey ( key ) ) defs = global ;
323+
324+ if ( defs != null )
325+ {
326+ var def = defs [ key ] as ReferenceDefinition ;
327+ DefKeys = def . Keys ;
328+ }
329+ else
330+ {
331+ Message . Show ( "Failed to find key " + DefKey + "!" , "Collection Resolve Failed" , "Ok" ) ;
332+ }
333+ }
334+
335+ foreach ( var key in DefKeys )
336+ {
337+ Dictionary < string , DataDefinition > defs = null ;
338+ if ( local . ContainsKey ( key . Item1 . ToLower ( ) ) ) defs = local ;
339+ else if ( global . ContainsKey ( key . Item1 . ToLower ( ) ) ) defs = global ;
340+
341+ if ( defs != null )
342+ {
343+ var childDef = defs [ key . Item1 . ToLower ( ) ] ;
344+ var childWrapperDef = new CollectionChildDefinition ( ) ;
345+ childWrapperDef . WrappedDefinition = childDef ;
346+
347+ ChildDefinitions . Add ( childWrapperDef ) ;
348+ Keys . Add ( new Tuple < CollectionChildDefinition , string > ( childWrapperDef , key . Item2 ) ) ;
349+ }
350+ else if ( key . Item1 != "---" )
351+ {
352+ Message . Show ( "Failed to find key " + key . Item1 + "!" , "Collection Resolve Failed" , "Ok" ) ;
353+ }
354+ }
355+
292356 foreach ( var def in ChildDefinitions ) def . WrappedDefinition . RecursivelyResolve ( local , global , referenceableDefinitions ) ;
293357 foreach ( var def in AdditionalDefs ) def . RecursivelyResolve ( local , global , referenceableDefinitions ) ;
294358 }
0 commit comments