Exemple #1
0
        /// <summary>
        /// Performs matching based on the algorithm described in <see cref="GroupHint"/>'s class summary.
        /// </summary>
        public int MatchScore(GroupHint other)
        {
            int i = 0;

            // the group "" is considered the default, and all group hints are considered
            // to be a match for the default group, so the score is 1.
            if (other.Components[0] == "")
            {
                return(1);
            }

            foreach (string otherComponent in other.Components)
            {
                if (_components.Length <= i)
                {
                    if (i > 0) //at least one component the same, and "other" has more components.
                    {
                        i = -i;
                    }

                    break;
                }

                if (otherComponent == _components[i])
                {
                    ++i;
                }
                else if (i > 0)         //at least one component the same
                {
                    //Use alphabetical comparison to determine sign.
                    var compare = String.Compare(_components[i], otherComponent, true);
                    if (compare < 0)
                    {
                        i = -i;
                    }
                    break;
                }
                else
                {
                    break;
                }
            }

            // if there were any matching components, the score is increased by 1 (because
            // the 'default' score is 1.  If the 'other component' is not a default component
            // then the score remains at 0, because there are no matches.
            if (i > 0)
            {
                ++i;
            }
            else if (i < 0)
            {
                --i;
            }

            return(i);
        }
        /// <summary>
        /// Appends the specified action to the specified XML action model.  The "group-hint"
        /// attribute of the action to be inserted is compared with the "group-hint" of the
        /// actions in the xml model and an appropriate place to insert the action is determined
        /// based on the MatchScore method of the <see cref="GroupHint"/>.
        /// </summary>
        /// <param name="xmlActionModel">the "action-model" node to insert an action into</param>
        /// <param name="action">the action to be inserted</param>
        /// <returns>a boolean indicating whether anything was added/removed/modified</returns>
        private static bool AppendActionToXmlModel(XmlDocument document, XmlElement xmlActionModel, IAction action)
        {
            if (null != FindXmlAction(xmlActionModel, action))
            {
                return(false);
            }

            XmlNode insertionPoint    = null;
            bool    insertBefore      = false;
            int     currentGroupScore = 0;

            foreach (XmlElement xmlAction in xmlActionModel.GetElementsByTagName("action"))
            {
                string hint      = xmlAction.GetAttribute("group-hint");
                var    groupHint = new GroupHint(hint);

                int groupScore = action.GroupHint.MatchScore(groupHint);
                if (groupScore > 0 && groupScore >= Math.Abs(currentGroupScore))
                {
                    //"greater than" current score
                    insertionPoint    = xmlAction;
                    currentGroupScore = groupScore;
                    insertBefore      = false;
                }
                else if (groupScore < 0 && Math.Abs(groupScore) > Math.Abs(currentGroupScore))
                {
                    //"less than"
                    insertionPoint    = xmlAction;
                    currentGroupScore = groupScore;
                    insertBefore      = true;
                }
            }

            XmlElement newXmlAction = CreateXmlAction(document, action);

            if (insertionPoint != null)
            {
                if (insertBefore)
                {
                    xmlActionModel.InsertBefore(newXmlAction, insertionPoint);
                }
                else
                {
                    xmlActionModel.InsertAfter(newXmlAction, insertionPoint);
                }
            }
            else
            {
                xmlActionModel.AppendChild(newXmlAction);
            }

            return(true);
        }
Exemple #3
0
        private AbstractAction(string id, string path, IResourceResolver resourceResolver)
        {
            Platform.CheckForEmptyString(id, "id");
            Platform.CheckForEmptyString(path, "path");

            _resourceResolver = resourceResolver;
            _actionId         = id;
            _formerActionIds  = new List <string>();
            _path             = new ActionPath(path, resourceResolver);
            _groupHint        = new GroupHint(string.Empty);
            _label            = string.Empty;
            _tooltip          = string.Empty;
            _iconSet          = null;
            _available        = true;
            _permissible      = false;
        }
Exemple #4
0
        private AbstractAction(IAction concreteAction)
        {
            Platform.CheckForNullReference(concreteAction, "concreteAction");
            Platform.CheckTrue(concreteAction.Persistent, "Action must be persistent.");

            _resourceResolver = concreteAction.ResourceResolver;
            _actionId         = concreteAction.ActionID;
            _formerActionIds  = new List <string>(concreteAction.FormerActionIDs);
            _path             = new ActionPath(concreteAction.Path.ToString(), concreteAction.ResourceResolver);
            _groupHint        = new GroupHint(concreteAction.GroupHint.Hint);
            _label            = concreteAction.Label;
            _tooltip          = concreteAction.Tooltip;
            _iconSet          = concreteAction.IconSet;
            _available        = concreteAction.Available;
            _permissible      = concreteAction.Permissible;
        }