Beispiel #1
0
        /// <summary>
        /// Initializes from a sort-settings, which include user-sort-order specification.
        /// </summary>
        /// <param name="sortSettings">Sorting settings (per-property + user-order).</param>
        public UserOrderResourceComparer(UserOrderSortSettings sortSettings)
        {
            // Store the sorting settings
            if (sortSettings == null)
            {
                throw new ArgumentNullException("sortSettings");
            }
            _sortsettings = sortSettings;

            // Process the user sorting order if it comes along with the sort settings
            UpdateUserOrder(null, null);
            sortSettings.UserOrderChanged += new EventHandler(UpdateUserOrder);

            // Collect the prop-types
            _propTypes = new PropDataType[_sortsettings.SortProps.Length];
            for (int a = 0; a < _sortsettings.SortProps.Length; a++)
            {
                _propTypes[a] = Core.ResourceStore.PropTypes[_sortsettings.SortProps[a]].DataType;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Porcesses the given user sorting order and precaches for faster access.
        /// </summary>
        protected void UpdateUserOrder(object sender, EventArgs e)
        {
            UserOrderSortSettings ss = _sortsettings;

            if (ss == null)
            {
                _hashUserOrder = null;
                return;
            }

            lock ( ss )
            {
                // Erase the cache if no user sort order specified
                if (ss.UserOrder == null)
                {
                    _hashUserOrder = null;
                    return;
                }

                // Prepare the hash map that holds the resource IDs of the resources for which the sort order is set.
                // Due to IntHashSet implementation, two IDs picked out from the hash set can be compared by the order in which they were added, which is right the reverse order of the resources on that level.
                ss.UserOrder.ReadOrder(ref _hashUserOrder);
            }
        }
Beispiel #3
0
        public int CompareResources(IResource r1, IResource r2)
        {
            int propEquals           = 0;
            UserOrderSortSettings ss = _sortsettings;

            for (int i = 0; i < _sortsettings.SortProps.Length; i++)
            {
                // Compare against the user sort order (if that should be done before this step)
                if ((_hashUserOrder != null) &&
                    (ss != null) &&
                    (ss.ApplyUserOrderBefore == i) &&
                    ((propEquals = -_hashUserOrder.BucketComparer(r1.OriginalId, r2.OriginalId)) != 0))
                {
                    return(propEquals * (ss.UserOrderSortDirection ? 1 : -1));
                }

                // Compare the current pair of properties
                int propId = _sortsettings.SortProps[i];
                if (propId == ResourceProps.Type)
                {
                    propEquals = r1.Type.CompareTo(r2.Type);
                }
                else if (propId == ResourceProps.DisplayName)
                {
                    propEquals = String.Compare(r1.DisplayName, r2.DisplayName, true, CultureInfo.CurrentCulture);
                }
                else if (propId == ResourceProps.Id)
                {
                    propEquals = r1.Id - r2.Id;
                }
                else if (_propTypes[i] == PropDataType.Link)
                {
                    propEquals = r1.GetPropText(propId).CompareTo(r2.GetPropText(propId));
                }
                else
                {                 // Compare the property values, checking for the Null case also
                    IComparable cp;
                    object      prop1 = r1.GetProp(propId);
                    object      prop2 = r2.GetProp(propId);
                    if (prop1 == null)
                    {
                        propEquals = prop2 == null ? 0 : -1;
                    }
                    else if (prop2 == null)
                    {
                        propEquals = 1;
                    }
                    else if ((cp = prop1 as IComparable) != null)
                    {
                        propEquals = cp.CompareTo(prop2);
                    }
                }
                if (propEquals != 0)
                {
                    return(propEquals * (_sortsettings.SortDirections[i] ? 1 : -1));
                }
            }

            // If all the properties didn't rule out the sort order, check if the user order should be applied after all of them
            if ((_hashUserOrder != null) &&
                (ss != null) &&
                ((ss.ApplyUserOrderBefore < 0) || (ss.ApplyUserOrderBefore >= _sortsettings.SortProps.Length)) &&
                ((propEquals = -_hashUserOrder.BucketComparer(r1.OriginalId, r2.OriginalId)) != 0))
            {
                return(propEquals * (ss.UserOrderSortDirection ? 1 : -1));
            }

            return(0);
        }