Click here to Skip to main content
12,349,716 members (26,755 online)
Click here to Skip to main content

Tagged as

Stats

15.9K views
2.1K downloads
18 bookmarked
Posted

How To Embed An Application Into a Docking Library

, 3 Sep 2011 CPOL
Step by Step conversion of an application into a Docking application component
Examples
Step by step SofaComponization
0. Original CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.suo
Data
Properties
1. From Application To Component
1. From Application To Component.suo
CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.csproj.user
Data
Properties
SofaBasicContainer
Properties
app.manifest
Resources
Sofa.bmp
Sofa
SofaBasicContainer.csproj - Copie.user
Perspectives
_Dev_SofaBasicContainer.csproj.user
_SofaBasicContainer.csproj.user
2. From Simple Component to sub-Components
2. From Simple Component to sub-Components.suo
CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.csproj.user
Data
MainComponent.xaml.cs.bak
Properties
SofaBasicContainer
BaseWindow.xaml.cs.bak
Properties
app.manifest
Resources
Sofa.bmp
Sofa
SofaBasicContainer.csproj - Copie.user
Perspectives
_Dev_SofaBasicContainer.csproj.user
_SofaBasicContainer.csproj.user
3. About Perspectives
3. About Perspectives.suo
CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.csproj.user
Data
MainComponent.xaml.cs.bak
Properties
Service References
SofaBasicContainer
BaseWindow.xaml.cs.bak
Properties
app.manifest
Resources
Sofa.bmp
Sofa
SofaBasicContainer.csproj - Copie.user
Perspectives
_Dev_SofaBasicContainer.csproj.user
_SofaBasicContainer.csproj.user
External dependencies
log4net.dll
Mef.Codeplex
System.ComponentModel.Composition.Codeplex.dll
System.ComponentModel.Composition.Registration.dll
System.Reflection.Context.dll
Prism.Mef_Codeplex
Microsoft.Practices.Prism.dll
Microsoft.Practices.Prism.Interactivity.dll
Microsoft.Practices.Prism.MefExtensions.dll
Microsoft.Practices.Prism.UnityExtensions.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll
WPFToolkit.dll
Libraries
ExternalFrameworks.dll
Sofa.Commons.dll
Sofa.Component.dll
Sofa.Container.dll
Sofa.Themes.dll
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)

Share

About the Author

Sofa Team
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 3 Sep 2011
Article Copyright 2011 by Sofa Team
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid