[longomatch/newui: 33/157] Implement the disposable pattern for canvas and canvas objects



commit 7d00f5c598a96254e838dbf86feb1d86837af6f9
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Aug 21 12:48:00 2014 +0200

    Implement the disposable pattern for canvas and canvas objects

 LongoMatch.Core/Interfaces/Drawing/ICanvas.cs      |    4 +-
 LongoMatch.Drawing/Canvas.cs                       |   28 ++++++++++++++++++++
 LongoMatch.Drawing/CanvasObjects/CanvasObject.cs   |   27 +++++++++++++++++-
 LongoMatch.Drawing/CanvasObjects/TimeNodeObject.cs |    9 ++++++
 LongoMatch.Drawing/Widgets/Blackboard.cs           |   14 ++++-----
 LongoMatch.Drawing/Widgets/Timerule.cs             |    9 ++++++
 LongoMatch.GUI/Gui/Component/ButtonsWidget.cs      |    1 +
 LongoMatch.GUI/Gui/Component/CoordinatesTagger.cs  |    6 ++++
 .../Gui/Component/PlaysPositionViewer.cs           |    8 +++++
 LongoMatch.GUI/Gui/Component/Timeline.cs           |    6 ++++
 10 files changed, 100 insertions(+), 12 deletions(-)
---
diff --git a/LongoMatch.Core/Interfaces/Drawing/ICanvas.cs b/LongoMatch.Core/Interfaces/Drawing/ICanvas.cs
index 86d84d7..7d1e6bd 100644
--- a/LongoMatch.Core/Interfaces/Drawing/ICanvas.cs
+++ b/LongoMatch.Core/Interfaces/Drawing/ICanvas.cs
@@ -24,12 +24,12 @@ using LongoMatch.Interfaces.Drawing;
 namespace LongoMatch.Interfaces.Drawing
 {
 
-       public interface ICanvas
+       public interface ICanvas: IDisposable
        {
                 void Draw (IContext context, Area area);
        }
        
-       public interface ICanvasObject
+       public interface ICanvasObject: IDisposable
        {
                void Draw (IDrawingToolkit tk, Area area);
                bool Visible {set; get;}
diff --git a/LongoMatch.Drawing/Canvas.cs b/LongoMatch.Drawing/Canvas.cs
index 1133448..117e266 100644
--- a/LongoMatch.Drawing/Canvas.cs
+++ b/LongoMatch.Drawing/Canvas.cs
@@ -32,6 +32,7 @@ namespace LongoMatch.Drawing
                protected IWidget widget;
                protected double scaleX, scaleY;
                protected Point translation;
+               bool disposed;
 
                public Canvas (IWidget widget)
                {
@@ -43,6 +44,33 @@ namespace LongoMatch.Drawing
                        scaleY = 1;
                        translation = new Point (0, 0);
                }
+               
+               ~ Canvas ()
+               {
+                       if (! disposed) {
+                               Log.Error (String.Format ("Canvas {0} was not disposed correctly", this));
+                               Dispose (true);
+                       }
+               }
+
+               public void Dispose(){
+                       Dispose(true);
+                       GC.SuppressFinalize(this);
+               }
+
+               protected virtual void Dispose (bool disposing)
+               {
+                       if (disposing) {
+                               if (Objects != null) {
+                                       foreach (CanvasObject co in Objects) {
+                                               co.Dispose ();
+                                       }
+                               }
+                               Objects.Clear ();
+                               Objects = null;
+                               disposed = true;
+                       }
+               }
 
                public List<ICanvasObject> Objects {
                        get;
diff --git a/LongoMatch.Drawing/CanvasObjects/CanvasObject.cs 
b/LongoMatch.Drawing/CanvasObjects/CanvasObject.cs
index 0b5012b..668742f 100644
--- a/LongoMatch.Drawing/CanvasObjects/CanvasObject.cs
+++ b/LongoMatch.Drawing/CanvasObjects/CanvasObject.cs
@@ -18,6 +18,7 @@
 using LongoMatch.Interfaces.Drawing;
 using LongoMatch.Common;
 using LongoMatch.Store.Drawables;
+using System;
 
 namespace LongoMatch.Drawing.CanvasObjects
 {
@@ -25,12 +26,33 @@ namespace LongoMatch.Drawing.CanvasObjects
        {
                public delegate void CanvasHandler (CanvasObject co);
                public event CanvasHandler ClickedEvent;
+               
+               bool disposed;
 
                protected CanvasObject ()
                {
                        Visible = true;
                }
 
+               ~CanvasObject ()
+               {
+                       if (! disposed) {
+                               Log.Error (String.Format ("Canvas object {0} not disposed correctly", this));
+                               Dispose (true);
+                       }
+               }
+
+               public void Dispose ()
+               {
+                       Dispose (true);
+                       GC.SuppressFinalize (this);
+               }
+
+               protected virtual void Dispose (bool disposing)
+               {
+                       disposed = true;
+               }
+
                public virtual string Description {
                        get;
                        set;
@@ -64,7 +86,8 @@ namespace LongoMatch.Drawing.CanvasObjects
                public abstract void Draw (IDrawingToolkit tk, Area area);
        }
 
-       public abstract class CanvasButtonObject: CanvasObject {
+       public abstract class CanvasButtonObject: CanvasObject
+       {
        
                public bool Toggle {
                        get;
@@ -89,7 +112,7 @@ namespace LongoMatch.Drawing.CanvasObjects
                        EmitClickEvent ();
                }
        }
-       
+
        public abstract class CanvasDrawableObject<T>: CanvasObject, ICanvasDrawableObject where T: 
IBlackboardObject
        {
                
diff --git a/LongoMatch.Drawing/CanvasObjects/TimeNodeObject.cs 
b/LongoMatch.Drawing/CanvasObjects/TimeNodeObject.cs
index 75b2239..22d0c3a 100644
--- a/LongoMatch.Drawing/CanvasObjects/TimeNodeObject.cs
+++ b/LongoMatch.Drawing/CanvasObjects/TimeNodeObject.cs
@@ -35,6 +35,15 @@ namespace LongoMatch.Drawing.CanvasObjects
                        TimeNode = node;
                        SelectWhole = true;
                }
+               
+               protected override void Dispose (bool disposing)
+               {
+                       base.Dispose (disposing);
+                       if (needle != null) {
+                               needle.Dispose ();
+                               needle = null;
+                       }
+               }
 
                public TimeNode TimeNode {
                        get;
diff --git a/LongoMatch.Drawing/Widgets/Blackboard.cs b/LongoMatch.Drawing/Widgets/Blackboard.cs
index 64ef6f3..df25509 100644
--- a/LongoMatch.Drawing/Widgets/Blackboard.cs
+++ b/LongoMatch.Drawing/Widgets/Blackboard.cs
@@ -28,7 +28,7 @@ using LongoMatch.Interfaces;
 
 namespace LongoMatch.Drawing.Widgets
 {
-       public class Blackboard: BackgroundCanvas, IDisposable
+       public class Blackboard: BackgroundCanvas
        {
        
                public event ShowDrawToolMenuHandler ShowMenuEvent;
@@ -51,19 +51,14 @@ namespace LongoMatch.Drawing.Widgets
                        tool = DrawTool.Selection;
                }
 
-               public void Dispose ()
-               {
-                       Dispose (true);
-                       GC.SuppressFinalize (this);
-               }
-
-               protected virtual void Dispose (bool disposing)
+               protected override void Dispose (bool disposing)
                {
                        if (disposing) {
                                if (backbuffer != null)
                                        backbuffer.Dispose ();
                                backbuffer = null;
                        }
+                       base.Dispose (disposing);
                }
 
                public FrameDrawing Drawing {
@@ -135,6 +130,9 @@ namespace LongoMatch.Drawing.Widgets
                public void Clear (bool resetDrawing = true)
                {
                        ClearSelection ();
+                       foreach (CanvasObject co in Objects) {
+                               co.Dispose ();
+                       }
                        Objects.Clear ();
                        if (drawing != null && resetDrawing) {
                                drawing.Drawables.Clear ();
diff --git a/LongoMatch.Drawing/Widgets/Timerule.cs b/LongoMatch.Drawing/Widgets/Timerule.cs
index 1823fe8..e8ca0b4 100644
--- a/LongoMatch.Drawing/Widgets/Timerule.cs
+++ b/LongoMatch.Drawing/Widgets/Timerule.cs
@@ -38,6 +38,15 @@ namespace LongoMatch.Drawing.Widgets
                        CurrentTime = new Time (0);
                }
 
+               protected override void Dispose (bool disposing)
+               {
+                       base.Dispose (disposing);
+                       if (needle != null) {
+                               needle.Dispose ();
+                               needle = null;
+                       }
+               }
+
                public double Scroll {
                        set;
                        protected get;
diff --git a/LongoMatch.GUI/Gui/Component/ButtonsWidget.cs b/LongoMatch.GUI/Gui/Component/ButtonsWidget.cs
index 302c59d..8aae4b7 100644
--- a/LongoMatch.GUI/Gui/Component/ButtonsWidget.cs
+++ b/LongoMatch.GUI/Gui/Component/ButtonsWidget.cs
@@ -74,6 +74,7 @@ namespace LongoMatch.Gui.Component
                public override void Destroy ()
                {
                        Config.EventsBroker.Tick -= HandleTick;
+                       tagger.Dispose ();
                        base.Destroy ();
                }
                
diff --git a/LongoMatch.GUI/Gui/Component/CoordinatesTagger.cs 
b/LongoMatch.GUI/Gui/Component/CoordinatesTagger.cs
index adc33bd..99662f0 100644
--- a/LongoMatch.GUI/Gui/Component/CoordinatesTagger.cs
+++ b/LongoMatch.GUI/Gui/Component/CoordinatesTagger.cs
@@ -32,6 +32,12 @@ namespace LongoMatch.Gui.Component
                        Tagger = new PositionTagger (new WidgetWrapper (drawingarea));
                }
                
+               protected override void OnDestroyed ()
+               {
+                       Tagger.Dispose ();
+                       base.OnDestroyed ();
+               }
+
                public PositionTagger Tagger {
                        get;
                        set;
diff --git a/LongoMatch.GUI/Gui/Component/PlaysPositionViewer.cs 
b/LongoMatch.GUI/Gui/Component/PlaysPositionViewer.cs
index ed73ccc..c2f4beb 100644
--- a/LongoMatch.GUI/Gui/Component/PlaysPositionViewer.cs
+++ b/LongoMatch.GUI/Gui/Component/PlaysPositionViewer.cs
@@ -42,6 +42,14 @@ namespace LongoMatch.Gui.Component
                        Config.EventsBroker.PlaySelected += HandlePlaySelected;
                        menu = new PlaysMenu ();
                }
+               
+               protected override bool OnDestroyEvent (Gdk.Event evnt)
+               {
+                       return base.OnDestroyEvent (evnt);
+                       field.Destroy ();
+                       hfield.Destroy ();
+                       goal.Destroy ();
+               }
 
                public void LoadProject (Project project) {
                        this.project = project;
diff --git a/LongoMatch.GUI/Gui/Component/Timeline.cs b/LongoMatch.GUI/Gui/Component/Timeline.cs
index bcf6305..432a60a 100644
--- a/LongoMatch.GUI/Gui/Component/Timeline.cs
+++ b/LongoMatch.GUI/Gui/Component/Timeline.cs
@@ -66,6 +66,12 @@ namespace LongoMatch.Gui.Component
                        menu = new PlaysMenu ();
                }
                
+               protected override void OnDestroyed ()
+               {
+                       timerule.Dispose ();
+                       base.OnDestroyed ();
+               }
+               
                public TimeNode SelectedTimeNode {
                        set {
                        }


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