Click here to Skip to main content
15,883,901 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.3K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using Signum.Utilities;
using Signum.Utilities.ExpressionTrees;
using Signum.Utilities.Reflection;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using Signum.Entities;

namespace Signum.Windows
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class EntityList : EntityListBase
    {
        public static readonly DependencyProperty SelectionModeProperty =
            DependencyProperty.Register("SelectionMode", typeof(SelectionMode), typeof(EntityList), new UIPropertyMetadata(SelectionMode.Single));
        public SelectionMode SelectionMode
        {
            get { return (SelectionMode)GetValue(SelectionModeProperty); }
            set { SetValue(SelectionModeProperty, value); }
        }

        public static readonly DependencyProperty MoveProperty =
            DependencyProperty.Register("Move", typeof(bool), typeof(EntityBase), new FrameworkPropertyMetadata(false, (d, e) => ((EntityList)d).UpdateVisibility()));
        public bool Move
        {
            get { return (bool)GetValue(MoveProperty); }
            set { SetValue(MoveProperty, value); }
        }

        protected override void UpdateVisibility()
        {
            btCreate.Visibility = CanCreate() ? Visibility.Visible : Visibility.Collapsed;
            btFind.Visibility = CanFind() ? Visibility.Visible : Visibility.Collapsed;
            btView.Visibility = CanView() ? Visibility.Visible : Visibility.Collapsed;
            btRemove.Visibility = CanRemove() ? Visibility.Visible : Visibility.Collapsed;
            btUp.Visibility = Move ? (CanMoveUp() ? Visibility.Visible : Visibility.Hidden) : Visibility.Collapsed;
            btDown.Visibility = Move ? (CanMoveDown() ? Visibility.Visible : Visibility.Hidden) : Visibility.Collapsed;
        }

        protected internal override DependencyProperty CommonRouteLabelText()
        {
            return null;
        }

        protected internal override DependencyProperty CommonRouteValue()
        {
            return EntitiesProperty;
        }

        protected internal override DependencyProperty CommonRouteType()
        {
            return EntitiesTypeProperty;
        }

        private bool CanMoveUp()
        {
            return Entity != null && Entities.IndexOf(Entity) > 0;
        }

        private bool CanMoveDown()
        {
            return Entity != null && Entities.IndexOf(Entity) < Entities.Count - 1;
        }
     
        public override void EntitiesChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && CleanType != null)
            {
                EntitySettings es = Navigator.Manager.EntitySettings.TryGetC(CleanType);
                if (es!= null && es.CollectionViewOperations != null)
                {
                    var colView = CollectionViewSource.GetDefaultView(e.NewValue);
                    es.CollectionViewOperations(CleanLite, colView);
                }
            }
        }
    
        public EntityList()
        {
            InitializeComponent();
        }

        protected override void btCreate_Click(object sender, RoutedEventArgs e)
        {
            object value = OnCreate();

            if (value != null)
            {
                IList list = EnsureEntities();

                if (Move)
                {
                    int index = InsertIndex();
                    list.Insert(index, value);
                }
                else
                {
                    list.Add(value);
                }

                SetEntityUserInteraction(value); 
            }
        }

        private int InsertIndex()
        {
            object entity = Entity;
            return Entity != null ? Entities.IndexOf(entity) + 1 : Entities.Count;
        }

        protected override void btFind_Click(object sender, RoutedEventArgs e)
        {
            object value = OnFinding();
            if (value == null)
                return;

            IList list = EnsureEntities();

            if (Move)
            {
                int index = InsertIndex();
                if (value is object[])
                {
                    object[] array = ((object[])value);

                    for (int i = 0; i < array.Length; i++)
                    {
                        list.Insert(index + i, array[i]);
                    }
                }
                else
                    list.Insert(index, value);

                SetEntityUserInteraction(list[index]);
            }
            else
            {
                if (value is object[])
                    foreach (var a in ((object[])value))
		                 list.Add(a);
                else
                    list.Add(value);

                SetEntityUserInteraction(list[list.Count - 1]); 
            }
        }

        protected override void btView_Click(object sender, RoutedEventArgs e)
        {
            object entity = OnViewing(this.listBox.SelectedItem, false);

            if (entity != null)
            {
                IList list = this.EnsureEntities();
                int index = this.listBox.SelectedIndex;
                list.RemoveAt(index);
                list.Insert(index, entity);
                listBox.SelectedIndex = index;
            }
        }

        private void btDown_Click(object sender, RoutedEventArgs e)
        {
            if (!CanMoveDown())
                return;

            object entity = Entity;
            int index = Entities.IndexOf(entity);
            Entities.RemoveAt(index);
            Entities.Insert(index + 1, entity);
            listBox.SelectedIndex = index + 1;
            listBox.ScrollIntoView(entity);
        }

        private void btUp_Click(object sender, RoutedEventArgs e)
        {
            if(!CanMoveUp())
                return;

            object entity = Entity;
            int index = Entities.IndexOf(entity);
            Entities.RemoveAt(index);
            Entities.Insert(index - 1, entity);
            listBox.SelectedIndex = index - 1;
            listBox.ScrollIntoView(entity);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                RemoveElements(); 
                e.Handled = true; 
            }
        }

        protected override void btRemove_Click(object sender, RoutedEventArgs e)
        {
            RemoveElements();
        }

        private void RemoveElements()
        {
            ArrayList list = new ArrayList(listBox.SelectedItems);
            foreach (var item in list)
            {
                if (OnRemoving(item))
                    EnsureEntities().Remove(item);
            }
        }

        private void listBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            btView_Click(sender, null);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions