Click here to Skip to main content
15,885,985 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 Signum.Entities.DynamicQuery;
using Signum.Utilities;
using Signum.Entities;

namespace Signum.Windows
{
    public partial class QueryTokenBuilder : UserControl
    {
        public static readonly DependencyProperty TokenProperty =
              DependencyProperty.Register("Token", typeof(QueryToken), typeof(QueryTokenBuilder), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                  (d, e) =>
                  {
                      QueryTokenBuilder qtb = (QueryTokenBuilder)d;
                      if (qtb.IsLoaded)
                          qtb.UpdateTokenList((QueryToken)e.NewValue);
                  }));
        public QueryToken Token
        {
            get { return (QueryToken)GetValue(TokenProperty); }
            set { SetValue(TokenProperty, value); }
        }


        List<QueryToken> tokens = new List<QueryToken>(); 
        private void UpdateTokenList(QueryToken queryToken)
        {
            //if (tokens.LastOrDefault() == queryToken)
            //    return;

            if (queryToken == null)
                tokens = new List<QueryToken>();
            else
                tokens = queryToken.FollowC(a => a.Parent).Reverse().ToList();
            UpdateCombo();
        }

        public void UpdateTokenList()
        {
            UpdateTokenList(Token);
        }

        public event Func<QueryToken, QueryToken[]> SubTokensEvent;

        QueryToken[] OnSubTokens(QueryToken token)
        {
            if (SubTokensEvent != null)
                return SubTokensEvent(token);

            throw new InvalidOperationException("SubTokensEvent not set"); 
        }

        // StaticColumns.Select(c => QueryToken.NewColumn(c)).ToArray()

        //public static readonly DependencyProperty StaticColumnsProperty =
        //    DependencyProperty.Register("StaticColumns", typeof(IEnumerable<StaticColumn>), typeof(QueryTokenBuilder), new UIPropertyMetadata(null,
        //        (d, e) => ((QueryTokenBuilder)d).UpdateCombo()));
        //public IEnumerable<StaticColumn> StaticColumns
        //{
        //    get { return (IEnumerable<StaticColumn>)GetValue(StaticColumnsProperty); }
        //    set { SetValue(StaticColumnsProperty, value); }
        //}


        void UpdateCombo()
        {
            sp.Children.Clear(); 
            for (int i = 0; i < tokens.Count + 1; i++)
			{
                QueryToken[] subTokens = OnSubTokens(i == 0 ? null : tokens[i - 1]);

                if (i == tokens.Count && subTokens == null || subTokens.Length == 0)
                    break;

                int index = i == tokens.Count ? -1: Array.FindIndex(subTokens, a=>a.Key == tokens[i].Key);

                ComboBox cb = new ComboBox()
                {
                    Tag = i,
                    ItemsSource = subTokens,
                    SelectedIndex = index,
                };
                sp.Children.Add(cb); 
			}
        }


        void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)e.OriginalSource;
            int index = (int)cb.Tag;
            QueryToken newToken = (QueryToken)cb.SelectedItem;
            QueryToken[] subTokens = OnSubTokens(newToken);

            sp.Children.RemoveRange(index + 1, sp.Children.Count - (index + 1)); //all
            if (subTokens != null && subTokens.Length != 0)
                sp.Children.Add(new ComboBox
                {
                    Tag = index + 1,
                    ItemsSource = subTokens,
                    SelectedIndex = -1,
                });

            if (tokens.Count <= index)
                tokens.Add(newToken);
            else
            {
                tokens[index] = newToken;
                tokens.RemoveRange(index + 1, tokens.Count - (index + 1)); //all
            }

            Token = tokens.LastOrDefault();
        }

        public QueryTokenBuilder()
        {
            InitializeComponent();
            sp.AddHandler(ComboBox.SelectionChangedEvent, new SelectionChangedEventHandler(cb_SelectionChanged));
            this.Loaded += new RoutedEventHandler(QueryTokenBuilder_Loaded);
        }

        void QueryTokenBuilder_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateTokenList(); 
        }

        private void sp_DragOver(object sender, DragEventArgs e)
        {
             e.Effects = e.Data.GetDataPresent(typeof(FilterOption)) ? DragDropEffects.Copy : DragDropEffects.None;
        }

        private void sp_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(FilterOption)))
            {
                FilterOption filter = (FilterOption)e.Data.GetData(typeof(FilterOption));

                Token = filter.Token;
            }
        }
    }
}

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