[longomatch/redesign3: 139/156] Add TagStore to handle tags



commit dd3e0e329307e41c18ad62d6cf9984e0eb4af4d4
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Jun 2 00:00:07 2011 +0200

    Add TagStore to handle tags

 LongoMatch/Store/Play.cs    |   67 +++++++--------------------
 LongoMatch/Store/Project.cs |   55 +++++++---------------
 LongoMatch/Store/Tag.cs     |  105 ++++++++++++++++++++++++++++++-------------
 3 files changed, 108 insertions(+), 119 deletions(-)
---
diff --git a/LongoMatch/Store/Play.cs b/LongoMatch/Store/Play.cs
index 5e02666..50d7262 100644
--- a/LongoMatch/Store/Play.cs
+++ b/LongoMatch/Store/Play.cs
@@ -39,7 +39,9 @@ namespace LongoMatch.Store
 		#region Constructors
 		public Play() {
 			Drawings = new DrawingsList();
-			Tags = new List<Tag>();
+			Tags = new StringTagStore();
+			Players = new PlayersTagStore(); 
+			Teams = new TeamsTagStore();
 		}
 		#endregion
 
@@ -162,10 +164,21 @@ namespace LongoMatch.Store
 		//// <summary>
 		/// Play's tags
 		/// </summary>
-		public List<Tag> Tags {
+		public StringTagStore Tags {
 			get;
 			set;
 		}
+		
+		public PlayersTagStore Players {
+			get;
+			set;
+		}
+		
+		public TeamsTagStore Teams {
+			get;
+			set;
+		}
+		
 		#endregion
 
 		#region Public methods
@@ -181,58 +194,12 @@ namespace LongoMatch.Store
 		public bool HasFrame(int frame) {
 			return (frame>=StartFrame && frame<StopFrame);
 		}
-
-		/// <summary>
-		/// Adds a new tag to the play
-		/// </summary>
-		/// <param name="tag">
-		/// A <see cref="Tag"/>: the tag to add
-		/// </param>
-		public void AddTag(Tag tag) {
-			if(!Tags.Contains(tag))
-				Tags.Add(tag);
-		}
-
-		/// <summary>
-		/// Removes a tag to the play
-		/// </summary>
-		/// <param name="tag">
-		/// A <see cref="Tag"/>: the tag to remove
-		/// </param>
-		public void RemoveTag(Tag tag) {
-			if(Tags.Contains(tag))
-				Tags.Remove(tag);
-		}
-
-		/// <summary>
-		/// Return True if the play contains a similar tag
-		/// </summary>
-		/// <param name="name">
-		/// A <see cref="String"/> with the tag name
-		/// </param>
-		/// <param name="val">
-		/// A <see cref="System.Object"/> with tag value
-		/// </param>
-		/// <returns>
-		/// A <see cref="System.Boolean"/>
-		/// </returns>
-		public bool HasTag(String name, object val) {
-			return (from tag in Tags
-			        where(tag.Name == (string)name) && (tag.Value == val)
-			        select tag).Count() > 0;
-		}
-
+		
 		public override string ToString()
 		{
-			String[] tags = new String[Tags.Count];
-
-			for(int i=0; i<Tags.Count; i++)
-				tags[i] = Tags[i].Value.ToString();
-
 			return  "<b>"+Catalog.GetString("Name")+": </b>"+Name+"\n"+
 			        "<b>"+Catalog.GetString("Start")+": </b>"+Start.ToMSecondsString()+"\n"+
-			        "<b>"+Catalog.GetString("Stop")+": </b>"+Stop.ToMSecondsString()+"\n"+
-			        "<b>"+Catalog.GetString("Tags")+": </b>"+ String.Join(" ; ", tags);
+			        "<b>"+Catalog.GetString("Stop")+": </b>"+Stop.ToMSecondsString();
 		}
 		#endregion
 	}
diff --git a/LongoMatch/Store/Project.cs b/LongoMatch/Store/Project.cs
index 8b46da3..82e5512 100644
--- a/LongoMatch/Store/Project.cs
+++ b/LongoMatch/Store/Project.cs
@@ -51,7 +51,6 @@ namespace LongoMatch.Store
 
 		private List<Play> playsList;
 
-
 		#region Constructors
 		public Project() {
 			playsList = new List<Play>();
@@ -189,13 +188,6 @@ namespace LongoMatch.Store
 			        select play).ToList();
 		}
 
-		public List<Tag> Tags {
-			get {
-				/* FIXME: Fix that when I have decide what to do with tags*/
-				return new List<Tag>();
-			}
-		}
-
 		/// <summary>
 		/// Returns a <see cref="Gtk.TreeStore"/> in which project categories are
 		/// root nodes and their respectives plays child nodes
@@ -260,38 +252,27 @@ namespace LongoMatch.Store
 		#endregion
 
 		#region Private Methods
-		private void  FillList<T>(List<T> options, String tagName, TreeStore store) {
-			foreach(var tagValue in options) {
-				/* Add a root in the tree with the option name */
-				var iter = store.AppendValues(tagName);
-				var queryByTag =
-				        (from play in playsList
-				         where play.HasTag(tagName, tagValue) == true
-				         select play);
-				/* Then add as children of the Player in the tree */
-				foreach(Play play in queryByTag)
-					store.AppendValues(iter, play);
-			}
-		}
-
-		private TreeStore GetSubCategoryModel(TagSubCategory subcat) {
-			TreeStore dataFileListStore = new TreeStore(typeof(object));
-
-			FillList(subcat.ToList(), subcat.Name, dataFileListStore);
-			return dataFileListStore;
-		}
-
-		private TreeStore GetSubCategoryModel(PlayerSubCategory subcat) {
-			TreeStore dataFileListStore = new TreeStore(typeof(object));
+		private TreeStore GetPlayersModel(Team team) {
+			Dictionary<Player, TreeIter> dict = new Dictionary<Player, TreeIter>();
+			TreeStore store = new TreeStore(typeof(object));
 			TeamTemplate template;
 
-			foreach(Team team in subcat) {
-				if(team == Team.NONE)
-					continue;
-				template = team == Team.LOCAL?LocalTeamTemplate:VisitorTeamTemplate;
-				FillList(template, subcat.Name, dataFileListStore);
+			if(team == Team.NONE)
+				return store;
+			
+			template = team == Team.LOCAL?LocalTeamTemplate:VisitorTeamTemplate;
+			
+			foreach(var player in template) {
+				/* Add a root in the tree with the option name */
+				var iter = store.AppendValues(player);
+				dict.Add(player, iter);
 			}
-			return dataFileListStore;
+			
+			foreach (var play in playsList) {
+				foreach (var player in play.Players.AllUniqueElements)
+					store.AppendValues(dict[player], new object[1] {play});
+			}
+			return store;
 		}
 		#endregion
 	}
diff --git a/LongoMatch/Store/Tag.cs b/LongoMatch/Store/Tag.cs
index 0f6ccc2..40afe49 100644
--- a/LongoMatch/Store/Tag.cs
+++ b/LongoMatch/Store/Tag.cs
@@ -17,66 +17,107 @@
 //
 
 using System;
+using System.Linq;
+using System.Collections.Generic;
+
 using LongoMatch.Common;
+using LongoMatch.Interfaces;
 
 namespace LongoMatch.Store
 {
 
 	[Serializable]
-	public class Tag
+	public class TagsStore<T, W> where T:ISubCategory
 	{
-		public Tag() {
+		public TagsStore(){
+			Tags = new Dictionary<T, List<W>>();
 		}
-
-		public string Name {
+		
+		private Dictionary<T, List<W>> Tags {
 			get;
 			set;
 		}
-
-		public object Value {
-			get;
-			set;
+		
+		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 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 bool Contains(T subCategory) {
+			return (Tags.ContainsKey(subCategory));
 		}
+		
+		public bool Contains(T subCategory, W tag) {
+			return (Contains(subCategory) && Tags[subCategory].Contains(tag));
+		}
+		
+		public List<W> AllUniqueElements {
+			get {
+				return (from list in Tags.Values
+				        from player in list
+				        group player by player into g
+				        select g.Key).ToList();
+			}
+		}
+		
+		public List<W> GetTags(T subCategory) {
+			if (!Tags.ContainsKey(subCategory)) {
+				Log.Warning("Trying to get the tags of an unknow subcategory");
+				return new List<W>();
+			}
+			return Tags[subCategory];			
+		}
+
 	}
+	
+	
+	public class StringTagStore: TagsStore<TagSubCategory, StringTag> {}
+	
+	public class PlayersTagStore: TagsStore<PlayerSubCategory, Player> {}
+	
+	public class TeamsTagStore: TagsStore<PlayerSubCategory, Team> {}
 
+	
 	[Serializable]
-	public class StringTag
+	public class Tag<T>
 	{
-		public StringTag() {
-		}
-
-		public string Name {
-			get;
-			set;
+		public Tag() {
 		}
-
-		public string Value {
+		
+		public T Value {
 			get;
 			set;
 		}
 	}
 
 	[Serializable]
-	public class PlayerTag:Tag
+	public class StringTag: Tag<string>
 	{
-		public PlayerTag() {
-		}
-
-		public new Player Value {
-			get;
-			set;
-		}
+		public StringTag() {}
 	}
 
 	[Serializable]
-	public class TeamTag:Tag
+	public class PlayerTag: Tag<Player>
 	{
-		public TeamTag() {
-		}
+		public PlayerTag() {}
+	}
 
-		public new Team Value {
-			get;
-			set;
-		}
+	[Serializable]
+	public class TeamTag: Tag<Team>
+	{
+		public TeamTag() {}
 	}
 }



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