using System.Collections.Generic;
using System.Linq;
using Core.Common.Controls.Views;
using Core.Common.Gui;
using Core.Common.Gui.Forms;
using Core.Common.Gui.Plugin;
using Core.Components.Charting.Data;
using Core.Plugins.OxyPlot.Commands;
using Core.Plugins.OxyPlot.Forms;
using Core.Plugins.OxyPlot.Legend;
using Core.Plugins.OxyPlot.Properties;
namespace Core.Plugins.OxyPlot
{
///
/// This class ties all the components together to enable charting interaction.
///
public class OxyPlotGuiPlugin : GuiPlugin, IToolViewController, IDocumentViewController
{
private ChartingRibbon chartingRibbon;
private LegendController legendController;
private static ChartingInteractionController chartInteractionController;
private bool activated;
public override IRibbonCommandHandler RibbonCommandHandler
{
get
{
return chartingRibbon;
}
}
public override void Activate()
{
legendController = CreateLegendController(this);
chartInteractionController = CreateChartInteractionController(this);
chartingRibbon = CreateRibbon(legendController);
legendController.ToggleLegend();
Gui.ActiveViewChanged += GuiOnActiveViewChanged;
activated = true;
}
public override IEnumerable GetViewInfoObjects()
{
yield return new ViewInfo, ChartDataView>
{
Image = Resources.ChartIcon,
GetViewName = (v, o) => "Diagram"
};
}
public override void Dispose()
{
if (activated)
{
Gui.ActiveViewChanged -= GuiOnActiveViewChanged;
}
base.Dispose();
}
#region IToolWindowController
public bool IsToolWindowOpen()
{
return Gui.ToolWindowViews.Any(t => t.GetType() == typeof(T));
}
public void OpenToolView(IView toolView)
{
Gui.OpenToolView(toolView);
UpdateComponentsForActiveView();
}
public void CloseToolView(IView toolView)
{
Gui.CloseToolView(toolView);
}
#endregion
#region IDocumentViewController
public IView ActiveView
{
get
{
return Gui.ActiveView;
}
}
#endregion
///
/// Creates a new .
///
/// The to use for the controller
/// .
/// A new instance.
private static LegendController CreateLegendController(IToolViewController toolViewController)
{
var controller = new LegendController(toolViewController);
return controller;
}
///
/// Creates a new .
///
/// The to use for the controller.
/// A new instance.
private ChartingInteractionController CreateChartInteractionController(IDocumentViewController controller)
{
return new ChartingInteractionController(controller);
}
///
/// Creates the and the commands that will be used when clicking on the buttons.
///
/// The to use for the
/// .
/// A new instance.
private static ChartingRibbon CreateRibbon(LegendController legendController)
{
return new ChartingRibbon
{
OpenChartViewCommand = new OpenChartViewCommand(),
ToggleLegendViewCommand = new ToggleLegendViewCommand(legendController),
TogglePanningCommand = new TogglePanningCommand(chartInteractionController)
};
}
private void GuiOnActiveViewChanged(object sender, ActiveViewChangeEventArgs activeViewChangeEventArgs)
{
UpdateComponentsForActiveView();
}
///
/// Updates the components which the knows about so that it reflects
/// the currently active view.
///
private void UpdateComponentsForActiveView()
{
var chartView = Gui.ActiveView as IChartView;
if (chartView != null)
{
chartingRibbon.ShowChartingTab();
legendController.UpdateForChart(chartView.Chart);
}
else
{
chartingRibbon.HideChartingTab();
legendController.UpdateForChart(null);
}
}
}
}