<commit_before>﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using FreeSCADA.Common;
using FreeSCADA.Designer.Dialogs;
using FreeSCADA.Designer.Views;
using WeifenLuo.WinFormsUI.Docking;

namespace FreeSCADA.Designer
{
	class WindowManager: IDisposable
    {
		WeifenLuo.WinFormsUI.Docking.DockPanel dockPanel;
		MRUManager mruManager;

        List<DocumentView> documentViews = new List<DocumentView>();
        DocumentView currentDocument;

		ProjectContentView projectContentView;
        PropertyBrowserView propertyBrowserView;
		ToolBoxView toolBoxView;

		public WindowManager(DockPanel dockPanel, MRUManager mruManager)
		{
			this.dockPanel = dockPanel;
			this.mruManager = mruManager;

			mruManager.ItemClicked += new MRUManager.ItemClickedDelegate(OnMRUItemClicked);

			//Create toolwindows
			projectContentView = new ProjectContentView();
			projectContentView.Show(dockPanel, DockState.DockLeft);
			projectContentView.OpenEntity += new ProjectContentView.OpenEntityHandler(OnOpenProjectEntity);

			toolBoxView = new ToolBoxView();
			toolBoxView.Show(dockPanel, DockState.DockRight);
            
            propertyBrowserView = new PropertyBrowserView();
			propertyBrowserView.Show(toolBoxView.Pane, DockAlignment.Bottom, 0.6);

			//Connect Windows Manager to heleper events
			dockPanel.ActiveDocumentChanged += new EventHandler(OnActiveDocumentChanged);
		}

		public void ForceWindowsClose()
		{
            while (documentViews.Count > 0)
            {
                DocumentView doc = documentViews[0];
                //doc.HandleModifiedOnClose = false;
                doc.Close(); //this window should be removed from documentViews on closing
                documentViews.Remove(doc);
            }
            documentViews.Clear();

			projectContentView.Close();
			toolBoxView.Close();
			propertyBrowserView.Close();

			dockPanel.ActiveDocumentChanged -= new EventHandler(OnActiveDocumentChanged);
		}

		public void CreateNewSchema()
		{
			SchemaView view = new SchemaView();
			if(view.CreateNewDocument() == false)
			{
				System.Windows.MessageBox.Show( DialogMessages.CannotCreateSchema,
												DialogMessages.ErrorCaption,
												System.Windows.MessageBoxButton.OK,
												System.Windows.MessageBoxImage.Error);
				return;
			}

			//Generate unique name
			for (int i = 1; i < 1000; i++)
			{
				string newName = string.Format("{0} {1}", StringResources.UntitledSchema, i);
				bool hasTheSameDocument = false;
				foreach (DocumentView doc in documentViews)
				{
					if (doc is SchemaView && doc.DocumentName == newName)
					{
						hasTheSameDocument = true;
						break;
					}
				}
				if (hasTheSameDocument == false && !Env.Current.Project.ContainsEntity(ProjectEntityType.Schema,newName))
				{
					view.DocumentName = newName;
					break;
				}
			}
            
			view.ToolsCollectionChanged += toolBoxView.OnToolsCollectionChanged;
            view.SetCurrentTool += toolBoxView.OnSetCurrentTool;
            view.FormClosing += new FormClosingEventHandler(OnDocumentWindowClosing);
			documentViews.Add(view);
			view.Show(dockPanel, DockState.Document);
		}

		void OnDocumentWindowClosing(object sender, FormClosingEventArgs e)
		{
			DocumentView doc = (DocumentView)sender;
			if (doc.HandleModifiedOnClose && doc.IsModified)
			{
				System.Windows.MessageBoxResult res = System.Windows.MessageBox.Show(	DialogMessages.NotSavedDocument,
																						DialogMessages.SaveDocumentCaption,
																						System.Windows.MessageBoxButton.YesNoCancel,
																						System.Windows.MessageBoxImage.Warning);
				if (res == System.Windows.MessageBoxResult.Yes)
					doc.SaveDocument();
				if (res == System.Windows.MessageBoxResult.Cancel)
				{
					e.Cancel = true;
					return;
				}
			}
            //doc.ToolsCollectionChanged -= toolBoxView.OnToolsCollectionChanged;
            doc.FormClosing -= new FormClosingEventHandler(OnDocumentWindowClosing);
			documentViews.Remove(doc);
            propertyBrowserView.ShowProperties(new Object());
        }

        public void OnOpenProjectEntity(ProjectEntityType type, string name)
		{
			// Open your schema and other document types here by entity name
            DocumentView view = null;
            switch (type)
            {
                case ProjectEntityType.Schema:
                    foreach (DocumentView doc in documentViews)
                    {
                        if (doc is SchemaView)
                        {
                            if (doc.DocumentName == name)
                            {
                                doc.Activate();
                                return;
                            }
                        }
                    }
                    view = new SchemaView();
                    if (view == null || view.LoadDocument(name) == false)
                    {
                        System.Windows.MessageBox.Show(DialogMessages.CannotLoadSchema,
                                                        DialogMessages.ErrorCaption,
                                                        System.Windows.MessageBoxButton.OK,
                                                        System.Windows.MessageBoxImage.Error);
                        return;
                    }
                    view.ToolsCollectionChanged += toolBoxView.OnToolsCollectionChanged;
                    view.SetCurrentTool += toolBoxView.OnSetCurrentTool;
                    view.FormClosing += new FormClosingEventHandler(OnDocumentWindowClosing);
                    break;
                case ProjectEntityType.Archiver:
                    ShowArchiverSettings();
                    break;
                case ProjectEntityType.Script:
                    break;
                // etc....
                default:
                    break;
            }
            documentViews.Add(view);
            view.Show(dockPanel, DockState.Document);
		}

		/// <summary>
		/// Create or active existing "Events" view.
		/// </summary>
		public void ShowEvents()
		{
			foreach (DocumentView doc in documentViews)
			{
				if (doc is EventsView)
				{
					doc.Activate();
					return;
				}
			}<commit_msg>bugFix opening Archiver from ProjectView in Trunk branch<commit_after>﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using FreeSCADA.Common;
using FreeSCADA.Designer.Dialogs;
using FreeSCADA.Designer.Views;
using WeifenLuo.WinFormsUI.Docking;

namespace FreeSCADA.Designer
{
	class WindowManager: IDisposable
    {
		WeifenLuo.WinFormsUI.Docking.DockPanel dockPanel;
		MRUManager mruManager;

        List<DocumentView> documentViews = new List<DocumentView>();
        DocumentView currentDocument;

		ProjectContentView projectContentView;
        PropertyBrowserView propertyBrowserView;
		ToolBoxView toolBoxView;

		public WindowManager(DockPanel dockPanel, MRUManager mruManager)
		{
			this.dockPanel = dockPanel;
			this.mruManager = mruManager;

			mruManager.ItemClicked += new MRUManager.ItemClickedDelegate(OnMRUItemClicked);

			//Create toolwindows
			projectContentView = new ProjectContentView();
			projectContentView.Show(dockPanel, DockState.DockLeft);
			projectContentView.OpenEntity += new ProjectContentView.OpenEntityHandler(OnOpenProjectEntity);

			toolBoxView = new ToolBoxView();
			toolBoxView.Show(dockPanel, DockState.DockRight);
            
            propertyBrowserView = new PropertyBrowserView();
			propertyBrowserView.Show(toolBoxView.Pane, DockAlignment.Bottom, 0.6);

			//Connect Windows Manager to heleper events
			dockPanel.ActiveDocumentChanged += new EventHandler(OnActiveDocumentChanged);
		}

		public void ForceWindowsClose()
		{
            while (documentViews.Count > 0)
            {
                DocumentView doc = documentViews[0];
                //doc.HandleModifiedOnClose = false;
                doc.Close(); //this window should be removed from documentViews on closing
                documentViews.Remove(doc);
            }
            documentViews.Clear();

			projectContentView.Close();
			toolBoxView.Close();
			propertyBrowserView.Close();

			dockPanel.ActiveDocumentChanged -= new EventHandler(OnActiveDocumentChanged);
		}

		public void CreateNewSchema()
		{
			SchemaView view = new SchemaView();
			if(view.CreateNewDocument() == false)
			{
				System.Windows.MessageBox.Show( DialogMessages.CannotCreateSchema,
												DialogMessages.ErrorCaption,
												System.Windows.MessageBoxButton.OK,
												System.Windows.MessageBoxImage.Error);
				return;
			}

			//Generate unique name
			for (int i = 1; i < 1000; i++)
			{
				string newName = string.Format("{0} {1}", StringResources.UntitledSchema, i);
				bool hasTheSameDocument = false;
				foreach (DocumentView doc in documentViews)
				{
					if (doc is SchemaView && doc.DocumentName == newName)
					{
						hasTheSameDocument = true;
						break;
					}
				}
				if (hasTheSameDocument == false && !Env.Current.Project.ContainsEntity(ProjectEntityType.Schema,newName))
				{
					view.DocumentName = newName;
					break;
				}
			}
            
			view.ToolsCollectionChanged += toolBoxView.OnToolsCollectionChanged;
            view.SetCurrentTool += toolBoxView.OnSetCurrentTool;
            view.FormClosing += new FormClosingEventHandler(OnDocumentWindowClosing);
			documentViews.Add(view);
			view.Show(dockPanel, DockState.Document);
		}

		void OnDocumentWindowClosing(object sender, FormClosingEventArgs e)
		{
			DocumentView doc = (DocumentView)sender;
			if (doc.HandleModifiedOnClose && doc.IsModified)
			{
				System.Windows.MessageBoxResult res = System.Windows.MessageBox.Show(	DialogMessages.NotSavedDocument,
																						DialogMessages.SaveDocumentCaption,
																						System.Windows.MessageBoxButton.YesNoCancel,
																						System.Windows.MessageBoxImage.Warning);
				if (res == System.Windows.MessageBoxResult.Yes)
					doc.SaveDocument();
				if (res == System.Windows.MessageBoxResult.Cancel)
				{
					e.Cancel = true;
					return;
				}
			}
            //doc.ToolsCollectionChanged -= toolBoxView.OnToolsCollectionChanged;
            doc.FormClosing -= new FormClosingEventHandler(OnDocumentWindowClosing);
			documentViews.Remove(doc);
            propertyBrowserView.ShowProperties(new Object());
        }

        public void OnOpenProjectEntity(ProjectEntityType type, string name)
		{
			// Open your schema and other document types here by entity name
            DocumentView view = null;
            switch (type)
            {
                case ProjectEntityType.Schema:
                    foreach (DocumentView doc in documentViews)
                    {
                        if (doc is SchemaView)
                        {
                            if (doc.DocumentName == name)
                            {
                                doc.Activate();
                                return;
                            }
                        }
                    }
                    view = new SchemaView();
                    if (view == null || view.LoadDocument(name) == false)
                    {
                        System.Windows.MessageBox.Show(DialogMessages.CannotLoadSchema,
                                                        DialogMessages.ErrorCaption,
                                                        System.Windows.MessageBoxButton.OK,
                                                        System.Windows.MessageBoxImage.Error);
                        return;
                    }
                    view.ToolsCollectionChanged += toolBoxView.OnToolsCollectionChanged;
                    view.SetCurrentTool += toolBoxView.OnSetCurrentTool;
                    view.FormClosing += new FormClosingEventHandler(OnDocumentWindowClosing);
                    documentViews.Add(view);
                    view.Show(dockPanel, DockState.Document);
                    break;
                case ProjectEntityType.Archiver:
                    ShowArchiverSettings();
                    break;
                case ProjectEntityType.Script:
                    break;
                // etc....
                default:
                    break;
            }
		}

		/// <summary>
		/// Create or active existing "Events" view.
		/// </summary>
		public void ShowEvents()
		{
			foreach (DocumentView doc in documentViews)
			{
				if (doc is EventsView)
				{
					doc.Activate();
					return;
				}
			}
{'fraction_non_alphanumeric': 0.08233330684256536, 'fraction_numerical': 0.0017483906858459827, 'mean_word_length': 1.9862363550071191, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3482468', 'n_tokens_mistral': 3688, 'n_tokens_neox': 3090, 'n_words': 609}