// Copyright (C) Stichting Deltares 2016. 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.Linq;
using Core.Common.Controls.Views;
using Core.Common.Gui;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.Forms.ViewHost;
using Core.Components.Gis.Data;
using Core.Plugins.Map.Properties;
namespace Core.Plugins.Map.Legend
{
///
/// This class controls the actions which are related to controlling visibility and updating contents of a .
///
public class MapLegendController : IDisposable
{
private readonly IViewController viewController;
private readonly IContextMenuBuilderProvider contextMenuBuilderProvider;
///
/// Fired when the map legend has been opened.
///
public EventHandler OnOpenLegend;
private IView legendView;
///
/// Creates a new instance of .
///
/// The to invoke actions upon.
/// The to create context menus.
/// Thrown when
/// or is null.
public MapLegendController(IViewController viewController, IContextMenuBuilderProvider contextMenuBuilderProvider)
{
if (viewController == null)
{
throw new ArgumentNullException("viewController", @"Cannot create a MapLegendController when the view controller is null.");
}
if (contextMenuBuilderProvider == null)
{
throw new ArgumentNullException("contextMenuBuilderProvider", @"Cannot create a MapLegendController when the context menu builder provider is null.");
}
this.viewController = viewController;
this.contextMenuBuilderProvider = contextMenuBuilderProvider;
}
///
/// Gets a value indicating whether the is visible.
///
public bool IsMapLegendViewOpen
{
get
{
return legendView != null && viewController.ViewHost.ToolViews.Contains(legendView);
}
}
///
/// Toggles the visibility of the .
///
public void ToggleView()
{
if (IsMapLegendViewOpen)
{
CloseLegendView();
}
else
{
OpenLegendView();
}
}
///
/// Updates the data for the if it is open.
///
/// The to show. If null the data
/// will be cleared.
public void Update(MapData data)
{
if (IsMapLegendViewOpen)
{
legendView.Data = data;
}
}
public void Dispose()
{
CloseLegendView();
}
///
/// Opens the .
///
private void OpenLegendView()
{
legendView = new MapLegendView(contextMenuBuilderProvider);
viewController.ViewHost.AddToolView(legendView, ToolViewLocation.Left);
viewController.ViewHost.SetImage(legendView, Resources.MapIcon);
if (OnOpenLegend != null)
{
OnOpenLegend(this, EventArgs.Empty);
}
}
///
/// Closes the .
///
private void CloseLegendView()
{
viewController.ViewHost.Remove(legendView);
legendView = null;
}
}
}