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

How to create a VS 2012 like application - Wide IDE Framework (v0.1)

Rate me:
Please Sign up or sign in to vote.
4.64/5 (25 votes)
2 Apr 2013LGPL38 min read 66.1K   9.5K   113  
Participatory IDE framework built using WPF, PRISM and other open source projects
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wide.Core.Logging;
using Wide.Core.Services;
using Wide.Interfaces;
using Wide.Interfaces.Events;
using Wide.Interfaces.Services;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using System.Windows.Controls;
using Wide.Interfaces.Themes;
using CommandManager = Wide.Core.Services.CommandManager;

namespace Wide.Core
{
    public class CoreModule : IModule
    {
        private readonly IUnityContainer _container;

        public CoreModule(IUnityContainer container)
        {
            _container = container;
        }

        public void Initialize()
        {
            EventAggregator.GetEvent<SplashMessageUpdateEvent>().Publish(new SplashMessageUpdateEvent { Message = "Loading Core Module" });            
            _container.RegisterType<TextViewModel>();
            _container.RegisterType<TextModel>();
            _container.RegisterType<TextView>();
            _container.RegisterType<AllFileHandler>();

            _container.RegisterType<IOpenFileService, OpenFileService>(new ContainerControlledLifetimeManager());
            _container.RegisterType<ICommandManager, CommandManager>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IContentHandlerRegistry, ContentHandlerRegistry>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IThemeManager, ThemeManager>(new ContainerControlledLifetimeManager());
            _container.RegisterType<ILoggerService, NLogService>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IToolbarService, ToolbarService>(new ContainerControlledLifetimeManager());
            _container.RegisterType<AbstractMenuItem, MenuItemViewModel>(new ContainerControlledLifetimeManager(), new InjectionConstructor(new InjectionParameter(typeof(string), "$MAIN$"), new InjectionParameter(typeof(int), 1), new InjectionParameter(typeof(ImageSource), null), new InjectionParameter(typeof(ICommand), null), new InjectionParameter(typeof(KeyGesture), null), new InjectionParameter(typeof(bool), false), new InjectionParameter(typeof(IUnityContainer), this._container)));
            _container.RegisterType<ToolbarViewModel>(new InjectionConstructor(new InjectionParameter(typeof(string), "$MAIN$"), new InjectionParameter(typeof(int), 1), new InjectionParameter(typeof(ImageSource), null), new InjectionParameter(typeof(ICommand), null), new InjectionParameter(typeof(bool), false), new InjectionParameter(typeof(IUnityContainer), this._container)));

            //Register a default file opener
            IContentHandlerRegistry registry = _container.Resolve<IContentHandlerRegistry>();
            registry.Register(_container.Resolve<AllFileHandler>());

            AppCommands();
            AppTheme();
            //AppMenu();
            //AppToolbar();

            //Try resolving a workspace
            IWorkspace workspace;
            try
            {
                workspace = _container.Resolve<AbstractWorkspace>();
            }
            catch
            {
                _container.RegisterType<AbstractWorkspace, Workspace>(new ContainerControlledLifetimeManager());
            }
        }

        private void AppCommands()
        {
            ICommandManager manager = _container.Resolve<ICommandManager>();

            //TODO: Check if you can hook up to the Workspace.ActiveDocument.CloseCommand
            DelegateCommand closeCommand = new DelegateCommand(CloseDocument, CanExecuteCloseDocument);
            manager.RegisterCommand("CLOSE", closeCommand);
        }

        #region Commands
        private bool CanExecuteCloseDocument()
        {
            IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
            return workspace.ActiveDocument != null;
        }

        private void CloseDocument()
        {
            IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
            MessageBoxResult res = MessageBoxResult.Cancel;
            if (workspace.ActiveDocument.Model.IsDirty)
            {
                //means the document is dirty - show a message box and then handle based on the user's selection
                res = MessageBox.Show(string.Format("Save changes for document '{0}'?", workspace.ActiveDocument.Title), "Are you sure?", MessageBoxButton.YesNoCancel);
                if (res == MessageBoxResult.Yes)
                {
                    workspace.ActiveDocument.Handler.SaveContent(workspace.ActiveDocument);
                }
                if (res != MessageBoxResult.Cancel)
                {
                    workspace.Documents.Remove(workspace.ActiveDocument);
                }
            }
            else
            {
                workspace.Documents.Remove(workspace.ActiveDocument);
            }

        }
        #endregion

        private void AppTheme()
        {
            IThemeManager manager = _container.Resolve<IThemeManager>();
            //manager.AddTheme(new DefaultTheme());
            //manager.AddTheme(new DarkTheme());
        }

        private IEventAggregator EventAggregator
        {
            get { return _container.Resolve<IEventAggregator>(); }
        }
    }
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
United States United States
Hello! I'm a 26 year old, WPF/C# self-learned software developer, musician from India with a Master's degree in Computer science. I believe that every piece of software and information/knowledge is available for free for those who seek it.

My projects are available in my Webpage hosted on Github. I am eager to learn new things everyday, explore new technologies and solve problems.

Comments and Discussions