/// <summary> /// Метод добавления хоста к данной группе /// </summary> /// <param name="nameHost">Имя хоста</param> /// <param name="ip">IP-адрес хоста</param> /// <param name="description">Описание хоста</param> /// <returns>Возвращает созданный хост</returns> public LCTreeNodeHost AddHost(string nameHost, string ip, string description) { // создаем новый узел Host LCTreeNodeHost lcTreeNodeHost = new LCTreeNodeHost { Text = nameHost, IP = ip, Barcode = "", Login = "", Password = "", Description = description, ContextMenuStrip = LCTreeNode.computerContextMenuStrip, ImageIndex = 3 }; this.Nodes.Add(lcTreeNodeHost); lcTreeNodeHost.UpdateLC(); this.WriteListBoxOperation("Добавлен компьтер : " + nameHost); return(lcTreeNodeHost); }
/// <summary> /// Метод добавления дочерних узлов в дерево /// </summary> /// <param name="xnod">XML элемент</param> /// <param name="newNode">Компонент TreeNode в котором создаются узлы</param> private void AddChildrenDOMXML(XElement xnod, TreeNode newNode) { if (xnod.NodeType == System.Xml.XmlNodeType.Element) { switch (xnod.Name.ToString()) { case "Group": { LCTreeNodeGroup lcTreeNodeGroup = new LCTreeNodeGroup { Text = xnod.Attribute("NameGroup").Value, Description = xnod.Attribute("Description").Value, ContextMenuStrip = LCTreeNode.groupContextMemuStrip, ImageIndex = 2 }; newNode.Nodes.Add(lcTreeNodeGroup); lcTreeNodeGroup.UpdateLC(); newNode = lcTreeNodeGroup; } break; case "NoList": { LCTreeNodeNoList lcTreeNodeNoList = new LCTreeNodeNoList { Text = xnod.Attribute("NameGroup").Value, Description = xnod.Attribute("Description").Value, ContextMenuStrip = LCTreeNode.noListContextMenuStrip, ImageIndex = 2 }; lcTreeNodeNoList.ToolTipText += lcTreeNodeNoList.Text; lcTreeNodeNoList.ToolTipText += "\n" + lcTreeNodeNoList.Description; newNode.Nodes.Add(lcTreeNodeNoList); newNode = lcTreeNodeNoList; } break; case "Host": { LCTreeNodeHost lcTreeNodeHost = new LCTreeNodeHost { Text = xnod.Attribute("NameHost").Value, IP = xnod.Attribute("IP").Value, Barcode = xnod.Attribute("Barcode").Value, Login = xnod.Attribute("Login").Value, Password = xnod.Attribute("Password").Value, Description = xnod.Attribute("Description").Value, TypeHost = (LCTypeHost)Enum.Parse(typeof(LCTypeHost), xnod.Attribute("TypeHost").Value), ContextMenuStrip = LCTreeNode.computerContextMenuStrip }; newNode.Nodes.Add(lcTreeNodeHost); lcTreeNodeHost.UpdateLC(); return; } case "Subnet": { LCTreeNodeSubnet lcTreeNodeSubnet = new LCTreeNodeSubnet { Text = xnod.Attribute("NameSubnet").Value, IPSubnet = xnod.Attribute("IPSubnet").Value, MaskSubnet = xnod.Attribute("MaskSubnet").Value, Description = xnod.Attribute("Description").Value, ContextMenuStrip = LCTreeNode.subnetContextMenuStrip, ImageIndex = 5 }; newNode.Nodes.Add(lcTreeNodeSubnet); lcTreeNodeSubnet.UpdateLC(); newNode = lcTreeNodeSubnet; } break; case "Root": { //MessageBox.Show("yes"); //newNode.ImageIndex = 1; //newNode.Text = mapAttributes.Item(1).Value; } break; } foreach (XElement element in xnod.Elements()) { AddChildrenDOMXML(element, newNode); } } }
/// <summary> /// Метод открытия узла дерева. /// </summary> /// <param name="treeNode">Открываемый узел.</param> private void OpenLCTreeNode(TreeNode treeNode) { if (treeNode == null) { return; } LCTreeNode lcTreeNode = (LCTreeNode)treeNode; switch (lcTreeNode.LCObjectType) { case LCObjectType.Host: { this.tabControlObject.SelectedTab = this.tabPageHosts; LCTreeNodeHost lcHost = (LCTreeNodeHost)lcTreeNode; // Делаем активным список хостов this.listViewHosts.Focus(); foreach (ListViewItem curilv in this.listViewHosts.Items) { if (curilv.Tag == lcHost) { // Выделяем нужный элемент в списке curilv.Selected = true; return; } } // определяем родительскую группу элемента LCTreeNode lcNode = (LCTreeNode)lcHost.Parent; // Пока сделано так, в будущем предполагаются что ПК состоят только в сетях. Кроме группы <Не в списке> if (lcNode.LCObjectType == LCObjectType.SubNet) { ListViewGroup lvg = null; foreach (ListViewGroup curGroup in this.listViewHosts.Groups) { if (curGroup.Tag == lcNode) { // Группа существует lvg = curGroup; break; } } if (lvg == null) { // Такой группы еще нет, надо её создать lvg = new ListViewGroup(lcNode.Text); } lvg.Tag = lcNode; this.listViewHosts.Groups.Add(lvg); ListViewItem lvi = new ListViewItem(new string[] { lcHost.TypeHost.ToString(), lcHost.IP, lcHost.Text, lcHost.ParentGroup, lcHost.DescriptionStr }, lvg) { Tag = lcHost }; lcHost.Tag = lvi; this.listViewHosts.Items.Add(lvi); lcHost.UpdateLC(); lvi.Selected = true; } else { ListViewItem lvi = new ListViewItem(new string[] { lcHost.TypeHost.ToString(), lcHost.IP, lcHost.Text, lcHost.ParentGroup, lcHost.DescriptionStr }) { Tag = lcHost }; lcHost.Tag = lvi; this.listViewHosts.Items.Add(lvi); lcHost.UpdateLC(); lvi.Selected = true; } break; } case LCObjectType.SubNet: { this.tabControlObject.SelectedTab = this.tabPageSubnets; this.listViewSubnets.Focus(); LCTreeNodeSubnet lcSubnet = (LCTreeNodeSubnet)lcTreeNode; foreach (ListViewItem cutilv in this.listViewSubnets.Items) { if (cutilv.Tag == lcSubnet) { cutilv.Selected = true; return; } } ListViewItem lvi = new ListViewItem(new string[] { lcSubnet.Text, lcSubnet.IPSubnet, lcSubnet.MaskSubnet, lcSubnet.ParentGroup, lcSubnet.DescriptionStr }) { Tag = lcSubnet }; lcSubnet.Tag = lvi; this.listViewSubnets.Items.Add(lvi); lvi.Selected = true; break; } case LCObjectType.Group: { this.tabControlObject.SelectedTab = this.tabPageGroups; this.listViewGroups.Focus(); LCTreeNodeGroup lcGroup = (LCTreeNodeGroup)lcTreeNode; foreach (ListViewItem curilv in this.listViewGroups.Items) { if (curilv.Tag == lcGroup) { curilv.Selected = true; return; } } ListViewItem lvi = new ListViewItem(new string[] { lcGroup.Text, lcGroup.ParentGroup, lcGroup.DescriptionStr }) { Tag = lcGroup }; lcGroup.Tag = lvi; this.listViewGroups.Items.Add(lvi); lvi.Selected = true; break; } } }