Index: src/DeltaShell/DeltaShell.Plugins.SharpMapGis.Gui/SharpMapGisGuiPlugin.cs =================================================================== diff -u -r18f9b18cab9da0c768badb3084415e993a5414ee -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/DeltaShell/DeltaShell.Plugins.SharpMapGis.Gui/SharpMapGisGuiPlugin.cs (.../SharpMapGisGuiPlugin.cs) (revision 18f9b18cab9da0c768badb3084415e993a5414ee) +++ src/DeltaShell/DeltaShell.Plugins.SharpMapGis.Gui/SharpMapGisGuiPlugin.cs (.../SharpMapGisGuiPlugin.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -33,45 +33,58 @@ [Extension(typeof(IPlugin))] public class SharpMapGisGuiPlugin : GuiPlugin { - private bool isActive; private static IGui gui; - private static SharpMapGisGuiPlugin instance; private static MapLegendView mapLegendView; private static IGisGuiService gisGuiService; + private bool isActive; private IRibbonCommandHandler ribbonCommandHandler; - public SharpMapGisGuiPlugin() { - instance = this; + Instance = this; gisGuiService = new GisGuiService(this); } public override string Name { - get { return "GIS (UI)"; } + get + { + return "GIS (UI)"; + } } public override string DisplayName { - get { return "SharpMap GIS Plugin (UI)"; } + get + { + return "SharpMap GIS Plugin (UI)"; + } } public override string Description { - get { return SharpMapGis.Properties.Resources.SharpMapGisApplicationPlugin_Description; } + get + { + return SharpMapGis.Properties.Resources.SharpMapGisApplicationPlugin_Description; + } } public override string Version { - get { return GetType().Assembly.GetName().Version.ToString(); } + get + { + return GetType().Assembly.GetName().Version.ToString(); + } } public override bool IsActive { - get { return isActive; } + get + { + return isActive; + } } public override IRibbonCommandHandler RibbonCommandHandler @@ -89,26 +102,132 @@ public override IGui Gui { - get { return gui; } - set { gui = value; } + get + { + return gui; + } + set + { + gui = value; + } } - public static SharpMapGisGuiPlugin Instance - { - get { return instance; } - } + public static SharpMapGisGuiPlugin Instance { get; private set; } public IGisGuiService GisGuiService { - get { return gisGuiService; } - set { gisGuiService = value; } + get + { + return gisGuiService; + } + set + { + gisGuiService = value; + } } public MapLegendView MapLegendView { - get { return 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(); @@ -156,7 +275,7 @@ } gui = null; - instance = null; + Instance = null; mapLegendView = null; gisGuiService = null; } @@ -166,7 +285,10 @@ UpdateMapLegendView(); var mapView = GetFocusedMapView(view); - if (mapView == null) return; + if (mapView == null) + { + return; + } mapView.MapControl.SelectedFeaturesChanged -= MapControlSelectedFeaturesChanged; mapView.MapControl.MouseDoubleClick -= mapView_MouseDoubleClick; @@ -180,64 +302,72 @@ public override IEnumerable GetViewInfoObjects() { - yield return new ViewInfo {Description = "Map"}; + yield return new ViewInfo + { + Description = "Map" + }; yield return new ViewInfo - { - Description = "Attribute table", - CompositeViewType = typeof(ProjectItemMapView), - GetViewName = (v,o) => o. Name + " Attributes" - }; - yield return new ViewInfo { Description = "Table" }; + { + Description = "Attribute table", + CompositeViewType = typeof(ProjectItemMapView), + GetViewName = (v, o) => o.Name + " Attributes" + }; + yield return new ViewInfo + { + Description = "Table" + }; yield return new ViewInfo + { + Description = "Map", + AdditionalDataCheck = o => CanLayerProvidersCreateLayerFor(o), + GetViewData = o => o, + GetViewName = (v, o) => o.Name, + OnActivateView = (v, o) => { - Description = "Map", - AdditionalDataCheck = o => CanLayerProvidersCreateLayerFor(o), - GetViewData = o => o, - GetViewName = (v, o) => o.Name, - OnActivateView = (v, o) => - { - var layerData = o; - var layer = v.MapView.GetLayerForData(layerData); - if (layer != null) - { - v.MapView.EnsureVisible(layer); + var layerData = o; + var layer = v.MapView.GetLayerForData(layerData); + if (layer != null) + { + v.MapView.EnsureVisible(layer); - if (MapLegendView != null) - { - MapLegendView.EnsureVisible(layer); - } - } - }, - AfterCreate = (v, o) => + if (MapLegendView != null) { - var mapLayerProviders = Gui.Plugins.Select(p => p.MapLayerProvider).Where(p => p != null).ToList(); + MapLegendView.EnsureVisible(layer); + } + } + }, + AfterCreate = (v, o) => + { + var mapLayerProviders = Gui.Plugins.Select(p => p.MapLayerProvider).Where(p => p != null).ToList(); - v.CreateLayerForData = (lo, ld) => MapLayerProviderHelper.CreateLayersRecursive(lo, null, mapLayerProviders, ld); - v.RefreshLayersAction = (l, ld, po) => MapLayerProviderHelper.RefreshLayersRecursive(l, ld, mapLayerProviders, po); - }, - CloseForData = (v, o) => v.Data.Equals(o) // only close the view if it is the root object - }; + v.CreateLayerForData = (lo, ld) => MapLayerProviderHelper.CreateLayersRecursive(lo, null, mapLayerProviders, ld); + v.RefreshLayersAction = (l, ld, po) => MapLayerProviderHelper.RefreshLayersRecursive(l, ld, mapLayerProviders, po); + }, + CloseForData = (v, o) => v.Data.Equals(o) // only close the view if it is the root object + }; } - + public override IMenuItem GetContextMenu(object sender, object data) { //custom treenodes for maplegend view if (sender is TreeNode) { - var treeNode = (TreeNode)sender; + var treeNode = (TreeNode) sender; if (treeNode.TreeView.Parent == mapLegendView) { return mapLegendView != null ? mapLegendView.GetContextMenu(data) : null; } - } return null; } public override IEnumerable GetProjectTreeViewNodePresenters() { - yield return new MapProjectTreeViewNodePresenter {GuiPlugin = this}; + yield return new MapProjectTreeViewNodePresenter + { + GuiPlugin = this + }; } public override bool CanDrop(object source, object target) @@ -246,8 +376,8 @@ { return true; } - - if (source is IList && target is Map && ((IEnumerable)source).Cast().FirstOrDefault() is IGeometry) + + if (source is IList && target is Map && ((IEnumerable) source).Cast().FirstOrDefault() is IGeometry) { return true; } @@ -259,7 +389,7 @@ { var map = target as Map; - if(map == null) + if (map == null) { return; } @@ -271,24 +401,33 @@ if (source is IList) { - var items = (IList)source; - - if(items.Count > 0 && items[0] is IGeometry) + var items = (IList) source; + + if (items.Count > 0 && items[0] is IGeometry) { var provider = new FeatureCollection(); foreach (var item in items) { var geometry = (IGeometry) item; provider.Features.Add(geometry); } - ILayer layer = new VectorLayer { DataSource = provider }; + ILayer layer = new VectorLayer + { + DataSource = provider + }; map.Layers.Add(layer); } if (items.Count > 0 && items[0] is IFeature) { - var provider = new FeatureCollection {Features = items}; - ILayer layer = new VectorLayer { DataSource = provider }; + var provider = new FeatureCollection + { + Features = items + }; + ILayer layer = new VectorLayer + { + DataSource = provider + }; map.Layers.Add(layer); } } @@ -299,97 +438,14 @@ yield return GetType().Assembly; } - /// - /// 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) + internal static MapView GetFocusedMapView(IView view = null) { - 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; - } - }; + return gui.GetFocusedMapView(view); } - 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; - } - } - private void ApplicationProjectClosing(Project project) { - if(gui == null) + if (gui == null) { return; } @@ -400,7 +456,7 @@ } } - static void GuiSelectionChanged(object sender, SelectedItemChangedEventArgs e) + private static void GuiSelectionChanged(object sender, SelectedItemChangedEventArgs e) { // Send selection to all relevant views var selection = gui.Selection; @@ -414,10 +470,10 @@ view.MapControl.SelectedFeaturesChanged += MapControlSelectedFeaturesChanged; } } - else if (selection is IEnumerable && ((IEnumerable)selection).OfType().Any()) + else if (selection is IEnumerable && ((IEnumerable) selection).OfType().Any()) { // If the selection was made from within the map control, also select it in the corresponding attribute table - var selectedFeatures = ((IEnumerable)selection).OfType(); + var selectedFeatures = ((IEnumerable) selection).OfType(); if (!selectedFeatures.Any()) { return; @@ -431,11 +487,6 @@ } } - internal static MapView GetFocusedMapView(IView view = null) - { - return gui.GetFocusedMapView(view); - } - private void DocumentViewsActiveViewChanged(object sender, EventArgs e) { UpdateMapLegendView(); @@ -487,7 +538,7 @@ private static void UpdateMapLegendView() { - if(mapLegendView == null) + if (mapLegendView == null) { return; } @@ -505,22 +556,21 @@ { mapLegendView.Data = null; } - } private static void RefreshRibbonItems() { gui.MainWindow.ValidateItems(); } - static void MapMapRendered(Graphics g) + private static void MapMapRendered(Graphics g) { RefreshRibbonItems(); } // TODO: WTF?!? Use mapControl.SelectTool.SelectTool.SelectionChanged += ... // Reason why not to: AddTool and EditTool also have their own selection mechanism - static void MapControlSelectedFeaturesChanged(object sender, EventArgs e) + private static void MapControlSelectedFeaturesChanged(object sender, EventArgs e) { gui.SelectionChanged -= GuiSelectionChanged; @@ -540,16 +590,16 @@ gui.SelectionChanged += GuiSelectionChanged; } - static void mapView_MouseDoubleClick(object sender, MouseEventArgs e) + private static void mapView_MouseDoubleClick(object sender, MouseEventArgs e) { - IEnumerable selectedFeatures = ((MapControl)sender).SelectedFeatures; + IEnumerable selectedFeatures = ((MapControl) sender).SelectedFeatures; if (selectedFeatures == null || !selectedFeatures.Any()) { return; } var data = selectedFeatures.First(); - + gui.CommandHandler.OpenView(data); }