using System; using System.Collections.Generic; using System.Windows.Forms; using System.Text; namespace Mesnac.PlugIn.View { public class DefaultViewContent : Form, IViewContent { private string _viewContentID = String.Empty; private object _myOwner = null; private string _titleName = null; private bool _isRealClose = false; private List _actionList = null; #region 实现IViewContent成员 public virtual string ViewContentID { get { return this._viewContentID; } set { this._viewContentID = value; } } public virtual object MyOwner { get { if (this._myOwner == null) { this._myOwner = this; } return this._myOwner; } set { this._myOwner = value; } } /// /// 功能描述 /// public virtual string TitleName { get { if (String.IsNullOrWhiteSpace(this._titleName)) { return this.Text; } else { return this._titleName; } } set { this._titleName = value; } } /// /// 是否真正关闭 /// public bool IsRealClose { get { return _isRealClose; } set { _isRealClose = value; } } /// /// 视图控件集合 /// public Control Control { get { return this; } } public List AllControls { get { List lst = new List(); lst = this.GetAllControls(lst, this); return lst; } } public List GetAllControls(List allControls, Control topControl) { if (allControls == null) allControls = new List(); if (topControl.Controls != null && topControl.Controls.Count > 0) { foreach (Control c in topControl.Controls) { allControls = GetAllControls(allControls, c); allControls.Add(c); } } return allControls; } /// /// 操作项集合 /// public virtual List ActionList { get { if (this._actionList == null || this._actionList.Count == 0) { List lst = new List(); foreach (Control c in this.AllControls) { //按钮 if (c is Button) { string actionName = c.Text; if (c.Text.IndexOf("(") > 0) { actionName = c.Text.Substring(0, c.Text.IndexOf("(") - 1); } lst.Add(new ActionItem(c.Text, c)); } //菜单项 //工具栏按钮 } return lst; } else { return this._actionList; } } } /// /// 添加一个操作项 /// /// public virtual void AddActionItem(ActionItem actionItem) { if (this._actionList == null) { this._actionList = new List(); } this._actionList.Add(actionItem); } /// /// 向操作集合中添加一个操作项 /// /// 操作名称 /// 功能控件 public virtual void AddActionItem(string actionName, Control control) { if (this._actionList == null) { this._actionList = new List(); } this._actionList.Add(new ActionItem(actionName, control)); } /// /// 移除操作项 /// /// public virtual void RemoveActionItem(Control control) { if (this._actionList != null) { for (int i = 0; i < this._actionList.Count; i++) { if (this._actionList[i].Control == control) { this._actionList.RemoveAt(i); break; } } } } /// /// 清空操作项集合 /// public virtual void ClearActionList() { if (this._actionList != null) { this._actionList.Clear(); } else { this._actionList = new List(); } } /// /// 触发重新载入事件 /// /// 事件对象 public void TriggerReloadEvent(System.EventArgs e) { if (Reload != null) { Reload(this, e); } } public virtual void RedrawContent() { } public virtual event EventHandler TitleNameChanged; /// /// 重新载入事件 /// public virtual event EventHandler Reload; #endregion #region 辅助方法 /// /// 获取源控件的顶层容器 /// /// 源控件 /// 返回顶层容器 private Control GetTopContainer(Control source) { Control c = source.Parent; if (c == null) { return this; } else { return GetTopContainer(c); } } #endregion } }