private void InitializeWorkbenchWindowHandler(object sender, dvEventArgs e)
        {
            // backend is about to start a new child program to a workbench
            inUC TrimControl = e.Content;

            TrimControl.evOpenWin += new EventHandler <dvEventArgs>(OpenTrimWindowHandler);
        }
        private void InitializeTrimWindowHandler(object sender, dvEventArgs e)
        {
            // backend is about to start a new program, define event to use to open program in container
            inUC TrimControl = e.Content;

            //Move module referens for Trim component for the newley created instance. [Dashboard compliance]
            if (dvStartContainingModuleDic.ContainsKey((dvStart)sender))
            {
                inUCContainingModuleDic.Add(TrimControl, dvStartContainingModuleDic[(dvStart)sender]);
                dvStartContainingModuleDic.Remove((dvStart)sender);
            }

            TrimControl.evOpenWin += new EventHandler <dvEventArgs>(OpenTrimWindowHandler);
        }
        private void OpenTrimWindowHandler(object sender, dvEventArgs e)
        {
            // backend has created a new UserControl, show it as a new tab or new window or  ...
            inUC        TrimControl = e.Content;
            IDialogView OmsControl  = WorkItem.SmartParts.AddNew <DialogView>();

            OmsControl.Id = programStartSequence++;

            //Save the containing module for the OmsControl to use when it is closing. [Dashboard compliance]
            object containingModule = null;

            if (inUCContainingModuleDic.ContainsKey((inUC)sender))
            {
                containingModule = inUCContainingModuleDic[(inUC)sender];
                inUCContainingModuleDic.Remove((inUC)sender);
            }

            //System.Diagnostics.Debug.WriteLine("Opening new trim Dialog: " + OmsControl.Id);
            OmsControl.Show(TrimControl);
            SmartPartInfo spInfo = null;

            switch (e.Type)
            {
            case dvWinType.Main:
                ShellInteractionService.Show(OmsControl);
                OpenTabs.Add(OmsControl);
                break;

            case dvWinType.Modal:
                // currently not used!
                break;

            case dvWinType.Float:
                // currently not used!
                break;
            }

            TrimControl.evCloseWin += (s, args) =>
            {
                // backend confirms it is OK to close a program
                OmsControl.OKToClose();
                //System.Diagnostics.Debug.WriteLine("About to close Dialog " + OmsControl.Id);

                if (OmsControl.ParentProgram != null)
                {
                    IDialogView parent = OmsControl.ParentProgram;
                    parent.RemoveChildProgram(OmsControl);
                    OmsControl.ParentProgram = null;
                }

                switch (e.Type)
                {
                case dvWinType.Main:

                    //Check containing module and use it´s ShellInteractionService to close the OmsControl. [Dashboard compliance]
                    if (containingModule != null)
                    {
                        if (containingModule.GetType().GetProperty("ShellInteractionService") != null)
                        {
                            ((IShellInteractionService)containingModule.GetType().GetProperty("ShellInteractionService").GetValue(containingModule, null)).Close(OmsControl);
                        }
                    }
                    else
                    {
                        ShellInteractionService.Close(OmsControl);
                    }

                    WorkItem.SmartParts.Remove(OmsControl);
                    OpenTabs.Remove(OmsControl);
                    if (OpenTabs.Count == 0 && isWindowCloseRequested)
                    {
                        // user has requested to close the window and no open programs remain
                        Application.Current.MainWindow.Close();
                    }
                    break;

                case dvWinType.Modal:
                    // currently not used
                    break;

                case dvWinType.Float:
                    // currently not used
                    break;
                }
            };

            TrimControl.evTitleWin += (s, args) =>
            {
                // set title and description (as tooltip on tab)
                string   programInfo      = (string)((FrameworkElement)TrimControl).Tag;
                char[]   c                = { ' ' };
                string[] programInfoSplit = programInfo.Split(c);
                string   title            = programInfoSplit[0];
                int      titleLength      = title.Length;
                if (OmsControl.ParentProgram != null)
                {
                    string parentTitle = OmsControl.ParentProgram.ProgramName;
                    title = parentTitle + " > " + title;
                }
                OmsControl.ProgramName = title;
                string description = null;
                if (programInfo.Length > titleLength)
                {
                    description = programInfo.Substring(titleLength + 1).Trim();
                    OmsControl.ProgramDescription = description;
                }

                if (startParametersDictionary.ContainsKey(title))
                {
                    string startParams = startParametersDictionary[title];
                    OmsControl.StartParameters = startParams;
                    startParametersDictionary.Remove(title);
                }

                //System.Diagnostics.Debug.WriteLine("Setting title on Dialog " + OmsControl.Id + " to " + title);

                switch (e.Type)
                {
                case dvWinType.Main:
                    spInfo             = new SmartPartInfo();
                    spInfo.Title       = title;
                    spInfo.Description = description;
                    ShellInteractionService.Show(OmsControl, spInfo);

                    break;

                case dvWinType.Float:
                    // currently not used
                    break;
                }
            };

            if (workbenchParentView != null)
            {
                // backend is about to start a new sub program to a workbench
                workbenchParentView.AddChildProgram(OmsControl);
                OmsControl.ParentProgram = workbenchParentView;
                workbenchParentView      = null;
            }

            TrimControl.evSendString += (s, args) =>
            {
                // event received when starting and stopping workbench programs
                // or when the actions menu is to be updated
                string receivedStr = (string)((FrameworkElement)TrimControl).Tag;
                receivedStr = System.Web.HttpUtility.HtmlDecode(receivedStr);
                if (receivedStr.StartsWith("startProgram"))
                {
                    OmsControl.IsWorkbenchParent = true;
                    workbenchParentView          = OmsControl;
                    dvStart TrimStartClient = new dvStart();
                    TrimStartClient.evCreate += new EventHandler <dvEventArgs>(InitializeWorkbenchWindowHandler);
                    try
                    {
                        string startWorkbenchProgXML = createStartWorkbenchProgramXml(receivedStr);
                        TrimStartClient.newWindow(startWorkbenchProgXML);
                    }
                    catch (SecurityTokenExpiredException ex)
                    { }
                }
                else if (receivedStr.StartsWith("stopProgram"))
                {
                    foreach (IDialogView childProgram in OmsControl.GetChildPrograms())
                    {
                        object childObject = childProgram.TrimControl;
                        if (childObject is inUC)
                        {
                            inUC childControl = childObject as inUC;
                            childControl.requestClose();
                        }
                    }
                }
                else if (receivedStr.StartsWith("actions:"))
                {
                    OmsControl.ShowActions(receivedStr.Substring(8));
                }
                else if (receivedStr.StartsWith("savesearch:"))
                {
                    OmsControl.SaveSeach(receivedStr.Substring(11));
                }
                else if (receivedStr.StartsWith("savedashboard:"))
                {
                    OmsControl.SaveInDashboard(receivedStr.Substring(14));
                }
            };
        }