コード例 #1
0
ファイル: MenuDriver.cs プロジェクト: victorsamun/NSimulator
 /// <summary>
 ///   Инициализация драйвера меню.
 /// </summary>
 public MenuDriver()
 {
     this.rootContext = new MenuContext ();
     this.current = this.rootContext;
 }
コード例 #2
0
ファイル: MenuDriver.cs プロジェクト: victorsamun/NSimulator
 /// <inheritdoc />
 public void ToRoot()
 {
     this.current = this.rootContext;
 }
コード例 #3
0
ファイル: MenuDriver.cs プロジェクト: victorsamun/NSimulator
        /// <inheritdoc />
        public void SetRootContext(IMenuContext context)
        {
            if (context == null)
                throw new ArgumentNullException ("context");

            if (!(context is MenuContext))
                throw new InvalidCastException ();

            this.rootContext = context as MenuContext;
            this.current = this.rootContext;
        }
コード例 #4
0
ファイル: MenuDriver.cs プロジェクト: victorsamun/NSimulator
        /// <inheritdoc />
        public void ToParent()
        {
            if (this.current == this.rootContext)
                return;

            this.current = this.current.Parent as MenuContext;
        }
コード例 #5
0
ファイル: MenuDriver.cs プロジェクト: victorsamun/NSimulator
        /// <summary>
        ///   Исполняет команду, указанную в строке.
        /// </summary>
        /// <remarks>
        ///   При исполнении команды происходит автодополнение контекстов и команды.
        /// </remarks>
        /// <param name = "command">Команда, которую нужно исполнить.</param>
        /// <exception cref = "ArgumentNullException"><paramref name = "command" /> является <c>null</c>.</exception>
        /// <exception cref = "MenuCommandNotFoundException">Команда не найдена.</exception>
        /// <exception cref = "MenuElementNameIsAmbiguousException">Название элемента меню неоднозначно.</exception>
        /// <exception cref = "NotEnoughParametersException">Недостаточно параметров контекста.</exception>
        public void Invoke(string command)
        {
            if (command == null)
                throw new ArgumentNullException ("command");

            var tokens = new Queue <string> (command.Split (new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

            var context = this.current;

            var context_params = new Dictionary <MenuContext, List <string>> ();

            while (tokens.Count != 0) {
                var token = tokens.Dequeue ();

                if (token.Equals (COMMAND_PARENT)) {
                    context = (context.Parent ?? this.rootContext) as MenuContext;
                    continue;
                }

                if (token.Equals (COMMAND_ROOT)) {
                    context = this.rootContext;
                    continue;
                }

                var completion = Completion (context, token);
                var count = completion.Count (_ => _.Name == token);

                if (completion.Count () == 0)
                    throw new MenuCommandNotFoundException (token, context.Name);

                if (count == 1)
                    completion = completion.Where (_ => _.Name == token);
                else if (completion.Count () > 1)
                    throw new MenuElementNameIsAmbiguousException (token, context.Name);

                var elem = completion.First ();

                if (elem is IMenuContextView) {
                    context = elem as MenuContext;
                    if (context.ParametersCount > tokens.Count)
                        throw new NotEnoughParametersException (context.Name, tokens.Count, context.ParametersCount);

                    var parameters = new List <string> ();
                    for (var i = 0; i < context.ParametersCount; ++ i)
                        parameters.Add (tokens.Dequeue ());
                    if (!context_params.ContainsKey (context))
                        context_params.Add (context, parameters);
                    else
                        context_params [context] = parameters;

                    continue;
                }

                if (elem is IMenuCommandView) {
                    this.current = context;
                    foreach (var param in context_params) {
                        for (var i = 0; i < param.Value.Count; ++i)
                            param.Key [i] = param.Value [i];
                    }

                    (elem as MenuCommand).Invoke (tokens.ToArray ());
                    break;
                }
            }

            this.current = context;
            foreach (var param in context_params) {
                for (var i = 0; i < param.Value.Count; ++i)
                    param.Key [i] = param.Value [i];
            }
        }