[longomatch/redesign3] Refactor tags store to use a List



commit e307859642b53d4f284f9d54fa1f53cc89688968
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Mon Aug 22 20:43:27 2011 +0200

    Refactor tags store to use a List
    
    Using a list instead of the previous dictionary is properly handled
    by db4o and should be easier to serialize

 LongoMatch/Gui/Component/PlayersTaggerWidget.cs |   20 +++----
 LongoMatch/Gui/Component/StringTaggerWidget.cs  |   38 ++++++-------
 LongoMatch/Gui/Component/TaggerWidget.cs        |    7 +--
 LongoMatch/Gui/Dialog/PlayersSelectionDialog.cs |   54 +++++++----------
 LongoMatch/Gui/Dialog/TaggerDialog.cs           |   15 ++---
 LongoMatch/Interfaces/ITag.cs                   |   29 +++++++++
 LongoMatch/LongoMatch.mdp                       |    1 +
 LongoMatch/Makefile.am                          |    1 +
 LongoMatch/Store/Tag.cs                         |   13 +++-
 LongoMatch/Store/TagStore.cs                    |   70 +++++++++++------------
 10 files changed, 131 insertions(+), 117 deletions(-)
---
diff --git a/LongoMatch/Gui/Component/PlayersTaggerWidget.cs b/LongoMatch/Gui/Component/PlayersTaggerWidget.cs
index 5511ff6..bcbcd5d 100644
--- a/LongoMatch/Gui/Component/PlayersTaggerWidget.cs
+++ b/LongoMatch/Gui/Component/PlayersTaggerWidget.cs
@@ -28,32 +28,30 @@ namespace LongoMatch.Gui.Component
 	[System.ComponentModel.ToolboxItem(true)]
 	public partial class PlayersTaggerWidget : Gtk.Bin
 	{
-		private List<PlayerTag> players;
+		private PlayerSubCategory subcat;
+		private PlayersTagStore players;
 		private TeamTemplate template;
-		private bool allowMultiple;
 		
-		public PlayersTaggerWidget (String subcategoryName, bool allowMultiple,
-		                            TeamTemplate template, List<PlayerTag> players) {
+		public PlayersTaggerWidget (PlayerSubCategory subcat, TeamTemplate template,
+		                            PlayersTagStore players) {
 			this.Build ();
-			editbutton.Clicked += OnEditClicked;
+			this.subcat = subcat;
 			this.players = players;
 			this.template = template;
-			this.allowMultiple = allowMultiple;
-			CategoryLabel.Markup = "<b>" + subcategoryName + "</b>";
+			CategoryLabel.Markup = "<b>" + subcat.Name + "</b>";
 			LoadTagsLabel();
+			editbutton.Clicked += OnEditClicked;
 		}
 		
 		private void LoadTagsLabel () {
-			var playersNames = players.Select(p => p.Value.Name).ToArray();
+			var playersNames = players.GetTags(subcat).Select(p => p.Value.Name).ToArray();
 			playerslabel.Text = String.Join(" ; ", playersNames);
 		}
 		
 		protected virtual void OnEditClicked (object sender, System.EventArgs e)
 		{
-			PlayersSelectionDialog dialog = new PlayersSelectionDialog(allowMultiple);
+			PlayersSelectionDialog dialog = new PlayersSelectionDialog(subcat, template, players);
 			dialog.TransientFor = this.Toplevel as Gtk.Window;
-			dialog.Template = template;
-			dialog.SelectedPlayers = players;
 			dialog.Run();
 			dialog.Destroy();
 			LoadTagsLabel();
diff --git a/LongoMatch/Gui/Component/StringTaggerWidget.cs b/LongoMatch/Gui/Component/StringTaggerWidget.cs
index eab6dfd..642c150 100644
--- a/LongoMatch/Gui/Component/StringTaggerWidget.cs
+++ b/LongoMatch/Gui/Component/StringTaggerWidget.cs
@@ -28,35 +28,31 @@ namespace LongoMatch.Gui.Component
 	public partial class StringTaggerWidget : Gtk.Bin
 	{
 		private Dictionary<StringTag, CheckButton> dict;
-		private List<StringTag> tags;
+		private StringTagStore tags;
 		private RadioButton firstRB;
-		string subcategory;
+		TagSubCategory subcategory;
 		
-		public StringTaggerWidget ()
+		public StringTaggerWidget (TagSubCategory subcategory, StringTagStore tags)
 		{
 			this.Build ();
-			dict = new Dictionary<StringTag, CheckButton>();
+			this.subcategory = subcategory;
+			this.tags = tags;
+			PopulateGui();
+			UpdateTags();
 		}
 		
-		public TagSubCategory SubCategory {
-			set {
-				subcategory = value.Name;
-				Title = subcategory;
-				foreach (string tag in value)
-					AddTagWidget(new StringTag{Value=tag}, !value.AllowMultiple);
-			}
+		private void PopulateGui() {
+			Title = subcategory.Name;
+			dict = new Dictionary<StringTag, CheckButton>();
+			foreach (string tag in subcategory)
+				AddTagWidget(new StringTag{Value=tag, SubCategory=subcategory},
+				             !subcategory.AllowMultiple);
 		}
 		
-		public List<StringTag> Tags {
-			set{
-				tags = value;
-				foreach (var tag in tags) {
-					if (dict.ContainsKey(tag)) 	
-						dict[tag].Active = true;
-				}
-			}
-			get {
-				return tags;
+		public void UpdateTags() {
+			foreach (var tag in tags.GetTags(subcategory)) {
+				if (dict.ContainsKey(tag)) 	
+					dict[tag].Active = true;
 			}
 		}
 		
diff --git a/LongoMatch/Gui/Component/TaggerWidget.cs b/LongoMatch/Gui/Component/TaggerWidget.cs
index 141639b..c0f36f1 100644
--- a/LongoMatch/Gui/Component/TaggerWidget.cs
+++ b/LongoMatch/Gui/Component/TaggerWidget.cs
@@ -36,11 +36,8 @@ namespace LongoMatch.Gui.Component
 			table1.NRows = 1;
 		}
 		
-		public void AddSubCategory(TagSubCategory subcat, List<StringTag> tags){
-			StringTaggerWidget tagger = new StringTaggerWidget();
-			tagger.SubCategory = subcat;
-			tagger.Tags = tags;
-			
+		public void AddSubCategory(TagSubCategory subcat, StringTagStore tags){
+			StringTaggerWidget tagger = new StringTaggerWidget(subcat, tags);
 			table1.Attach(tagger,0, 1, table1.NRows-1, table1.NRows);
 			table1.NRows ++;
 			tagger.Show();
diff --git a/LongoMatch/Gui/Dialog/PlayersSelectionDialog.cs b/LongoMatch/Gui/Dialog/PlayersSelectionDialog.cs
index d44bb14..3913cf3 100644
--- a/LongoMatch/Gui/Dialog/PlayersSelectionDialog.cs
+++ b/LongoMatch/Gui/Dialog/PlayersSelectionDialog.cs
@@ -29,45 +29,35 @@ namespace LongoMatch.Gui.Dialog
 
 	public partial class PlayersSelectionDialog : Gtk.Dialog
 	{
-		TeamTemplate template;
-		List<PlayerTag> selectedPlayers;
-		Dictionary<CheckButton, PlayerTag> checkButtonsDict;
-		bool useRadioButtons;
-		RadioButton firstRB;
+		private PlayerSubCategory subcat;
+		private TeamTemplate template;
+		private PlayersTagStore players;
+		private Dictionary<CheckButton, PlayerTag> checkButtonsDict;
+		private RadioButton firstRB;
 		
-		public PlayersSelectionDialog(bool useRadioButtons)
+		public PlayersSelectionDialog(PlayerSubCategory subcat, TeamTemplate template,
+		                              PlayersTagStore players)
 		{
 			this.Build();
-			checkButtonsDict = new Dictionary<CheckButton, PlayerTag>();
-			this.useRadioButtons = useRadioButtons;
+			this.subcat = subcat;
+			this.template = template;
+			this.players = players;
+			SetPlayersInfo();
+			UpdateSelectedPlayers();
 		}
 		
-		public TeamTemplate Template {
-			set{
-				SetPlayersInfo(value);
-			}
-		}
-		
-		public List<PlayerTag> SelectedPlayers {
-			set {
-				this.selectedPlayers = value;
-				foreach(var pair in checkButtonsDict)
-					pair.Key.Active = value.Contains(pair.Value);
-			}
-			get {
-				return selectedPlayers;
+		private void UpdateSelectedPlayers () {
+			foreach(var pair in checkButtonsDict) {
+				pair.Key.Active = players.Contains(pair.Value);
 			}
 		}
 
-		private void SetPlayersInfo(TeamTemplate template) {
+		private void SetPlayersInfo() {
 			List<PlayerTag> playersList;
 			int i=0;
 
-			if(this.template != null)
-				return;
-
-			this.template = template;
-			playersList = template.PlayingPlayersList.Select(p => new PlayerTag {Value=p}).ToList();
+			checkButtonsDict = new Dictionary<CheckButton, PlayerTag>();
+			playersList = template.PlayingPlayersList.Select(p => new PlayerTag {Value=p, SubCategory=subcat}).ToList();
 
 			table1.NColumns =(uint)(playersList.Count/10);
 			table1.NRows =(uint) 10;
@@ -75,7 +65,7 @@ namespace LongoMatch.Gui.Dialog
 			foreach(PlayerTag player in playersList) {
 				CheckButton button;
 				
-				if (useRadioButtons) {
+				if (!subcat.AllowMultiple) {
 					if (firstRB == null)
 						button = firstRB = new RadioButton("");
 					else
@@ -103,10 +93,10 @@ namespace LongoMatch.Gui.Dialog
 			CheckButton button = sender as CheckButton;
 			PlayerTag player = checkButtonsDict[button];
 			
-			if (button.Active && !selectedPlayers.Contains(player))
-				selectedPlayers.Add(player);
+			if (button.Active && !players.Contains(player))
+				players.Add(player);
 			else if (!button.Active)
-				selectedPlayers.Remove(player);
+				players.Remove(player);
 		}
 	}
 }
diff --git a/LongoMatch/Gui/Dialog/TaggerDialog.cs b/LongoMatch/Gui/Dialog/TaggerDialog.cs
index 4d8c645..5698731 100644
--- a/LongoMatch/Gui/Dialog/TaggerDialog.cs
+++ b/LongoMatch/Gui/Dialog/TaggerDialog.cs
@@ -46,25 +46,25 @@ namespace LongoMatch.Gui.Dialog
 			
 			/* Iterate over all subcategories, adding a widget only for the FastTag ones */
 			foreach (var subcat in cat.SubCategories) {
+				if (!subcat.FastTag)
+					continue;
 				if (subcat is TagSubCategory) {
 					var tagcat = subcat as TagSubCategory;
-					if (tagcat.FastTag)
-						AddTagSubcategory(tagcat, tags.GetTags(tagcat));
+					AddTagSubcategory(tagcat, tags);
 				} else if (subcat is PlayerSubCategory) {
 					var tagcat = subcat as PlayerSubCategory;
-					if (tagcat.FastTag)
-						AddPlayerSubcategory(tagcat, players.GetTags(tagcat));
+					AddPlayerSubcategory(tagcat, players);
 				}
 			}
 		}
 		
-		public void AddTagSubcategory (TagSubCategory subcat, List<StringTag> tags){
+		public void AddTagSubcategory (TagSubCategory subcat, StringTagStore tags){
 			/* the notebook starts invisible */
 			tagsnotebook.Visible = true;
 			taggerwidget1.AddSubCategory(subcat, tags);
 		}
 		
-		public void AddPlayerSubcategory (PlayerSubCategory subcat, List<PlayerTag> tags){
+		public void AddPlayerSubcategory (PlayerSubCategory subcat, PlayersTagStore tags){
 			TeamTemplate template;
 			
 			/* the notebook starts invisible */
@@ -76,8 +76,7 @@ namespace LongoMatch.Gui.Dialog
 			else
 				template = visitorTeamTemplate;
 			
-			PlayersTaggerWidget widget = new PlayersTaggerWidget(subcat.Name, subcat.AllowMultiple,
-			                                                     template, tags);
+			PlayersTaggerWidget widget = new PlayersTaggerWidget(subcat, template, tags);
 			widget.Show();
 			playersbox.PackStart(widget, false, true, 0);
 		}
diff --git a/LongoMatch/Interfaces/ITag.cs b/LongoMatch/Interfaces/ITag.cs
new file mode 100644
index 0000000..90c3e0a
--- /dev/null
+++ b/LongoMatch/Interfaces/ITag.cs
@@ -0,0 +1,29 @@
+// 
+//  Copyright (C) 2011 andoni
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+using LongoMatch.Store;
+
+namespace LongoMatch.Interfaces
+{
+	public interface ITag<T>
+	{
+		ISubCategory SubCategory {set;get;}
+		T Value {set;get;}
+	}
+}
+
diff --git a/LongoMatch/LongoMatch.mdp b/LongoMatch/LongoMatch.mdp
index 5f8b5d0..9823f35 100644
--- a/LongoMatch/LongoMatch.mdp
+++ b/LongoMatch/LongoMatch.mdp
@@ -198,6 +198,7 @@
     <File subtype="Code" buildaction="Compile" name="Services/DataBase.cs" />
     <File subtype="Directory" buildaction="Compile" name="DB" />
     <File subtype="Code" buildaction="Compile" name="Store/TagStore.cs" />
+    <File subtype="Code" buildaction="Compile" name="Interfaces/ITag.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
diff --git a/LongoMatch/Makefile.am b/LongoMatch/Makefile.am
index 4e5b1d4..8ef7445 100644
--- a/LongoMatch/Makefile.am
+++ b/LongoMatch/Makefile.am
@@ -114,6 +114,7 @@ SOURCES = \
 	Handlers/HotKeysManager.cs \
 	Handlers/VideoDrawingsManager.cs \
 	Interfaces/ISubCategory.cs \
+	Interfaces/ITag.cs \
 	Interfaces/ITemplates.cs \
 	IO/CSVExport.cs \
 	IO/XMLReader.cs \
diff --git a/LongoMatch/Store/Tag.cs b/LongoMatch/Store/Tag.cs
index ff1f44d..e066f23 100644
--- a/LongoMatch/Store/Tag.cs
+++ b/LongoMatch/Store/Tag.cs
@@ -27,11 +27,16 @@ namespace LongoMatch.Store
 {
 
 	[Serializable]
-	public class Tag<T>
+	public class Tag<T>: ITag<T>
 	{
 		public Tag() {
 		}
 		
+		public ISubCategory SubCategory {
+			set;
+			get;
+		}
+		
 		public T Value {
 			get;
 			set;
@@ -49,7 +54,7 @@ namespace LongoMatch.Store
 			StringTag tag = obj as StringTag;
             if (tag == null)
 				return false;
-			return Value.Equals (tag.Value);
+			return Value.Equals (tag.Value) && SubCategory.Equals(tag.SubCategory);
 		}
 		
 		public override int GetHashCode ()
@@ -68,7 +73,7 @@ namespace LongoMatch.Store
 			PlayerTag tag = obj as PlayerTag;
             if (tag == null)
 				return false;
-			return Value.Equals (tag.Value);
+			return Value.Equals (tag.Value) && SubCategory.Equals(tag.SubCategory) ;
 		}
 		
 		public override int GetHashCode ()
@@ -87,7 +92,7 @@ namespace LongoMatch.Store
 			TeamTag tag = obj as TeamTag;
             if (tag == null)
 				return false;
-			return Value.Equals (tag.Value);
+			return Value.Equals (tag.Value) && SubCategory.Equals(tag.SubCategory);
 		}
 		
 		public override int GetHashCode ()
diff --git a/LongoMatch/Store/TagStore.cs b/LongoMatch/Store/TagStore.cs
index 16918a4..73f43e8 100644
--- a/LongoMatch/Store/TagStore.cs
+++ b/LongoMatch/Store/TagStore.cs
@@ -18,69 +18,67 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using LongoMatch.Common;
 using LongoMatch.Interfaces;
+using LongoMatch.Store;
 
 namespace LongoMatch.Store
 {
 	[Serializable]
-	public class TagsStore<T, W> where T:ISubCategory
+	public class TagsStore<T, W> where T:ITag<W>
 	{
-		private Dictionary<T, List<W>> tags;
+		private List<T> tagsList;
 		
 		public TagsStore(){
-			tags = new Dictionary<T, List<W>>();
+			tagsList = new List<T>();
 		}
 		
-		public void Add(T subCategory, W tag) {
-			Log.Debug(String.Format("Adding tag {0} to subcategory{1}", subCategory, tag));
-			if (!tags.ContainsKey(subCategory))
-				tags.Add(subCategory, new List<W>());
-			tags[subCategory].Add(tag);
+		public List<T> Tags {
+			get{
+				return tagsList;
+			}
+			set {
+				tagsList = value;
+			}
 		}
 		
-		public void Remove(T subCategory, W tag) {
-			if (!tags.ContainsKey(subCategory)) {
-				Log.Warning(String.Format("Trying to remove tag {0} from unknown subcategory{1}",
-				                          subCategory, tag));
-				return;
-			}
-			tags[subCategory].Remove(tag);
-			if (tags[subCategory].Count == 0)
-				tags.Remove(subCategory);
+		public void Add(T tag) {
+			Log.Debug(String.Format("Adding tag {0} with subcategory{1}", tag, tag.SubCategory));
+			tagsList.Add(tag);
 		}
 		
-		public bool Contains(T subCategory) {
-			return (tags.ContainsKey(subCategory));
+		public void Remove(T tag) {
+			try {
+				tagsList.Remove (tag);
+			} catch (Exception e) {
+				Log.Warning("Error removing tag " + tag.ToString());
+				Log.Exception(e);
+			}
 		}
 		
-		public bool Contains(T subCategory, W tag) {
-			return (Contains(subCategory) && tags[subCategory].Contains(tag));
+		public bool Contains(T tag) {
+			return tagsList.Contains(tag);
 		}
 		
-		public List<W> AllUniqueElements {
+		public List<T> AllUniqueElements {
 			get {
-				return (from list in tags.Values
-				        from player in list
-				        group player by player into g
+				return (from tag in tagsList
+				        group tag by tag into g
 				        select g.Key).ToList();
 			}
 		}
 		
-		public List<W> GetTags(T subCategory) {
-			if (!tags.ContainsKey(subCategory)) {
-				Log.Debug(String.Format("Adding subcategory {0} to store", subCategory.Name));
-				tags[subCategory] = new List<W>();
-			}
-			return tags[subCategory];			
+		public List<T> GetTags(ISubCategory subCategory) {
+			return (from tag in tagsList
+			        where tag.SubCategory.Equals(subCategory)
+			        select tag).ToList();
 		}
-
 	}
 	
+	public class StringTagStore: TagsStore<StringTag, string> {}
 	
-	public class StringTagStore: TagsStore<TagSubCategory, StringTag> {}
-	
-	public class PlayersTagStore: TagsStore<PlayerSubCategory, PlayerTag> {}
+	public class PlayersTagStore: TagsStore<PlayerTag, Player> {}
 	
-	public class TeamsTagStore: TagsStore<PlayerSubCategory, TeamTag> {}
+	public class TeamsTagStore: TagsStore<TeamTag, Team> {}
 }
 



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