Exemplo n.º 1
0
        public EditRecipe()
        {
            Initialize();
            if (RecipeBooks.Current != null)
            {
                ComboBoxOfItemName.Items.AddRange(RecipeBooks.Current.IngredientsList.Select(itemChoosed => itemChoosed.Name).OrderBy(index => index).ToArray());
                ComboBoxOfItemName.AddAutoComplete(o => o.ToString());
            }
            ComboBoxOfItemName.SelectedIndexChanged += (o, e) =>
            {
                if (ComboBoxOfItemName.SelectedIndex >= 0)
                {
                    var tempItem = RecipeBooks.Current.IngredientsList.FirstOrDefault(choosed => string.Compare(choosed.Name, ComboBoxOfItemName.Text) == 0);
                    if (tempItem == null)
                    {
                        return;
                    }
                }
            };
            ComboBoxOfAmount.Items.AddRange(AmountNumbers.DefaultNumbers.Cast <object>().ToArray());
            ComboBoxOfAmount.SelectedIndex = AmountNumbers.DefaultNumbers.IndexWhere(o => o.Equals(1));
            ComboBoxOfAmount.Click        += (o, e) =>
            {
                var NewItemName = ComboBoxOfItemName.Text;
                var item        = RecipeBooks.Current.IngredientsList.FirstOrDefault(indexStr => string.Compare(indexStr.Name, NewItemName, ignoreCase: true) == 0);
                if (item == null)
                {
                    if (string.IsNullOrWhiteSpace(NewItemName))
                    {
                        MessageBox.Show("No item selected!");
                    }
                    else
                    {
                        MessageBox.Show("There is no item named '{0}'!", NewItemName);
                    }
                    return;
                }
                else if (this.ingredientsTable.Rows.Cast <DataGridViewRow>().Any(stringToCompare => string.Compare(NewItemName, stringToCompare.Cells[this.itemColumn.Index].Value.ToString(), ignoreCase: true) == 0))
                {
                    MessageBox.Show("{0} is already in the recipe!", ComboBoxOfItemName.Text);
                    return;
                }
                double quantity;
                if (!double.TryParse(ComboBoxOfAmount.Text, out quantity) || quantity <= 0)
                {
                    MessageBox.Show("'{0}' is not a valid value for amount!", ComboBoxOfAmount.Text);
                    return;
                }
                this.AddIngredient(item, quantity);
            };

            RemoveButtonColumn.CellClicked((g, e) => g.Rows.RemoveAt(e.RowIndex));

            amountColumn.CellValidating((o, e) =>
            {
                double value;
                if (!double.TryParse(e.FormattedValue.ToString(), out value) || value <= 0)
                {
                    MessageBox.Show("'{0}' is not a valid value for quantity!", e.FormattedValue.ToString());
                    e.Cancel = true;
                }
            });

            createNewItem.Click += (o, e) =>
            {
                Ingredient newItem = Dialogs.Edit <ItemEditor, Ingredient>(allowSetNewName: false);
                if (newItem != null)
                {
                    ComboBoxOfItemName.Items.Add(newItem.Name);
                    ComboBoxOfItemName.SelectedIndex = ComboBoxOfItemName.Items.Count - 1;
                    ComboBoxOfItemName.SelectedIndex = ComboBoxOfItemName.Items.Cast <double>().IndexWhere(d => d == 1);
                }
            };

            this.save.Click += (o, e) =>
            {
                if (this.ShowSaveAsEditWhenReadOnly && this.ReadOnly)
                {
                    this.ReadOnly = false;
                }
                else
                {
                    this.RaiseSaveButtonClicked(this);
                }
            };

            this.adding.Click += (o, e) =>
            {
                var NewItemName = ComboBoxOfItemName.Text;
                var item        = RecipeBooks.Current.IngredientsList.FirstOrDefault(i => string.Compare(i.Name, NewItemName, ignoreCase: true) == 0);
                if (item == null)
                {
                    if (string.IsNullOrWhiteSpace(NewItemName))
                    {
                        MessageBox.Show("No item selected!");
                    }
                    else
                    {
                        MessageBox.Show("There is no item named '{0}'!", NewItemName);
                    }

                    return;
                }
                else if (ingredientsTable.Rows.Cast <DataGridViewRow>().Any(indexCellString => string.Compare(NewItemName, indexCellString.Cells[this.itemColumn.Index].Value.ToString(), ignoreCase: true) == 0))
                {
                    MessageBox.Show("{0} is already in the recipe!", ComboBoxOfItemName.Text);
                    return;
                }

                double tempAmount;
                if (!double.TryParse(ComboBoxOfAmount.Text, out tempAmount) || tempAmount <= 0)
                {
                    MessageBox.Show("'{0}' is not a valid value of amount of product!", ComboBoxOfAmount.Text);
                    return;
                }
                AddIngredient(item, tempAmount);
            };

            this.cancel.Click += (o, e) =>
            {
                if (this.ShowSaveAsEditWhenReadOnly)
                {
                    this.ReadOnly = true;
                    this.SetRecipe(this.recipe);
                }
                else
                {
                    this.RaiseCancelButtonClicked(this);
                }
            };
        }
Exemplo n.º 2
0
        private void InitializeItemsTab()
        {
            IngredientDeleteColumn.CellClicked((g, e) =>
            {
                var itemId = (Guid)g.Rows[e.RowIndex].Cells[IngredientIdColumn.Index].Value;
                var item   = RecipeBooks.Current.IngredientsList.SingleOrDefault(r => r.Id == itemId);
                if (item == null)
                {
                    MessageBox.Show("Recipe was already deleted!");
                    return;
                }

                var usages = RecipeBooks.Current.RecipesList
                             .Where(r => r.Ingredients.Any(i => i.ItemId == itemId))
                             .ToArray();
                if (usages.Length > 0)
                {
                    Errors.Alert(string.Format("Cannot delete {0} since it is currently part of {1} recipe(s) ({2})",
                                               item.Name, usages.Length, string.Join(", ", usages.Select(r => r.Name))));
                }
                else if (Errors.IsUserSure(string.Format("Delete item \"{0}\"?", g.Rows[e.RowIndex].Cells[IngredientIdColumn.Index].Value)))
                {
                    RecipeBooks.Current.Remove(item);
                    g.Rows.RemoveAt(e.RowIndex);
                }
            });

            TypedEventHandler <DataGridView, DataGridViewCellEventArgs> displayItem = (g, e) =>
            {
                if (this.itemEditor.ReadOnly)
                {
                    var itemId = (Guid)g.Rows[e.RowIndex].Cells[IngredientIdColumn.Index].Value;
                    var item   = RecipeBooks.Current.Get <Ingredient>(itemId);
                    this.itemEditor.SetItem(item);
                }
            };

            IngredientTableCreator.SelectedRowChanged(displayItem);

            createNewItem.Click += (o, e) =>
            {
                var newItem = Dialogs.Edit <EditItem, Ingredient>(allowSetNewName: false);
                if (newItem != null)
                {
                    MessageBox.Show("Error");
                    IngredientTableCreator.Rows.Add(this.CreateRow(newItem));
                }
            };

            this.itemEditor.SaveButtonClicked += ie =>
            {
                Ingredient     item;
                IList <string> errorMessages;
                if (ie.TryGetItem(out item, out errorMessages))
                {
                    if (Dialogs.SaveWithUniqueName(item, allowSetNewName: false))
                    {
                        ie.ReadOnly = true;
                    }
                }
                else
                {
                    Errors.Alert(string.Join(Environment.NewLine, errorMessages.ToArray()));
                }
            };

            TextBoxForSearchItem.TextChanged += (o, e) =>
            {
                string text = TextBoxForSearchItem.Text.ToLower();

                var matches = RecipeBooks.Current.IngredientsList
                              .Where(i => i.Name.ToLower().Contains(text))
                              .Select(i => i.Id)
                              .ToSet();

                IngredientTableCreator.Rows
                .Cast <DataGridViewRow>()
                .ForEach(r => r.Visible = matches.Contains((Guid)r.Cells[IngredientIdColumn.Index].Value));
            };

            TablesController.Selected += (o, e) =>
            {
                if (e.TabPage == TableOfItems && RecipeBooks.Current != null)
                {
                    IngredientTableCreator.Rows.Clear();
                    IngredientTableCreator.Rows.AddRange(RecipeBooks.Current.IngredientsList.OrderBy(i => i.Name).Select(this.CreateRow).ToArray());
                    if (IngredientTableCreator.Rows.Count > 0)
                    {
                        displayItem(IngredientTableCreator, new DataGridViewCellEventArgs(0, 0));
                    }
                }
            };
        }
Exemplo n.º 3
0
        private void InitializeRecipesTab()
        {
            this.deleteRecipeColumn.CellClicked((g, e) =>
            {
                var recipeId = (Guid)g.Rows[e.RowIndex].Cells[this.recipeIdColumn.Index].Value;
                var recipe   = RecipeBooks.Current.RecipesList.SingleOrDefault(r => r.Id == recipeId);
                if (recipe == null)
                {
                    MessageBox.Show("Recipe was already deleted!");
                }
                else if (Errors.IsUserSure(string.Format("Delete recipe \"{0}\"?", g.Rows[e.RowIndex].Cells[this.recipeNameColumn.Index].Value)))
                {
                    RecipeBooks.Current.Remove(recipe);
                    g.Rows.RemoveAt(e.RowIndex);
                    this.itemEditor.ShowSaveAsEditWhenReadOnly = true;
                }
            });

            TypedEventHandler <DataGridView, DataGridViewCellEventArgs> displayRecipe = (g, e) =>
            {
                if (this.recipeEditor.ReadOnly)
                {
                    var recipeId = (Guid)g.Rows[e.RowIndex].Cells[this.recipeIdColumn.Index].Value;
                    var recipe   = RecipeBooks.Current.RecipesList.Single(r => r.Id == recipeId);
                    this.recipeEditor.SetRecipe(recipe);
                }
            };

            this.recipeGrid.SelectedRowChanged(displayRecipe);

            this.createNewRecipeButton.Click += (o, e) =>
            {
                var newRecipe = Dialogs.Edit <EditRecipe, Recipe>(allowSetNewName: false);
                if (newRecipe != null)
                {
                    this.recipeGrid.Rows.Add(this.CreateRow(newRecipe));
                }
            };

            this.recipeEditor.SaveButtonClicked += re =>
            {
                Recipe         recipe;
                IList <string> errorMessages;
                if (re.TryGetRecipe(out recipe, out errorMessages))
                {
                    if (Dialogs.SaveWithUniqueName(recipe, allowSetNewName: false))
                    {
                        re.ReadOnly = true;
                    }
                }
                else
                {
                    Errors.Alert(string.Join(Environment.NewLine, errorMessages.ToArray()));
                }
            };

            TextBoxForSearchRecipe.TextChanged += (o, e) =>
            {
                var text = TextBoxForSearchRecipe.Text.ToLower();

                var matches = (from r in RecipeBooks.Current.RecipesList
                               where r.Name.ToLower().Contains(text) ||
                               r.Source.ToLower().Contains(text) ||
                               r.Steps.ToLower().Contains(text) ||
                               r.Notes.ToLower().Contains(text) ||
                               r.Ingredients.Any(i => r.RecipeBook.Get <Ingredient>(i.ItemId).Name.ToLower().Contains(text))
                               select r.Id)
                              .ToSet();

                this.recipeGrid.Rows
                .Cast <DataGridViewRow>()
                .ForEach(r => r.Visible = matches.Contains((Guid)r.Cells[this.recipeIdColumn.Index].Value));
            };

            Action refreshRecipes = () =>
            {
                if (RecipeBooks.Current != null)
                {
                    this.recipeGrid.Rows.Clear();
                    this.recipeGrid.Rows.AddRange(RecipeBooks.Current.RecipesList
                                                  .OrderBy(r => r.Name)
                                                  .Select(this.CreateRow)
                                                  .ToArray());
                    if (this.recipeGrid.Rows.Count > 0)
                    {
                        displayRecipe(this.recipeGrid, new DataGridViewCellEventArgs(0, 0));
                    }
                }
            };

            TablesController.Selected += (o, e) =>
            {
                if (e.TabPage == this.recipesTable)
                {
                    refreshRecipes();
                }
            };

            refreshRecipes();
        }
Exemplo n.º 4
0
        public RecipeEditor()
        {
            InitializeComponent();
            if (RecipeBooks.Current != null)
            {
                this.itemNameComboBox.Items.AddRange(RecipeBooks.Current.Items.Select(i => i.Name).OrderBy(n => n).ToArray());
                this.itemNameComboBox.AddAutoComplete(o => o.ToString());
            }
            this.itemNameComboBox.SelectedIndexChanged += (o, e) =>
            {
                if (this.itemNameComboBox.SelectedIndex >= 0)
                {
                    var item = RecipeBooks.Current.Items.FirstOrDefault(i => string.Compare(i.Name, this.itemNameComboBox.Text) == 0);
                    if (item == null)
                    {
                        return;
                    }
                }
            };
            this.quantityComboBox.Items.AddRange(Constants.DefaultQuantities.Cast <object>().ToArray());
            this.quantityComboBox.SelectedIndex = Constants.DefaultQuantities.IndexWhere(o => o.Equals(1));
            this.addButton.Click += (o, e) =>
            {
                var itemName = itemNameComboBox.Text;
                var item     = RecipeBooks.Current.Items.FirstOrDefault(i => string.Compare(i.Name, itemName, ignoreCase: true) == 0);
                if (item == null)
                {
                    Utils.Alert(string.IsNullOrWhiteSpace(itemName) ? "No item selected!" : string.Format("There is no item named '{0}'!", itemName));
                    this.itemNameComboBox.SelectAndFocus();
                    return;
                }
                else if (this.ingredientsTable.Rows.Cast <DataGridViewRow>().Any(r => string.Compare(itemName, r.Cells[this.itemColumn.Index].Value.ToString(), ignoreCase: true) == 0))
                {
                    Utils.Alert(string.Format("{0} is already in the recipe!", this.itemNameComboBox.Text));
                    this.itemNameComboBox.SelectAndFocus();
                    return;
                }
                double quantity;
                if (!double.TryParse(this.quantityComboBox.Text, out quantity) || quantity <= 0)
                {
                    Utils.Alert(string.Format("'{0}' is not a valid value for quantity!", this.quantityComboBox.Text));
                    this.quantityComboBox.SelectAndFocus();
                    return;
                }
                this.AddIngredient(item, quantity);
                this.itemNameComboBox.SelectAndFocus();
            };

            this.RemoveButtonColumn.CellClicked((g, e) => g.Rows.RemoveAt(e.RowIndex));
            this.QuantityColumn.CellValidating((o, e) =>
            {
                double value;
                if (!double.TryParse(e.FormattedValue.ToString(), out value) || value <= 0)
                {
                    Utils.Alert(string.Format("'{0}' is not a valid value for quantity!", e.FormattedValue));
                    e.Cancel = true;
                }
            });

            this.createNewItemButton.Click += (o, e) =>
            {
                Item newItem = Dialogs.Edit <ItemEditor, Item>(allowOverwrite: false);
                if (newItem != null)
                {
                    this.itemNameComboBox.Items.Add(newItem.Name);
                    this.itemNameComboBox.SelectedIndex = this.itemNameComboBox.Items.Count - 1;
                    this.quantityComboBox.SelectedIndex = this.quantityComboBox.Items.Cast <double>().IndexWhere(d => d == 1);
                }
            };

            this.saveButton.Click += (o, e) =>
            {
                if (this.ShowSaveAsEditWhenReadOnly && this.ReadOnly)
                {
                    this.ReadOnly = false;
                }
                else
                {
                    this.RaiseSaveButtonClicked(this);
                }
            };

            this.cancelButton.Click += (o, e) =>
            {
                if (this.ShowSaveAsEditWhenReadOnly)
                {
                    this.ReadOnly = true;
                    this.SetRecipe(this.recipe);
                }
                else
                {
                    this.RaiseCancelButtonClicked(this);
                }
            };
        }