Click here to Skip to main content
15,891,316 members
Articles / Desktop Programming / WPF

Sofa, AvalonDock and MEF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
6 May 2011CPOL4 min read 54.9K   3K   36  
Sofa makes the link between AvalonDock and MEF, both using composable elements
��using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Collections.ObjectModel;

using Sofa.Commons;

using Sofa.Container;

using AvalonDock;

using log4net;

using System.Reflection;

using System.ComponentModel;

using System.IO;

using System.Deployment.Application;

using System.ComponentModel.Composition;





namespace SofaBasicContainer

{



    [Export("Sofa.Examples.SofaExplorer.MainWindowContract", typeof(Window))]

    public partial class BaseWindow : Window, IBaseContainer, IPartImportsSatisfiedNotification

    { 

        static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);



        //All components are here loaded as factories

        [ImportMany("Sofa.Examples.SofaExplorer.Container_FactoryContract")]

        public ExportFactory<object, ISofaComponent_Metadata>[] MEFFactories { get; set; }



        PerspectiveHelper perspectiveHelper;



        public SofaContainer SofaContainer

        {

            get { return sofaContainer; }

            set { sofaContainer = value; }

        }



        //Not used, required by IBaseContainer

        public SofaMenu SofaMenu

        {

            get { return sofaMenu; }

            set { sofaMenu = value; }

        }



        public BaseWindow()

        {

            InitializeComponent();



            PerspectiveTitle = "...";

            perspectiveHelper = new PerspectiveHelper(sofaContainer, this);

            ViewComponents.IsEnabled = false;



            ViewPerspectives.DataContext = perspectiveHelper.templatesList;            

        }



        public void OnImportsSatisfied()

        {

            foreach (var factory in MEFFactories)

            {

                SofaContainer.RegisterComponent(factory);

            }



            sofaContainer.CmpModelList.Sort(delegate(CmpModel cmp1, CmpModel cmp2) { return cmp1.Label.CompareTo(cmp2.Label); });

            ViewComponents.DataContext = sofaContainer.CmpModelList;

        }



        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            if (Parameters.Default.CurrentPerspective != "")

            {

                Perspective p = sofaContainer.LoadPerspective(Parameters.Default.CurrentPerspective);

                PerspectiveTitle = Parameters.Default.CurrentPerspective;

                perspectiveHelper.UpdatePerspectiveStatus();

            }

        }



        //Closing event handle declared in xaml

        private void Window_Closing(object sender, CancelEventArgs e)

        {

            perspectiveHelper.SaveCurrentPerspective();

            e.Cancel = !sofaContainer.CloseAllComponents();

        }





        //Aplications menu

        private void LoadComponent_Click(object sender, RoutedEventArgs e)

        {

            String componentName = (((System.Windows.Controls.MenuItem)sender).Tag.ToString());

            sofaContainer.OpenComponent(componentName);

        }



        private void CloseComponent_Click(object sender, RoutedEventArgs e)

        {

            CmpLoaded cmpView = null;

            

            //Search the component on IsActiveXxxxxx = true (in case there's more that 1 instance component)

            //First try IsActiveContent (Component is in a DockablePane)

            try

            {

                cmpView = sofaContainer.CmpLoadedList.Single(c => ((ManagedContent)c.VisualControl).IsActiveContent == true);

            } catch {}

            //If not found

            if (cmpView == null)

            {

                //Then try IsActiveDocument (Component is in a DocumentPane)

                try

                {

                    cmpView = sofaContainer.CmpLoadedList.Single(c => ((ManagedContent)c.VisualControl).IsActiveDocument == true);

                }

                catch { }

            }



            if (cmpView != null) sofaContainer.CloseComponent(cmpView.GUID);

        }



        private void CloseAllComponent_Click(object sender, RoutedEventArgs e)

        {

            sofaContainer.CloseAllComponents();

        }



        public string PerspectiveTitle

        {

            get { return (string)GetValue(PerspectiveTitleProperty); }

            set { SetValue(PerspectiveTitleProperty, "SofaBasicContainer - " + value); }

        }



        public static readonly DependencyProperty PerspectiveTitleProperty =

            DependencyProperty.Register("PerspectiveTitle", typeof(string), typeof(BaseWindow), new UIPropertyMetadata(string.Empty));





        //Perspectives menu

        private void LoadPerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.LoadPerspective(((System.Windows.Controls.MenuItem)sender).Header.ToString());

        }



        private void SavePerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.SaveCurrentPerspective();

        }



        private void NewPerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.NewPerspective();

        }



        private void DeletePerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.DeletePerspective();

        }



        private void RenamePerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.RenamePerspective();

        }



    }

}



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