00001
00002 using System;
00003 using System.Collections.Generic;
00004 using Foodolini.Activities;
00005 using Foodolini.BusinessLogic;
00006 using Gtk;
00007
00008 namespace Foodolini.Activities.Cookbook
00009 {
00010
00011 [FoodoliniActivity("Cookbook", false, "book.png")]
00012 [System.ComponentModel.ToolboxItem(true)]
00013 public partial class CookbookActivity : Gtk.Bin, IActivity
00014 {
00015 private IOwner owner;
00016 private NodeStore recipeStorage = new NodeStore(typeof(RecipeAdapter));
00017 private RecipeViewer recipeViewer = null;
00018
00019 public CookbookActivity()
00020 {
00021 this.Build();
00022
00023
00024 this.nvRecipes.NodeStore = this.recipeStorage;
00025 this.nvRecipes.AppendColumn("Recipes", new CellRendererText(), delegate(TreeViewColumn tree_column, CellRenderer cell, ITreeNode node){
00026 RecipeAdapter n = (RecipeAdapter)node;
00027 CellRendererText c = (CellRendererText)cell;
00028 c.Text = n.Title;
00029 });
00030 this.nvRecipes.AppendColumn("Meal type", new CellRendererText(), delegate(TreeViewColumn tree_column, CellRenderer cell, ITreeNode node){
00031 RecipeAdapter n = (RecipeAdapter)node;
00032 CellRendererText c = (CellRendererText)cell;
00033 c.Text = n.Type;
00034 });
00035 this.nvRecipes.AppendColumn("Rating", new CellRendererText(), delegate(TreeViewColumn tree_column, CellRenderer cell, ITreeNode node){
00036 RecipeAdapter n = (RecipeAdapter)node;
00037 CellRendererText c = (CellRendererText)cell;
00038 c.Text = n.Rating.ToString("0.00");
00039 });
00040 bxSearchRecipe.ShowAll();
00041
00042
00043 expAdvancedCriteria.Expanded = false;
00044 scExpirationDate.Sensitive = false;
00045 scRating.Sensitive = false;
00046
00047 }
00048
00052 protected virtual void OnBtnSearchClicked (object sender, System.EventArgs e)
00053 {
00054 SearchRecipes();
00055 }
00056
00060 protected virtual void OnEntSearchRecipesActivated (object sender, System.EventArgs e)
00061 {
00062 SearchRecipes();
00063 }
00064
00068 private void SearchRecipes()
00069 {
00070 this.recipeStorage.Clear();
00071 Criteria criteria = new Criteria();
00072 List<string> searchString = new List<string>();
00073
00074
00075 foreach(string keyword in entSearchRecipes.Text.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries))
00076 if(keyword.Length > 2)
00077 searchString.Add(keyword);
00078 criteria.searchString = searchString;
00079 List<Recipe> recipes = new List<Recipe>();
00080
00081
00082 criteria.Meal = Meal.None;
00083 if(cbBreakfast.Active)
00084 criteria.Meal |= Meal.Breakfast;
00085 if(cbLunch.Active)
00086 criteria.Meal |= Meal.Lunch;
00087 if(cbDinner.Active)
00088 criteria.Meal |= Meal.Dinner;
00089 if(cbOther.Active)
00090 criteria.Meal |= Meal.Other;
00091
00092
00093 if(cbRating.Active)
00094 {
00095 criteria.rating = true;
00096 criteria.ratingPriority = (int)scRating.Value;
00097 }
00098
00099
00100 if(cbExpirateDate.Active)
00101 {
00102 criteria.expirationDate = true;
00103 criteria.expirationDatePriority = (int)scExpirationDate.Value;
00104 }
00105
00106 recipes = Recipe.ListByCriteria(criteria);
00107
00108 if(recipes.Count != 0) {
00109 foreach(Recipe recipe in recipes)
00110 this.recipeStorage.AddNode(new RecipeAdapter(recipe));
00111 this.nvRecipes.CursorChanged += ViewRecipe;
00112 this.nvRecipes.RowActivated += EditRecipe;
00113 }else {
00114 this.recipeStorage.AddNode(new RecipeAdapter(new Recipe("No recipes found")));
00115 this.nvRecipes.CursorChanged -= ViewRecipe;
00116 this.nvRecipes.RowActivated -= EditRecipe;
00117 }
00118 }
00119
00120
00121
00126 protected virtual void SearchCriteriaChanged (object sender, System.EventArgs e)
00127 {
00128 scExpirationDate.Sensitive = cbExpirateDate.Active;
00129 scRating.Sensitive = cbRating.Active;
00130 }
00131
00135 void HandleOnRecipeViewed(object sender, RecipeEventArgs e)
00136 {
00137 SearchRecipes();
00138 }
00139
00143 void HandleOnRecipeCreated(object sender, RecipeEventArgs e)
00144 {
00145 this.recipeStorage.AddNode(new RecipeAdapter(e.Recipe));
00146 }
00147
00148 #region New activities
00153 protected virtual void ViewRecipe (object sender, System.EventArgs e)
00154 {
00155 if(nvRecipes.NodeSelection.SelectedNode != null){
00156 if(this.recipeViewer != null)
00157 {
00158 this.recipeViewer.OnRecipeViewed -= HandleOnRecipeViewed;
00159 this.recipeViewer.OnRecipeEdited -= HandleOnRecipeViewed;
00160 this.recipeViewer.OnRecipeDeleted -= HandleOnRecipeViewed;
00161 this.recipeViewer.Unregister();
00162 this.recipeViewer.Destroy();
00163 }
00164
00165 this.recipeViewer = new RecipeViewer();
00166 this.recipeViewer.Register(this.owner);
00167 this.recipeViewer.SetParamenters(((RecipeAdapter)nvRecipes.NodeSelection.SelectedNode).Recipe);
00168 bxCookbook.Add(this.recipeViewer);
00169
00170 this.recipeViewer.OnRecipeViewed += HandleOnRecipeViewed;
00171 this.recipeViewer.OnRecipeEdited += HandleOnRecipeViewed;
00172 this.recipeViewer.OnRecipeDeleted += HandleOnRecipeViewed;
00173
00174 bxCookbook.ShowAll();
00175 }
00176 }
00177
00181 protected virtual void OnCreateRecipeClick (object sender, System.EventArgs e)
00182 {
00183 RecipeCreater rc = this.owner.PushActivity<RecipeCreater>();
00184 rc.OnRecipeCreated += HandleOnRecipeCreated;
00185 }
00186
00190 protected virtual void EditRecipe (object o, Gtk.RowActivatedArgs args)
00191 {
00192 RecipeEditor re = this.owner.PushActivity<RecipeEditor>();
00193 re.SetParamenters(((RecipeAdapter)this.nvRecipes.NodeSelection.SelectedNode).Recipe);
00194 re.OnRecipeEdited += HandleOnRecipeViewed;
00195 }
00196
00197 #endregion
00198
00199 #region IActivity
00200
00201 public void Register(IOwner owner){
00202 this.owner = owner;
00203 }
00204
00205 public void Unregister(){
00206 this.owner = null;
00207 }
00208
00209
00210 public Widget Widget{
00211 get{
00212 return this;
00213 }
00214 }
00215 #endregion
00216
00217 }
00218 }