@@ -99,6 +99,10 @@ private void SetAsStart(ViewModelNode node)
9999 public SimpleCommandWithParameter < MyPoint > CommandSelect { get ; set ; }
100100 public SimpleCommandWithParameter < MyPoint > CommandCut { get ; set ; }
101101 public SimpleCommandWithParameter < MyPoint > CommandPartMoveAllNode { get ; set ; }
102+ public SimpleCommandWithParameter < string > CommandLogDebug { get ; set ; }
103+ public SimpleCommandWithParameter < string > CommandLogError { get ; set ; }
104+ public SimpleCommandWithParameter < string > CommandLogInformation { get ; set ; }
105+ public SimpleCommandWithParameter < string > CommandLogWarning { get ; set ; }
102106 public SimpleCommandWithParameter < MyPoint > CommandPartMoveAllSelectedNode { get ; set ; }
103107
104108 public SimpleCommandWithParameter < ViewModelConnector > CommandAddFreeConnect { get ; set ; }
@@ -138,6 +142,11 @@ private void SetupCommands()
138142 CommandPartMoveAllSelectedNode = new SimpleCommandWithParameter < MyPoint > ( PartMoveAllSelectedNode ) ;
139143 CommandZoom = new SimpleCommandWithParameter < object > ( Zoom ) ;
140144
145+ CommandLogDebug = new SimpleCommandWithParameter < string > ( LogDebug ) ;
146+ CommandLogError = new SimpleCommandWithParameter < string > ( LogError ) ;
147+ CommandLogInformation = new SimpleCommandWithParameter < string > ( LogInformation ) ;
148+ CommandLogWarning = new SimpleCommandWithParameter < string > ( LogWarning ) ;
149+
141150 //CommandAddConnect = new Command<ViewModelConnect, ViewModelConnect>(this, AddConnect, DeleteConnect);
142151 //CommandDeleteNode = new Command<MyPoint, ViewModelNode>(this, DeleteNode,);
143152 CommandSelect = new SimpleCommandWithParameter < MyPoint > ( StartSelect ) ;
@@ -360,7 +369,7 @@ private void ValidateNodeName(ValidateObjectProperty<ViewModelNode, string> obj)
360369 {
361370 if ( ! String . IsNullOrWhiteSpace ( obj . Property ) )
362371 {
363- if ( ! Nodes . Any ( x => x . Name == obj . Property ) )
372+ if ( ! NodeExist ( obj . Property ) )
364373 {
365374 obj . Obj . Name = obj . Property ;
366375 }
@@ -370,12 +379,23 @@ private void ValidateConnectName(ValidateObjectProperty<ViewModelConnector, stri
370379 {
371380 if ( ! String . IsNullOrWhiteSpace ( obj . Property ) )
372381 {
373- if ( ! this . Nodes . Where ( x => x . Transitions . Any ( x => x . Name == obj . Property ) ) . Any ( ) )
382+ if ( ! ConnectExist ( obj . Property ) )
374383 {
375384 obj . Obj . Name = obj . Property ;
376385 }
377386 }
378387 }
388+ private bool ConnectExist ( string nameConnect )
389+ {
390+ var t = this . Nodes . SelectMany ( x => x . Transitions ) ;
391+ return this . Nodes . SelectMany ( x=> x . Transitions ) . Any ( x=> x . Name == nameConnect ) ;
392+ }
393+
394+ private bool NodeExist ( string nameNode )
395+ {
396+ return Nodes . Any ( x => x . Name == nameNode ) ;
397+ }
398+
379399 private void New ( )
380400 {
381401 this . Nodes . Clear ( ) ;
@@ -384,39 +404,73 @@ private void New()
384404 this . SetupStartState ( ) ;
385405 }
386406 private void Open ( string fileName )
387- {
407+ {
388408 this . Nodes . Clear ( ) ;
389409 this . Connects . Clear ( ) ;
410+
390411 XDocument xDocument = XDocument . Load ( fileName ) ;
391412 XElement stateMachineXElement = xDocument . Element ( "StateMachine" ) ;
392413 if ( stateMachineXElement == null )
393414 {
394- LogError ( "File is not correct" ) ;
395- SetupStartState ( ) ;
415+ Error ( "not contanins StateMachine" ) ;
396416 return ;
397417 }
398- var States = stateMachineXElement . Element ( "States" ) ? . Elements ( ) ? . ToList ( ) ;
399- States ? . ForEach ( x => this . Nodes . Add ( ViewModelNode . FromXElement ( this , x ) ) ) ;
400- var startState = stateMachineXElement . Element ( "StartState" ) ? . Attribute ( "Name" ) ? . Value ;
401-
402- if ( string . IsNullOrEmpty ( startState ) )
403- this . SetupStartState ( ) ;
404- else
405- this . SetAsStart ( this . Nodes . Single ( x => x . Name == startState ) ) ;
406-
407- var Transitions = stateMachineXElement . Element ( "Transitions" ) ? . Elements ( ) ? . ToList ( ) ;
418+ #region setup states/nodes
419+
420+ var States = stateMachineXElement . Element ( "States" ) ? . Elements ( ) ? . ToList ( ) ?? new List < XElement > ( ) ;
421+ ViewModelNode viewModelNode = null ;
422+ foreach ( var state in States )
423+ {
424+ viewModelNode = ViewModelNode . FromXElement ( this , state , out string errorMesage , NodeExist ) ;
425+ if ( WithError ( errorMesage , x => Nodes . Add ( x ) , viewModelNode ) )
426+ return ;
427+ }
428+
429+ #region setup start state
430+
431+ var startState = stateMachineXElement . Element ( "StartState" ) ? . Attribute ( "Name" ) ? . Value ;
432+
433+ if ( string . IsNullOrEmpty ( startState ) )
434+ this . SetupStartState ( ) ;
435+ else
436+ this . SetAsStart ( this . Nodes . Single ( x => x . Name == startState ) ) ;
437+
438+ #endregion setup start state
439+
440+ #endregion setup states/nodes
441+
442+ #region setup Transitions/connects
443+
444+ var Transitions = stateMachineXElement . Element ( "Transitions" ) ? . Elements ( ) ? . ToList ( ) ?? new List < XElement > ( ) ;
408445 ViewModelConnect viewModelConnect ;
409446 foreach ( var transition in Transitions )
410447 {
448+ viewModelConnect = ViewModelConnector . FromXElement ( this , transition , out string errorMesage , ConnectExist ) ;
449+ if ( WithError ( errorMesage , x => Connects . Add ( x ) , viewModelConnect ) )
450+ return ;
451+ }
411452
412- viewModelConnect = ViewModelConnector . FromXElement ( this , transition ) ;
413- if ( viewModelConnect != null )
453+ #endregion setup Transitions/connects
454+
455+ bool WithError < T > ( string errorMessage , Action < T > action , T obj )
456+ {
457+ if ( string . IsNullOrEmpty ( errorMessage ) )
458+ {
459+ if ( ! object . Equals ( obj , default ( T ) ) )
460+ action . Invoke ( obj ) ;
461+ }
462+ else
414463 {
415- this . Connects . Add ( viewModelConnect ) ;
464+ Error ( errorMessage ) ;
465+ return true ;
416466 }
467+ return false ;
468+ }
469+ void Error ( string errorMessage )
470+ {
471+ LogError ( "File is not valid: " + errorMessage ) ;
472+ SetupStartState ( ) ;
417473 }
418- //Transitions?.ForEach(x => this.Connects.Add(ViewModelConnect.FromXElement(this, x)));
419-
420474 }
421475 private void Save ( string fileName )
422476 {
0 commit comments