00001 using System;
00002 using Foodolini.BusinessLogic;
00003 using Gtk;
00004 using System.Collections.Generic;
00005 using Foodolini.Activities;
00006
00007 namespace Foodolini.Activities.Ingredients
00008 {
00013 public partial class IngredientDialog : Gtk.Dialog
00014 {
00018 public Ingredient Ingredient { get; private set; }
00019
00023 NodeStore nutrientsStore = new NodeStore (typeof(NutrientAdapter));
00024
00028 ListStore categoryStore = new ListStore (typeof(string));
00029
00033 private string selectedCategory = string.Empty;
00037 private static string defaultName = "New ingredient";
00038
00053 public IngredientDialog (Ingredient ingredient, bool isNew, string selectedCategory)
00054 {
00055 this.Build ();
00056 this.Ingredient = ingredient;
00057 this.selectedCategory = selectedCategory;
00058 SetIngredient (isNew);
00059 }
00060
00068 public IngredientDialog (string selectedCategory) : this(new Ingredient (defaultName), true, selectedCategory)
00069 {
00070 }
00071
00078 public IngredientDialog (Ingredient ingredient) : this(ingredient, false, string.Empty)
00079 {
00080 }
00081
00082 public IngredientDialog () : this(new Ingredient (defaultName), true, string.Empty)
00083 {
00084 }
00085
00092 private void SetIngredient (bool isNew)
00093 {
00094
00095
00096 this.Title = this.Ingredient.LongDescription;
00097
00098 if (isNew)
00099 btnDelete.Sensitive = false;
00100
00101 if (!isNew)
00102 txtLongDescription.Text = this.Ingredient.LongDescription ?? string.Empty;
00103 txtShortDescription.Text = this.Ingredient.ShortDescription ?? string.Empty;
00104 txtCommercialName.Text = this.Ingredient.CommercialName ?? string.Empty;
00105 txtManufacturer.Text = this.Ingredient.Manufacturer ?? string.Empty;
00106
00107
00108 if (this.Ingredient.ShelfLife != TimeSpan.Zero) {
00109 this.sbShelfDays.Value = this.Ingredient.ShelfLife.Days;
00110 }
00111
00112
00113 if (this.Ingredient.ExpirationAfterOpening != TimeSpan.MaxValue) {
00114 this.sbOpenDays.Value = this.Ingredient.ExpirationAfterOpening.Days;
00115 }
00116
00117
00118 List<string> categories = new List<string> (Ingredient.Categories);
00119 categories.Sort ();
00120
00121 foreach (string category in categories)
00122 categoryStore.AppendValues (category);
00123
00124 cbFoodGroup.Model = categoryStore;
00125 this.cbFoodGroup.QueueResize ();
00126
00127 if (!isNew)
00128 SelectActiveCategory (this.Ingredient.Category);
00129 else
00130 SelectActiveCategory (selectedCategory);
00131
00132
00133 var nutDef = new List<Nutrient> (Nutrient.ListNutritionDefinitions ());
00134 nutDef.Sort ((n1, n2) => (n1.SortOrder.CompareTo (n2.SortOrder)));
00135
00136
00137 foreach (var n in nutDef) {
00138 double amount = 0;
00139 if (this.Ingredient.Nutrients.ContainsKey (n))
00140 amount = this.Ingredient.Nutrients[n];
00141 NutrientAdapter node = new NutrientAdapter (n, amount);
00142 this.nutrientsStore.AddNode (node);
00143 }
00144 this.nutrientsView.NodeStore = nutrientsStore;
00145
00146
00147 this.nutrientsView.AppendColumn ("Definition", new CellRendererText (), "text", 0);
00148
00149
00150 TreeViewColumn valueColumn = new TreeViewColumn ();
00151 valueColumn.Title = "Value ";
00152 CellRendererSpin valueRenderer = new CellRendererSpin ();
00153 valueRenderer.Editable = true;
00154 valueRenderer.Adjustment = new Adjustment (0.0, 0, double.MaxValue, 0.01, 0.01, 1);
00155 valueRenderer.Digits = 2;
00156 valueColumn.PackStart (valueRenderer, true);
00157 valueColumn.SetCellDataFunc (valueRenderer, (column, cell, node) => { ((CellRendererSpin)cell).Text = ((NutrientAdapter)node).Value.ToString ("0.00"); });
00158
00159
00160 valueRenderer.EditingStarted += (o, args) =>
00161 {
00162 var node = (NutrientAdapter)nutrientsStore.GetNode (new TreePath (args.Path));
00163 ((SpinButton)args.Editable).Value = node.Value;
00164 };
00165
00166 valueRenderer.Edited += (o, args) =>
00167 {
00168 var node = (NutrientAdapter)nutrientsStore.GetNode (new TreePath (args.Path));
00169 double newValue;
00170 if (double.TryParse (args.NewText, out newValue))
00171 node.Value = newValue;
00172 };
00173
00174 this.nutrientsView.AppendColumn (valueColumn);
00175 this.nutrientsView.AppendColumn ("Unit", new CellRendererText (), "text", 2);
00176 }
00177
00185 private void SelectActiveCategory (string category)
00186 {
00187 int i = 0;
00188
00189 foreach (object[] o in categoryStore) {
00190 string cat = o[0].ToString ();
00191 if (cat == category) {
00192 TreeIter it;
00193 if (categoryStore.GetIterFromString (out it, i + ""))
00194 cbFoodGroup.SetActiveIter (it);
00195 break;
00196 }
00197 i++;
00198 }
00199 }
00200
00204 public void SaveIngredient ()
00205 {
00206
00207 this.Ingredient.ShortDescription = txtShortDescription.Text;
00208 this.Ingredient.LongDescription = txtLongDescription.Text;
00209 this.Ingredient.Manufacturer = txtManufacturer.Text;
00210 this.Ingredient.CommercialName = txtCommercialName.Text;
00211
00212
00213 TimeSpan shelfLife = new TimeSpan (DateTime.MinValue.AddDays (sbShelfDays.ValueAsInt).Ticks);
00214 this.Ingredient.ShelfLife = shelfLife;
00215
00216
00217 TimeSpan expirationAfterOpening = new TimeSpan (DateTime.MinValue.AddDays (sbOpenDays.ValueAsInt).Ticks);
00218 if (expirationAfterOpening.Ticks > 0)
00219 this.Ingredient.ExpirationAfterOpening = expirationAfterOpening;
00220 else
00221 this.Ingredient.ExpirationAfterOpening = TimeSpan.MaxValue;
00222
00223
00224 TreeIter it;
00225 if (this.cbFoodGroup.GetActiveIter (out it))
00226 this.Ingredient.Category = this.categoryStore.GetValue (it, 0).ToString ();
00227
00228 foreach (NutrientAdapter node in nutrientsStore) {
00229 if (this.Ingredient.Nutrients.ContainsKey (node.Nutrient)) {
00230 this.Ingredient.Nutrients[node.Nutrient] = node.Value;
00231
00232 } else if (node.Value > 0) {
00233 this.Ingredient.Nutrients.Add (node.Nutrient, node.Value);
00234 }
00235 }
00236 this.Ingredient.Save ();
00237 }
00238
00239 protected virtual void OnNutrientsViewRowActivated (object o, Gtk.RowActivatedArgs args)
00240 {
00241 TreeIter it;
00242 nutrientsView.Selection.GetSelected (out it);
00243 }
00244 }
00245 }