Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / WPF

How To Embed An Application Into a Docking Library

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
3 Sep 2011CPOL7 min read 29.3K   2.5K   19  
Step by Step conversion of an application into a Docking application component
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Sofa.Container;
using Sofa.Commons;
using System.IO;
using System.Windows;
using System.Deployment.Application;


namespace SofaBasicContainer
{
    public enum Mode
    {
        TemplatesOnly,
        All
    }

    public class PerspectiveHelper
    {
        #region Declarations

        SofaContainer sofaContainer;
        BaseWindow baseWindow;

        string optionalTemplateSelection = "<Optional template selection>";

        //Used for BaseWindow menu
        public List<string> perspectivesList
        {
            get
            {
                //List all perspectives (and not templates) in the Perspective directory.
                return listPerspectives(Mode.TemplatesOnly);
            }
        }

        string _perspectiveDirectoryName
        {
            get
            {
                if (!ApplicationDeployment.IsNetworkDeployed)
                    return string.Concat(ContainerParameter.DebugDirectoryLocation, "\\Sofa\\Perspectives");
                else
                    return string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\\Sofa\\Perspectives");
            }
        }

        #endregion Declarations

        public PerspectiveHelper(SofaContainer sofaContainer, BaseWindow baseWindow)
        {
            this.sofaContainer = sofaContainer;
            this.baseWindow = baseWindow;
        }

        #region Methods

        public void SaveCurrentPerspective()
        {
            if (sofaContainer.CurrentPerspective != null)
            {
                sofaContainer.SavePerspective(sofaContainer.CurrentPerspective);
            }
        }

        public void NewPerspective()
        {
            //PerspectiveName UserControl is used for both New and Rename actions. Customize it for New
            PerspectiveName perspectiveName = new PerspectiveName();
            perspectiveName.Name.Text = "";
            perspectiveName.textForTemplatesList.Text = "Template to use:";
            perspectiveName.templatesList.ItemsSource = listPerspectives(Mode.All);
            perspectiveName.templatesList.SelectedIndex = 0;
            perspectiveName.oldName.Visibility = Visibility.Collapsed;

            int res = 0;
            while (perspectiveName.Name.Text == "" && res != -1)
            {
                //Show the window in a "modal" mode and ask for name or Cancel
                perspectiveName.Owner = baseWindow;
                res = perspectiveName.DoModal();
                if (res != -1)
                {
                    if (perspectiveName.Name.Text == "")
                    {
                        MessageBoxResult result = MessageBox.Show(
                           "The currentPerspective name cannot be empty.",
                           "Error",
                           System.Windows.MessageBoxButton.OKCancel,
                           System.Windows.MessageBoxImage.Exclamation
                        );
                    }
                    else
                    {
                        SaveCurrentPerspective();
                        string templateName = (perspectiveName.templatesList.SelectedItem.ToString() == optionalTemplateSelection) ? null : perspectiveName.templatesList.SelectedItem.ToString();
                        //SofaContainer method to create a new perspective.
                        //SofaContainer "packaged" action: Also load the new perspective and update the SofaContainer.CurrentPerspective property and the 
                        Perspective p = sofaContainer.NewPerspective(perspectiveName.Name.Text, templateName);
                        UpdatePerspectiveStatus();
                    }
                }
            }
        }

        public void RenamePerspective()
        {
            //PerspectiveName UserControl is used for both New and Rename actions. Customize it for Rename
            PerspectiveName perspectiveName = new PerspectiveName();
            perspectiveName.Name.Text = "";
            perspectiveName.windowLabel.Text = "Rename perspective";
            perspectiveName.textForTemplatesList.Text = "Old name";
            perspectiveName.templatesList.Visibility = Visibility.Collapsed;
            perspectiveName.oldName.Text = sofaContainer.CurrentPerspective.Name;

            //Show the window in a "modal" mode and ask for name or Cancel
            perspectiveName.Owner = baseWindow;
            int res = perspectiveName.DoModal();
            if (res == 1)
            {
                string newName = perspectiveName.Name.Text;
                sofaContainer.RenamePerspective(sofaContainer.CurrentPerspective, newName);
                SaveCurrentPerspective();
                UpdatePerspectiveStatus();
            }
        }

        public void LoadPerspective(string perspectiveName)
        {
            SaveCurrentPerspective();
            Perspective p = sofaContainer.LoadPerspective(perspectiveName);
            UpdatePerspectiveStatus();
        }

        public void DeletePerspective()
        {
            //SofaContainer "packaged" action: Also deletes the xml file of the perspective 
            sofaContainer.DeletePerspective(sofaContainer.CurrentPerspective);
            sofaContainer.LoadPerspective(null);
            UpdatePerspectiveStatus();
        }

        #endregion Methods

        #region Utils

        string getNameFromFileName(string fileName)
        {
            fileName = fileName == null ? "" : fileName;
            int p = fileName.IndexOf(".xml");
            if (p != -1) fileName = fileName.Substring(0, p);
            string[] fileNames = fileName.Split('\\');
            if (fileNames.Length >= 1) return fileNames[fileNames.Length - 1];
            else return "";
        }


        List<string> listPerspectives(Mode mode)
        {
            List<string> templatesList = new List<string>();
            //Add "<Optional template selection>"
            if (mode == Mode.All) templatesList.Add(optionalTemplateSelection);
            
            DirectoryInfo di = new DirectoryInfo(_perspectiveDirectoryName);            
            FileInfo[] rgFiles = null;
            rgFiles = di.GetFiles("*.xml");

            foreach (FileInfo fi in rgFiles)
            {
                //tests should be improved, but this is an example...
                //"MainComponent.xml" specific template, cannot be used
                if (fi.Name != "MainComponent.xml" && (mode == Mode.TemplatesOnly && !fi.Name.StartsWith("_") ) )
                {
                    templatesList.Add(getNameFromFileName(fi.Name));
                }
            }
            return templatesList;
        }

        public void UpdatePerspectiveStatus()
        {
            //Refresh the currentPerspective menu in case there's modification in perspectives list
            baseWindow.ViewPerspectives.ItemsSource = listPerspectives(Mode.TemplatesOnly);
            
            //PerspectiveTitle is used for the Container title
            baseWindow.PerspectiveTitle = sofaContainer.CurrentPerspective.Name;

            //Save CurrentPerspective.Name so that it can be reloaded on loading the Container
            Parameters.Default.Componization_CurrentPerspective = sofaContainer.CurrentPerspective.Name;
            Parameters.Default.Save();
        }
        #endregion Utils

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions