/// <summary>
        /// Groups all annotations of annotation collection.
        /// </summary>
        /// <param name="annotationViewer">The annotation viewer.</param>
        /// <param name="undoManager">The undo manager.</param>
        public static void GroupAllAnnotations(AnnotationViewer annotationViewer, CompositeUndoManager undoManager)
        {
            // cancel the annotation building
            annotationViewer.CancelAnnotationBuilding();

            // get reference to the annotation collection of focused image
            AnnotationDataCollection annotationDataCollection = annotationViewer.AnnotationDataController[annotationViewer.FocusedIndex];

            if (annotationDataCollection.Count == 0)
            {
                return;
            }

            // begin the composite undo action
            undoManager.BeginCompositeAction("Group all annotations");

            try
            {
                // save the references to annotations in array
                AnnotationData[] annotationDataArray = annotationDataCollection.ToArray();

                // clear the annotation collection of focused image
                annotationDataCollection.Clear();

                // create the group annotation
                GroupAnnotationData groupAnnotationData = new GroupAnnotationData();

                // for each annotation in array
                for (int i = 0; i < annotationDataArray.Length; i++)
                {
                    // add annotations from array to group annotation
                    groupAnnotationData.Items.Add(annotationDataArray[i]);
                }

                // add the group annotation to the annotation collection of focused image
                annotationDataCollection.Add(groupAnnotationData);

                annotationViewer.FocusedAnnotationData = groupAnnotationData;
            }
            finally
            {
                // end the composite undo action
                undoManager.EndCompositeAction();
            }
        }
        /// <summary>
        /// Groups/ungroups selected annotations of annotation collection.
        /// </summary>
        /// <param name="annotationViewer">The annotation viewer.</param>
        /// <param name="undoManager">The undo manager.</param>
        public static void GroupUngroupSelectedAnnotations(AnnotationViewer annotationViewer, CompositeUndoManager undoManager)
        {
            // cancel annotation building
            annotationViewer.CancelAnnotationBuilding();

            // get selected annotations
            Collection <AnnotationView> selectedAnnotations = annotationViewer.AnnotationVisualTool.SelectedAnnotations;

            if (selectedAnnotations.Count == 0)
            {
                return;
            }

            // if several annotations are selected
            if (selectedAnnotations.Count > 1)
            {
                // begin the composite undo action
                undoManager.BeginCompositeAction("Group annotations");

                try
                {
                    // create group annotation data
                    GroupAnnotationData groupAnnotationData = new GroupAnnotationData();
                    // create annotation view array
                    AnnotationView[] annotations = new AnnotationView[selectedAnnotations.Count];
                    // copy selected annotations to array
                    selectedAnnotations.CopyTo(annotations, 0);
                    // for each annotation in array
                    foreach (AnnotationView view in annotations)
                    {
                        groupAnnotationData.Items.Add(view.Data);
                        annotationViewer.AnnotationDataCollection.Remove(view.Data);
                    }
                    // add group annotation to viewer
                    annotationViewer.AnnotationDataCollection.Add(groupAnnotationData);
                    // update focused annotation data
                    annotationViewer.FocusedAnnotationData = groupAnnotationData;
                    // update selected annotations
                    annotationViewer.AnnotationVisualTool.SelectedAnnotations.Clear();
                    annotationViewer.AnnotationVisualTool.SelectedAnnotations.Add(annotationViewer.FocusedAnnotationView);
                }
                finally
                {
                    // end the composite undo action
                    undoManager.EndCompositeAction();
                }
            }
            else
            {
                GroupAnnotationView groupAnnotationView = annotationViewer.AnnotationVisualTool.SelectedAnnotations[0] as GroupAnnotationView;
                // if focused annotation is group
                if (groupAnnotationView != null)
                {
                    // begin the composite undo action
                    undoManager.BeginCompositeAction("Ungroup annotations");

                    try
                    {
                        // clear selected annotations
                        selectedAnnotations.Clear();

                        // get annotation data
                        GroupAnnotationData groupAnnotationData = (GroupAnnotationData)groupAnnotationView.Data;
                        // remove group annotation from annotation data collection
                        annotationViewer.AnnotationDataCollection.Remove(groupAnnotationData);
                        // add group annotation items to annotation data collection
                        annotationViewer.AnnotationDataCollection.AddRange(groupAnnotationData.Items.ToArray());
                        // if the annotation viewer allows multiple selection of annotations
                        if (annotationViewer.AnnotationMultiSelect)
                        {
                            // for each annotation data in group annotation items
                            foreach (AnnotationData itemData in groupAnnotationData.Items)
                            {
                                selectedAnnotations.Add(annotationViewer.AnnotationViewCollection.FindView(itemData));
                            }
                        }

                        // remove annotations from group annotation
                        groupAnnotationData.Items.Clear();
                        // dispose group annotation
                        groupAnnotationData.Dispose();
                    }
                    finally
                    {
                        // end the composite undo action
                        undoManager.EndCompositeAction();
                    }
                }
            }
        }