using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using Csla; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Reflection; namespace VEPROMS.CSLA.Library { public delegate void VETreeNodeEvent(object sender, VETreeNodeEventArgs args); public class VETreeNodeEventArgs : EventArgs { public VETreeNodeEventArgs() { ; } public VETreeNodeEventArgs(string info) { _Info = info; } public VETreeNodeEventArgs(int value) { _Value = value; } public VETreeNodeEventArgs(string info, int value) { _Info = info; _Value = value; } private int _Value; public int Value { get { return _Value; } set { _Value = value; } } private string _Info; public string Info { get { return _Info; } set { _Info = value; } } } public class VETreeNode : TreeNode { #region Events public event VETreeNodeEvent LoadingChildrenSQL; public event VETreeNodeEvent LoadingChildernMax; public event VETreeNodeEvent LoadingChildernValue; public event VETreeNodeEvent LoadingChildernDone; private void OnLoadingChildrenSQL(object sender, VETreeNodeEventArgs args) { if (LoadingChildrenSQL != null) LoadingChildrenSQL(sender, args); } private void OnLoadingChildrenMax(object sender, VETreeNodeEventArgs args) { if (LoadingChildernMax != null) LoadingChildernMax(sender, args); } private void OnLoadingChildrenValue(object sender, VETreeNodeEventArgs args) { if (LoadingChildernValue != null) LoadingChildernValue(sender, args); } private void OnLoadingChildrenDone(object sender, VETreeNodeEventArgs args) { if (LoadingChildernDone != null) LoadingChildernDone(sender, args); } #endregion #region Business Methods protected IVEDrillDownReadOnly _VEObject; public IVEDrillDownReadOnly VEObject { get { return _VEObject; } set { _VEObject = value; base.Text = _VEObject.ToString(); ResetNode(); } } //public void Refresh() //{ // if (_VEObject != null) // Text = _VEObject.ToString(); //} // Only load the Children Once protected bool _ChildrenLoaded = false; public bool ChildrenLoaded { get { return _ChildrenLoaded; } } // Reset Node //public void CloseNode() //{ // ResetNode(); //} private void SetProperty(string name) { PropertyInfo propertyInfoObj = _VEObject.GetType().GetProperty(name); if (propertyInfoObj == null) return; PropertyInfo propertyInfoThis = this.GetType().GetProperty(name); if (propertyInfoThis == null) return; try { propertyInfoThis.SetValue(this, propertyInfoObj.GetValue(_VEObject, null), null); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); } } public void ResetNode() { if (_VEObject!=null && _VEObject.HasChildren && _ChildrenLoaded == false) { _ChildrenLoaded = false;// Reset the children loaded flag this.Nodes.Add("Dummy");// Add a Dummy Node so that the item will appear to be expanable. } else { _ChildrenLoaded = true;// Reset the children loaded flag } if (base.Text != _VEObject.ToString()) base.Text = _VEObject.ToString(); SetProperty("ForeColor"); SetProperty("BackColor"); SetProperty("ImageIndex"); SetProperty("IsVisible"); SetProperty("SelectedImageIndex"); // ToDo: Need to reset object as well } public void RefreshNode() { ResetNode();// Drop Children LoadChildren();// Load Children } public static VETreeNode GetFolder(int folderID) { VETreeNode tn = new VETreeNode(FolderInfo.Get(folderID)); //tn.Nodes.Add("Dummy"); tn.ResetNode(); return tn; } // public abstract void LoadChildren(); //private long _Start; //private Dictionary _Timings=new Dictionary(); //private void tReset() //{ // _Timings = new Dictionary(); // _Start = DateTime.Now.Ticks; //} //private void tNext(string msg) //{ // long tEnd = DateTime.Now.Ticks; // long tDiff = tEnd - _Start; // if (_Timings.ContainsKey(msg)) tDiff += _Timings[msg]; // _Timings[msg] = tDiff; // _Start = DateTime.Now.Ticks; //} //private void tShowResults() //{ // Console.WriteLine("Timings"); // long total=0; // foreach (string msg in _Timings.Keys) // { // total += _Timings[msg]; // Console.WriteLine("{0}\t\"{1}\"", TimeSpan.FromTicks(_Timings[msg]).TotalMilliseconds, msg); // } // Console.WriteLine("{0}\t\"Total\"", TimeSpan.FromTicks(total).TotalMilliseconds); //} public virtual void LoadChildren() { LoadChildren(true); } public virtual void LoadChildren(bool allParts) { if (!_ChildrenLoaded) { this.Nodes.Clear(); //tReset(); DateTime tStart = DateTime.Now; OnLoadingChildrenSQL(this, new VETreeNodeEventArgs()); IList ol; if(_VEObject.GetType() == typeof(StepInfo) ||_VEObject.GetType() == typeof(SectionInfo) ||_VEObject.GetType() == typeof(ItemInfo) ) ol = ((ItemInfo)_VEObject).GetChildren(allParts); else ol = _VEObject.GetChildren(); //tNext("GetChildren"); if (ol != null) { OnLoadingChildrenMax(this, new VETreeNodeEventArgs(ol.Count)); this.TreeView.BeginUpdate(); int icnt = 0; foreach (IVEDrillDownReadOnly o in ol) { OnLoadingChildrenValue(this,new VETreeNodeEventArgs( ++icnt)); //tNext("Cycle"); try { VETreeNode tmp = new VETreeNode(o); //tNext("Allocate VETreeNode"); //tmp.ResetNode(); if (o.HasChildren) tmp.Nodes.Add("Dummy");// Add a Dummy Node so that the item will appear to be expanable. else tmp._ChildrenLoaded = true;// Reset the children loaded flag //tNext("Add Children"); this.Nodes.Add(tmp); //tNext("Add To Nodes"); } catch (Exception ex) { Console.WriteLine("{0}\r\n{1}", ex.Message, ex.InnerException); } } //} //tNext("Set Nodes"); this.TreeView.EndUpdate(); //tNext("End Update"); //tShowResults(); } _ChildrenLoaded = true; OnLoadingChildrenDone(this, new VETreeNodeEventArgs(TimeSpan.FromTicks(DateTime.Now.Ticks - tStart.Ticks).TotalSeconds.ToString())); } } //public IVEReadOnlyItem GetCsla() //{ // return _VEObject; //} #endregion #region Factory Methods // Constructors public VETreeNode() : base("NoText") { ; } public VETreeNode(IVEDrillDownReadOnly o) : base(o.ToString()) { _VEObject = o;// Save the BusinessObject ResetNode(); } public VETreeNode(string s) : base(s) { _VEObject = null;// Save the BusinessObject //ResetNode(); } #endregion } //public class VETreeNodeBase : VETreeNode //where T : VETreeNode, new() //{ // #region Factory Methods // private static int _ICount = 0; // public VETreeNodeBase(string s) : base(s) { ;} // public VETreeNodeBase(IVEReadOnlyItem o) : base(o) { ;} // protected VETreeNodeBase() : base() { // } // private DateTime tNext(DateTime tStart, string msg) // { // DateTime tEnd = DateTime.Now; // TimeSpan ts = new TimeSpan(tEnd.Ticks-tStart.Ticks); // Console.WriteLine("{0} - {1}", ts.TotalMilliseconds, msg); // return tEnd; // } // public override void LoadChildren() // { // if (!_ChildrenLoaded) // { // this.Nodes.Clear(); // "".TrimEnd("\r\n".ToCharArray()); // IList ol = _VEObject.GetChildren(); // if (ol != null) // { // this.TreeView.BeginUpdate(); // DateTime tStart = DateTime.Now; // T[] tmpa = new T[ol.Count]; // tStart = tNext(tStart, "Allocate Array"); // for (int i = 0; i < ol.Count; i++) tmpa[i] = new T(); // tStart = tNext(tStart, "Allocate Nodes"); // int ii = 0; // foreach (IVEReadOnlyItem o in ol) // { // try // { // tmpa[ii++].VEObject = o; // } // catch (Exception ex) // { // Console.WriteLine("{0}\r\n{1}", ex.Message, ex.InnerException); // } // } // tStart = tNext(tStart, "Set Nodes"); // for (int i = 0; i < ol.Count; i++) tmpa[i].ResetNode(); // tStart = tNext(tStart, "Reset Nodes"); // this.Nodes.AddRange(tmpa); // tStart = tNext(tStart, "Add Range"); // //foreach (IVEReadOnlyItem o in ol) // //{ // // try // // { // // T tmp = new T(); // // tmp.VEObject = o; // // this.Nodes.Add(tmp); // // } // // catch (Exception ex) // // { // // Console.WriteLine("{0}\r\n{1}", ex.Message, ex.InnerException); // // } // //} // this.TreeView.EndUpdate(); // } // _ChildrenLoaded = true; // } // } //#endregion //} /* public class VEFolder : VETreeNodeBase { public static VEFolder GetFolder(int folderID) { return new VEFolder(FolderInfo.Get(folderID)); } //public static VEFolder LoadTree() //{ // VEFolder root = null; // FolderInfoList fil = FolderInfoList.Get(); // Dictionary dicMissing = new Dictionary(); // Dictionary dicExists = new Dictionary(); // foreach (FolderInfo fi in fil) // { // VEFolder ftp = null; // if (dicExists.ContainsKey(fi.ParentID)) // { // ftp = dicExists[fi.ParentID]; // } // else // { // if (fi.ParentID != fi.FolderID) // { // ftp = new VEFolder(fi.ParentID.ToString()); // dicMissing.Add(fi.ParentID, ftp); // dicExists.Add(fi.ParentID, ftp); // } // } // VEFolder ft = null; // if (dicMissing.ContainsKey(fi.FolderID)) // { // ft = dicMissing[fi.FolderID]; // ft.VEObject = fi; // dicMissing.Remove(fi.FolderID); // } // else // { // ft = new VEFolder(fi); // dicExists.Add(fi.FolderID, ft); // } // if (fi.ParentID == fi.FolderID) // root = ft; // else // ftp.Nodes.Add(ft); // } // //root.FindTree = dicExists; // return root; //} private VEFolder(string s) : base(s) { ;} public VEFolder(IVEReadOnlyItem o) : base(o) { ;} } public class VEVersion : VETreeNodeBase { } public class VEProcedure : VETreeNodeBase { //public override void LoadChildren() //{ // if (!_ChildrenLoaded) // { // this.Nodes.Clear(); // Dictionary dicSect = new Dictionary(); // SectionInfoList ol = (SectionInfoList)_VEObject.GetChildren(); // if (ol != null) // { // foreach (SectionInfo o in ol) // { // try // { // VESection tmp = new VESection(); // tmp.VEObject = o; // if (dicSect.ContainsKey(o.PPath)) // dicSect[o.PPath].Nodes.Add(tmp); // else // this.Nodes.Add(tmp); // dicSect[o.Path] = tmp; // } // catch (Exception ex) // { // Console.WriteLine("{0}\r\n{1}", ex.Message, ex.InnerException); // } // } // } // _ChildrenLoaded = true; // } //} } public class VESection : VETreeNodeBase { } public class VEStep : VETreeNodeBase { } */ //public class VETree : VETreeNodeBase //{ // public VETree() { ;} // public static VETree GetFolder(int folderID) // { // return new VETree(FolderInfo.Get(folderID)); // } // //private VEFolder(string s) : base(s) { ;} // public VETree(IVEReadOnlyItem o) : base(o) { ;} //} //public class VETree : VETreeNode //{ // public VETree() { ;} // public static VETree GetFolder(int folderID) // { // VETree tn = new VETree(FolderInfo.Get(folderID)); // tn.ResetNode(); // return tn; // } // //private VEFolder(string s) : base(s) { ;} // public VETree(IVEReadOnlyItem o) : base(o) { ;} // private DateTime tNext(DateTime tStart, string msg) // { // DateTime tEnd = DateTime.Now; // TimeSpan ts = new TimeSpan(tEnd.Ticks - tStart.Ticks); // Console.WriteLine("{0} - {1}", ts.TotalMilliseconds, msg); // return tEnd; // } // public override void LoadChildren() // { // if (!_ChildrenLoaded) // { // this.Nodes.Clear(); // "".TrimEnd("\r\n".ToCharArray()); // IList ol = _VEObject.GetChildren(); // if (ol != null) // { // this.TreeView.BeginUpdate(); // DateTime tStart = DateTime.Now; // //VETree[] tmpa = new VETree[ol.Count]; // //tStart = tNext(tStart, "Allocate Array"); // ////for (int i = 0; i < ol.Count; i++) tmpa[i] = new VETree(); // ////tStart = tNext(tStart, "Allocate Nodes"); // //int ii = 0; // //foreach (IVEReadOnlyItem o in ol) // //{ // // try // // { // // tmpa[ii++] = new VETree(o); // // //tmpa[ii++].VEObject = o; // // } // // catch (Exception ex) // // { // // Console.WriteLine("{0}\r\n{1}", ex.Message, ex.InnerException); // // } // //} // //tStart = tNext(tStart, "Set Nodes"); // //for (int i = 0; i < ol.Count; i++) tmpa[i].ResetNode(); // //tStart = tNext(tStart, "Reset Nodes"); // //this.Nodes.AddRange(tmpa); // //tStart = tNext(tStart, "Add Range"); // this.TreeView.DrawMode = TreeViewDrawMode.OwnerDrawAll; // foreach (IVEReadOnlyItem o in ol) // { // try // { // VETree tmp = new VETree(o); // //tmp.VEObject = o; // this.Nodes.Add(tmp); // } // catch (Exception ex) // { // Console.WriteLine("{0}\r\n{1}", ex.Message, ex.InnerException); // } // } // tStart = tNext(tStart, "Set Nodes"); // this.TreeView.DrawMode = TreeViewDrawMode.Normal; // tStart = tNext(tStart, "DrawMode"); // this.TreeView.EndUpdate(); // tStart = tNext(tStart, "End Update"); // this.TreeView.Refresh(); // tStart = tNext(tStart, "Refresh"); // } // _ChildrenLoaded = true; // } // } //} }