public static void InitializeCommandTree(NodeWithName document)
 {
     // Initialize commands
     document.Initialize();
     var elements = Flatten(document.Children);
     Parallel.ForEach(elements, e => e.Initialize());
 }
        public void ApplyCriteria(string criteria, Stack<NodeWithName> ancestors,NodeWithName startPoint)
        {
            if (IsCriteriaMatched(criteria, startPoint))
            {
                startPoint.IsVisible = true;
                foreach (var ancestor in ancestors)
                {
                    ancestor.IsVisible = true;
                    ancestor.Expanded = !String.IsNullOrEmpty(criteria);
                }
            }
            else
                startPoint.IsVisible = false;

            ancestors.Push(startPoint);
                foreach (var child in startPoint.Children)
                    if(child.Type != null && !child.Type.Contains("ImageFrame" )&&
                       !child.Type.Contains("Polygon") && !child.Type.Contains("PolyPoint") )
                        ApplyCriteria(criteria, ancestors,child as NodeWithName);

                ancestors.Pop();
        }
        //when we hit enter in the mainwindow and we are on a pgroup we want to jump to the next frame and open the same p group
        public void JumpToNextImageFrame()
        {
            //if we have an polygroup selected go up to the parent imagedata and go to the next child frame and then open to the polygroup.
            if (_currentSelectedNode is PolygonGroup)
            {
                //Close all the previous children
                var parentFrame = ((_currentSelectedNode as PolygonGroup).Parent as ImageFrame);
                if ( parentFrame != null)
                {
                    parentFrame.Expanded = false;
                    foreach (var child in parentFrame.Children)
                    {
                        (child as NodeWithName).Expanded = false;
                        (child as NodeWithName).IsSelected = false;
                    }
                }

                //Get the index of the current Frame from the ImageData and close and deselect the imageFrame
                var index = (parentFrame.Parent as ImageData).Children.IndexOf(parentFrame);
                ((parentFrame.Parent as ImageData).Children[index] as
                    ImageFrame).IsSelected = false;
                ((parentFrame.Parent as ImageData).Children[index] as
                    ImageFrame).Expanded = false;

                //Check to see if there is another frame or not
                if (index + 1 < ((parentFrame.Parent as ImageData).Children.Count))
                {
                    ++index;
                    //Set the viewmodellocator varialbes and Open the next Frame
                    _viewModelLocator.ImageFrameView.Frame = ((parentFrame.Parent as ImageData).Children[index] as ImageFrame);
                    _viewModelLocator.ImageFrameView.Frame.Expanded = true;
                    _viewModelLocator.ImageFrameView.Polygon = null;
                    _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true;
                    _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                    CurrentView = _viewModelLocator.ImageFrameView;
                    var pGroup = new PolygonGroup();
                    foreach (var groups in _viewModelLocator.ImageFrameView.Frame.Children)
                    {
                        if (groups.Name == _lastPolygonGroupName)
                        {
                            pGroup = groups as PolygonGroup;
                        }
                    }
                    if (pGroup.Name == "New Polygon Group" && _viewModelLocator.ImageFrameView.Frame.Children.Count >0)
                    {
                        pGroup = _viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup;
                    }
                    else if (_viewModelLocator.ImageFrameView.Frame.Children.Count == 0)
                    {
                        _viewModelLocator.ImageFrameView.Frame.IsSelected = true;
                        return;
                    }

                    (pGroup as PolygonGroup).Expanded = true;

                    _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup;
                    //Set the current node to the first Polygon of the group
                    if ((pGroup as NodeWithName).Children.Count != 0)
                    {
                        _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon;
                        ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true;
                    }
                    else
                    {
                        (pGroup as PolygonGroup).IsSelected = true;
                        _currentSelectedNode = (pGroup as NodeWithName);
                    }
                }
                //if we are about to go OVER the count of children, we should try to go to the next image entirely
                else
                {
                    var folder = _viewModelLocator.ImageView.Image.Parent as Folder;
                    //find the image index in the folder(since every image has to at some point be in a folder this shouldn't be a problem
                    var imageIndex = folder.Images.IndexOf(parentFrame.Parent as ImageData);

                    if (imageIndex + 1 < folder.Images.Count)
                    {
                        ++imageIndex;
                    }

                    var nextImage = folder.Images[imageIndex];

                    //right before we change image and frames, we de expand them first.
                    parentFrame.Expanded = false;
                    (parentFrame.Parent as ImageData).Expanded = false;

                    //Set the viewmodellocator varialbes and Open the next Frame
                    _viewModelLocator.ImageFrameView.Frame = nextImage.Children[0] as ImageFrame;
                    _viewModelLocator.ImageFrameView.Frame.Expanded = true;
                    (_viewModelLocator.ImageFrameView.Frame.Parent as ImageData).Expanded = true;
                    _viewModelLocator.ImageFrameView.Polygon = null;
                    _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true;
                    _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                    CurrentView = _viewModelLocator.ImageFrameView;

                    var pGroup = new PolygonGroup();
                    foreach (var groups in _viewModelLocator.ImageFrameView.Frame.Children)
                    {
                        if (groups.Name == _lastPolygonGroupName)
                        {
                            pGroup = groups as PolygonGroup;
                        }
                    }
                    if (pGroup.Name == "New Polygon Group")
                    {
                        pGroup = _viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup;
                    }

                    (pGroup as PolygonGroup).Expanded = true;

                    _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup;
                    //Set the current node to the first Polygon of the group
                    if ((pGroup as NodeWithName).Children.Count != 0)
                    {
                        _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon;
                        ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true;
                    }
                    else
                    {
                        (pGroup as PolygonGroup).IsSelected = true;
                        _currentSelectedNode = (pGroup as NodeWithName);
                    }
                }
            }
                //pretty the same as the above just with the currentselected as the polygon itself so you don't have to go back up to the parent Polygroup
            else if (_currentSelectedNode is Polygon)
            {
                //Close all the previous children
                var parentFrame = ((_currentSelectedNode as Polygon).Parent as PolygonGroup).Parent as ImageFrame;
                if (parentFrame != null)
                {
                    parentFrame.Expanded = false;
                    foreach (var child in parentFrame.Children)
                    {
                        (child as NodeWithName).Expanded = false;
                        (child as NodeWithName).IsSelected = false;
                    }
                }

                var index = (parentFrame.Parent as ImageData).Children.IndexOf(parentFrame);
                ((parentFrame.Parent as ImageData).Children[index] as
                    ImageFrame).IsSelected = false;
                ((parentFrame.Parent as ImageData).Children[index] as
                    ImageFrame).Expanded = false;

                if (index + 1 < ((parentFrame.Parent as ImageData).Children.Count))
                {
                    ++index;
                    _viewModelLocator.ImageFrameView.Frame = ((parentFrame.Parent as ImageData).Children[index] as ImageFrame);
                    _viewModelLocator.ImageFrameView.Frame.Expanded = true;
                    _viewModelLocator.ImageFrameView.Polygon = null;
                    _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true;
                    _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                    CurrentView = _viewModelLocator.ImageFrameView;

                    if(_viewModelLocator.ImageFrameView.Frame.Children.Count >0)
                    {
                        var pGroup = _viewModelLocator.ImageFrameView.Frame.Children.First(t => t.Name == _lastPolygonGroupName);

                        (pGroup as PolygonGroup).Expanded = true;

                        //double check if there is a polygon to set onto next frame else just select the group
                        if ((pGroup as PolygonGroup).Children.Count != 0)
                        {
                            ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true;
                            _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon;
                        }
                        else
                        {
                            (pGroup as PolygonGroup).IsSelected = true;
                            _currentSelectedNode = (pGroup as NodeWithName);
                        }
                        _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup;
                    }
                    else
                    {
                        _viewModelLocator.ImageFrameView.Frame.IsSelected = true;
                    }
                }
                //if we are about to go OVER the count of children, we should try to go to the next image entirely
                else
                {
                    var folder = _viewModelLocator.ImageView.Image.Parent as Folder;
                    //find the image index in the folder(since every image has to at some point be in a folder this shouldn't be a problem
                    var imageIndex = folder.Images.IndexOf(parentFrame.Parent as ImageData);

                    if (imageIndex + 1 < folder.Images.Count)
                    {
                        ++imageIndex;
                    }

                    var nextImage = folder.Images[imageIndex];

                    //right before we change image and frames, we de expand them first.
                    parentFrame.Expanded = false;
                    (parentFrame.Parent as ImageData).Expanded = false;

                    //Set the viewmodellocator varialbes and Open the next Frame
                    _viewModelLocator.ImageFrameView.Frame = nextImage.Children[0] as ImageFrame;
                    _viewModelLocator.ImageFrameView.Frame.Expanded = true;
                    (_viewModelLocator.ImageFrameView.Frame.Parent as ImageData).Expanded = true;
                    _viewModelLocator.ImageFrameView.Polygon = null;
                    _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true;
                    _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                    CurrentView = _viewModelLocator.ImageFrameView;
                    var pGroup = _viewModelLocator.ImageFrameView.Frame.Children.First(t => t.Name == _lastPolygonGroupName);

                    (pGroup as PolygonGroup).Expanded = true;

                    _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup;
                    //Set the current node to the first Polygon of the group
                    if ((pGroup as NodeWithName).Children.Count != 0)
                    {
                        _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon;
                        ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true;
                    }
                    else
                    {
                        (pGroup as PolygonGroup).IsSelected = true;
                        _currentSelectedNode = (pGroup as NodeWithName);
                    }
                }
            }
            else if (CurrentSelectedNode is ImageFrame)
            {
                //Close all the previous children
                var parentFrame = _currentSelectedNode as ImageFrame;
                if (parentFrame != null)
                {
                    parentFrame.Expanded = false;
                    foreach (var child in parentFrame.Children)
                    {
                        (child as NodeWithName).Expanded = false;
                        (child as NodeWithName).IsSelected = false;
                    }
                }
                //Close the last imageframe we had open.
                var index = (parentFrame.Parent as ImageData).Children.IndexOf(parentFrame);
                ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).IsSelected = false;
                ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).Expanded = false;

                //if there's frames left in the image, we'll open the next one
                if (index + 1 < ((parentFrame.Parent as ImageData).Children.Count))
                {
                    ++index;
                    _viewModelLocator.ImageFrameView.Frame = ((parentFrame.Parent as ImageData).Children[index] as ImageFrame);
                    _viewModelLocator.ImageFrameView.Frame.Expanded = true;
                    _viewModelLocator.ImageFrameView.Polygon = null;
                    _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true;
                    _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                    CurrentView = _viewModelLocator.ImageFrameView;
                    if (_viewModelLocator.ImageFrameView.Frame.Children.Count > 0)
                    {
                        (_viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup).IsSelected = true;
                    }
                    else
                    {
                        _viewModelLocator.ImageFrameView.Frame.IsSelected = true;
                    }
                }
                else
                {
                    var folder = _viewModelLocator.ImageView.Image.Parent as Folder;
                    //find the image index in the folder(since every image has to at some point be in a folder this shouldn't be a problem
                    var imageIndex = folder.Images.IndexOf(parentFrame.Parent as ImageData);

                    if (imageIndex + 1 < folder.Images.Count)
                    {
                        ++imageIndex;
                    }

                    var nextImage = folder.Images[imageIndex];

                    //right before we change image and frames, we de expand them first.
                    parentFrame.Expanded = false;
                    (parentFrame.Parent as ImageData).Expanded = false;

                    //Set the viewmodellocator varialbes and Open the next Frame
                    _viewModelLocator.ImageFrameView.Frame = nextImage.Children[0] as ImageFrame;
                    //expand and select it.
                    _viewModelLocator.ImageFrameView.Frame.Expanded = true;
                    _viewModelLocator.ImageFrameView.Frame.IsSelected = true;
                    //expand the image itself to show the frames
                    (_viewModelLocator.ImageFrameView.Frame.Parent as ImageData).Expanded = true;
                    _viewModelLocator.ImageFrameView.Polygon = null;
                    _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true;
                    _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                    CurrentView = _viewModelLocator.ImageFrameView;
                    //Try to open the first polygroup if there is one
                    if (_viewModelLocator.ImageFrameView.Frame.Children.Count > 0)
                    {
                        (_viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup).IsSelected = true;
                    }
                    else
                    {
                        _viewModelLocator.ImageFrameView.Frame.IsSelected = true;
                    }
                }
            }
        }
        public void FindMatches(string criteria, string fullPath, Stack<NodeWithName> ancestors, NodeWithName startPoint)
        {
            if (IsCriteriaMatched(criteria, startPoint))
            {
                if(startPoint is ImageData)
                {
                    (startPoint as ImageData).ReimportImageData(fullPath);

                    MessageBox.Show(String.Format("We reimported {0} successfully.", Path.GetFileNameWithoutExtension(criteria)));

                }
            }

            ancestors.Push(startPoint);
            if (startPoint.Children != null && startPoint.Children.Count > 0 )
            {
                foreach (var child in startPoint.Children)
                    if (child.Type != null && !child.Type.Contains(typeof(ImageFrame).ToString()) &&
                       (child.Type != "Polygon" && child.Type !=  "PolyPoint" && child.Type != "PolygonGroup"))
                        FindMatches(criteria, fullPath, ancestors, child as NodeWithName);
            }

            ancestors.Pop();
        }
        public void ExecuteSelectedItemChangedCommand(object o)
        {
            var e = o as RoutedPropertyChangedEventArgs<object>;
            //To enable Delete key to remove nodes, we will store the last item you clicked on
            //cause you won't try to delete something without clicking on it anyways

            if (e.NewValue is Document)
            {
                _viewModelLocator.DocumentView.Document = e.NewValue as Document;
                CurrentView = _viewModelLocator.DocumentView;
                _currentSelectedNode = e.NewValue as Document;
                _currentSelectedNode.Type = "Document";
            }
            else if (e.NewValue is Folder)
            {
                _viewModelLocator.FolderView.Folder = e.NewValue as Folder;
                CurrentView = _viewModelLocator.FolderView;
                _currentSelectedNode = e.NewValue as Folder;
                _currentSelectedNode.Type = "Folder";
            }
            else if (e.NewValue is ImageData)
            {
                _viewModelLocator.ImageView.Image = e.NewValue as ImageData;
                CurrentView = _viewModelLocator.ImageView;
                _currentSelectedNode = e.NewValue as ImageData;
                _currentSelectedNode.Type = "ImageData";
            }
            else if (e.NewValue is ImageFrame)
            {
                _viewModelLocator.ImageFrameView.Frame = e.NewValue as ImageFrame;
                _viewModelLocator.ImageFrameView.PolygonGroup = null;
                _viewModelLocator.ImageFrameView.Polygon = null;
                _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = false;
                _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                CurrentView = _viewModelLocator.ImageFrameView;

                _currentSelectedNode = e.NewValue as ImageFrame;
                _currentSelectedNode.Type = "ImageFrame";

            }
            else if (e.NewValue is PolygonGroup)
            {
                _viewModelLocator.ImageFrameView.Frame = (e.NewValue as PolygonGroup).Parent as ImageFrame;

                //Save the polygroup name that has been last opened, we'll use this to try and Drill down to the
                //same polygroup name when going to a new frame.
                _viewModelLocator.ImageFrameView.PolygonGroup = e.NewValue as PolygonGroup;
                _lastPolygonGroupName = _viewModelLocator.ImageFrameView.PolygonGroup.Name;
                _viewModelLocator.ImageFrameView.Polygon = null;
                _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true;
                _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false;
                CurrentView = _viewModelLocator.ImageFrameView;
                _currentSelectedNode = e.NewValue as PolygonGroup;
                _currentSelectedNode.Type = "PolygonGroup";
            }
            else if (e.NewValue is Polygon)
            {
                _viewModelLocator.ImageFrameView.Frame = ((e.NewValue as Polygon).Parent as PolygonGroup).Parent as ImageFrame;
                _viewModelLocator.ImageFrameView.PolygonGroup = ((e.NewValue as Polygon).Parent as PolygonGroup);
                _viewModelLocator.ImageFrameView.Polygon = e.NewValue as Polygon;
                _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = false;
                _viewModelLocator.ImageFrameView.ShowPolygonTextBox = true;
                CurrentView = _viewModelLocator.ImageFrameView;
                _currentSelectedNode = e.NewValue as Polygon;
                _currentSelectedNode.Type = "Polygon";
            }
            else
            {
                CurrentView = null;
            }
        }
 public void ExecuteCopyCommand(object o)
 {
     //put the polygon/group data in a copy variable (as the _currentSelectedNode will end up being whatever you click to paste in)
     _copyData = _currentSelectedNode;
 }
        //method to create a new polygon with a button click etc.
        public void CreateNewPolygon()
        {
            if (_currentSelectedNode is Polygon)
            {
                var group = _currentSelectedNode.Parent as PolygonGroup;
                var newPoly = new Polygon();
                newPoly.Parent = group;
                newPoly.Name = "Polygon " + (group.Children.Count + 1);
                newPoly.Initialize();

                group.Children.Add(newPoly);

                var index = group.Children.IndexOf(newPoly);
                (group.Children[index] as Polygon).IsSelected = true;
                _currentSelectedNode = group.Children[index] as NodeWithName;
            }
        }
 private bool IsCriteriaMatched(string criteria, NodeWithName check)
 {
     return String.IsNullOrEmpty(criteria) || check.Name.ToLower() == (criteria.ToLower()) && check is ImageData;
 }
 private static void TransformPolygon(NodeWithName @group, IEnumerable<Vector2> points, string name)
 {
     var poly = new Polygon {Name = name, Parent = @group};
     foreach (var point in points)
     {
         var x = ConvertUnits.ToDisplayUnits(point.X);
         var y = ConvertUnits.ToDisplayUnits(point.Y);
         poly.AddChild(new PolyPoint((int) x, (int) y) {Parent = poly});
     }
     @group.AddChild(poly);
 }
        private bool IsCriteriaMatched(string criteria, NodeWithName check)
        {
            if(!ExcludeApproved)
                return String.IsNullOrEmpty(criteria) || check.Name.ToLower().Contains(criteria.ToLower());

            if(check.Approved)
                return false;

                return String.IsNullOrEmpty(criteria) || check.Name.ToLower().Contains(criteria.ToLower());
        }