www.pudn.com > WorkflowDesigner.rar > WfAbstractActivity.cs, change:2007-11-28,size:13453b


using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Drawing; 
using System.Xml; 
namespace WorkflowDesigner.Designer 
{ 
    /// <summary> 
    /// 节点可视化对象基类 
    /// </summary> 
    public abstract class WfAbstractActivity:DesignerVisualObject 
    { 
        #region 字段 
 
        /// <summary> 
        /// 可视化对象名称 
        /// </summary> 
        private String _name; 
        /// <summary> 
        /// 所属工作文档 
        /// </summary> 
        private WfFlowDocument _flow; 
 
        /// <summary> 
        /// 当前节点所在位置的x轴坐标 
        /// </summary> 
        protected int _x; 
 
        /// <summary> 
        /// 当前节点所在位置的y轴坐标 
        /// </summary> 
        protected int _y; 
 
        /// <summary> 
        /// 存放图标矩形框 
        /// </summary> 
        protected Rectangle _rect; 
 
        /// <summary> 
        ///  宽度和长度之和(因为画出来是个方形) 
        /// </summary> 
        public const int WidthAndHeight = 30; 
        #endregion 
 
        #region 属性 
        /// <summary> 
        /// 可视化对象名称 
        /// </summary> 
        public String Name 
        { 
            get { return _name; } 
            set { _name = value; } 
        } 
 
        /// <summary> 
        /// 所属工作文档 
        /// </summary> 
        public WfFlowDocument Flow 
        { 
            get { return _flow; } 
            set { _flow = value; } 
        } 
 
        /// <summary> 
        /// 当前节点所在位置的x轴坐标 
        /// </summary> 
        public int X 
        { 
            get 
            { 
                return _x; 
            } 
            set 
            { 
                _x = value; 
                //根据图形的中心点绘制存放图标的矩形 
                _rect = new Rectangle(_x - WidthAndHeight / 2, _y - WidthAndHeight / 2, WidthAndHeight, WidthAndHeight); 
            } 
        } 
        /// <summary> 
        /// 当前节点所在位置的y轴坐标 
        /// </summary> 
        public int Y 
        { 
            get 
            { 
                return _y; 
            } 
            set 
            { 
                _y = value; 
                //根据图形的中心点绘制存放图标的矩形 
                _rect = new Rectangle(_x - WidthAndHeight / 2, _y - WidthAndHeight / 2, WidthAndHeight, WidthAndHeight); 
            } 
        } 
        #endregion 
 
        #region 构造函数 
        /// <summary> 
        /// 构造函数 
        /// </summary> 
        /// <param name="x">鼠标所在位置的x轴坐标</param> 
        /// <param name="y">鼠标所在位置的y轴坐标</param> 
        public WfAbstractActivity(int x, int y) 
        { 
 
            //得到中心点坐标 
            this._x = x; 
            this._y = y; 
            //根据中心点坐标绘制调整节点矩形 
            _rect = new Rectangle(_x - WidthAndHeight / 2, _y - WidthAndHeight / 2, WidthAndHeight, WidthAndHeight); 
        } 
        #endregion 
 
        #region 操作 
 
        #region 私有 
        #endregion 
 
        #region 保护 
        /// <summary> 
        /// 画图标 
        /// </summary> 
        /// <param name="g">绘图图面</param> 
        protected abstract  void DrawIcon(Graphics g); 
        /// <summary> 
        /// 得到节点类型 
        /// </summary> 
        /// <returns>该节点的类型</returns> 
        protected abstract String GetActivityType(); 
 
        /// <summary> 
        /// 创建xml节点后 
        /// </summary> 
        /// <param name="actElement">xml节点</param> 
        protected virtual  void AfterCreateXmlElement(XmlElement actElement) 
        { 
        } 
        #endregion 
 
        #region 公用 
        /// <summary> 
        /// 判断鼠标点击出是否有可视化对象 
        /// </summary> 
        /// <param name="x">鼠标所在位置的x轴坐标</param> 
        /// <param name="y">鼠标所在位置的y轴坐标</param> 
        /// <returns>点击出的可视化对象</returns> 
        public override HitTestState HitTest(int x, int y) 
        { 
            if (this._rect.Contains(x, y)) 
            { 
                return HitTestState.Center; 
            } 
            else 
            { 
                return HitTestState.None; 
            } 
        } 
 
        /// <summary> 
        /// 对齐到画布上的表格 
        /// </summary> 
        public override void AlignToGrid() 
        { 
            //如果中间点的x轴坐标小于0那么中间点的x轴坐标为0 
            if (_x < 0) 
            { 
                _x = 0; 
            } 
            //如果中间点的x轴坐标大于等于2000那么中间点的x轴坐标为2000 
            else if (_x >= 2000) 
            { 
                _x = 2000; 
            } 
            else 
            { 
                //否则对齐到最近的表格线位置 
                _x = AlignToNumber(_x, GridSize); 
            } 
            //如果中间点的x轴坐标小于0那么中间点的y轴坐标为0 
            if (_y < 0) 
            { 
                _y = 0; 
            } 
            //如果中间点的y轴坐标大于等于2000那么中间点的x轴坐标为2000 
            else if (_y >= 2000) 
            { 
                _y = 2000; 
            } 
            else 
            { 
                //否则对齐到最近的表格线位置 
                _y = AlignToNumber(_y, GridSize); 
            } 
            //重新画中间点的矩形 
            _rect = new Rectangle(_x - WidthAndHeight / 2, _y - WidthAndHeight / 2, WidthAndHeight, WidthAndHeight); 
        } 
 
        /// <summary> 
        /// 框选 
        /// </summary> 
        /// <param name="rect">选中的矩形范围</param> 
        public override void RangeSelect(Rectangle rect) 
        { 
            if (rect.Contains(this._rect)) 
            { 
                this.IsSelected = true; 
            } 
        } 
 
        /// <summary> 
        /// 绘制 
        /// </summary> 
        /// <param name="g">绘图图面</param> 
        public override void Draw(Graphics g) 
        { 
            DrawIcon(g); 
            StringFormat format1 = new StringFormat(StringFormatFlags.NoClip); 
            format1.Alignment = StringAlignment.Center; 
            format1.LineAlignment = StringAlignment.Near; 
            Brush brush = new SolidBrush(Color.Black); 
            Font font = new Font("新宋体", 9); 
            g.DrawString(Name, font, brush, new PointF(_x, _y + WidthAndHeight / 2 + 5), format1); 
            format1.Dispose(); 
            font.Dispose(); 
            brush.Dispose(); 
        } 
 
 
        /// <summary> 
        /// 移动 
        /// </summary> 
        /// <param name="offX">移动新位置的x轴坐标</param> 
        /// <param name="offY">移动新位置的y轴坐标</param> 
        public override void Move(int offX, int offY) 
        { 
  
                //将中心点坐标加上偏移值,得到最新的流转中间调整点 
                _x += offX; 
                _y += offY; 
                //根据中心点坐标绘制调整节点矩形 
                _rect = new Rectangle(_x - WidthAndHeight / 2, _y - WidthAndHeight / 2, WidthAndHeight, WidthAndHeight); 
             
        } 
 
        /// <summary> 
        /// 打开属性对话框 
        /// </summary> 
        public override void OpenPropertyDialog() 
        { 
        } 
 
        /// <summary> 
        /// 图形矩阵 
        /// </summary> 
        /// <returns>返回一个图形矩阵</returns> 
        public override Rectangle GetRange() 
        { 
            return this._rect; 
        } 
        /// <summary> 
        /// 初始化xml节点 
        /// </summary> 
        /// <param name="actElement">xml节点</param> 
        public virtual void InitFromXml(XmlElement actElement) 
        { 
            Name = actElement.Attributes["name"].Value; 
        } 
        /// <summary> 
        /// 保存xml元素 
        /// </summary> 
        /// <param name="actsElement">xml节点</param> 
        public void SaveToXmlElement(XmlElement actsElement) 
        { 
            //需要改动 
            XmlElement actElement = actsElement.OwnerDocument.CreateElement("activity"); 
 
            XmlAttribute attr = actsElement.OwnerDocument.CreateAttribute("name"); 
            attr.Value = Name; 
            actElement.Attributes.Append(attr); 
 
            attr = actElement.OwnerDocument.CreateAttribute("x"); 
            attr.Value = X.ToString(); 
            actElement.Attributes.Append(attr); 
 
            attr = actElement.OwnerDocument.CreateAttribute("y"); 
            attr.Value = Y.ToString(); 
            actElement.Attributes.Append(attr); 
 
            attr = actElement.OwnerDocument.CreateAttribute("type"); 
            attr.Value = GetActivityType(); 
            actElement.Attributes.Append(attr); 
 
 
 
 
            XmlElement transesElement = actsElement.OwnerDocument.CreateElement("transitions"); 
            foreach (DesignerVisualObject vo in Flow.VisualObjectList) 
            { 
                 
                 
                if (vo is WfTransition) 
                { 
                    WfTransition trans = vo as WfTransition; 
                    if (trans.StartActivity == this) 
                    { 
                        XmlElement transElement = transesElement.OwnerDocument.CreateElement("transition"); 
 
                        if (trans.Name != "") 
                        { 
                            attr = transElement.OwnerDocument.CreateAttribute("name"); 
                            attr.Value = trans.Name; 
                            transElement.Attributes.Append(attr); 
                        } 
 
                        attr = transElement.OwnerDocument.CreateAttribute("forward"); 
                        attr.Value = trans.EndActivity.Name; 
                        transElement.Attributes.Append(attr); 
 
                        attr = transElement.OwnerDocument.CreateAttribute("type"); 
                        attr.Value = "0"; 
                        transElement.Attributes.Append(attr); 
 
                        attr = transElement.OwnerDocument.CreateAttribute("x"); 
                        attr.Value = trans.X.ToString(); 
                        transElement.Attributes.Append(attr); 
 
                        attr = transElement.OwnerDocument.CreateAttribute("y"); 
                        attr.Value = trans.Y.ToString(); 
                        transElement.Attributes.Append(attr); 
 
                         
                        //加参数 
                        XmlElement tcsElement = transesElement.OwnerDocument.CreateElement("transconditions"); 
                        foreach (TransCondition tc in trans.Conditions) 
                        { 
                            XmlElement tcElement = transesElement.OwnerDocument.CreateElement("transcondition"); 
 
                            attr = transElement.OwnerDocument.CreateAttribute("param"); 
                            attr.Value = tc.Param.Name; 
                            tcElement.Attributes.Append(attr); 
 
                            attr = transElement.OwnerDocument.CreateAttribute("oper"); 
                            attr.Value = tc.Oper; 
                            tcElement.Attributes.Append(attr); 
 
                            attr = transElement.OwnerDocument.CreateAttribute("value"); 
                            attr.Value = tc.Value; 
                            tcElement.Attributes.Append(attr); 
 
                            tcsElement.AppendChild(tcElement); 
                        } 
                        transElement.AppendChild(tcsElement); 
                        transesElement.AppendChild(transElement); 
                    } 
                } 
 
                if (vo is WfBackTransition) 
                { 
                    WfBackTransition backtrans = vo as WfBackTransition; 
                    if (backtrans.StartActivity == this) 
                    { 
                        XmlElement transElement = transesElement.OwnerDocument.CreateElement("transition"); 
 
                        if (backtrans.Name != "") 
                        { 
                            attr = transElement.OwnerDocument.CreateAttribute("name"); 
                            attr.Value = backtrans.Name; 
                            transElement.Attributes.Append(attr); 
                        } 
 
                        attr = transElement.OwnerDocument.CreateAttribute("forward"); 
                        attr.Value = backtrans.EndActivity.Name; 
                        transElement.Attributes.Append(attr); 
 
                        attr = transElement.OwnerDocument.CreateAttribute("type"); 
                        attr.Value = "1"; 
                        transElement.Attributes.Append(attr); 
 
                        attr = transElement.OwnerDocument.CreateAttribute("x"); 
                        attr.Value = backtrans.X.ToString(); 
                        transElement.Attributes.Append(attr); 
 
                        attr = transElement.OwnerDocument.CreateAttribute("y"); 
                        attr.Value = backtrans.Y.ToString(); 
                        transElement.Attributes.Append(attr); 
 
                         
                        transesElement.AppendChild(transElement); 
                    } 
                } 
            } 
            actElement.AppendChild(transesElement); 
 
            actsElement.AppendChild(actElement); 
 
            AfterCreateXmlElement(actElement); 
        } 
        #endregion 
        #endregion 
 
         
 
         
 
         
 
         
    } 
}