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.Controls.Views;
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
{
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());
}
}
///
/// 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());
}
}
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 void RemoveViewsAndData(IEnumerable toolViews)
{
// set all tool windows where dataObject was used to null
foreach (var view in toolViews)
{
view.Data = null;
}
}
}
}