Ejemplo n.º 1
0
        public SimpleTreeListWindow()
        {
            InitializeComponent();
            treeControl.LabelEdit = true;
            //treeControl.ChangeNodeImageOnExpand = true;
            treeControl.AllowDrop        = true;
            treeControl.ShowNodeToolTips = true;

            treeControl.OnTreeNodeSelectEvent += TreeControl_OnTreeNodeSelectEvent;

            this.treeControl.CanExpandGetter += delegate(TreeNode parentNode)
            {
                if (parentNode == null)
                {
                    return(true);
                }
                object x = parentNode.Tag;
                if (x == null)
                {
                    return(false);
                }
                if (x is string)
                {
                    return(true);            //folder
                }
                Type type = x.GetType();
                if (x is RootGroupTreeFolder)
                {
                    Type etype = (x as RootGroupTreeFolder).EntityType;
                    if (ModelHelper.IsSingleHierEntity(etype))
                    {
                        return(Dm.Instance.FindRootList(etype).Count > 0);
                    }
                    else
                    {
                        return(Dm.Instance.FindAll(etype).Count > 0);
                    }
                }
                else if (x is BranchGroupTreeFolder)
                {
                    BranchGroupTreeFolder bf = (x as BranchGroupTreeFolder);
                    if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.foreinProperty) != null)
                    {
                        return(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity).Count > 0);
                    }
                    else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.foreinProperty) != null)
                    {
                        return(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                    bf.RefEntityInfo.foreinProperty.Name).Count > 0);
                    }
                    else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.foreinProperty) != null)
                    {
                        return(Dm.Instance.ResolveOneToOneRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                   bf.RefEntityInfo.foreinProperty.Name) != null);
                    }
                }
                else
                {
                    JEntity entityAttr = x.GetType().GetCustomAttribute <JEntity>();
                    if (entityAttr != null)
                    {
                        if (showGroupsFolder)
                        {
                            List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x, false);
                            return(rels.Count(s => (s.foreinProperty != null)) > 0);

                            /*
                             * foreach (var rt in rels)
                             * {
                             *  HashSet<object> refs = rt.Records;
                             *  if (refs.Count > 0)
                             *  {
                             *      return true;
                             *  }
                             * }
                             */
                        }
                        else
                        {
                            //find all ManyToOne relations rels to this entity type
                            List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x);
                            foreach (var rt in rels)
                            {
                                HashSet <object> refs = rt.Records;
                                if (refs.Count > 0)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
                return(false);
            };
            treeControl.ChildrenGetter += delegate(TreeNode parentNode)
            {
                IList lo = new List <object>();
                if (parentNode == null)
                {
                    //root
                    foreach (var e in rootEntites)
                    {
                        RootGroupTreeFolder ef = new RootGroupTreeFolder();
                        ef.EntityType = e;
                        lo.Add(ef);
                    }
                }
                else
                {
                    object x = parentNode.Tag;
                    if (x == null)
                    {
                        return(new List <object>());
                    }
                    if (x is RootGroupTreeFolder)
                    {
                        Type type = (x as RootGroupTreeFolder).EntityType;
                        if (ModelHelper.IsSingleHierEntity(type))
                        {
                            return(Dm.Instance.FindRootList(type));
                        }
                        else
                        {
                            return(Dm.Instance.FindAll(type));
                        }
                    }
                    else if (x is BranchGroupTreeFolder)
                    {
                        BranchGroupTreeFolder bf = (x as BranchGroupTreeFolder);
                        if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.foreinProperty) != null)
                        {
                            return(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity));
                        }
                        else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.foreinProperty) != null)
                        {
                            return(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                        bf.RefEntityInfo.foreinProperty.Name));
                        }
                        else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.foreinProperty) != null)
                        {
                            lo.Add(Dm.Instance.ResolveOneToOneRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                       bf.RefEntityInfo.foreinProperty.Name));
                        }
                    }
                    else
                    {
                        bool    isSingleHierEntity = ModelHelper.IsSingleHierEntity(x.GetType());
                        JEntity entityAttr         = x.GetType().GetCustomAttribute <JEntity>();
                        if (entityAttr != null)
                        {
                            if (showGroupsFolder)
                            {
                                //List<RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x, false);
                                List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x);
                                foreach (var rt in rels)
                                {
                                    if (rt.foreinProperty != null)
                                    {
                                        if (rt.IsSelfRelation() && isSingleHierEntity)
                                        {
                                            //IList lll = Dm.Instance.ResolveOneToManyRelation(x, rt.RefEntity,
                                            //   rt.foreinProperty.Name);
                                            //foreach (var l1 in lll)
                                            //  lo.Add(l1);
                                            foreach (var l1 in rt.Records)
                                            {
                                                lo.Add(l1);
                                            }
                                        }
                                        else
                                        {
                                            if (rt.Records.Count > 0)
                                            {
                                                BranchGroupTreeFolder bf = new BranchGroupTreeFolder();
                                                bf.ParentObject  = x;
                                                bf.RefEntityInfo = rt;
                                                lo.Add(bf);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x);

                                foreach (var rt in rels)
                                {
                                    HashSet <object> refs = rt.Records;
                                    if (refs.Count > 0)
                                    {
                                        foreach (var l in refs)
                                        {
                                            lo.Add(l);
                                        }
                                    }
                                }
                            }
                        }
                        return(lo);
                    }
                }
                return(lo);
            };
            treeControl.AfterEditTreeNodeLabel += delegate(object model, string labelText)
            {
                if (model == null)
                {
                    return(false);
                }
                Type    t          = model.GetType();
                JEntity entityAttr = t.GetCustomAttribute <JEntity>();
                if (entityAttr != null)
                {
                    PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(t);
                    if (pName != null)
                    {
                        object oldValue = pName.GetValue(model);
                        try
                        {
                            pName.SetValue(model, labelText);//force exception if no set method
                            Dm.Instance.SaveObject(model);
                            return(true);
                        }
                        catch (JValidationException ex)
                        {
                            AppManager.ShowValidationErrorMessage(ex.ValidationResult);
                            pName.SetValue(model, oldValue);
                        }
                    }
                }
                return(false);
            };
        }
        public SimpleTreeListWindow()
        {
            InitializeComponent();
            treeControl.LabelEdit = true;
            //treeControl.ChangeNodeImageOnExpand = true;
            treeControl.AllowDrop        = true;
            treeControl.ShowNodeToolTips = true;

            treeControl.OnTreeNodeSelectEvent += TreeControl_OnTreeNodeSelectEvent;

            this.treeControl.CanExpandGetter += delegate(TreeNode currentNode)
            {
                if (currentNode == null)
                {
                    return(true);
                }
                //currentNode may be not object node (may by branch node), so find real object node to view object
                TreeNode currentObjectNode = (currentNode.Tag is BranchGroupTreeFolder) ? currentNode.Parent : currentNode;
                TreeNode parentObjectNode  = (currentObjectNode != null && currentObjectNode.Parent != null)
                    ? ((currentObjectNode.Parent.Tag is BranchGroupTreeFolder) ? currentObjectNode.Parent.Parent : currentObjectNode.Parent) : null;

                object curentObject = (currentNode.Tag is TreeObjectWrap) ? (currentNode.Tag as TreeObjectWrap).Tag : currentNode.Tag;
                object parentObject = (parentObjectNode != null) ? ((parentObjectNode.Tag is TreeObjectWrap)? (parentObjectNode.Tag as TreeObjectWrap).Tag : parentObjectNode.Tag) : null;

                RefEntityInfo currentNodeRel = (currentObjectNode != null && currentObjectNode.Tag is TreeObjectWrap) ? (currentObjectNode.Tag as TreeObjectWrap).Rel : null;
                RefEntityInfo parentNodeRel  = (parentObjectNode != null && parentObjectNode.Tag is TreeObjectWrap) ? (parentObjectNode.Tag as TreeObjectWrap).Rel : null;

                if (curentObject == null)
                {
                    return(false);
                }
                if (curentObject is string)
                {
                    return(true);                       //folder
                }
                Type type = curentObject.GetType();
                if (curentObject is RootGroupTreeFolder)
                {
                    Type etype = (curentObject as RootGroupTreeFolder).EntityType;
                    if (ModelHelper.IsSingleHierEntity(etype))
                    {
                        return(Dm.Instance.FindRootList(etype).Count > 0);
                    }
                    else
                    {
                        return(Dm.Instance.FindAll(etype).Count > 0);
                    }
                }
                else if (curentObject is BranchGroupTreeFolder)
                {
                    BranchGroupTreeFolder bf = (curentObject as BranchGroupTreeFolder);
                    if (bf.RefEntityInfo.PropertyInForeign != null)
                    {
                        if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.PropertyInForeign) != null)
                        {
                            return(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity).Count > 0);
                        }
                        else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                        {
                            return(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                        bf.RefEntityInfo.PropertyInForeign.Name).Count > 0);
                        }
                        else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                        {
                            return(Dm.Instance.ResolveOneToOneRelationReverse(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                              bf.RefEntityInfo.PropertyInForeign.Name) != null);
                        }
                    }
                    else if (bf.RefEntityInfo.PropertyInSource != null)
                    {
                        if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInSource) != null || AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInSource) != null)
                        {
                            object o = AttrHelper.GetPropertyValue(bf.ParentObject, bf.RefEntityInfo.PropertyInSource);
                            return(o != null);
                        }
                    }
                }
                else
                {
                    JEntity entityAttr = curentObject.GetType().GetCustomAttribute <JEntity>();
                    if (entityAttr != null)
                    {
                        //if (showGroupsFolder)
                        //{
                        //   List<RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(parentObject, false);
                        //   return rels.Count(s => (s.RefFromProperty != null || s.RefToProperty != null)) > 0;
                        //}
                        //else
                        //{
                        //find all ManyToOne relations rels to this entity type
                        List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(curentObject);
                        foreach (var rt in rels)
                        {
                            HashSet <object> refs = rt.Records;
                            if (refs.Count > 0)
                            {
                                return(true);
                            }
                        }
                        //}
                    }
                }
                return(false);
            };
            treeControl.ChildrenGetter += delegate(TreeNode currentNode)
            {
                IList lo = new List <object>();
                if (currentNode == null)
                {
                    //root
                    foreach (var e in rootEntites)
                    {
                        RootGroupTreeFolder ef = new RootGroupTreeFolder();
                        ef.EntityType = e;
                        lo.Add(ef);
                    }
                }
                else
                {
                    TreeNode currentObjectNode = (currentNode.Tag is BranchGroupTreeFolder) ? currentNode.Parent : currentNode;
                    TreeNode parentObjectNode  = (currentObjectNode != null && currentObjectNode.Parent != null)
                        ? ((currentObjectNode.Parent.Tag is BranchGroupTreeFolder) ? currentObjectNode.Parent.Parent : currentObjectNode.Parent) : null;

                    object currentObject = (currentNode.Tag is TreeObjectWrap) ? (currentNode.Tag as TreeObjectWrap).Tag : currentNode.Tag;
                    object parentObject  = (parentObjectNode != null) ? ((parentObjectNode.Tag is TreeObjectWrap) ? (parentObjectNode.Tag as TreeObjectWrap).Tag : parentObjectNode.Tag) : null;

                    RefEntityInfo currentNodeRel = (currentObjectNode != null && currentObjectNode.Tag is TreeObjectWrap) ?  (currentObjectNode.Tag as TreeObjectWrap).Rel : null;
                    RefEntityInfo parentNodeRel  = (parentObjectNode != null && parentObjectNode.Tag is TreeObjectWrap) ? (parentObjectNode.Tag as TreeObjectWrap).Rel : null;

                    if (currentObject == null)
                    {
                        return(new List <object>());
                    }
                    if (currentObject is RootGroupTreeFolder)
                    {
                        Type type = (currentObject as RootGroupTreeFolder).EntityType;
                        if (ModelHelper.IsSingleHierEntity(type))
                        {
                            return(WrapList(Dm.Instance.FindRootList(type), null));
                        }
                        else
                        {
                            return(WrapList(Dm.Instance.FindAll(type), null));
                        }
                    }
                    else if (currentObject is BranchGroupTreeFolder)
                    {
                        BranchGroupTreeFolder bf = (currentObject as BranchGroupTreeFolder);

                        if (savedTreeState.IsRelationVisible(bf.RefEntityInfo.SourceEntity, bf.RefEntityInfo.Name))
                        {
                            if (bf.RefEntityInfo.PropertyInForeign != null)
                            {
                                if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.PropertyInForeign) != null)
                                {
                                    return(WrapList(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity), bf.RefEntityInfo));
                                }
                                else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                                {
                                    return(WrapList(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                                         bf.RefEntityInfo.PropertyInForeign.Name), bf.RefEntityInfo));
                                }
                                else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                                {
                                    lo.Add(new TreeObjectWrap()
                                    {
                                        Tag = Dm.Instance.ResolveOneToOneRelationReverse(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                                         bf.RefEntityInfo.PropertyInForeign.Name),
                                        Rel = bf.RefEntityInfo
                                    });
                                }
                            }
                            else if (bf.RefEntityInfo.PropertyInSource != null)
                            {
                                if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInSource) != null || AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInSource) != null)
                                {
                                    object o = AttrHelper.GetPropertyValue(bf.ParentObject, bf.RefEntityInfo.PropertyInSource);

                                    if (o != null)
                                    {
                                        if (parentObject != null && parentObject.Equals(o) &&
                                            currentNodeRel != null && currentNodeRel.PropertyInForeign != null && bf.RefEntityInfo.PropertyInSource != null &&
                                            currentNodeRel.PropertyInForeign == bf.RefEntityInfo.PropertyInSource)
                                        {
                                        }
                                        else
                                        {
                                            lo.Add(new TreeObjectWrap()
                                            {
                                                Tag = o, Rel = bf.RefEntityInfo
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        bool    isSingleHierEntity = ModelHelper.IsSingleHierEntity(currentObject.GetType());
                        JEntity entityAttr         = currentObject.GetType().GetCustomAttribute <JEntity>();
                        if (entityAttr != null)
                        {
                            List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(currentObject);
                            foreach (var rt in rels)
                            {
                                if (savedTreeState.IsRelationVisible(rt.SourceEntity, rt.Name))
                                {
                                    bool isSelfRelation = rt.IsSelfRelation();
                                    if ((notShowGroupsFolderTypes.Contains(rt.ForeignEntity) == false && rt.PropertyInForeign != null) && !(isSelfRelation && isSingleHierEntity))
                                    {
                                        if (rt.Records.Count > 0)
                                        {
                                            BranchGroupTreeFolder bf = new BranchGroupTreeFolder();
                                            bf.ParentObject  = currentObject;
                                            bf.RefEntityInfo = rt;
                                            lo.Add(bf);
                                        }
                                    }
                                    else
                                    {
                                        foreach (var l in rt.Records)
                                        {
                                            if (!(parentObject != null && parentObject.Equals(l) &&
                                                  currentNodeRel != null && currentNodeRel.PropertyInForeign != null && rt.PropertyInSource != null &&
                                                  currentNodeRel.PropertyInForeign == rt.PropertyInSource))
                                            {
                                                lo.Add(new TreeObjectWrap()
                                                {
                                                    Tag = l, Rel = rt
                                                });
                                            }
                                        }
                                    }
                                }
                            }//foreach (var rt in rels)
                        }
                        return(lo);
                    }
                }
                return(lo);
            };
            treeControl.AfterEditTreeNodeLabel += delegate(object model, string labelText)
            {
                if (model == null)
                {
                    return(false);
                }
                Type    t          = model.GetType();
                JEntity entityAttr = t.GetCustomAttribute <JEntity>();
                if (entityAttr != null)
                {
                    PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(t);
                    if (pName != null)
                    {
                        object oldValue = pName.GetValue(model);
                        try
                        {
                            pName.SetValue(model, labelText);//force exception if no set method
                            Dm.Instance.SaveObject(model);
                            return(true);
                        }
                        catch (JValidationException ex)
                        {
                            AppManager.ShowValidationErrorMessage(ex.ValidationResult);
                            pName.SetValue(model, oldValue);
                        }
                    }
                }
                return(false);
            };
        }