// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Controls.TreeView;
using Core.Common.Controls.Views;
using Core.Common.Gui.ContextMenu;
using Core.Components.Gis.Data;
using Core.Components.Gis.Data.Removable;
using Core.Components.Gis.Forms;
using Core.Plugins.Map.PresentationObjects;
using MapResources = Core.Plugins.Map.Properties.Resources;
using GuiResources = Core.Common.Gui.Properties.Resources;
namespace Core.Plugins.Map.Legend
{
///
/// The view which shows the data that is added to a .
///
public sealed partial class MapLegendView : UserControl, ISelectionProvider, IView
{
private readonly IContextMenuBuilderProvider contextMenuBuilderProvider;
private IMapControl mapControl;
public event EventHandler SelectionChanged;
///
/// Creates a new instance of .
///
/// The to create context menus.
/// Thrown when is null.
public MapLegendView(IContextMenuBuilderProvider contextMenuBuilderProvider)
{
if (contextMenuBuilderProvider == null)
{
throw new ArgumentNullException(nameof(contextMenuBuilderProvider),
$@"Cannot create a {typeof(MapLegendView).Name} when the context menu builder provider is null.");
}
this.contextMenuBuilderProvider = contextMenuBuilderProvider;
InitializeComponent();
Text = MapResources.General_Map;
RegisterTreeNodeInfos();
treeViewControl.SelectedDataChanged += TreeViewControlSelectedDataChanged;
}
///
/// Gets or sets the for which this legend view is configured
/// to show the internal map data for.
///
public IMapControl MapControl
{
private get
{
return mapControl;
}
set
{
mapControl = value;
Data = value?.Data;
}
}
public object Selection
{
get
{
var mapDataContext = treeViewControl.SelectedData as MapDataContext;
if (mapDataContext != null)
{
return mapDataContext.WrappedData;
}
return treeViewControl.SelectedData;
}
}
public object Data
{
get
{
return (MapData) treeViewControl.Data;
}
set
{
treeViewControl.Data = (MapData) value;
}
}
private void TreeViewControlSelectedDataChanged(object sender, EventArgs e)
{
SelectionChanged?.Invoke(this, new EventArgs());
}
private void RegisterTreeNodeInfos()
{
treeViewControl.RegisterTreeNodeInfo(new TreeNodeInfo
{
Text = context => context.WrappedData.Name,
Image = GetImage,
ChildNodeObjects = MapDataContextGetChildNodeObjects,
CanDrag = (context, o) => true,
CanCheck = context => !(context.WrappedData is MapDataCollection),
IsChecked = context => context.WrappedData.IsVisible,
OnNodeChecked = MapDataContextOnNodeChecked,
CanDrop = MapDataContextCanDropAndInsert,
CanInsert = MapDataContextCanDropAndInsert,
OnDrop = MapDataContextOnDrop,
CanRemove = (context, parent) => CanRemoveMapData(context.WrappedData, parent),
OnNodeRemoved = (context, parent) => RemoveFromParent(context.WrappedData, parent),
ContextMenuStrip = MapDataContextContextMenuStrip
});
treeViewControl.RegisterTreeNodeInfo(new TreeNodeInfo
{
Text = mapDataCollection => mapDataCollection.Name,
Image = mapDataCollection => GuiResources.folder,
ChildNodeObjects = GetCollectionChildNodeObjects,
CanDrag = (collection, parentData) => true,
CanDrop = MapDataCollectionCanDropAndInsert,
CanInsert = MapDataCollectionCanDropAndInsert,
OnDrop = MapDataCollectionOnDrop,
ContextMenuStrip = MapDataCollectionContextMenuStrip
});
}
private static object[] GetChildNodeObjects(MapDataCollection mapDataCollection)
{
return mapDataCollection.Collection.Reverse()
.Select(mapData => new MapDataContext(mapData, mapDataCollection))
.Cast