/// <summary>
        /// 启动对象
        /// </summary>
        public void Start()
        {
            if (this.DesignMode == true)
            {
                return;
            }
            WriterCommandEventArgs args = new WriterCommandEventArgs();

            args.EditorControl = this.EditControl;
            args.Document      = this.Document;
            this.Document.FixDomState();
            foreach (object control in this._CommandTable.Keys)
            {
                string actionName = _CommandTable[control];
                if (actionName == null || actionName.Trim().Length == 0)
                {
                    continue;
                }
                actionName = actionName.Trim();
                if (_BindingEventControls.Contains(control) == false)
                {
                    BindingEvent(control);
                }
                WriterCommand cmd = this.CommandContainer.GetCommand(actionName);
                if (cmd != null)
                {
                    // 初始化命令绑定的用户界面控件
                    args.UIElement = control;
                    args.Mode      = WriterCommandEventMode.InitalizeUIElement;
                    cmd.Invoke(args);
                }
            }//foreach
            this.UpdateBindingControlStatus();
        }
Beispiel #2
0
        /// <summary>
        /// 添加动作,添加前会删除列表中相同名称的动作
        /// </summary>
        /// <param name="a">动作对象</param>
        /// <returns>动作在列表中的需要</returns>
        public int Add(WriterCommand a)
        {
            WriterCommand old = this[a.Name];

            if (old != null)
            {
                this.List.Remove(old);
            }
            //a.myDocument = this.Document;
            return(this.List.Add(a));
        }
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="commandName">命令文本</param>
        /// <param name="showUI">是否允许显示用户界面</param>
        /// <param name="parameter">用户参数</param>
        /// <returns>执行操作后的返回值</returns>
        public object ExecuteCommand(string commandName, bool showUI, object parameter)
        {
            WriterCommand cmd = this.CommandContainer.GetCommand(commandName);

            if (cmd != null)
            {
                return(InnerExecuteCommand(cmd, null, null, showUI, parameter));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 判断指定名称的命令的状态是否处于选中状态
        /// </summary>
        /// <param name="commandName">命令名称</param>
        /// <returns>该命令是否处于选中状态</returns>
        public bool IsCommandChecked(string commandName)
        {
            WriterCommand cmd = this.CommandContainer.GetCommand(commandName);

            if (cmd != null)
            {
                WriterCommandEventArgs args = new WriterCommandEventArgs(
                    this.EditControl,
                    this.Document,
                    WriterCommandEventMode.QueryState);
                args.ShowUI = true;
                cmd.Invoke(args);
                return(args.Checked);
            }
            return(false);
        }
        public void UpdateBindingControlStatus(string specifyCommandName)
        {
            try
            {
                bolIsUpdatingBindControlStatus = true;
                if (specifyCommandName != null)
                {
                    specifyCommandName = specifyCommandName.Trim();
                    if (specifyCommandName.Length == 0)
                    {
                        specifyCommandName = null;
                    }
                }

                ArrayList controls = new ArrayList(_CommandTable.Keys);
                foreach (object control in controls)
                {
                    if (IsControlDisposed(control))
                    {
                        _CommandTable.Remove(control);
                        continue;
                    }
                    string name = _CommandTable[control];

                    if (specifyCommandName != null &&
                        string.Compare(specifyCommandName, name, true) != 0)
                    {
                        continue;
                    }
                    WriterCommand cmd = this.CommandContainer.GetCommand(name);
                    if (cmd != null)
                    {
                        UpdateControlStates(control, cmd);
                    }
                }//foreach
            }
            finally
            {
                bolIsUpdatingBindControlStatus = false;
            }
        }
        /// <summary>
        /// 内部的执行控件绑定的动作对象
        /// </summary>
        /// <param name="sender">控件对象</param>
        /// <param name="args">用户界面事件参数</param>
        private void InnerExecuteControlCommand(object sender, EventArgs args)
        {
            if (sender == null)
            {
                return;
            }
            if (_ValueModifiedControls.Contains(sender))
            {
                _ValueModifiedControls.Remove(sender);
            }
            string name = _CommandTable[sender];

            if (name == null)
            {
                return;
            }
            name = name.Trim();
            if (name.Length == 0)
            {
                return;
            }
            if (this.CommandContainer == null)
            {
                return;
            }

            if (this.IsUpdatingBindControlStatus)
            {
                return;
            }

            WriterCommand cmd = this.CommandContainer.GetCommand(name);

            if (cmd == null)
            {
                return;
            }

            InnerExecuteCommand(cmd, sender, args, true, null);
        }
        private void UpdateControlStates(object control, WriterCommand command)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            WriterCommandEventArgs args = new WriterCommandEventArgs(
                this.EditControl,
                this.Document,
                WriterCommandEventMode.QueryState);

            args.UIElement = control;
            command.Invoke(args);
            if (control is System.Windows.Forms.Control)
            {
                System.Windows.Forms.Control ctl = (System.Windows.Forms.Control)control;
                if (command == null)
                {
                    ctl.Enabled = false;
                    ctl.Visible = false;
                }
                else
                {
                    ctl.Enabled = args.Enabled;
                    ctl.Visible = args.Visible;
                    if (ctl is CheckBox)
                    {
                        ((CheckBox)ctl).Checked = args.Checked;
                    }
                    else if (ctl is RadioButton)
                    {
                        ((RadioButton)ctl).Checked = args.Checked;
                    }
                }
            }
            else if (control is System.Windows.Forms.ToolStripItem)
            {
                System.Windows.Forms.ToolStripItem item =
                    (System.Windows.Forms.ToolStripItem)control;
                if (command == null)
                {
                    item.Enabled = false;
                    item.Visible = false;
                }
                else
                {
                    item.Enabled = args.Enabled;
                    item.Visible = args.Visible;
                    if (item is System.Windows.Forms.ToolStripButton)
                    {
                        ((ToolStripButton)item).Checked = args.Checked;
                    }
                    else if (item is ToolStripMenuItem)
                    {
                        ((ToolStripMenuItem)item).Checked = args.Checked;
                    }
                    else if (item is ToolStripComboBox)
                    {
                        ToolStripComboBox cbo = (ToolStripComboBox)item;
                        cbo.Text = Convert.ToString(args.Parameter);
                    }
                    else if (item is ToolStripTextBox)
                    {
                        ToolStripTextBox txt = (ToolStripTextBox)item;
                        txt.Text = Convert.ToString(args.Parameter);
                    }
                }
            }
            else if (control is MenuItem)
            {
                MenuItem item = (MenuItem)control;
                if (command == null)
                {
                    item.Enabled = false;
                    item.Visible = false;
                }
                else
                {
                    item.Enabled = args.Enabled;
                    item.Visible = args.Visible;
                    item.Checked = args.Checked;
                }
            }
            args.Mode      = WriterCommandEventMode.UpdateUIElement;
            args.UIElement = control;
            command.Invoke(args);
        }
        /// <summary>
        /// 读取控件状态
        /// </summary>
        /// <param name="commandName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool ReadControlState(string commandName, WriterCommandEventArgs args)
        {
            if (this.EditControl == null || this.EditControl.InDesignMode)
            {
                // 编辑器控件不存在获得处于设计模式则不执行后续代码
                return(false);
            }

            WriterCommand cmd = this.CommandContainer.GetCommand(commandName);

            if (cmd == null)
            {
                return(false);
            }
            bool result = false;

            foreach (object control in this._CommandTable.Keys)
            {
                if (string.Compare(_CommandTable[control], commandName, true) == 0)
                {
                    if (control is System.Windows.Forms.CheckBox)
                    {
                        args.Checked = ((System.Windows.Forms.CheckBox)control).Checked;
                        result       = true;
                    }
                    else if (control is RadioButton)
                    {
                        args.Checked = ((RadioButton)control).Checked;
                    }
                    else if (control is System.Windows.Forms.ToolStripButton)
                    {
                        args.Checked = ((System.Windows.Forms.ToolStripButton)control).Checked;
                        result       = true;
                    }
                    else if (control is System.Windows.Forms.ToolStripMenuItem)
                    {
                        args.Checked = ((System.Windows.Forms.ToolStripMenuItem)control).Checked;
                        result       = true;
                    }
                    else if (control is System.Windows.Forms.MenuItem)
                    {
                        args.Checked = ((System.Windows.Forms.MenuItem)control).Checked;
                        result       = true;
                    }
                    else if (control is TextBoxBase)
                    {
                        args.Parameter = ((TextBox)control).Text;
                        result         = true;
                    }
                    else if (control is ToolStripTextBox)
                    {
                        args.Parameter = ((ToolStripTextBox)control).Text;
                        result         = true;
                    }
                    else if (control is ToolStripComboBox)
                    {
                        args.Parameter = ((ToolStripComboBox)control).Text;
                        result         = true;
                    }
                }
            }//foreach
            return(result);
        }
        private object InnerExecuteCommand(
            WriterCommand cmd,
            object uiControl,
            EventArgs eventArgs,
            bool showUI,
            object parameter)
        {
            WriterCommandEventArgs cmdArgs = new WriterCommandEventArgs();

            cmdArgs.EditorControl = this.EditControl;
            if (this.EditControl != null)
            {
                cmdArgs.Host = this.EditControl.AppHost;
            }
            cmdArgs.Name        = cmd.Name;
            cmdArgs.Document    = this.Document;
            cmdArgs.UIElement   = uiControl;
            cmdArgs.UIEventArgs = eventArgs;
            cmdArgs.ShowUI      = showUI;
            cmdArgs.Parameter   = parameter;

            cmdArgs.Mode = WriterCommandEventMode.QueryState;

            cmd.Invoke(cmdArgs);
            this.ReadControlState(cmd.Name, cmdArgs);
            if (cmdArgs.Enabled)
            {
                _IsExecutingCommand = true;
                cmdArgs.Mode        = WriterCommandEventMode.Invoke;
                cmdArgs.Cancel      = false;
                if (this.EditControl == null)
                {
                    try
                    {
                        cmd.Invoke(cmdArgs);
                    }
                    finally
                    {
                        _IsExecutingCommand = false;
                    }
                }
                else
                {
                    try
                    {
                        cmd.Invoke(cmdArgs);
                        this.EditControl.OnAfterExecuteCommand(cmdArgs);
                    }
                    catch (Exception ext)
                    {
                        this.EditControl.OnCommandError(cmd, cmdArgs, ext);
                    }
                    finally
                    {
                        _IsExecutingCommand = false;
                    }
                }
                if (cmdArgs.RefreshLevel == UIStateRefreshLevel.Current)
                {
                    // 刷新当前命令绑定的用户界面元素状态
                    this.UpdateBindingControlStatus(cmd.Name);
                }
                else if (cmdArgs.RefreshLevel == UIStateRefreshLevel.All)
                {
                    // 刷新所有的被绑定命令的用户界面元素状态
                    this.UpdateBindingControlStatus();
                }
                return(cmdArgs.Result);
            }//if
            return(null);
        }
Beispiel #10
0
 /// <summary>
 /// 删除动作
 /// </summary>
 /// <param name="a">要删除的动作</param>
 public void Remove(WriterCommand a)
 {
     this.List.Remove(a);
 }