aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.IDE.UI/MainWindow.xaml
blob: 032d38c81f4da78a8943b93ff773e4a44aeec337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<controls:MetroWindow x:Class="Tango.Scripting.IDE.UI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:local="clr-namespace:Tango.Scripting.IDE.UI"
        xmlns:ide="clr-namespace:Tango.Scripting.IDE;assembly=Tango.Scripting.IDE"
        mc:Ignorable="d"
        Title="MainWindow" Height="720" Width="1280" d:DataContext="{d:DesignInstance Type=local:MainWindowVM, IsDesignTimeCreatable=False}" TitlebarHeight="40" TitleCaps="False" BorderBrush="Gray" BorderThickness="1" WindowStartupLocation="CenterScreen">
    <Grid>
        <ide:ScriptIDEView2 DataContext="{Binding IdeVM}" />
    </Grid>
</controls:MetroWindow>
Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.BL.Entities;
using Tango.Logging;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Authentication;
using Tango.MachineStudio.Common.Modules;

namespace Tango.MachineStudio.UI.Modules
{
    /// <summary>
    /// Represents the Machine Studio default <see cref="IStudioModuleLoader">module loader</see>.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="Tango.MachineStudio.Common.Modules.IStudioModuleLoader" />
    public class DefaultStudioModuleLoader : ExtendedObject, IStudioModuleLoader
    {
        private IAuthenticationProvider _authenticationProvider;
        private bool _loaded;

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultStudioModuleLoader"/> class.
        /// </summary>
        /// <param name="authenticationProvider">The authentication provider.</param>
        public DefaultStudioModuleLoader(IAuthenticationProvider authenticationProvider)
        {
            _authenticationProvider = authenticationProvider;
            AllModules = new SynchronizedObservableCollection<IStudioModule>();
            UserModules = new SynchronizedObservableCollection<IStudioModule>();
            _authenticationProvider.CurrentUserChanged += _authenticationProvider_CurrentUserChanged;
        }

        /// <summary>
        /// Handles the authentication provider user changed event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void _authenticationProvider_CurrentUserChanged(object sender, User e)
        {
            if (e != null)
            {
                LoadModules();
            }
        }

        private SynchronizedObservableCollection<IStudioModule> _allModules;
        /// <summary>
        /// Gets all loaded modules.
        /// </summary>
        public SynchronizedObservableCollection<IStudioModule> AllModules
        {
            get { return _allModules; }
            private set { _allModules = value; RaisePropertyChangedAuto(); }
        }

        private SynchronizedObservableCollection<IStudioModule> _userModules;
        /// <summary>
        /// Gets all the user permitted modules.
        /// </summary>
        public SynchronizedObservableCollection<IStudioModule> UserModules
        {
            get { return _userModules; }
            private set { _userModules = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Loads all available Machine Studio modules.
        /// </summary>
        public void LoadModules()
        {
            if (!_loaded)
            {
                ////Preloaded

                //LogManager.Log(String.Format("Loading module '{0}'...", nameof(StubsModule)));
                //AllModules.Add(new StubsModule());
                //LogManager.Log(String.Format("Loading module '{0}'...", nameof(DBModule)));
                //AllModules.Add(new DBModule());
                //LogManager.Log(String.Format("Loading module '{0}'...", nameof(MachineDesignerModule)));
                //AllModules.Add(new MachineDesignerModule());
                ////Preloaded

                AllModules.Clear();
                string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                foreach (var file in Directory.GetFiles(assemblyFolder, "*.dll").Where(x => x.Contains("MachineStudio")))
                {
                    try
                    {
                        Assembly moduleAssembly = null;
                        moduleAssembly = Assembly.LoadFrom(file);

                        if (moduleAssembly != null)
                        {
                            foreach (var moduleType in moduleAssembly.GetTypes().Where(x => !x.IsInterface && typeof(IStudioModule).IsAssignableFrom(x) && !x.IsAbstract))
                            {
                                if (!AllModules.ToList().Exists(x => x.GetType() == moduleType))
                                {
                                    try
                                    {
                                        LogManager.Log(String.Format("Loading module '{0}'...", moduleType.Name));
                                        var module = Activator.CreateInstance(moduleType) as IStudioModule;
                                        //SimpleIoc.Default.Register(() => module);
                                        AllModules.Add(module);
                                    }
                                    catch (Exception ex)
                                    {
                                        LogManager.Log(ex, "Could not load module " + moduleType.Name);
                                    }
                                }
                            }
                        }
                    }
                    catch { }
                }

                _loaded = true;
            }

            AllModules = AllModules.OrderBy(x => x.GetType().GetCustomAttribute<StudioModuleAttribute>().Index).ToSynchronizedObservableCollection();

            UserModules.Clear();

            if (_authenticationProvider.CurrentUser != null)
            {
                UserModules = AllModules.Where(x => _authenticationProvider.CurrentUser.HasPermission(x.Permission)).ToSynchronizedObservableCollection();
            }

            ModulesLoaded?.Invoke(this, new EventArgs());
        }

        /// <summary>
        /// Gets the studio module of type T if loaded.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetStudioModule<T>() where T : IStudioModule
        {
            return UserModules.OfType<T>().FirstOrDefault();
        }

        /// <summary>
        /// Occurs when all modules are initialized.
        /// </summary>
        public event EventHandler ModulesLoaded;
    }
}