Source

main / Source / Excel / Excel.AddIn / Excel.AddIn / Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Research.Dcxl.Excel.BizLogic;
using System.Collections.ObjectModel;
using Microsoft.Office.Core;

namespace Microsoft.Research.Dcxl.Excel.Addin
{
    public partial class Form1 : Form
    {
        private IMetadataManager metadataManager;

        public Form1()
        {
            InitializeComponent();
            InitializeManagers();
        }
        private void InitializeManagers()
        {
            metadataManager = new MetadataManager(Globals.ThisAddIn.Application.ActiveWorkbook, Globals.Factory);
        }

        private void generateMetada_Click(object sender, EventArgs e)
        {
            MetadataListViewModel metadataListViewModel = GetMetadata();

            MetadataViewPane dialog = new MetadataViewPane();
            System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper(dialog);
            helper.Owner = (IntPtr)ThisAddIn.ExcelApplication.Hwnd;

            metadataListViewModel.RequestClose += new EventHandler(dialog.OnRequestClose);
            dialog.Model = metadataListViewModel;

            dialog.UpdateLayout();

            dialog.ShowDialog();

            if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
            {
                List<MetadataDetail> updatedMetadata = new List<MetadataDetail>();
                foreach (var item in dialog.Model.MetadataList)
                {
                    MetadataDetail model = new MetadataDetail();
                    model.SetValuesFrom(item);
                    updatedMetadata.Add(model);
                }

                metadataManager.UpdateMetadata(updatedMetadata);
            }

            metadataListViewModel.RequestClose -= new EventHandler(dialog.OnRequestClose);
            dialog.Close();
            Form1.ActiveForm.Close();
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Close();
        }

        private void ignore_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Close();
        }
        private MetadataListViewModel GetMetadata()
        {
            var metadata = metadataManager.GetMetadata();
            MetadataListViewModel metadataListViewModel = new MetadataListViewModel();

            foreach (var item in metadata)
            {
                MetadataViewModel model = new MetadataViewModel();
                model.SetValuesFrom(item);
                metadataListViewModel.MetadataList.Add(model);
                if (item.Name == Common.Constants.CitationContent)
                {
                    metadataListViewModel.UpdateCitation();
                }
            }
            return metadataListViewModel;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}