using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using Tango.BL; using Tango.BL.Entities; using Tango.Core.Commands; using Tango.Core.DI; using Tango.MachineStudio.Common; using Tango.MachineStudio.Common.StudioApplication; using Tango.SharedUI; namespace Tango.MachineStudio.DB.ViewModels { public class MainViewVM : StudioViewModel { private bool _isLoading; public bool IsLoading { get { return _isLoading; } set { _isLoading = value; RaisePropertyChangedAuto(); } } private bool _notLoaded; public bool NotLoaded { get { return _notLoaded; } set { _notLoaded = value; RaisePropertyChangedAuto(); } } public RelayCommand LoadCommand { get; set; } public MainViewVM() : base() { NotLoaded = true; LoadCommand = new RelayCommand(LoadAdapter,() => !IsLoading); } private async void LoadAdapter() { IsLoading = true; InvalidateRelayCommands(); await Task.Factory.StartNew(() => { ObservablesEntitiesAdapter.Instance.Initialize(); }); IsLoading = false; NotLoaded = false; } public override void OnApplicationReady() { } //public override void OnModuleRequest(params object[] args) //{ // if (args != null && args.Length > 0 && args[0] is IObservableEntity) // { // var arg = args[0]; // String vmName = arg.GetType().Name + "sViewVM"; // Type vmType = Assembly.GetAssembly(typeof(MainViewVM)).GetTypes().SingleOrDefault(x => x.Name == vmName); // if (vmType == null) // { // vmName = arg.GetType().BaseType.Name + "sViewVM"; // vmType = Assembly.GetAssembly(typeof(MainViewVM)).GetTypes().SingleOrDefault(x => x.Name == vmName); // } // if (vmType != null) // { // var vm = TangoIOC.Default.GetInstance(vmType); // vmType.GetProperty("SelectedEntity").SetValue(vm, arg); // vmType.GetMethod("OnEdit", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(vm, new object[] { }); // } // } //} } }
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;
}
}