/// <summary>
        /// Create and return an application node
        /// </summary>
        /// <param name="app"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        private TreeNode CreateApplicationNode(IHostedApplication app, Microsoft.Ccf.Csr.Session session)
        {
            // Add the app's icon to the tabControl's list
            ImageList imageList = app.GetIconList();

            if (imageList != null && imageList.Images.Count > 0)
            {
                System.Drawing.Image image = imageList.Images[0];
                sessionsTree.ImageList.Images.Add(image);
            }

            TreeNode node = new TreeNode(app.ApplicationName);

            node.Tag = app;

            // if there was no image for this application, then the image for
            // the previous application will be used.
            node.ImageIndex         = sessionsTree.ImageList.Images.Count - 1;
            node.SelectedImageIndex = node.ImageIndex;

            if (session != null)
            {
                // If this app has the current selection, select it in the tree
                if (app == session.FocusedApplication)
                {
                    sessionsTree.SelectedNode = node;
                }
            }

            return(node);
        }
        /// <summary>
        /// Removes a session from the session explorer tree.
        /// </summary>
        /// <param name="session"></param>
        public void RemoveSession(Microsoft.Ccf.Csr.Session session)
        {
            foreach (TreeNode node in sessionsTree.Nodes)
            {
                if (session == node.Tag)
                {
                    sessionsTree.Nodes.Remove(node);
                    break;
                }
            }

            updateFields();  // updates some fields to current state
        }
 /// <summary>
 /// Add tagged global and tagged non-global applications nodes to the Session Explorer
 /// </summary>
 public void AddWorkflowApplicationNodes(Microsoft.Ccf.Csr.Session session, bool selected)
 {
     // find the session
     foreach (TreeNode sessionNode in sessionsTree.Nodes)
     {
         if (session.Name.ToString() == sessionNode.Text.ToString())
         {
             // add each tagged application node to the tree under that session
             foreach (IHostedApplication app in session)
             {
                 if (session.AppHost.IsTaggedApplication(app))
                 {
                     sessionNode.Nodes.Add(CreateApplicationNode(app, session));
                 }
             }
             return;
         }
     }
 }
        /// <summary>
        /// Adds the session to the tree control if its not already there
        /// </summary>
        public void AddSession(Microsoft.Ccf.Csr.Session session, bool selected)
        {
            TreeNode parent, node;

            // if there is no session name, then hide this session
            if (session == null || session.Name == null)
            {
                return;
            }

            // see if active session is already in the session explorer
            foreach (TreeNode sessionNode in sessionsTree.Nodes)
            {
                if (sessionNode.Tag == session)
                {
                    sessionNode.Text = session.Name; // in case the name has changed
                    return;
                }
            }

            parent     = new TreeNode(session.Name);
            parent.Tag = session;

            // find where to insert this session node at based on the sort
            bool sortByName = (SortBy.SelectedItem as string == localize.SESSION_EXPLORER_STR_SORTBY_CUSTOMER_NAME);
            int  position   = 0;

            for (; position < sessionsTree.Nodes.Count; position++)
            {
                node = sessionsTree.Nodes[position];

                if (sortByName && parent.Text.CompareTo(node.Text) < 0)
                {
                    break;
                }
                if (!sortByName)
                {
                    Session oldSession = node.Tag as Session;
                    if (session != null && session.StartTime < oldSession.StartTime)
                    {
                        break;
                    }
                }
            }
            sessionsTree.Nodes.Insert(position, parent);

            // if asked to select this node or if its the active session, then select it
            if (selected || session == sessionManager.ActiveSession)
            {
                sessionsTree.SelectedNode = parent;
            }

            // Add the applications in this session to the session explorer view
            foreach (IHostedApplication app in session)
            {
                if ((app as IHostedApplication3) != null && (app as IHostedApplication3).IsListed)
                {
                    // This prevents external apps that are not integrated
                    // into CCF from being shown in the session explorer.
                    if (app.TopLevelWindow != null)
                    {
                        // if workflow has not started, empty workflow means no workflow has started
                        if (session.Workflow == string.Empty)
                        {
                            // only add untagged applications
                            if (!session.AppHost.IsTaggedApplication(app))
                            {
                                node = CreateApplicationNode(app, session);
                                parent.Nodes.Add(node);
                            }
                        }
                        else  //workflow has started so add all apps
                        {
                            node = CreateApplicationNode(app, session);
                            parent.Nodes.Add(node);
                        }
                    }
                }
            }

            sessionsTree.ExpandAll();

            updateFields();  // updates some fields to current state
        }