Index: src/DeltaShell/DeltaShell.Plugins.SharpMapGis.Gui/Forms/ProjectItemMapView.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/DeltaShell/DeltaShell.Plugins.SharpMapGis.Gui/Forms/ProjectItemMapView.cs (.../ProjectItemMapView.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/DeltaShell/DeltaShell.Plugins.SharpMapGis.Gui/Forms/ProjectItemMapView.cs (.../ProjectItemMapView.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.ComponentModel; using System.Drawing; using System.Linq; using System.Windows.Forms; @@ -10,37 +11,44 @@ using DelftTools.Utils; using DelftTools.Utils.Collections; using DelftTools.Utils.Collections.Generic; -using DelftTools.Utils.Editing; +using DeltaShell.Plugins.SharpMapGis.Gui.Properties; using GisSharpBlog.NetTopologySuite.Geometries; using SharpMap.Api.Layers; using SharpMap.Layers; +using IEditableObject = DelftTools.Utils.Editing.IEditableObject; namespace DeltaShell.Plugins.SharpMapGis.Gui.Forms { public partial class ProjectItemMapView : UserControl, ICompositeView, ISearchableView { - private MapView mapView; + private readonly Dictionary layerDataDictionary = new Dictionary(); + private readonly IEventedList childViews = new EventedList(); + public Action, object> RefreshLayersAction; private IProjectItem mapData; private Func, ILayer> createLayerForData; - private readonly Dictionary layerDataDictionary = new Dictionary(); private ILayer mapDataLayer; - public Action, object> RefreshLayersAction; - private readonly IEventedList childViews = new EventedList(); private bool viewChangeInitiatedHere; + private int IsEditingCount = 0; + public ProjectItemMapView() { InitializeComponent(); AddMapView(); - childViews.Add(mapView); + childViews.Add(MapView); childViews.CollectionChanged += (sender, args) => { if (viewChangeInitiatedHere) + { return; // prevent recursion + } var layerEditor = args.Item as ILayerEditorView; - if (layerEditor == null) return; + if (layerEditor == null) + { + return; + } viewChangeInitiatedHere = true; try @@ -49,18 +57,18 @@ { case NotifyCollectionChangeAction.Add: { - var layer = mapView.GetLayerForData(layerEditor.Data); + var layer = MapView.GetLayerForData(layerEditor.Data); if (layer != null) { layerEditor.Layer = layer; } - mapView.TabControl.AddView(layerEditor); + MapView.TabControl.AddView(layerEditor); } break; case NotifyCollectionChangeAction.Remove: layerEditor.Layer = null; - mapView.TabControl.RemoveView(layerEditor); + MapView.TabControl.RemoveView(layerEditor); break; default: throw new ArgumentOutOfRangeException(); @@ -72,12 +80,15 @@ } }; } - - public MapView MapView { get { return mapView; } } + public MapView MapView { get; private set; } + public Func, ILayer> CreateLayerForData { - get { return createLayerForData; } + get + { + return createLayerForData; + } set { createLayerForData = value; @@ -87,10 +98,13 @@ public object Data { - get { return mapData; } + get + { + return mapData; + } set { - UnSubscribe(); + UnSubscribe(); mapData = (IProjectItem) value; @@ -102,33 +116,95 @@ public Image Image { - get { return Properties.Resources.Map; } - set + get { - + return Resources.Map; } + set {} } public IEventedList ChildViews { - get { return childViews; } + get + { + return childViews; + } } - public bool HandlesChildViews { get { return false; } } - + public bool HandlesChildViews + { + get + { + return false; + } + } + + public ViewInfo ViewInfo { get; set; } + + public void RefreshLayers() + { + if (RefreshLayersAction != null) + { + RefreshLayersAction(mapDataLayer, layerDataDictionary, null); + } + } + public void ActivateChildView(IView childView) { - mapView.ActivateChildView(childView); + MapView.ActivateChildView(childView); } public void EnsureVisible(object item) { - if (mapView != null) - mapView.EnsureVisible(item); + if (MapView != null) + { + MapView.EnsureVisible(item); + } } - public ViewInfo ViewInfo { get; set; } + public IEnumerable> SearchItemsByText(string text, bool caseSensitive, Func isSearchCancelled, + Action setProgressPercentage) + { + return MapView.SearchItemsByText(text, caseSensitive, isSearchCancelled, setProgressPercentage); + } + protected override void Dispose(bool disposing) + { + if (MapView != null) + { + if (disposing) + { + MapView.TabControl.BeforeDispose(); + } + MapView.TabControl.ViewCollectionChanged -= MapTabCollectionChanged; + MapView.GetLayerForData = null; + MapView.GetDataForLayer = null; + + if (MapView.Map != null) + { + var map = MapView.Map; + MapView.Map = null; // prevent further rendering + // project item map view owns the map, so we should dispose the layers here: + foreach (var layer in map.Layers) + { + layer.DisposeLayersRecursive(); + } + } + + if (disposing) + { + MapView.Dispose(); + } + MapView = null; + } + + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + private void UnSubscribe() { var notifyCollectionChange = mapData as INotifyCollectionChange; @@ -159,68 +235,76 @@ } } - private int IsEditingCount = 0; - - void MapDataPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + private void MapDataPropertyChanged(object sender, PropertyChangedEventArgs e) { if (!(Equals(sender, mapData)) || e.PropertyName != "IsEditing") { - return; + return; } - + var editableObject = sender as IEditableObject; - if (editableObject == null) return; + if (editableObject == null) + { + return; + } if (editableObject.IsEditing) + { IsEditingCount++; + } else if (IsEditingCount > 0) + { IsEditingCount--; + } - if(IsEditingCount != 0) return; + if (IsEditingCount != 0) + { + return; + } RefreshLayers(); } private void MapDataCollectionChanged(object sender, NotifyCollectionChangingEventArgs e) { //only refresh when IsEditing is false, set by the MapDataPropertyChanged, checked by the count (IsEditingCount) being 0 - if (IsEditingCount != 0) return; + if (IsEditingCount != 0) + { + return; + } RefreshLayers(); } - public void RefreshLayers() + private void AddMapView() { - if (RefreshLayersAction != null) + MapView = new MapView { - RefreshLayersAction(mapDataLayer, layerDataDictionary, null); - } - } + Dock = DockStyle.Fill, + Map = + { + Name = "Map" + }, + IsTabControlVisible = false, + GetDataForLayer = layer => layerDataDictionary != null && layerDataDictionary.ContainsKey(layer) + ? layerDataDictionary[layer] + : null, + GetLayerForData = o => layerDataDictionary != null && layerDataDictionary.ContainsValue(o) + ? layerDataDictionary.FirstOrDefault(x => x.Value == o).Key + : null + }; - private void AddMapView() - { - mapView = new MapView - { - Dock = DockStyle.Fill, - Map = {Name = "Map"}, - IsTabControlVisible = false, - GetDataForLayer = layer => layerDataDictionary != null && layerDataDictionary.ContainsKey(layer) - ? layerDataDictionary[layer] - : null, - GetLayerForData = o => layerDataDictionary != null && layerDataDictionary.ContainsValue(o) - ? layerDataDictionary.FirstOrDefault(x => x.Value == o).Key - : null - }; + MapView.TabControl.ViewCollectionChanged += MapTabCollectionChanged; - mapView.TabControl.ViewCollectionChanged += MapTabCollectionChanged; - - Controls.Add(mapView); + Controls.Add(MapView); } private void MapTabCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (viewChangeInitiatedHere) + { return; + } // apparently a tab was added / remove through another path (Open Layer Editor in map legend, // for example); update our child views @@ -256,39 +340,45 @@ return; } - mapView.Map.Layers.Clear(); + MapView.Map.Layers.Clear(); mapDataLayer = createLayerForData(mapData, layerDataDictionary); if (mapDataLayer != null) { - mapView.Map.Layers.Add(mapDataLayer); + MapView.Map.Layers.Add(mapDataLayer); mapDataLayer.CanBeRemovedByUser = false; } if (mapDataLayer != null && (mapDataLayer.Envelope == null || mapDataLayer.Envelope.Area <= 1)) { - mapView.Map.ZoomToFit(new Envelope(new Coordinate(0, 0), new Coordinate(2000, 2000))); + MapView.Map.ZoomToFit(new Envelope(new Coordinate(0, 0), new Coordinate(2000, 2000))); } else { - mapView.Map.ZoomToExtents(); + MapView.Map.ZoomToExtents(); } } private static IList CreateMapLayerInfoList(ILayer layer) { return CreateLayerByPathLookup(layer, "") - .Select(pathLayerKvp => new GeneratedMapLayerInfo(pathLayerKvp.Value) {ParentPath = pathLayerKvp.Key}) - .ToList(); + .Select(pathLayerKvp => new GeneratedMapLayerInfo(pathLayerKvp.Value) + { + ParentPath = pathLayerKvp.Key + }) + .ToList(); } private static IEnumerable> CreateLayerByPathLookup(ILayer layer, string path) { yield return new KeyValuePair(path, layer); var groupLayer = layer as GroupLayer; - if (groupLayer == null) yield break; + if (groupLayer == null) + { + yield break; + } var parentPath = string.Format("{0}\\{1}", path, groupLayer.Name); foreach (var kvp in groupLayer.Layers.SelectMany(subLayer => CreateLayerByPathLookup(subLayer, parentPath))) @@ -305,52 +395,16 @@ foreach (var pathGeneratedMapLayerInfoKvp in generatedMapLayerInfoByPath) { var key = string.Format("{0}\\{1}", pathGeneratedMapLayerInfoKvp.Key, pathGeneratedMapLayerInfoKvp.Value.Name); - + ILayer generatedLayer; layerByPathLookup.TryGetValue(key, out generatedLayer); - if (generatedLayer == null) return; - - pathGeneratedMapLayerInfoKvp.Value.SetToLayer(generatedLayer); - } - } - - protected override void Dispose(bool disposing) - { - if (mapView != null) - { - if (disposing) + if (generatedLayer == null) { - mapView.TabControl.BeforeDispose(); + return; } - mapView.TabControl.ViewCollectionChanged -= MapTabCollectionChanged; - mapView.GetLayerForData = null; - mapView.GetDataForLayer = null; - if (mapView.Map != null) - { - var map = mapView.Map; - mapView.Map = null; // prevent further rendering - // project item map view owns the map, so we should dispose the layers here: - foreach (var layer in map.Layers) - layer.DisposeLayersRecursive(); - } - - if (disposing) - mapView.Dispose(); - mapView = null; + pathGeneratedMapLayerInfoKvp.Value.SetToLayer(generatedLayer); } - - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); } - - public IEnumerable> SearchItemsByText(string text, bool caseSensitive, Func isSearchCancelled, - Action setProgressPercentage) - { - return mapView.SearchItemsByText(text, caseSensitive, isSearchCancelled, setProgressPercentage); - } } -} +} \ No newline at end of file