using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Base.Workflow;
using Core.Common.Controls;
using Core.Common.Controls.Swf.Table;
using Core.Common.Gui;
using Core.Common.Gui.Forms;
using Core.Common.Utils;
using Core.GIS.GeoAPI.Extensions.Feature;
using Core.GIS.GeoAPI.Geometries;
using Core.GIS.SharpMap.Api.Layers;
using Core.GIS.SharpMap.Data.Providers;
using Core.GIS.SharpMap.Layers;
using Core.GIS.SharpMap.Map;
using Core.GIS.SharpMap.UI.Forms;
using Core.GIS.SharpMap.UI.Tools;
using Core.GIS.SharpMap.UI.Tools.Decorations;
using Core.Plugins.SharpMapGis.Gui.Forms;
using Core.Plugins.SharpMapGis.Gui.Forms.MapLegendView;
using Core.Plugins.SharpMapGis.Gui.NodePresenters;
using PropertyInfo = Core.Common.Gui.PropertyInfo;
namespace Core.Plugins.SharpMapGis.Gui
{
public class SharpMapGisGuiPlugin : GuiPlugin
{
private static IGui gui;
private static MapLegendView mapLegendView;
private static IGisGuiService gisGuiService;
private IRibbonCommandHandler ribbonCommandHandler;
public SharpMapGisGuiPlugin()
{
Instance = this;
gisGuiService = new GisGuiService(this);
}
public override IRibbonCommandHandler RibbonCommandHandler
{
get
{
if (ribbonCommandHandler == null)
{
var ribbon = new Ribbon();
ribbonCommandHandler = ribbon;
}
return ribbonCommandHandler;
}
}
public override IGui Gui
{
get
{
return gui;
}
set
{
gui = value;
}
}
public static SharpMapGisGuiPlugin Instance { get; private set; }
public IGisGuiService GisGuiService
{
get
{
return gisGuiService;
}
set
{
gisGuiService = value;
}
}
public MapLegendView MapLegendView
{
get
{
return mapLegendView;
}
}
///
/// Creates a object for
/// a IEnumerable of TFeature that is present part of a TFeatureContainer
///
///
/// Model - boundaries
///
/// var viewInfo = CreateAttributeTableViewInfo]]>(m => m.Boundaries, () => Gui)
///
///
/// Type of the sub features
/// Type of the feature container
/// Function for getting the IEnumerable of from
/// Function for getting an
public static ViewInfo, ILayer, VectorLayerAttributeTableView> CreateAttributeTableViewInfo(Func> getCollection, Func getGui)
{
return new ViewInfo, ILayer, VectorLayerAttributeTableView>
{
Description = "Attribute Table",
GetViewName = (v, o) => o.Name,
AdditionalDataCheck = o =>
{
var container = getGui().Application.Project.Items.OfType().FirstOrDefault(fc => Equals(o, getCollection(fc)));
return container != null;
},
GetViewData = o =>
{
var centralMap = getGui().DocumentViews.OfType()
.FirstOrDefault(v => v.MapView.GetLayerForData(o) != null);
return centralMap == null ? null : centralMap.MapView.GetLayerForData(o);
},
CompositeViewType = typeof(ProjectItemMapView),
GetCompositeViewData = o => getGui().Application.Project.Items.OfType().FirstOrDefault(fc =>
{
if (fc is TFeatureContainer)
{
return Equals(o, getCollection((TFeatureContainer) fc));
}
return false;
}),
AfterCreate = (v, o) =>
{
var centralMap = getGui().DocumentViews.OfType()
.FirstOrDefault(vi => vi.MapView.GetLayerForData(o) != null);
if (centralMap == null)
{
return;
}
v.DeleteSelectedFeatures = () => centralMap.MapView.MapControl.DeleteTool.DeleteSelection();
v.OpenViewMethod = ob => getGui().CommandHandler.OpenView(ob);
v.ZoomToFeature = feature => centralMap.MapView.EnsureVisible(feature);
v.CanAddDeleteAttributes = false;
}
};
}
public void InitializeMapLegend()
{
if ((mapLegendView == null) || (mapLegendView.IsDisposed))
{
mapLegendView = new MapLegendView(gui)
{
OnOpenLayerAttributeTable = layer =>
{
var mapView = GetFocusedMapView();
if (mapView == null)
{
return;
}
var layerData = mapView.GetDataForLayer != null
? mapView.GetDataForLayer(layer)
: null;
if (!Gui.DocumentViewsResolver.OpenViewForData(layerData, typeof(ILayerEditorView)) && layer is VectorLayer)
{
mapView.OpenLayerAttributeTable(layer, o => gui.CommandHandler.OpenView(o));
}
},
Text = Properties.Resources.SharpMapGisPluginGui_InitializeMapLegend_Map_Contents
};
}
// TODO: subscribe to mapLegendView.TreeView.SelectionChanged! and update Gui.Selection if necessary
if (gui.ToolWindowViews != null)
{
gui.ToolWindowViews.Add(mapLegendView, ViewLocation.Left | ViewLocation.Bottom);
gui.ToolWindowViews.ActiveView = mapLegendView;
}
}
public override void Activate()
{
InitializeMapLegend();
if (Gui != null && Gui.DocumentViews != null && Gui.DocumentViews.ActiveView != null)
{
DocumentViewsActiveViewChanged(Gui, null);
}
gui.SelectionChanged += GuiSelectionChanged;
gui.DocumentViews.ActiveViewChanged += DocumentViewsActiveViewChanged;
gui.DocumentViews.ChildViewChanged += DocumentViewsActiveViewChanged;
gui.Application.ProjectClosing += ApplicationProjectClosing;
if (gui.Application.ActivityRunner != null)
{
gui.Application.ActivityRunner.ActivityCompleted += ActivityRunnerOnActivityCompleted;
}
}
public override void Dispose()
{
if (gui != null)
{
gui.SelectionChanged -= GuiSelectionChanged;
var documentViews = gui.DocumentViews;
if (documentViews != null)
{
documentViews.ChildViewChanged -= DocumentViewsActiveViewChanged;
documentViews.ActiveViewChanged -= DocumentViewsActiveViewChanged;
}
var application = gui.Application;
if (gui.Application != null)
{
application.ProjectClosing -= ApplicationProjectClosing;
if (application.ActivityRunner != null)
{
application.ActivityRunner.ActivityCompleted -= ActivityRunnerOnActivityCompleted;
}
}
}
gui = null;
Instance = null;
mapLegendView = null;
gisGuiService = null;
}
public override void OnViewRemoved(IView view)
{
UpdateMapLegendView();
var mapView = GetFocusedMapView(view);
if (mapView == null)
{
return;
}
mapView.MapControl.SelectedFeaturesChanged -= MapControlSelectedFeaturesChanged;
mapView.MapControl.MouseDoubleClick -= mapView_MouseDoubleClick;
mapView.Map.MapRendered -= MapMapRendered;
}
public override IEnumerable GetPropertyInfos()
{
return SharpMapGisPropertyInfoProvider.GetPropertyInfos();
}
public override IEnumerable GetViewInfoObjects()
{
yield return new ViewInfo