Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/CS/DockingDemo.Wpf/MVVM/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/CS/DockingDemo.Wpf/MVVM/VS2010ViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Diagnostics;
using DevExpress.Xpf.Docking;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using System.IO;
using System.Windows.Controls;
using DevExpress.Xpf.DemoBase.Helpers;
using DevExpress.DemoData.Utils;
using DevExpress.Xpf.DemoBase.Helpers.TextColorizer;

namespace DockingDemo.MVVM {

    public class VS2010DocumentViewModel : DocumentViewModel {
        public CodeLanguageText CodeLanguageText { get; private set; }
        public CodeLanguage CodeLanguage { get; private set; }
        protected override string TargetName { get { return "DocumentHost"; } }
        public VS2010DocumentViewModel(string displayName) : base(displayName) {
        }
        public VS2010DocumentViewModel() {
        }
        static int count = 0;
        public override bool Open() {
            CodeLanguage = count % 2 == 0 ? CodeLanguage.XAML : CodeLanguage.CS;
            count++;
            DisplayName = string.Format("File{0}.{1}", count, CodeLanguage.ToString().ToLower());
            Glyph = new BitmapImage(new Uri("/DockingDemo;component/Images/VS2010Docking/FileCS_16x16.png", UriKind.Relative));
            Description = CodeLanguage == CodeLanguage.XAML ? "Windows Markup File" : "Visual C# Source file";
            Footer = string.Format("c:\\...\\DockingDemo\\{0}", DisplayName);
            string filename = "VS2010Docking." + CodeLanguage.ToString().ToLower();
            CodeLanguageText = new CodeLanguageText(CodeLanguage, () => { return GetCodeText(filename); });
            return true;
        }
        string GetCodeText(string name) {
            System.Reflection.Assembly assembly = typeof(VS2010DocumentViewModel).Assembly;
            using(Stream stream = AssemblyHelper.GetEmbeddedResourceStream(assembly, DemoHelper.GetPath("Docs/", assembly) + name, true)) {
                if(stream == null) return string.Empty;
                using(StreamReader reader = new StreamReader(stream)) {
                    return reader.ReadToEnd();
                }
            }
        }
    }
    public class VS2010MainWindowViewModel : MainWindowViewModel {
        public VS2010MainWindowViewModel() {
        }
        protected override void InitDefaultLayout() {
            OpenOrCloseWorkspace(_ToolboxViewModel);
            OpenOrCloseWorkspace(_SolutionExplorerViewModel);
            OpenOrCloseWorkspace(_PropertiesViewModel);
            OpenOrCloseWorkspace(_ErrorListViewModel);
            VS2010DocumentViewModel document = new VS2010DocumentViewModel();
            document.Open();
            OpenOrCloseWorkspace(document, true);
        }
        protected override DocumentViewModel GetDocumentViewModel() {
            return new VS2010DocumentViewModel();
        }
        protected override List<CommandViewModel> CreateFileCommands() {
            CommandViewModel newCommand = new CommandViewModel("New", null) { IsSubItem = true };
            CommandViewModel newProject = new CommandViewModel("Project...", new RelayCommand(new Action<object>(OnNewFileExecuted))) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/NewProject_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.N, ModifierKeys.Control | ModifierKeys.Shift) };
            CommandViewModel newWebSite = new CommandViewModel("Web Site...", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Solution Explorer.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.N, ModifierKeys.Shift | ModifierKeys.Alt), IsEnabled = false };
            CommandViewModel newFile = new CommandViewModel("File...", new RelayCommand(new Action<object>(OnNewFileExecuted))) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/File_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.N, ModifierKeys.Control) };
            newCommand.Commands = new List<CommandViewModel>() { newProject, newFile };

            CommandViewModel openCommand = new CommandViewModel("Open", null) { IsSubItem = true, };
            CommandViewModel openProject = new CommandViewModel("Project/Solution...", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/OpenSolution_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.O, ModifierKeys.Control | ModifierKeys.Shift), IsEnabled = false };
            CommandViewModel openWebSite = new CommandViewModel("Web Site...", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Solution Explorer.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.O, ModifierKeys.Shift | ModifierKeys.Alt), IsEnabled = false };
            CommandViewModel openFile = new CommandViewModel("File...", new RelayCommand(new Action<object>(OnNewFileExecuted))) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/OpenFile_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.O, ModifierKeys.Control) };
            openCommand.Commands = new List<CommandViewModel>() { openProject, openFile };

            CommandViewModel closeFile = new CommandViewModel("Close", new RelayCommand(new Action<object>(OnCloseFileExecuted)));
            CommandViewModel closeSolution = new CommandViewModel("Close Solution", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/CloseSolution_16x16.png")};
            CommandViewModel save = new CommandViewModel("Save", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Save_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control) };
            CommandViewModel saveAll = new CommandViewModel("Save All", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/SaveAll_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control | ModifierKeys.Shift) };

            CommandViewModel recentFilesCommand = new CommandViewModel("Recent files", null) { IsSubItem = true, };
            CommandViewModel recentProjectsCommand = new CommandViewModel("Recent projects and solutions", null) { IsSubItem = true, };

            return new List<CommandViewModel>() {
                newCommand, openCommand, GetSeparator(), closeFile, closeSolution, GetSeparator(), save,
                saveAll, GetSeparator(), recentFilesCommand, recentProjectsCommand };
        }
        CommandViewModel GetSeparator() {
            return new CommandViewModel("", null) { IsSeparator = true };
        }
        protected override List<BarModel> CreateBars() {
            List<BarModel> bars =  base.CreateBars();

            BarModel model = new BarModel("Standard");
            var commands = CreateToolbarCommands();
            foreach(var cmd in commands) {
                model.Commands.Add(cmd);
            }
            bars.Add(model);
            return bars;
        }
        List<CommandViewModel> CreateToolbarCommands() {
            CommandViewModel newProject = new CommandViewModel("New Project", new RelayCommand(new Action<object>(OnNewFileExecuted))) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/NewProject_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.N, ModifierKeys.Control | ModifierKeys.Shift) };
            CommandViewModel newFile = new CommandViewModel("Add New Item", new RelayCommand(new Action<object>(OnNewFileExecuted))) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/File_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.N, ModifierKeys.Control) };
            CommandViewModel openFile = new CommandViewModel("Open File", new RelayCommand(new Action<object>(OnNewFileExecuted))) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/OpenFile_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.O, ModifierKeys.Control) };

            CommandViewModel save = new CommandViewModel("Save", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Save_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control) };
            CommandViewModel saveAll = new CommandViewModel("Save All", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/SaveAll_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control | ModifierKeys.Shift) };

            CommandViewModel cut = new CommandViewModel("Cut", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Cut_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control | ModifierKeys.Shift) };
            CommandViewModel copy = new CommandViewModel("Copy", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Copy_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control | ModifierKeys.Shift) };
            CommandViewModel paste = new CommandViewModel("Paste", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Paste_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control | ModifierKeys.Shift) };

            CommandViewModel undo = new CommandViewModel("Undo", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Undo_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control | ModifierKeys.Shift) };
            CommandViewModel redo = new CommandViewModel("Redo", null) { Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Redo_16x16.png"), KeyGesture = KeyGestureHelper.GetKeyGesure(Key.S, ModifierKeys.Control | ModifierKeys.Shift) };

            return new List<CommandViewModel>() {
                newProject, newFile, openFile, save, saveAll, GetSeparator(), cut, copy, paste, GetSeparator(), undo, redo };
        }
        void OnNewFileExecuted(object param) {
            VS2010DocumentViewModel document = new VS2010DocumentViewModel();
            document.Open();
            OpenOrCloseWorkspace(document, true);
        }
        void OnCloseFileExecuted(object param) {
        }
        static class KeyGestureHelper {
            public static KeyGesture GetKeyGesure(Key key, ModifierKeys modifiers){
                KeyGesture k = new KeyGesture(key, modifiers);
                string s = k.GetDisplayStringForCulture(System.Globalization.CultureInfo.InvariantCulture);
                return new KeyGesture(key, modifiers);
            }
        }
    }
}