[longomatch/redesign: 33/96] Add a base class for templates serialization
- From: Andoni Morales Alastruey <amorales src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [longomatch/redesign: 33/96] Add a base class for templates serialization
- Date: Tue, 29 Mar 2011 18:18:31 +0000 (UTC)
commit 16011bac6ffb3d58879056eeb58aa84977a38964
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date: Tue Dec 21 22:10:43 2010 +0100
Add a base class for templates serialization
LongoMatch/DB/CategoriesTemplate.cs | 30 +++++++++++++++-
LongoMatch/DB/TagsTemplate.cs | 15 +++++++-
LongoMatch/DB/TeamTemplate.cs | 27 ++++----------
LongoMatch/DB/Template.cs | 43 ++++++++++++++++++++++
LongoMatch/Gui/Component/ProjectDetailsWidget.cs | 8 ++--
LongoMatch/Gui/Dialog/TemplatesEditor.cs | 4 +-
LongoMatch/LongoMatch.mdp | 1 +
LongoMatch/Main.cs | 2 +-
8 files changed, 102 insertions(+), 28 deletions(-)
---
diff --git a/LongoMatch/DB/CategoriesTemplate.cs b/LongoMatch/DB/CategoriesTemplate.cs
index 9685b8c..e708708 100644
--- a/LongoMatch/DB/CategoriesTemplate.cs
+++ b/LongoMatch/DB/CategoriesTemplate.cs
@@ -21,6 +21,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Gdk;
+using LongoMatch.Common;
using LongoMatch.TimeNodes;
namespace LongoMatch.DB
@@ -35,7 +36,7 @@ namespace LongoMatch.DB
/// The <see cref="LongoMatch.DB.Project"/> must handle all the changes
/// </summary>
[Serializable]
- public class Categories
+ public class Categories: Template
{
private List<Category> categoriesList;
@@ -173,5 +174,32 @@ namespace LongoMatch.DB
orderby c.Position
select c.Stop).ToList();
}
+
+ public static Categories Load(string filePath) {
+ return Load<Categories>(filePath);
+ }
+
+ public static Categories DefaultTemplate() {
+ Categories defaultTemplate = new Categories();
+ defaultTemplate.FillDefaultTemplate();
+ return defaultTemplate;
+ }
+
+ private void FillDefaultTemplate() {
+ Color c = new Color((Byte)255, (Byte)0, (Byte)0);
+ HotKey h = new HotKey();
+
+ for (int i=1; i<=20;i++) {
+ AddCategory(new Category{
+ Name = "Category " + i,
+ Color = c,
+ Start = new Time{Seconds = 10},
+ Stop = new Time {Seconds = 10},
+ SortMethod = SortMethodType.SortByStartTime,
+ HotKey = h,
+ Position = i-1,
+ });
+ }
+ }
}
}
diff --git a/LongoMatch/DB/TagsTemplate.cs b/LongoMatch/DB/TagsTemplate.cs
index aa2ad90..4273084 100644
--- a/LongoMatch/DB/TagsTemplate.cs
+++ b/LongoMatch/DB/TagsTemplate.cs
@@ -24,7 +24,7 @@ namespace LongoMatch.DB
{
[Serializable]
- public class TagsTemplate
+ public class TagsTemplate: Template
{
List<Tag> tagsList;
public TagsTemplate()
@@ -55,5 +55,18 @@ namespace LongoMatch.DB
public IEnumerator<Tag> GetEnumerator(){
return tagsList.GetEnumerator();
}
+ public static TagsTemplate DefaultTemplate() {
+ TagsTemplate defaultTemplate = new TagsTemplate();
+ defaultTemplate.FillDefaultTemplate();
+ return defaultTemplate;
+ }
+
+ public static TagsTemplate Load(string filePath) {
+ return Load<TagsTemplate>(filePath);
+ }
+
+ private void FillDefaultTemplate() {
+ //FIXME: To implement
+ }
}
}
diff --git a/LongoMatch/DB/TeamTemplate.cs b/LongoMatch/DB/TeamTemplate.cs
index 9da016d..6095b60 100644
--- a/LongoMatch/DB/TeamTemplate.cs
+++ b/LongoMatch/DB/TeamTemplate.cs
@@ -29,7 +29,7 @@ namespace LongoMatch.DB
{
[Serializable]
- public class TeamTemplate
+ public class TeamTemplate: Template
{
private List<Player> playersList;
@@ -72,28 +72,17 @@ namespace LongoMatch.DB
playersList.Add(player);
}
- public void Save(string filepath) {
- IFormatter formatter = new BinaryFormatter();
- Stream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None);
- formatter.Serialize(stream, this);
- stream.Close();
- }
-
- public static TeamTemplate LoadFromFile(string filepath) {
- IFormatter formatter = new BinaryFormatter();
- Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
- TeamTemplate obj = (TeamTemplate) formatter.Deserialize(stream);
- stream.Close();
- return obj;
- }
-
- public static TeamTemplate DefautlTemplate(int playersCount) {
+ public static TeamTemplate DefaultTemplate(int playersCount) {
TeamTemplate defaultTemplate = new TeamTemplate();
- defaultTemplate.CreateDefaultTemplate(playersCount);
+ defaultTemplate.FillDefaultTemplate(playersCount);
return defaultTemplate;
}
- private void CreateDefaultTemplate(int playersCount) {
+ public static TeamTemplate Load(string filePath) {
+ return Load<TeamTemplate>(filePath);
+ }
+
+ private void FillDefaultTemplate(int playersCount) {
for (int i=1; i<=playersCount;i++) {
playersList.Add(new Player{
Name = "Player " + i,
diff --git a/LongoMatch/DB/Template.cs b/LongoMatch/DB/Template.cs
new file mode 100644
index 0000000..5e8d6f9
--- /dev/null
+++ b/LongoMatch/DB/Template.cs
@@ -0,0 +1,43 @@
+//
+// Copyright (C) 2010 Andoni Morales Alastruey
+//
+// 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 System.IO;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+
+namespace LongoMatch.DB
+{
+ public abstract class Template
+ {
+ public void Save(string filepath) {
+ IFormatter formatter = new BinaryFormatter();
+ Stream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None);
+ formatter.Serialize(stream, this);
+ stream.Close();
+ }
+
+ protected static T Load<T>(string filepath) {
+ IFormatter formatter = new BinaryFormatter();
+ Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
+ var obj = formatter.Deserialize(stream);
+ stream.Close();
+ return (T)obj;
+ }
+ }
+}
+
diff --git a/LongoMatch/Gui/Component/ProjectDetailsWidget.cs b/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
index bad702e..8a8b77a 100644
--- a/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
+++ b/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
@@ -437,9 +437,9 @@ namespace LongoMatch.Gui.Component
}
localcombobox.Active = index;
visitorcombobox.Active = index;
- LocalTeamTemplate = TeamTemplate.LoadFromFile(System.IO.Path.Combine(MainClass.TemplatesDir(),
+ LocalTeamTemplate = TeamTemplate.Load(System.IO.Path.Combine(MainClass.TemplatesDir(),
LocalTeamTemplateFile));
- VisitorTeamTemplate = TeamTemplate.LoadFromFile(System.IO.Path.Combine(MainClass.TemplatesDir(),
+ VisitorTeamTemplate = TeamTemplate.Load(System.IO.Path.Combine(MainClass.TemplatesDir(),
VisitorTeamTemplateFile));
}
@@ -545,14 +545,14 @@ namespace LongoMatch.Gui.Component
protected virtual void OnVisitorcomboboxChanged(object sender, System.EventArgs e)
{
- VisitorTeamTemplate = TeamTemplate.LoadFromFile(System.IO.Path.Combine(MainClass.TemplatesDir(),
+ VisitorTeamTemplate = TeamTemplate.Load(System.IO.Path.Combine(MainClass.TemplatesDir(),
VisitorTeamTemplateFile));
}
protected virtual void OnLocalcomboboxChanged(object sender, System.EventArgs e)
{
- LocalTeamTemplate = TeamTemplate.LoadFromFile(System.IO.Path.Combine(MainClass.TemplatesDir(),
+ LocalTeamTemplate = TeamTemplate.Load(System.IO.Path.Combine(MainClass.TemplatesDir(),
LocalTeamTemplateFile));
}
diff --git a/LongoMatch/Gui/Dialog/TemplatesEditor.cs b/LongoMatch/Gui/Dialog/TemplatesEditor.cs
index 63b6d71..d0373bb 100644
--- a/LongoMatch/Gui/Dialog/TemplatesEditor.cs
+++ b/LongoMatch/Gui/Dialog/TemplatesEditor.cs
@@ -115,7 +115,7 @@ namespace LongoMatch.Gui.Dialog
}
private void UpdateTeamTemplate() {
- SetTeamTemplate(TeamTemplate.LoadFromFile(templateName));
+ SetTeamTemplate(TeamTemplate.Load(templateName));
SetSensitive(true);
}
@@ -219,7 +219,7 @@ namespace LongoMatch.Gui.Dialog
CategoriesWriter.CreateNewTemplate(name+fileExtension);
}
else {
- TeamTemplate tt = TeamTemplate.DefautlTemplate(count);
+ TeamTemplate tt = TeamTemplate.DefaultTemplate(count);
tt.Save(System.IO.Path.Combine(MainClass.TemplatesDir(), name+fileExtension));
}
diff --git a/LongoMatch/LongoMatch.mdp b/LongoMatch/LongoMatch.mdp
index ec36f94..5aec8df 100644
--- a/LongoMatch/LongoMatch.mdp
+++ b/LongoMatch/LongoMatch.mdp
@@ -180,6 +180,7 @@
<File subtype="Code" buildaction="Compile" name="IO/SectionsReader.cs" />
<File subtype="Code" buildaction="Compile" name="IO/SectionsWriter.cs" />
<File subtype="Code" buildaction="Compile" name="DB/CategoriesTemplate.cs" />
+ <File subtype="Code" buildaction="Compile" name="DB/Template.cs" />
</Contents>
<References>
<ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
diff --git a/LongoMatch/Main.cs b/LongoMatch/Main.cs
index 19ba188..3bbdc5e 100644
--- a/LongoMatch/Main.cs
+++ b/LongoMatch/Main.cs
@@ -146,7 +146,7 @@ namespace LongoMatch
fConfig = System.IO.Path.Combine(TemplatesDir(),"default.tem");
if (!System.IO.File.Exists(fConfig)) {
- TeamTemplate tt = TeamTemplate.DefautlTemplate(20);
+ TeamTemplate tt = TeamTemplate.DefaultTemplate(20);
tt.Save(fConfig);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]