Esempio n. 1
0
        /// <summary>
        /// Handles the <see cref="ButtonBase.Click"/> event for the "Add Entity" <see
        /// cref="Button"/>.</summary>
        /// <param name="sender">
        /// The <see cref="Object"/> where the event handler is attached.</param>
        /// <param name="args">
        /// A <see cref="RoutedEventArgs"/> object containing event data.</param>
        /// <remarks><para>
        /// <b>OnEntityAdd</b> shows an error message if the <see cref="ImageSection"/> does not
        /// currently define any images. Otherwise, <b>OnEntityAdd</b> displays a <see
        /// cref="Dialog.ChangeIdentifier"/> dialog, followed by a <see cref="Dialog.ChangeEntity"/>
        /// dialog, allowing the user to define a new entity. The new entity copies the properties
        /// of the first selected item in the "Entity" list view, if any; otherwise, it is created
        /// with default properties.
        /// </para><para>
        /// If the user confirmed both dialogs, <b>OnEntityAdd</b> adds the new entity to the
        /// "Entity" list view and to the <see cref="CurrentEntities"/> collection, and sets the
        /// <see cref="SectionTabItem.DataChanged"/> flag.</para></remarks>

        private void OnEntityAdd(object sender, RoutedEventArgs args)
        {
            args.Handled = true;

            // abort if there are no images
            if (MasterSection.Instance.Images.Collection.Count == 0)
            {
                MessageBox.Show(MainWindow.Instance,
                                Global.Strings.DialogImageNone, Global.Strings.TitleImageNone,
                                MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            // ask user for new entity ID
            var entities = CurrentEntities;
            var dialog   = new Dialog.ChangeIdentifier(CurrentDefaultId,
                                                       Global.Strings.TitleEntityIdEnter, entities.ContainsKey, false);

            dialog.Owner = MainWindow.Instance;
            if (dialog.ShowDialog() != true)
            {
                return;
            }

            // retrieve new entity ID
            string id = String.Intern(dialog.Identifier);

            // create new entity based on selected entity, if any
            EntityClass entity, selection = EntityList.SelectedItem as EntityClass;

            if (selection == null)
            {
                entity = EntityClass.Create(id, CurrentCategory);
            }
            else
            {
                entity    = (EntityClass)selection.Clone();
                entity.Id = id;
            }

            // let user make changes to new entity
            var entityDialog = new Dialog.ChangeEntity(entity)
            {
                Owner = MainWindow.Instance
            };

            if (entityDialog.ShowDialog() != true)
            {
                return;
            }

            // add entity to section table
            entities.Add(id, entity);

            // update list view and select new item
            EntityList.Items.Refresh();
            EntityList.SelectAndShow(entity);

            // broadcast data changes
            EnableListButtons();
            SectionTab.DataChanged = true;
        }