예제 #1
0
        /// <summary>
        /// Replaces handler with a new one in the given event delegate.
        /// </summary>
        /// <param name="agentConfig">Agent on which the event list should exist.</param>
        /// <param name="eventPropertyInfo">Property info to the event list on the agent config.</param>
        /// <param name="oldHandler">Old handler to remove.</param>
        /// <param name="newHandler">New handler to add.</param>
        /// <returns>Whether the old could be removed (and the new could be added).</returns>
        public static bool ReplaceHandler([NotNull] this IAgentConfig agentConfig,
                                          [NotNull] PropertyInfo eventPropertyInfo,
                                          [NotNull] IHandler oldHandler, [NotNull] IHandler newHandler)
        {
            var result = agentConfig.RemoveHandler(eventPropertyInfo, oldHandler.GetType());

            if (result)
            {
                agentConfig.AddHandler(eventPropertyInfo, newHandler);
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Adds an entry to the tree view. Also adds it to the internal collection.
        /// </summary>
        /// <param name="eventProperty">Identifier for the event. Using the <see cref="PropertyInfo" /> we can review the type on which it's declared and the given property name for the event name.</param>
        /// <param name="handler">The actual handler for this tree node.</param>
        /// <exception cref="ArgumentException">When a handler already exist for the given event.</exception>
        public void AddEntry([NotNull] PropertyInfo eventProperty, [NotNull] IHandler handler)
        {
            Debug.Assert(eventProperty.DeclaringType != null, "Declaring type must never be null");
            Debug.Assert(eventProperty.IsValidHandlerPropertyType(),
                         $"Parameter {nameof(eventProperty)} must be of type IList<IAgentConfig>.");

            IAgentConfig agentConfig = null;
            TreeNode     groupNode   = null;
            TreeNode     eventNode   = null;
            TreeNode     handlerNode = null;

            // do group search / create
            // TODO Replace is dirty
            var groupName = eventProperty.DeclaringType.Name.Replace("AgentConfig", "");

            foreach (TreeNode node in treeViewEntries.Nodes)
            {
                if (node.Name == groupName)
                {
                    groupNode   = node;
                    agentConfig = _agentConfigs.Single(x => x.GetType() == eventProperty.DeclaringType);
                    break;
                }
            }

            if (groupNode == null)
            {
                groupNode = new TreeNode(groupName)
                {
                    Name = groupName
                };
                treeViewEntries.Nodes.Add(groupNode);
                agentConfig = _agentConfigs.SingleOrDefault(x => x.GetType() == eventProperty.DeclaringType);
                if (agentConfig == null)
                {
                    agentConfig = (IAgentConfig)Activator.CreateInstance(eventProperty.DeclaringType);
                    _agentConfigs.Add(agentConfig);
                }
            }

            // do event search / create
            var eventName = eventProperty.Name;

            if (groupNode.Nodes.Count > 0)
            {
                foreach (TreeNode node in groupNode.Nodes)
                {
                    if (node.Name == eventName)
                    {
                        eventNode = node;
                        break;
                    }
                }
            }

            if (eventNode == null)
            {
                eventNode = new TreeNode(eventName)
                {
                    Name = eventName
                };

                groupNode.Nodes.Add(eventNode);
            }


            // do handler search / create
            if (groupNode.Nodes.Count > 0)
            {
                foreach (TreeNode subNode in eventNode.Nodes)
                {
                    if (subNode.Name == handler.Name)
                    {
                        // as for now the GUI limits the usage of multiple handlers of the same type for an event.
                        throw new ArgumentException(
                                  $"Handler {handler.Name} is not allowed here. It's already been added.");
                    }
                }
            }

            // add new handler node if the type does not exist already
            handlerNode = new TreeNode(handler.GetType().Name)
            {
                Name = handler.GetType().Name
            };
            eventNode.Nodes.Add(handlerNode);

            agentConfig.AddHandler(eventProperty, handler);

            saveToolStripMenuItem.Enabled   = true;
            saveAsToolStripMenuItem.Enabled = true;
        }