private void InitCommands()
        {
            #region MenuStrip

            #region AddMenuEntityCommand

            _addMenuEntityCommand = new AddMenuEntityCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_currentDataType != null && _currentDataType.Equals(typeof(MenuEntity)));
                },
                //此项所属的项的Id
                GetArgumentHandler = () =>
                {
                    if (_contextData is MenuEntity)
                    {
                        MenuEntity entity = (MenuEntity)_contextData;
                        return(entity.Id);
                    }
                    else
                    {
                        //正在添加的项为顶层
                        return(null);
                    }
                }
            };

            #endregion

            #region EditMenuEntityCommand

            _editMenuEntityCommand = new EditMenuEntityCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(MenuEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.GetSelectedItem <MenuEntity>() != null));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItem <MenuEntity>()); }
            };

            #endregion

            #region DeleteMenuEntityCommand

            _deleteMenuEntityCommand = new DeleteMenuEntityCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(MenuEntity));

                    return(currentType && (_controller.GetSelectedItems <MenuEntity>().Count > 0));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItems <MenuEntity>()); }
            };

            #endregion

            #region MoveUpMenuEntityCommand

            _moveBeforeMenuEntityCommand = new MoveBeforeMenuEntityCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(MenuEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex > 0 &&
                                           _controller.GetSelectedItem <MenuEntity>() != null));
                },
                GetMenuId       = () => { return(_controller.GetSelectedItem <MenuEntity>().Id); },
                GetBeforeMenuId = () =>
                {
                    return(_controller.GetItem <MenuEntity>(
                               _controller.SelectedItemIndex - 1).Id);
                }
            };

            #endregion

            #region MoveDownMenuEntityCommand

            _moveAfterMenuEntityCommand = new MoveAfterMenuEntityCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(MenuEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex < _controller.ItemsCount - 1 &&
                                           _controller.GetSelectedItem <MenuEntity>() != null));
                },
                GetMenuId      = () => { return(_controller.GetSelectedItem <MenuEntity>().Id); },
                GetAfterMenuId = () =>
                {
                    return(_controller.GetItem <MenuEntity>(
                               _controller.SelectedItemIndex + 1).Id);
                }
            };

            #endregion

            #endregion

            #region ToolStrip

            #region Page

            _addToolStripPageCommand = new AddToolStripPageCommand();

            _editToolStripPageCommand = new EditToolStripPageCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripPageEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.GetSelectedItem <ToolStripPageEntity>() != null));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItem <ToolStripPageEntity>()); }
            };

            _removeToolStripPageCommand = new RemoveToolStripPageCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripPageEntity));

                    return(currentType && (_controller.GetSelectedItems <ToolStripPageEntity>().Count > 0));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItems <ToolStripPageEntity>()); }
            };

            _moveBeforeToolStripPageCommand = new MoveBeforeToolStripPageCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripPageEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex > 0 &&
                                           _controller.GetSelectedItem <ToolStripPageEntity>() != null));
                },
                GetId       = () => { return(_controller.GetSelectedItem <ToolStripPageEntity>().Id); },
                GetBeforeId = () =>
                {
                    return(_controller.GetItem <ToolStripPageEntity>(
                               _controller.SelectedItemIndex - 1).Id);
                }
            };

            _moveAfterToolStripPageCommand = new MoveAfterToolStripPageCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripPageEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex < _controller.ItemsCount - 1 &&
                                           _controller.GetSelectedItem <ToolStripPageEntity>() != null));
                },
                GetId      = () => { return(_controller.GetSelectedItem <ToolStripPageEntity>().Id); },
                GetAfterId = () =>
                {
                    return(_controller.GetItem <ToolStripPageEntity>(
                               _controller.SelectedItemIndex + 1).Id);
                }
            };

            #endregion

            #region Group

            _addToolStripGroupCommand = new AddToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_currentDataType != null && _currentDataType.Equals(typeof(ToolStripGroupEntity)) &&
                           _contextData != null && _contextData is ToolStripPageEntity);
                },
                //此项所属的项的Id
                GetArgumentHandler = () =>
                {
                    if (_contextData is ToolStripPageEntity)
                    {
                        ToolStripPageEntity entity = (ToolStripPageEntity)_contextData;
                        return(entity.Id);
                    }
                    else
                    {
                        return(null);
                    }
                }
            };

            _editToolStripGroupCommand = new EditToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripGroupEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.GetSelectedItem <ToolStripGroupEntity>() != null));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItem <ToolStripGroupEntity>()); }
            };

            _removeToolStripGroupCommand = new RemoveToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripGroupEntity));

                    return(currentType && (_controller.GetSelectedItems <ToolStripGroupEntity>().Count > 0));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItems <ToolStripGroupEntity>()); }
            };

            _moveBeforeToolStripGroupCommand = new MoveBeforeToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripGroupEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex > 0 &&
                                           _controller.GetSelectedItem <ToolStripGroupEntity>() != null));
                },
                GetId       = () => { return(_controller.GetSelectedItem <ToolStripGroupEntity>().Id); },
                GetBeforeId = () =>
                {
                    return(_controller.GetItem <ToolStripGroupEntity>(
                               _controller.SelectedItemIndex - 1).Id);
                }
            };

            _moveAfterToolStripGroupCommand = new MoveAfterToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripGroupEntity));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex < _controller.ItemsCount - 1 &&
                                           _controller.GetSelectedItem <ToolStripGroupEntity>() != null));
                },
                GetId      = () => { return(_controller.GetSelectedItem <ToolStripGroupEntity>().Id); },
                GetAfterId = () =>
                {
                    return(_controller.GetItem <ToolStripGroupEntity>(
                               _controller.SelectedItemIndex + 1).Id);
                }
            };

            #endregion

            #region Items

            #region AddToolStripItemCommand

            _addToolStripItemCommand = new AddToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_currentDataType != null && _currentDataType.Equals(typeof(ToolStripItemAbstract)) &&
                           _contextData != null && _contextData is ToolStripGroupEntity);
                },
                //此项所属的项的Id
                GetArgumentHandler = () =>
                {
                    if (_contextData is ToolStripGroupEntity)
                    {
                        ToolStripGroupEntity entity = (ToolStripGroupEntity)_contextData;
                        return(entity.Id);
                    }
                    else
                    {
                        return(null);
                    }
                }
            };

            #endregion

            #region EditToolStripItemCommand

            _editToolStripItemCommand = new EditToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripItemAbstract));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.GetSelectedItem <ToolStripItemAbstract>() != null));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItem <ToolStripItemAbstract>()); }
            };

            #endregion

            #region DeleteToolStripItemCommand

            _deleteToolStripItemCommand = new DeleteToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripItemAbstract));

                    return(currentType && (_controller.GetSelectedItems <ToolStripItemAbstract>().Count > 0));
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedItems <ToolStripItemAbstract>()); }
            };

            #endregion

            #region MoveBeforeToolStripItemCommand

            _moveBeforeToolStripItemCommand = new MoveBeforeToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripItemAbstract));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex > 0 &&
                                           _controller.GetSelectedItem <ToolStripItemAbstract>() != null));
                },
                GetMenuId       = () => { return(_controller.GetSelectedItem <ToolStripItemAbstract>().Id); },
                GetBeforeMenuId = () =>
                {
                    return(_controller.GetItem <ToolStripItemAbstract>(
                               _controller.SelectedItemIndex - 1).Id);
                }
            };

            #endregion

            #region MoveAfterToolStripItemCommand

            _moveAfterToolStripItemCommand = new MoveAfterToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    bool currentType = _currentDataType != null && _currentDataType.Equals(typeof(ToolStripItemAbstract));

                    return(currentType && (_controller.SelectedItemsCount == 1 &&
                                           _controller.SelectedItemIndex < _controller.ItemsCount - 1 &&
                                           _controller.GetSelectedItem <ToolStripItemAbstract>() != null));
                },
                GetMenuId      = () => { return(_controller.GetSelectedItem <ToolStripItemAbstract>().Id); },
                GetAfterMenuId = () =>
                {
                    return(_controller.GetItem <ToolStripItemAbstract>(
                               _controller.SelectedItemIndex + 1).Id);
                }
            };

            #endregion

            #endregion

            #endregion
        }
示例#2
0
        private void InitCommands()
        {
            #region Commands

            #region Page

            _addToolStripPageCommand = new AddToolStripPageCommand();

            _editToolStripPageCommand = new EditToolStripPageCommand()
            {
                GetArgumentHandler = () =>
                {
                    ToolStripPageEntity selectedItem = _controller.GetSelectedData <ToolStripPageEntity>();
                    if (selectedItem == null)
                    {
                        return(null);
                    }
                    else
                    {
                        return(selectedItem);
                    }
                }
            };

            _removeToolStripPageCommand = new RemoveToolStripPageCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripPageEntity>() != null);
                },
                GetArgumentHandler = () =>
                {
                    return(new List <ToolStripPageEntity>()
                    {
                        _controller.GetSelectedData <ToolStripPageEntity>()
                    });
                }
            };

            _moveBeforeToolStripPageCommand = new MoveBeforeToolStripPageCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripPageEntity>() != null &&
                           _controller.GetPreSelectedData <ToolStripPageEntity>() != null);
                },
                GetId       = () => { return(_controller.GetSelectedData <ToolStripPageEntity>().Id); },
                GetBeforeId = () => { return(_controller.GetPreSelectedData <ToolStripPageEntity>().Id); }
            };

            _moveAfterToolStripPageCommand = new MoveAfterToolStripPageCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripPageEntity>(true) != null &&
                           _controller.GetNextSelectedData <ToolStripPageEntity>() != null);
                },
                GetId      = () => { return(_controller.GetSelectedData <ToolStripPageEntity>().Id); },
                GetAfterId = () => { return(_controller.GetNextSelectedData <ToolStripPageEntity>().Id); }
            };

            #endregion

            #region Group

            _addToolStripGroupCommand = new AddToolStripGroupCommand()
            {
                GetArgumentHandler = () =>
                {
                    ToolStripPageEntity selectedItem = _controller.GetSelectedData <ToolStripPageEntity>();
                    if (selectedItem == null)
                    {
                        return(String.Empty);
                    }
                    else
                    {
                        return(selectedItem.Id);
                    }
                }
            };

            _editToolStripGroupCommand = new EditToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripGroupEntity>() != null);
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedData <ToolStripGroupEntity>()); }
            };

            _removeToolStripGroupCommand = new RemoveToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripGroupEntity>() != null);
                },
                GetArgumentHandler = () =>
                {
                    return(new List <ToolStripGroupEntity>()
                    {
                        _controller.GetSelectedData <ToolStripGroupEntity>()
                    });
                }
            };

            _moveBeforeToolStripGroupCommand = new MoveBeforeToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripGroupEntity>() != null &&
                           _controller.GetPreSelectedData <ToolStripGroupEntity>() != null);
                },
                GetId       = () => { return(_controller.GetSelectedData <ToolStripGroupEntity>().Id); },
                GetBeforeId = () => { return(_controller.GetPreSelectedData <ToolStripGroupEntity>().Id); }
            };

            _moveAfterToolStripGroupCommand = new MoveAfterToolStripGroupCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripGroupEntity>(true) != null &&
                           _controller.GetNextSelectedData <ToolStripGroupEntity>() != null);
                },
                GetId      = () => { return(_controller.GetSelectedData <ToolStripGroupEntity>().Id); },
                GetAfterId = () => { return(_controller.GetNextSelectedData <ToolStripGroupEntity>().Id); }
            };

            #endregion

            #region Items

            _addToolStripItemCommand = new AddToolStripItemCommand()
            {
                GetArgumentHandler = () =>
                {
                    ToolStripGroupEntity selectedItem = _controller.GetSelectedData <ToolStripGroupEntity>();
                    if (selectedItem == null)
                    {
                        return(String.Empty);
                    }
                    else
                    {
                        return(selectedItem.Id);
                    }
                }
            };

            _editToolStripItemCommand = new EditToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripItemAbstract>() != null);
                },
                GetArgumentHandler = () => { return(_controller.GetSelectedData <ToolStripItemAbstract>()); }
            };

            _deleteToolStripItemCommand = new DeleteToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripItemAbstract>() != null);
                },
                GetArgumentHandler = () =>
                {
                    return(new List <ToolStripItemAbstract>()
                    {
                        _controller.GetSelectedData <ToolStripItemAbstract>()
                    });
                }
            };

            _moveBeforeToolStripItemCommand = new MoveBeforeToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripItemAbstract>() != null &&
                           _controller.GetPreSelectedData <ToolStripItemAbstract>() != null);
                },
                GetMenuId       = () => { return(_controller.GetSelectedData <ToolStripItemAbstract>().Id); },
                GetBeforeMenuId = () => { return(_controller.GetPreSelectedData <ToolStripItemAbstract>().Id); }
            };

            _moveAfterToolStripItemCommand = new MoveAfterToolStripItemCommand()
            {
                CanExcuteHandler = () =>
                {
                    return(_controller.GetSelectedData <ToolStripItemAbstract>(true) != null &&
                           _controller.GetNextSelectedData <ToolStripItemAbstract>() != null);
                },
                GetMenuId      = () => { return(_controller.GetSelectedData <ToolStripItemAbstract>().Id); },
                GetAfterMenuId = () => { return(_controller.GetNextSelectedData <ToolStripItemAbstract>().Id); }
            };

            #endregion

            #endregion
        }