Example #1
0
 /// <summary>
 /// Handler of load button.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void loadButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         OpenFileDialog fileDialog = new OpenFileDialog();
         fileDialog.Filter = "Файлы bin | *.bin";
         if (fileDialog.ShowDialog() == true)
         {
             using (Stream stream = File.Open(fileDialog.FileName, FileMode.Open))
             {
                 var    binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                 MyItem tree            = binaryFormatter.Deserialize(stream) as MyItem;
                 treeView.Items.Clear();
                 foreach (MyItem item in tree.Items)
                 {
                     MyTreeViewItem newItem = new MyTreeViewItem()
                     {
                         Products = item.Products, Header = item.Header.ToString()
                     };
                     treeView.Items.Add(newItem);
                     Rebuild(item, newItem);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show($"Произошла ошибка:\n{ex.Message}", "Ошибка");
     }
 }
Example #2
0
 /// <summary>
 /// Add global node to treeView.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void addGlobalNodeButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (nodeName.Text == null || nodeName.Text == "")
         {
             MessageBox.Show("Напишите название этого узла.", "Ошибка");
             return;
         }
         var t = treeView.Items.OfType <TreeViewItem>();
         if (t.Select(x => x.Header).Contains(nodeName.Text))
         {
             MessageBox.Show("В этом узле уже существует такой под. узел.", "Ошибка");
             return;
         }
         var node = new MyTreeViewItem()
         {
             Header = nodeName.Text
         };
         treeView.Items.Add(node);
     }
     catch (Exception ex)
     {
         MessageBox.Show($"Произошла ошибка:\n{ex.Message}", "Ошибка");
     }
 }
Example #3
0
 /// <summary>
 /// Another walkthrough tree in order to delete correct product.
 /// </summary>
 /// <param name="tree"> Another tree node.</param>
 /// <param name="product"> Product to delete.</param>
 private void DeleteDown(MyTreeViewItem tree, Product product)
 {
     if (tree.Products.Remove(product))
     {
         return;
     }
     foreach (MyTreeViewItem item in tree.Items)
     {
         DeleteDown(item, product);
     }
 }
Example #4
0
 /// <summary>
 /// Recursive method for wathkthrough tree.
 /// </summary>
 /// <param name="node"> Another node.</param>
 private void GoDownTree(MyTreeViewItem node)
 {
     foreach (Product product in node.Products)
     {
         productsGrid.Items.Add(product);
     }
     foreach (MyTreeViewItem item in node.Items)
     {
         GoDownTree(item);
     }
 }
Example #5
0
 /// <summary>
 /// Rebuilding deserialized tree from file to UI.
 /// </summary>
 /// <param name="tree"> Tree from file.</param>
 /// <param name="uiTree"> Tree to paste in UI.</param>
 private void Rebuild(MyItem tree, MyTreeViewItem uiTree)
 {
     foreach (MyItem item in tree.Items)
     {
         MyTreeViewItem newItem = new MyTreeViewItem()
         {
             Products = item.Products, Header = item.Header.ToString()
         };
         uiTree.Items.Add(newItem);
         TreeBuilder(newItem, item);
     }
 }
Example #6
0
 /// <summary>
 /// Build tree in order to serialize.
 /// </summary>
 /// <param name="tree"> UI tree.</param>
 /// <param name="receiveTree"> Serializable tree.</param>
 private void TreeBuilder(MyTreeViewItem tree, MyItem receiveTree)
 {
     foreach (MyTreeViewItem item in tree.Items)
     {
         MyItem newItem = new MyItem()
         {
             Products = item.Products, Header = item.Header.ToString(), Parent = receiveTree
         };
         receiveTree.Items.Add(newItem);
         TreeBuilder(item, newItem);
     }
 }
Example #7
0
        /// <summary>
        /// Find product in all tree.
        /// </summary>
        /// <param name="tree"> Tree to find into.</param>
        /// <param name="product"> Product to find.</param>
        /// <param name="findProduct"> List of acceptable products.</param>
        private void FindProduct(MyTreeViewItem tree, Product product, List <Product> findProduct)
        {
            var cur = tree.Products.Find(x => x.Path == product.Path);

            if (cur != null)
            {
                findProduct.Add(cur);
            }
            foreach (MyTreeViewItem item in tree.Items)
            {
                FindProduct(item, product, findProduct);
            }
        }
Example #8
0
 /// <summary>
 /// Another walkthrough tree(Building path of node).
 /// </summary>
 /// <param name="tree"> Tree to walk.</param>
 /// <param name="sb"> String to build path.</param>
 private void GoThrough(MyTreeViewItem tree, StringBuilder sb)
 {
     foreach (var product in tree.Products)
     {
         if (int.TryParse(lowValue.Text, out int left) && product.Left < left)
         {
             sb.Append($"{string.Join('/', AncestorsAndSelf(tree))};{product.Code};{product.Name};{product.Left}{Environment.NewLine}");
         }
     }
     foreach (MyTreeViewItem item in tree.Items)
     {
         GoThrough(item, sb);
     }
 }
Example #9
0
        /// <summary>
        /// Get all Anscestors of a node and a node itself.
        /// </summary>
        /// <param name="tree"> Node of a tree.</param>
        /// <returns> List of anscestors and node itself.</returns>
        private List <string> AncestorsAndSelf(MyTreeViewItem tree)
        {
            List <string> items = new List <string>();
            var           cur   = tree;

            while (cur != null)
            {
                items.Add(cur.Header.ToString());
                if (cur.Parent == null)
                {
                    cur = null;
                }
                else
                {
                    cur = cur.Parent as MyTreeViewItem;
                }
            }

            items.Reverse();
            return(items);
        }