using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using Core.Common.Base.Plugin;
using Core.Common.Controls.Views;
using Core.Common.Gui.Forms;
using Core.Common.Gui.Forms.MainWindow;
using Core.Common.Gui.Properties;
using Core.Common.Utils;
using log4net;
using log4net.Appender;
using UtilsResources = Core.Common.Utils.Properties.Resources;
namespace Core.Common.Gui
{
public class GuiCommandHandler : IGuiCommandHandler, IProjectCommands
{
private static readonly ILog Log = LogManager.GetLogger(typeof(GuiCommandHandler));
private readonly IGui gui;
public GuiCommandHandler(IGui gui)
{
this.gui = gui;
}
public object GetDataOfActiveView()
{
return gui.DocumentViews.ActiveView != null ? gui.DocumentViews.ActiveView.Data : null;
}
///
/// Makes the properties window visible and updates the to the
/// given .
///
/// The object for which to show its properties.
public void ShowPropertiesFor(object obj)
{
((MainWindow) gui.MainWindow).InitPropertiesWindowAndActivate();
gui.Selection = obj;
}
public bool CanShowPropertiesFor(object obj)
{
return gui.PropertyResolver.GetObjectProperties(obj) != null;
}
public bool CanOpenSelectViewDialog()
{
return gui.Selection != null && gui.DocumentViewsResolver.GetViewInfosFor(gui.Selection).Count() > 1;
}
public void OpenSelectViewDialog()
{
gui.DocumentViewsResolver.OpenViewForData(gui.Selection, true);
}
public bool CanOpenViewFor(object obj)
{
return gui.DocumentViewsResolver.GetViewInfosFor(obj).Any();
}
public void OpenView(object dataObject)
{
gui.DocumentViewsResolver.OpenViewForData(dataObject);
}
public void OpenViewForSelection()
{
gui.DocumentViewsResolver.OpenViewForData(gui.Selection);
}
public void OpenLogFileExternal()
{
bool logFileOpened = false;
try
{
var fileAppender =
LogManager.GetAllRepositories().SelectMany(r => r.GetAppenders()).OfType
().FirstOrDefault();
if (fileAppender != null)
{
var logFile = fileAppender.File;
Process.Start(logFile);
logFileOpened = true;
}
}
catch (Exception) {}
if (!logFileOpened)
{
MessageBox.Show(Resources.GuiCommandHandler_OpenLogFileExternal_Unable_to_open_log_file_Opening_log_file_directory_instead, Resources.GuiCommandHandler_OpenLogFileExternal_Unable_to_open_log_file);
Process.Start(SettingsHelper.GetApplicationLocalUserSettingsDirectory());
}
}
public object AddNewChildItem(object parent, IEnumerable childItemValueTypes)
{
using (var selectDataDialog = CreateSelectionDialogWithItems(GetSupportedDataItemInfosByValueTypes(parent, childItemValueTypes).ToList()))
{
if (selectDataDialog.ShowDialog() == DialogResult.OK)
{
return GetNewDataObject(selectDataDialog, parent);
}
return null;
}
}
public void AddNewItem(object parent)
{
if (gui.Project == null)
{
Log.Error(Resources.GuiCommandHandler_AddNewItem_There_needs_to_be_a_project_to_add_an_item);
}
using (var selectDataDialog = CreateSelectionDialogWithItems(gui.ApplicationCore.GetSupportedDataItemInfos(parent).ToList()))
{
if (selectDataDialog.ShowDialog() == DialogResult.OK)
{
var newItem = GetNewItem(selectDataDialog, parent);
if (newItem != null)
{
gui.Selection = newItem;
OpenViewForSelection();
}
}
}
}
///
/// Removes all document and tool views that are associated to the dataObject and/or its children.
///
///
public void RemoveAllViewsForItem(object dataObject)
{
if (dataObject == null || gui == null || gui.DocumentViews == null || gui.DocumentViews.Count == 0)
{
return;
}
foreach (var data in gui.GetAllDataWithViewDefinitionsRecursively(dataObject))
{
gui.DocumentViewsResolver.CloseAllViewsFor(data);
RemoveViewsAndData(gui.ToolWindowViews.Where(v => v.Data == data).ToArray());
}
}
public void AddItemToProject(object newItem)
{
gui.Project.Items.Add(newItem);
gui.Project.NotifyObservers();
}
private void AddProjectToMruList()
{
var mruList = (StringCollection) Settings.Default["mruList"];
if (mruList.Contains(gui.ProjectFilePath))
{
mruList.Remove(gui.ProjectFilePath);
}
mruList.Insert(0, gui.ProjectFilePath);
}
private static void UnselectActiveControlToForceBinding()
{
var elementWithFocus = Keyboard.FocusedElement;
if (elementWithFocus is WindowsFormsHost)
{
var host = (WindowsFormsHost) elementWithFocus;
ControlHelper.UnfocusActiveControl(host.Child as IContainerControl, true);
}
// add more if more cases are found (like a pure wpf case, for example)
}
private IEnumerable GetSupportedDataItemInfosByValueTypes(object parent, IEnumerable valueTypes)
{
return gui.ApplicationCore.GetSupportedDataItemInfos(parent).Where(dii => valueTypes.Contains(dii.ValueType));
}
private SelectItemDialog CreateSelectionDialogWithItems(IList dataItemInfos)
{
var selectDataDialog = new SelectItemDialog(gui.MainWindow);
foreach (var dataItemInfo in dataItemInfos)
{
selectDataDialog.AddItemType(dataItemInfo.Name, dataItemInfo.Category, dataItemInfo.Image, dataItemInfo);
}
return selectDataDialog;
}
private object GetNewItem(SelectItemDialog selectDataDialog, object parent)
{
var newDataObject = GetNewDataObject(selectDataDialog, parent);
if (newDataObject == null)
{
return null;
}
AddItemToProject(newDataObject);
return newDataObject;
}
private static object GetNewDataObject(SelectItemDialog selectDataDialog, object parent = null)
{
var dataItemInfo = selectDataDialog.SelectedItemTag as DataItemInfo;
if (dataItemInfo == null)
{
return null;
}
return dataItemInfo.CreateData != null ? dataItemInfo.CreateData(parent) : null;
}
private void RemoveViewsAndData(IEnumerable toolViews)
{
// set all tool windows where dataObject was used to null
foreach (var view in toolViews)
{
view.Data = null;
}
}
}
}