aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.Protobuf.UI/MainWindow.xaml.cs
blob: 00016e929436df08da608488c9abb0520dc30f4e (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
using MahApps.Metro.Controls;
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.Commands;
using Tango.Protobuf.Compilers;

namespace Tango.Protobuf.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        private String _sourceFolder;
        private CompilerFolderResult _lastFolderResult;

        public RelayCommand LoadFileCommand { get; set; }
        public RelayCommand LoadFolderCommand { get; set; }
        public RelayCommand CompileCommand { get; set; }
        public RelayCommand SaveCommand { get; set; }

        public List<Item> SourceItems
        {
            get { return (List<Item>)GetValue(SourceItemsProperty); }
            set { SetValue(SourceItemsProperty, value); }
        }
        public static readonly DependencyProperty SourceItemsProperty =
            DependencyProperty.Register("SourceItems", typeof(List<Item>), typeof(MainWindow), new PropertyMetadata(null));

        public String CurrentContent
        {
            get { return (String)GetValue(CurrentContentProperty); }
            set { SetValue(CurrentContentProperty, value); }
        }
        public static readonly DependencyProperty CurrentContentProperty =
            DependencyProperty.Register("CurrentContent", typeof(String), typeof(MainWindow), new PropertyMetadata(null));

        public List<ICompilerResult> TargetItems
        {
            get { return (List<ICompilerResult>)GetValue(TargetItemsProperty); }
            set { SetValue(TargetItemsProperty, value); }
        }
        public static readonly DependencyProperty TargetItemsProperty =
            DependencyProperty.Register("TargetItems", typeof(List<ICompilerResult>), typeof(MainWindow), new PropertyMetadata(null));

        public CompilerLanguage CompilerLanguage
        {
            get { return (CompilerLanguage)GetValue(CompilerLanguageProperty); }
            set { SetValue(CompilerLanguageProperty, value); }
        }
        public static readonly DependencyProperty CompilerLanguageProperty =
            DependencyProperty.Register("CompilerLanguage", typeof(CompilerLanguage), typeof(MainWindow), new PropertyMetadata(CompilerLanguage.CSharp));

        public MainWindow()
        {
            SourceItems = new List<Item>();
            TargetItems = new List<ICompilerResult>();

            LoadFileCommand = new RelayCommand(LoadFile);
            LoadFolderCommand = new RelayCommand(LoadFolder);
            CompileCommand = new RelayCommand(Compile);
            SaveCommand = new RelayCommand(Save);

            InitializeComponent();
        }

        private void LoadFolder()
        {
            var dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;
            CommonFileDialogResult result = dialog.ShowDialog();
            if (result == CommonFileDialogResult.Ok)
            {
                _sourceFolder = dialog.FileName;
                SourceItems = ItemProvider.GetItems(_sourceFolder);
            }
        }

        private void LoadFile()
        {
            throw new NotImplementedException();
        }

        private void Source_TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is FileItem)
            {
                CurrentContent = File.ReadAllText((e.NewValue as FileItem).Path);
            }
            else if (e.NewValue is CompilerFileResult)
            {
                CurrentContent = (e.NewValue as CompilerFileResult).Content;
            }
            else
            {
                CurrentContent = String.Empty;
            }
        }

        private void Compile()
        {
            IProtoCompiler compiler = CompilerFactory.CreateCompiler(CompilerLanguage);


            try
            {
                _lastFolderResult = compiler.CompileFolder(_sourceFolder, txtIncludes.Text.Split(','));
                TargetItems = _lastFolderResult.Results.ToList();
            }
            catch (CompilerException ex)
            {
                ExceptionWindow exWin = new ExceptionWindow(ex.Issues);
                exWin.Owner = this;
                exWin.ShowDialog();
            }
        }

        private void Save()
        {
            if (_lastFolderResult != null)
            {
                var dialog = new CommonOpenFileDialog();
                dialog.IsFolderPicker = true;
                CommonFileDialogResult result = dialog.ShowDialog();
                if (result == CommonFileDialogResult.Ok)
                {
                    _lastFolderResult.Save(dialog.FileName);
                }
            }
        }

    }
}