/// <summary>
        /// Show the menu (modally) and return the type of the command that was chosen
        /// </summary>
        /// <param name="commandTypes">command types to include in the menu</param>
        /// <param name="point">point to show the menu at</param>
        /// <param name="parentControl">parent control for menu</param>
        /// <param name="menuTextParams">substitution parameters for the menu text</param>
        /// <returns>Type of the command that was chosen (null if no command chosen)</returns>
        public static Type ShowModal(CommandManager commandManager, Control parentWindow, Point position, Type[] commandTypes)
        {
            //	Dynamically construct the collection of commands to be shown.
            CommandCollection commandCollection = new CommandCollection();

            foreach (Type commandType in commandTypes)
            {
                // verify that the type is correct
                if (!typeof(Command).IsAssignableFrom(commandType))
                {
                    throw new ArgumentException(
                              "Type passed is not a subclass of Command!");
                }

                // create an instance of the command and add it to the collection of commands to be shown.
                Command command = Activator.CreateInstance(commandType) as Command;
                commandCollection.Add(command);
            }

            //	Add the commands to the system command manager.
            commandManager.Add(commandCollection);

            //	Show the context menu modally.
            Command commandSelected = ShowModal(parentWindow, position, commandCollection, false);
            Type    type            = commandSelected == null ? null : commandSelected.GetType();

            commandSelected = null;

            //	Remove the commands from the system command manager.
            commandManager.Remove(commandCollection);

            //	Done!
            return(type);
        }
Пример #2
0
            /// <summary>
            /// Adds a command instance.
            /// </summary>
            /// <param name="command">The command instance to add.</param>
            public void Add(Command command)
            {
                //	Ensure that the command instance has not already been added.
                Debug.Assert(!commandInstanceCollection.Contains(command), String.Format(CultureInfo.InvariantCulture, "Command instance {0} already added.", command.Identifier));

                //	Add the command instance.
                if (!commandInstanceCollection.Contains(command))
                {
                    commandInstanceCollection.Add(command);
                }
            }
Пример #3
0
        /// <summary>
        /// Sets the specified array as the items of the collection.
        /// </summary>
        /// <param name="editValue">The collection to edit.</param>
        /// <param name="value">An array of objects to set as the collection items.</param>
        /// <returns>The newly created collection object or, otherwise, the collection indicated by the editValue parameter.</returns>
        protected override object SetItems(object editValue, object[] value)
        {
            CommandCollection commandCollection = (CommandCollection)editValue;

            commandCollection.Clear();
            foreach (Command command in value)
            {
                commandCollection.Add(command);
            }
            return(commandCollection);
        }
        /// <summary>
        /// Adds a command to the CommandContextManager.
        /// </summary>
        /// <param name="command">The Command to add.</param>
        /// <param name="commandContext">The context in which the command is added to the CommandManager.</param>
        public void AddCommand(Command command, CommandContext commandContext)
        {
            //	Ensure that the command is not null.
            Debug.Assert(command != null, "Command cannot be null");
            if (command == null)
            {
                return;
            }

            //	Ensure the the command has not already been added.
            if (commandContextTable.Contains(command))
            {
                Debug.Fail("Command " + command.Identifier + " was already added.");
                return;
            }

            //	Handle the command, adding it to the appropriate command collection.
            switch (commandContext)
            {
            //	Normal commands.
            case CommandContext.Normal:
                normalCommandCollection.Add(command);
                commandManager.Add(command);
                break;

            //	Activated commands.
            case CommandContext.Activated:
                activatedCommandCollection.Add(command);
                if (activated)
                {
                    commandManager.Add(command);
                }
                break;

            //	Entered commands.
            case CommandContext.Entered:
                enteredCommandCollection.Add(command);
                if (entered)
                {
                    commandManager.Add(command);
                }
                break;

            //	Can't happen.
            default:
                Debug.Fail("Unknown CommandContext");
                return;
            }

            //	Add the command to the command context table.
            commandContextTable[command] = commandContext;
        }
        /// <summary>
        /// Show the menu (modally) and return the type of the command that was chosen
        /// </summary>
        /// <param name="commandTypes">command types to include in the menu</param>
        /// <param name="point">point to show the menu at</param>
        /// <param name="parentControl">parent control for menu</param>
        /// <param name="menuTextParams">substitution parameters for the menu text</param>
        /// <returns>Type of the command that was chosen (null if no command chosen)</returns>
        public static Type ShowModal(CommandManager commandManager, Control parentWindow, Point position, Type[] commandTypes)
        {
            //	Dynamically construct the collection of commands to be shown.
            CommandCollection commandCollection = new CommandCollection();
            foreach (Type commandType in commandTypes)
            {
                // verify that the type is correct
                if (!typeof(Command).IsAssignableFrom(commandType))
                    throw new ArgumentException(
                        "Type passed is not a subclass of Command!");

                // create an instance of the command and add it to the collection of commands to be shown.
                Command command = Activator.CreateInstance(commandType) as Command;
                commandCollection.Add(command);
            }

            //	Add the commands to the system command manager.
            commandManager.Add(commandCollection);

            //	Show the context menu modally.
            Command commandSelected = ShowModal(parentWindow, position, commandCollection, false);
            Type type = commandSelected == null ? null : commandSelected.GetType();
            commandSelected = null;

            //	Remove the commands from the system command manager.
            commandManager.Remove(commandCollection);

            //	Done!
            return type;
        }