aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Protobuf/CompilerFactory.cs
blob: 7c0ee54a4f43391a8f40a5866631bd9b75c39024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Logging;

namespace Tango.Protobuf
{
    /// <summary>
    /// Factory class for protobuf compilers.
    /// </summary>
    public static class CompilerFactory
    {
        /// <summary>
        /// Creates a protobuf compiler instance by the specified language.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">Could not locate protobuf compiler for language " + language.ToString()</exception>
        public static IProtoCompiler CreateCompiler(CompilerLanguage language)
        {
            LogManager.Log("Generating protobuf compiler for " + language.ToString() + "...");

            foreach (var cType in typeof(CompilerFactory).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && typeof(IProtoCompiler).IsAssignableFrom(x)))
            {
                var instance = Activator.CreateInstance(cType) as IProtoCompiler;
                if (instance.Language == language) return instance;
            }

            throw LogManager.Log(new ArgumentException("Could not locate protobuf compiler for language " + language.ToString()));
        }

        /// <summary>
        /// Gets a collection of available compilers instance.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<IProtoCompiler> GetAvailableCompilers()
        {
            List<IProtoCompiler> compilers = new List<IProtoCompiler>();

            foreach (var cType in typeof(CompilerFactory).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && typeof(IProtoCompiler).IsAssignableFrom(x)))
            {
                var instance = Activator.CreateInstance(cType) as IProtoCompiler;
                compilers.Add(instance);
            }

            return compilers;
        }

        /// <summary>
        /// Creates a protobuf compiler instance by the specified language string.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">Could not locate protobuf compiler for language " + language.ToString()</exception>
        public static IProtoCompiler CreateCompiler(String language)
        {
            LogManager.Log("Generating protobuf compiler for " + language.ToString() + "...");

            CompilerLanguage lan = (CompilerLanguage)Enum.Parse(typeof(CompilerLanguage), language, true);

            foreach (var cType in typeof(CompilerFactory).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && typeof(IProtoCompiler).IsAssignableFrom(x)))
            {
                var instance = Activator.CreateInstance(cType) as IProtoCompiler;
                if (instance.Language == lan) return instance;
            }

            throw LogManager.Log(new ArgumentException("Could not locate protobuf compiler for language " + language.ToString()));
        }
    }
}
I; namespace Tango.PPC.UI.Notifications { /// <summary> /// Represents the default PPC notification provider. /// </summary> /// <seealso cref="Tango.Core.ExtendedObject" /> /// <seealso cref="Tango.PPC.Common.Notifications.INotificationProvider" /> public class DefaultNotificationProvider : ExtendedObject, INotificationProvider { private ConcurrentQueue<PendingNotification<MessageBoxVM, bool>> _pendingMessageBoxes; private ConcurrentQueue<PendingNotification<DialogAndView, DialogViewVM>> _pendingDialogs; /// <summary> /// Gets the collection of notification items. /// </summary> public ObservableCollection<NotificationItem> NotificationItems { get; private set; } /// <summary> /// Initializes a new instance of the <see cref="DefaultNotificationProvider"/> class. /// </summary> public DefaultNotificationProvider() { NotificationItems = new ObservableCollection<NotificationItem>(); _pendingMessageBoxes = new ConcurrentQueue<PendingNotification<MessageBoxVM, bool>>(); _pendingDialogs = new ConcurrentQueue<PendingNotification<DialogAndView, DialogViewVM>>(); PopNotificationCommand = new RelayCommand<NotificationItem>((x) => PopNotification(x)); NotificationItems.EnableCrossThreadOperations(); } private MessageBoxVM _currentMessageBox; /// <summary> /// Gets the current message box if any. /// </summary> public MessageBoxVM CurrentMessageBox { get { return _currentMessageBox; } private set { _currentMessageBox = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(HasMessageBox)); } } /// <summary> /// Gets a value indicating whether a message box is available. /// </summary> public bool HasMessageBox { get { return CurrentMessageBox != null; } } private FrameworkElement _currentDialog; /// <summary> /// Gets the current dialog if any. /// </summary> public FrameworkElement CurrentDialog { get { return _currentDialog; } private set { _currentDialog = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(HasDialog)); } } /// <summary> /// Gets a value indicating whether a dialog is available. /// </summary> public bool HasDialog { get { return CurrentDialog != null; } } /// <summary> /// Shows an error message box. /// </summary> /// <param name="message">The message.</param> /// <returns></returns> public Task ShowError(string message) { return ShowMessageBox(new MessageBoxVM() { Message = message, Icon = TouchIconKind.AlertOctagon, Title = "Error", Brush = Application.Current.Resources["TangoMessageBoxErrorBrush"] as Brush, }); } /// <summary> /// Shows an information message box. /// </summary> /// <param name="message">The message.</param> /// <returns></returns> public Task ShowInfo(string message) { return ShowMessageBox(new MessageBoxVM() { Message = message, Icon = TouchIconKind.InfoCircleSolid, Title = "Information", Brush = Application.Current.Resources["TangoMessageBoxInfoBrush"] as Brush, }); } /// <summary> /// Shows warning message box. /// </summary> /// <param name="message">The message.</param> /// <returns></returns> public Task ShowWarning(string message) { return ShowMessageBox(new MessageBoxVM() { Message = message, Icon = TouchIconKind.Alert, Title = "Warning", Brush = Application.Current.Resources["TangoMessageBoxWarningBrush"] as Brush, }); } /// <summary> /// Shows a question message box. /// </summary> /// <param name="message">The message.</param> /// <returns></returns> public Task<bool> ShowQuestion(string message) { return ShowMessageBox(new MessageBoxVM() { Message = message, Icon = TouchIconKind.QuestionCircleSolid, Title = "Confirm", HasCancel = true, Brush = Application.Current.Resources["TangoMessageBoxQuestionBrush"] as Brush, }); } /// <summary> /// Shows the message box. /// </summary> /// <param name="vm">The view model.</param> /// <returns></returns> private Task<bool> ShowMessageBox(MessageBoxVM vm) { TaskCompletionSource<bool> source = new TaskCompletionSource<bool>(); vm.Accepted += () => { OnMessageBoxClosed(); source.SetResult(true); }; vm.Canceled += () => { OnMessageBoxClosed(); source.SetResult(false); }; if (CurrentMessageBox == null) { CurrentMessageBox = vm; } else { _pendingMessageBoxes.Enqueue(new PendingNotification<MessageBoxVM, bool>(vm, source)); } return source.Task; } /// <summary> /// Called when the message box has been closed. /// </summary> private void OnMessageBoxClosed() { CurrentMessageBox = null; if (_pendingMessageBoxes.Count > 0) { PendingNotification<MessageBoxVM, bool> p = null; if (_pendingMessageBoxes.TryDequeue(out p)) { CurrentMessageBox = p.Item; } } } /// <summary> /// Inserts the notification item to the bottom of the notifications collection. /// </summary> /// <param name="item">The item.</param> /// <returns></returns> public NotificationItem PushNotification(NotificationItem item) { item.RemoveAction = () => { PopNotification(item); }; NotificationItems.Insert(0, item); RaisePropertyChanged(nameof(HasNotificationItems)); return item; } /// <summary> /// Pushes the notification. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public NotificationItem PushNotification<T>() where T : NotificationItem { return PushNotification(Activator.CreateInstance<T>()); } /// <summary> /// Removed the specified notification item. /// </summary> /// <param name="item">The item.</param> public void PopNotification(NotificationItem item) { NotificationItems.Remove(item); RaisePropertyChanged(nameof(HasNotificationItems)); } /// <summary> /// Gets a value indicating whether this instance has notification items. /// </summary> public bool HasNotificationItems { get { return NotificationItems.Count > 0; } } /// <summary> /// Gets the pop notification command. /// </summary> public RelayCommand<NotificationItem> PopNotificationCommand { get; private set; } /// <summary> /// Displays the specified dialog in a modal design. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="datacontext">The data context.</param> /// <param name="view">The view.</param> /// <returns></returns> public async Task<T> ShowDialog<T>(T datacontext, FrameworkElement view) where T : DialogViewVM { view.DataContext = datacontext; TangoIOC.Default.Inject(datacontext); view.Loaded += (_, __) => { view.DataContext = datacontext; datacontext.OnShow(); }; TaskCompletionSource<DialogViewVM> source = new TaskCompletionSource<DialogViewVM>(); datacontext.Accepted += () => { OnDialogClosed(); source.SetResult(datacontext); }; datacontext.Canceled += () => { OnDialogClosed(); source.SetResult(datacontext); }; if (CurrentDialog == null) { CurrentDialog = view; } else { _pendingDialogs.Enqueue(new PendingNotification<DialogAndView, DialogViewVM>(new DialogAndView(datacontext, view), source)); } var result = await source.Task; return result as T; } /// <summary> /// Called when [dialog closed]. /// </summary> private void OnDialogClosed() { CurrentDialog = null; if (_pendingDialogs.Count > 0) { PendingNotification<DialogAndView, DialogViewVM> p = null; if (_pendingDialogs.TryDequeue(out p)) { CurrentDialog = p.Item.View; } } } /// <summary> /// Displays the specified dialog in a modal design. /// The notification provider will try to locate the view automatically using conventions. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="datacontext">The data context.</param> /// <returns></returns> public Task<T> ShowDialog<T>(T datacontext) where T : DialogViewVM { var callingAssembly = datacontext.GetType().Assembly; String viewName = datacontext.GetType().FullName.Replace("VM", ""); var viewType = callingAssembly.GetType(viewName); if (viewType == null) { throw new NullReferenceException("View type for " + datacontext.GetType().Name + " could not be found!"); } var view = Activator.CreateInstance(viewType) as FrameworkElement; if (view == null) { throw new NullReferenceException("The view " + viewType.ToString() + " is not of type framework element."); } return ShowDialog<T>(datacontext, Activator.CreateInstance(viewType) as FrameworkElement); } /// <summary> /// Displays the specified dialog in a modal design. /// The data context instance will be automatically created. /// The notification provider will try to locate the view automatically using conventions. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public Task<T> ShowDialog<T>() where T : DialogViewVM { return ShowDialog<T>(Activator.CreateInstance<T>()); } /// <summary> /// Sets the global busy message. /// </summary> /// <param name="message">The message.</param> public void SetGlobalBusyMessage(string message) { GlobalBusyMessage = message; IsInGlobalBusyState = true; RaisePropertyChanged(nameof(IsInGlobalBusyState)); RaisePropertyChanged(nameof(GlobalBusyMessage)); } /// <summary> /// Releases the global busy message. /// </summary> public void ReleaseGlobalBusyMessage() { GlobalBusyMessage = null; IsInGlobalBusyState = false; RaisePropertyChanged(nameof(IsInGlobalBusyState)); RaisePropertyChanged(nameof(GlobalBusyMessage)); } /// <summary> /// Gets the current global busy message. /// </summary> public string GlobalBusyMessage { get; private set; } /// <summary> /// Gets a value indicating whether this instance is in global busy state. /// </summary> public bool IsInGlobalBusyState { get; private set; } private AppBarItem _currentAppBarItem; /// <summary> /// Gets the current application bar item. /// </summary> public AppBarItem CurrentAppBarItem { get { return _currentAppBarItem; } set { _currentAppBarItem = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(HasAppBarItem)); } } /// <summary> /// Gets a value indicating whether this instance has application bar item. /// </summary> public bool HasAppBarItem { get { return CurrentAppBarItem != null; } } /// <summary> /// Pushes the application bar item. /// </summary> /// <param name="appBarItem">The application bar item.</param> /// <returns></returns> public AppBarItem PushAppBarItem(AppBarItem appBarItem) { CurrentAppBarItem = appBarItem; appBarItem.RemoveAction = () => PopAppBarItem(appBarItem); return appBarItem; } /// <summary> /// Pushes the application bar item. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public AppBarItem PushAppBarItem<T>() where T : AppBarItem { return PushAppBarItem(Activator.CreateInstance<T>()); } /// <summary> /// Pops the application bar item. /// </summary> /// <param name="appBarItem">The application bar item.</param> public void PopAppBarItem(AppBarItem appBarItem) { CurrentAppBarItem = null; } } }