[longomatch] Use the correct serialization callbacks for Json.NET



commit 8b820792c592164bd2c0e7efdbb74e9e6286284f
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Wed Mar 25 17:39:34 2015 +0100

    Use the correct serialization callbacks for Json.NET

 LongoMatch.Core/Store/Templates/Dashboard.cs |  155 +++++++++++++++-----------
 LongoMatch.Core/Store/Templates/Team.cs      |   18 +---
 2 files changed, 92 insertions(+), 81 deletions(-)
---
diff --git a/LongoMatch.Core/Store/Templates/Dashboard.cs b/LongoMatch.Core/Store/Templates/Dashboard.cs
index 6016585..ed128d1 100644
--- a/LongoMatch.Core/Store/Templates/Dashboard.cs
+++ b/LongoMatch.Core/Store/Templates/Dashboard.cs
@@ -42,18 +42,19 @@ namespace LongoMatch.Core.Store.Templates
        /// The <see cref="LongoMatch.DB.Project"/> must handle all the changes
        /// </summary>
        [Serializable]
-       public class Dashboard: IStorable, ITemplate, IDeserializationCallback
+       public class Dashboard: IStorable, ITemplate
        {
 
                const int CAT_WIDTH = 120;
                const int CAT_HEIGHT = 80;
                const int MIN_WIDTH = 320;
                const int MIN_HEIGHT = 240;
-               
+
                /// <summary>
                /// Creates a new template
                /// </summary>
-               public Dashboard() {
+               public Dashboard ()
+               {
                        try {
                                FieldBackground = Config.FieldBackground;
                                HalfFieldBackground = Config.HalfFieldBackground;
@@ -62,8 +63,31 @@ namespace LongoMatch.Core.Store.Templates
                                /* Ingore for unit tests */
                        }
                        ID = Guid.NewGuid ();
-                       List = new List<DashboardButton>();
+                       List = new List<DashboardButton> ();
+               }
+
+               void InitializeLists ()
+               {
+                       // After being deserialized, make sure to create a default GamePeriod
+                       if (GamePeriods == null) {
+                               GamePeriods = new List<string> ();
+                               GamePeriods.Add ("1");
+                               GamePeriods.Add ("2");
+                       }
+               }
+
+               [OnDeserialized]
+               internal void OnDeserializedMethod (StreamingContext context)
+               {
+                       InitializeLists ();
+               }
+
+               [OnSerializing]
+               internal void OnSerializingMethod (StreamingContext context)
+               {
+                       InitializeLists ();
                }
+
                
                public Guid ID {
                        get;
@@ -75,17 +99,17 @@ namespace LongoMatch.Core.Store.Templates
                        get;
                        set;
                }
-               
+
                public List<DashboardButton> List {
                        get;
                        set;
                }
-               
+
                public string Name {
                        get;
                        set;
                }
-               
+
                public List<string> GamePeriods {
                        get;
                        set;
@@ -95,76 +119,62 @@ namespace LongoMatch.Core.Store.Templates
                        get;
                        set;
                }
-               
+
                public Image FieldBackground {
                        get;
                        set;
                }
-               
+
                public Image HalfFieldBackground {
                        get;
                        set;
                }
-               
+
                public Image GoalBackground {
                        get;
                        set;
                }
-               
+
                public bool DisablePopupWindow {
                        get;
                        set;
                }
-               
+
                [JsonIgnore]
                public List<Timer> Timers {
                        get {
-                               return List.OfType<Timer>().ToList();
+                               return List.OfType<Timer> ().ToList ();
                        }
                }
-               
+
                [JsonIgnore]
                public int CanvasWidth {
                        get {
                                if (List.Count == 0) {
                                        return MIN_WIDTH;
                                }
-                               return Math.Max (MIN_WIDTH, (int) List.Max (c => c.Position.X + c.Width));
+                               return Math.Max (MIN_WIDTH, (int)List.Max (c => c.Position.X + c.Width));
                        }
                }
-               
+
                [JsonIgnore]
                public int CanvasHeight {
                        get {
                                if (List.Count == 0) {
                                        return MIN_HEIGHT;
                                }
-                               return Math.Max (MIN_WIDTH, (int) List.Max (c => c.Position.Y + c.Height));
+                               return Math.Max (MIN_WIDTH, (int)List.Max (c => c.Position.Y + c.Height));
                        }
                }
-               
+
                [JsonIgnore]
                public Dictionary<string, List<Tag>> CommonTagsByGroup {
                        get {
-                               return List.OfType<TagButton>().Select(t=>t.Tag).
-                                       GroupBy(t => t.Group).ToDictionary (g => g.Key, g => g.ToList ());
-                       }
-               }
-
-               #region IDeserializationCallback implementation
-
-               void IDeserializationCallback.OnDeserialization (object sender)
-               {
-                       // After being deserialized, make sure to create a default GamePeriod
-                       if (GamePeriods == null) {
-                               GamePeriods = new List<string>();
-                               GamePeriods.Add ("1");
-                               GamePeriods.Add ("2");
+                               return List.OfType<TagButton> ().Select (t => t.Tag).
+                                       GroupBy (t => t.Group).ToDictionary (g => g.Key, g => g.ToList ());
                        }
                }
 
-               #endregion
-
                public void ChangeHotkey (DashboardButton button, HotKey hotkey)
                {
                        if (List.Count (d => d.HotKey == hotkey) > 0) {
@@ -173,103 +183,116 @@ namespace LongoMatch.Core.Store.Templates
                                button.HotKey = hotkey;
                        }
                }
-       
-               public void AddDefaultTags (AnalysisEventType ev) {
+
+               public void AddDefaultTags (AnalysisEventType ev)
+               {
                        ev.Tags.Add (new Tag (Catalog.GetString ("Success"),
-                                             Catalog.GetString ("Outcome")));
+                               Catalog.GetString ("Outcome")));
                        ev.Tags.Add (new Tag (Catalog.GetString ("Failure"),
-                                             Catalog.GetString ("Outcome")));
-               }       
-               
-               public AnalysisEventButton AddDefaultItem (int index) {
+                               Catalog.GetString ("Outcome")));
+               }
+
+               public AnalysisEventButton AddDefaultItem (int index)
+               {
                        AnalysisEventButton button;
                        AnalysisEventType evtype;
                        Color c = StyleConf.ButtonEventColor;
-                       HotKey h = new HotKey();
+                       HotKey h = new HotKey ();
                        
                        evtype = new AnalysisEventType {
                                Name = "Event Type " + index,
                                SortMethod = SortMethodType.SortByStartTime,
                                Color = c
                        };
-                       AddDefaultTags(evtype);
+                       AddDefaultTags (evtype);
 
                        button = new  AnalysisEventButton {
                                EventType = evtype,
-                               Start = new Time{TotalSeconds = 10},
-                               Stop = new Time {TotalSeconds = 10},
+                               Start = new Time{ TotalSeconds = 10 },
+                               Stop = new Time { TotalSeconds = 10 },
                                HotKey = h,
                                /* Leave the first row for the timers and score */
                                Position = new Point (10 + (index % 7) * (CAT_WIDTH + 10),
-                                                     10 + (index / 7 + 1) * (CAT_HEIGHT + 10)),
+                                       10 + (index / 7 + 1) * (CAT_HEIGHT + 10)),
                                Width = CAT_WIDTH,
                                Height = CAT_HEIGHT,
                        };
-                       List.Insert(index, button);
+                       List.Insert (index, button);
                        return button;
                }
 
-               public static Dashboard DefaultTemplate(int count) {
+               public static Dashboard DefaultTemplate (int count)
+               {
                        TagButton tagbutton;
                        TimerButton timerButton;
                        PenaltyCardButton cardButton;
                        ScoreButton scoreButton;
-                       List<string> periods = new List<string>();
-                       Dashboard template = new Dashboard();
+                       List<string> periods = new List<string> ();
+                       Dashboard template = new Dashboard ();
                        
-                       template.FillDefaultTemplate(count);
+                       template.FillDefaultTemplate (count);
                        periods.Add ("1");
                        periods.Add ("2");
                        template.GamePeriods = periods; 
 
                        tagbutton = new TagButton {
                                Tag = new Tag (Catalog.GetString ("Attack"), ""),
-                               Position = new Point (10, 10)};
+                               Position = new Point (10, 10)
+                       };
                        template.List.Add (tagbutton);
                        
                        tagbutton = new TagButton {
                                Tag = new Tag (Catalog.GetString ("Defense"), ""),
-                               Position = new Point (10 + (10 + CAT_WIDTH) * 1, 10)};
+                               Position = new Point (10 + (10 + CAT_WIDTH) * 1, 10)
+                       };
                        template.List.Add (tagbutton);
 
                        cardButton = new PenaltyCardButton {
                                PenaltyCard = new PenaltyCard (Catalog.GetString ("Red card"),
-                                                              Color.Red, CardShape.Rectangle),
-                               Position = new Point (10 + (10 + CAT_WIDTH) * 2, 10)};
+                                       Color.Red, CardShape.Rectangle),
+                               Position = new Point (10 + (10 + CAT_WIDTH) * 2, 10)
+                       };
                        template.List.Add (cardButton);
 
                        cardButton = new PenaltyCardButton {
                                PenaltyCard = new PenaltyCard (Catalog.GetString ("Yellow card"),
-                                                              Color.Yellow, CardShape.Rectangle),
-                               Position = new Point (10 + (10 + CAT_WIDTH) * 3, 10)};
+                                       Color.Yellow, CardShape.Rectangle),
+                               Position = new Point (10 + (10 + CAT_WIDTH) * 3, 10)
+                       };
                        template.List.Add (cardButton);
                        
                        scoreButton = new ScoreButton {
                                Score = new Score {
                                        Name = Catalog.GetString ("Free play goal"),
                                        Points = 1,
-                                       Color = StyleConf.ButtonScoreColor},
-                               Position = new Point (10 + (10 + CAT_WIDTH) * 4, 10)};
+                                       Color = StyleConf.ButtonScoreColor
+                               },
+                               Position = new Point (10 + (10 + CAT_WIDTH) * 4, 10)
+                       };
                        template.List.Add (scoreButton);
                        
                        scoreButton = new ScoreButton {
                                Score = new Score {
                                        Name = Catalog.GetString ("Penalty goal"),
                                        Points = 1,
-                                       Color = StyleConf.ButtonScoreColor},
-                               Position = new Point (10 + (10 + CAT_WIDTH) * 5, 10)};
+                                       Color = StyleConf.ButtonScoreColor
+                               },
+                               Position = new Point (10 + (10 + CAT_WIDTH) * 5, 10)
+                       };
                        template.List.Add (scoreButton);
                        
                        timerButton = new TimerButton {
-                               Timer = new Timer {Name = Catalog.GetString ("Ball playing")},
-                               Position = new Point (10 + (10 + CAT_WIDTH) * 6, 10)};
+                               Timer = new Timer { Name = Catalog.GetString ("Ball playing") },
+                               Position = new Point (10 + (10 + CAT_WIDTH) * 6, 10)
+                       };
                        template.List.Add (timerButton);
                        return template;
                }
 
-               private void FillDefaultTemplate(int count) {
-                       for(int i=1; i<=count; i++)
-                               AddDefaultItem(i-1);
+               private void FillDefaultTemplate (int count)
+               {
+                       for (int i = 1; i <= count; i++)
+                               AddDefaultItem (i - 1);
                }
        }
 }
diff --git a/LongoMatch.Core/Store/Templates/Team.cs b/LongoMatch.Core/Store/Templates/Team.cs
index 85de971..576cf2d 100644
--- a/LongoMatch.Core/Store/Templates/Team.cs
+++ b/LongoMatch.Core/Store/Templates/Team.cs
@@ -31,7 +31,7 @@ using System.IO;
 namespace LongoMatch.Core.Store.Templates
 {
        [Serializable]
-       public class Team: ITemplate, IDeserializationCallback
+       public class Team: ITemplate
        {
                private const int MAX_WIDTH = 100;
                private const int MAX_HEIGHT = 100;
@@ -39,9 +39,7 @@ namespace LongoMatch.Core.Store.Templates
                public Team ()
                {
                        TeamName = Catalog.GetString ("Team");
-                       if (Formation == null) {
-                               FormationStr = "1-4-3-3";
-                       }
+                       FormationStr = "1-4-3-3";
                        ID = Guid.NewGuid ();
                        List = new List<Player> ();
                        string path = Path.Combine (Config.IconsDir, StyleConf.DefaultShield);
@@ -56,6 +54,7 @@ namespace LongoMatch.Core.Store.Templates
                        Colors [1] = Color.Red1;
                }
 
+
                public Guid ID {
                        get;
                        set;
@@ -175,17 +174,6 @@ namespace LongoMatch.Core.Store.Templates
                        }
                }
 
-               #region IDeserializationCallback implementation
-
-               void IDeserializationCallback.OnDeserialization (object sender)
-               {
-                       if (Formation == null) {
-                               FormationStr = "1-4-3-3";
-                       }
-               }
-
-               #endregion
-
                public void RemovePlayers (List<Player> players, bool delete)
                {
                        List<Player> bench, starters;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]