using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base.Plugin;
using Core.Common.Gui.Forms;
using Core.Common.Gui.Properties;
using log4net;
namespace Core.Common.Gui
{
///
/// This class provides concrete implementation for ;
///
public class ProjectCommandsHandler : IProjectCommands
{
private static readonly ILog log = LogManager.GetLogger(typeof(ProjectCommandsHandler));
private readonly IProjectOwner projectOwner;
private readonly IWin32Window dialogOwner;
private readonly ApplicationCore applicationCore;
private readonly IApplicationSelection applicationSelection;
private readonly IDocumentViewController documentViewController;
///
/// Initializes a new instance of the class.
///
///
///
///
///
///
public ProjectCommandsHandler(IProjectOwner projectOwner, IWin32Window dialogParent,
ApplicationCore applicationCore, IApplicationSelection applicationSelection,
IDocumentViewController documentViewController)
{
this.projectOwner = projectOwner;
dialogOwner = dialogParent;
this.applicationCore = applicationCore;
this.applicationSelection = applicationSelection;
this.documentViewController = documentViewController;
}
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 (projectOwner.Project == null)
{
log.Error(Resources.GuiCommandHandler_AddNewItem_There_needs_to_be_a_project_to_add_an_item);
}
using (var selectDataDialog = CreateSelectionDialogWithItems(applicationCore.GetSupportedDataItemInfos(parent).ToList()))
{
if (selectDataDialog.ShowDialog() == DialogResult.OK)
{
var newItem = GetNewDataObject(selectDataDialog, parent);
if (newItem != null)
{
AddItemToProject(newItem);
applicationSelection.Selection = newItem;
documentViewController.DocumentViewsResolver.OpenViewForData(applicationSelection.Selection);
}
}
}
}
public void AddItemToProject(object newItem)
{
projectOwner.Project.Items.Add(newItem);
projectOwner.Project.NotifyObservers();
}
private IEnumerable GetSupportedDataItemInfosByValueTypes(object parent, IEnumerable valueTypes)
{
return applicationCore.GetSupportedDataItemInfos(parent).Where(dii => valueTypes.Contains(dii.ValueType));
}
private SelectItemDialog CreateSelectionDialogWithItems(IEnumerable dataItemInfos)
{
var selectDataDialog = new SelectItemDialog(dialogOwner);
foreach (var dataItemInfo in dataItemInfos)
{
selectDataDialog.AddItemType(dataItemInfo.Name, dataItemInfo.Category, dataItemInfo.Image, dataItemInfo);
}
return selectDataDialog;
}
private static object GetNewDataObject(SelectItemDialog selectDataDialog, object parent)
{
var dataItemInfo = selectDataDialog.SelectedItemTag as DataItemInfo;
if (dataItemInfo == null)
{
return null;
}
return dataItemInfo.CreateData != null ? dataItemInfo.CreateData(parent) : null;
}
}
}