Index: Core/Plugins/src/Core.Plugins.Map/Legend/MapLegendView.cs =================================================================== diff -u -r8905298103eb01ce13dd5c1a2f267f879d4fda3e -re6e469cae0962a8a697a1b6fbf9753eefaa0eb19 --- Core/Plugins/src/Core.Plugins.Map/Legend/MapLegendView.cs (.../MapLegendView.cs) (revision 8905298103eb01ce13dd5c1a2f267f879d4fda3e) +++ Core/Plugins/src/Core.Plugins.Map/Legend/MapLegendView.cs (.../MapLegendView.cs) (revision e6e469cae0962a8a697a1b6fbf9753eefaa0eb19) @@ -20,23 +20,15 @@ // All rights reserved. using System; -using System.IO; using System.Linq; using System.Windows.Forms; using Core.Common.Controls.TreeView; using Core.Common.Controls.Views; using Core.Common.Gui.ContextMenu; -using Core.Common.IO.Exceptions; -using Core.Common.Utils.Builders; using Core.Components.DotSpatial.Forms; using Core.Components.Gis.Data; -using Core.Components.Gis.IO.Readers; -using DotSpatial.Data; -using DotSpatial.Topology; -using log4net; using MapResources = Core.Plugins.Map.Properties.Resources; using GuiResources = Core.Common.Gui.Properties.Resources; -using ILog = log4net.ILog; namespace Core.Plugins.Map.Legend { @@ -45,8 +37,6 @@ /// public sealed partial class MapLegendView : UserControl, ISelectionProvider { - private static readonly ILog log = LogManager.GetLogger(typeof(MapLegendView)); - private readonly IContextMenuBuilderProvider contextMenuBuilderProvider; private readonly IWin32Window parentWindow; @@ -156,7 +146,11 @@ CanDrop = MapControlCanDrop, CanInsert = MapControlCanInsert, OnDrop = MapControlOnDrop, - ContextMenuStrip = MapDataCollectionContextMenuStrip + ContextMenuStrip = (mapDataCollection, parentData, treeView) => contextMenuBuilderProvider.Get(mapDataCollection, treeView) + .AddImportItem() + .AddSeparator() + .AddPropertiesItem() + .Build() }); } @@ -192,116 +186,6 @@ target.NotifyObservers(); } - private ContextMenuStrip MapDataCollectionContextMenuStrip(MapDataCollection mapDataCollection, object parentData, TreeViewControl treeView) - { - StrictContextMenuItem addMapLayerMenuItem = new StrictContextMenuItem( - MapResources.MapLegendView_MapDataCollectionContextMenuStrip__Add_MapLayer, - MapResources.MapLegendView_MapDataCollectionContextMenuStrip_Add_MapLayer_ToolTip, - MapResources.MapPlusIcon, - (sender, args) => ShowSelectShapeFileDialog(mapDataCollection)); - - return contextMenuBuilderProvider.Get(mapDataCollection, treeView) - .AddCustomItem(addMapLayerMenuItem) - .AddSeparator() - .AddPropertiesItem() - .Build(); - } - #endregion - - #region ShapeFileImporter - - private void ShowSelectShapeFileDialog(MapDataCollection mapDataCollection) - { - using (var dialog = new OpenFileDialog - { - Filter = string.Format("{0} (*.shp)|*.shp", MapResources.MapLegendView_ShowSelectShapeFileDialog_Shape_file), - Title = GuiResources.OpenFileDialog_Title - }) - { - if (dialog.ShowDialog(parentWindow) == DialogResult.OK) - { - CheckDataFormat(dialog.FileName, Path.GetFileNameWithoutExtension(dialog.FileName), mapDataCollection); - } - } - } - - private static void CheckDataFormat(string filePath, string title, MapDataCollection mapDataCollection) - { - try - { - var featureSet = Shapefile.OpenFile(filePath); - - FeatureBasedMapData importedData; - - switch (featureSet.FeatureType) - { - case FeatureType.Point: - case FeatureType.MultiPoint: - using (ShapeFileReaderBase reader = new PointShapeFileReader(filePath)) - { - importedData = GetShapeFileData(reader, title); - } - break; - case FeatureType.Line: - using (ShapeFileReaderBase reader = new PolylineShapeFileReader(filePath)) - { - importedData = GetShapeFileData(reader, title); - } - break; - case FeatureType.Polygon: - using (ShapeFileReaderBase reader = new PolygonShapeFileReader(filePath)) - { - importedData = GetShapeFileData(reader, title); - } - break; - default: - log.Error(MapResources.MapLegendView_CheckDataFormat_ShapeFile_Contains_Unsupported_Data); - return; - } - - mapDataCollection.Add(importedData); - - log.Info(MapResources.MapLegendView_CheckDataFormat_Shapefile_Is_Imported); - mapDataCollection.NotifyObservers(); - } - catch (ArgumentException) - { - string message = new FileReaderErrorMessageBuilder(filePath) - .Build(MapResources.MapLegendView_CheckDataFormat_File_does_not_contain_geometries); - log.Error(message); - } - catch (FileNotFoundException) - { - string message = new FileReaderErrorMessageBuilder(filePath) - .Build(MapResources.MapLegendView_CheckDataFormat_File_does_not_exist_or_misses_needed_files); - log.Error(message); - } - catch (IOException) - { - string message = new FileReaderErrorMessageBuilder(filePath) - .Build(MapResources.MapLegendView_CheckDataFormat_An_error_occurred_when_trying_to_read_the_file); - log.Error(message); - } - catch (CriticalFileReadException e) - { - log.Error(e.Message); - } - catch (Exception) - { - // Because NullReferenceException or NotImplementedException when reading in a corrupt shape file - // from a third party library is expected, we catch all the exceptions here. - string message = new FileReaderErrorMessageBuilder(filePath) - .Build(MapResources.MapLegendView_CheckDataFormat_An_error_occurred_when_trying_to_read_the_file); - log.Error(message); - } - } - - private static FeatureBasedMapData GetShapeFileData(ShapeFileReaderBase reader, string title) - { - return reader.ReadShapeFile(title); - } - - #endregion } } \ No newline at end of file Index: Core/Plugins/test/Core.Plugins.Map.Test/Legend/MapDataCollectionTreeNodeInfoTest.cs =================================================================== diff -u -r7a02214c204a80a085b50b8d5fd7f90b9e0b4149 -re6e469cae0962a8a697a1b6fbf9753eefaa0eb19 --- Core/Plugins/test/Core.Plugins.Map.Test/Legend/MapDataCollectionTreeNodeInfoTest.cs (.../MapDataCollectionTreeNodeInfoTest.cs) (revision 7a02214c204a80a085b50b8d5fd7f90b9e0b4149) +++ Core/Plugins/test/Core.Plugins.Map.Test/Legend/MapDataCollectionTreeNodeInfoTest.cs (.../MapDataCollectionTreeNodeInfoTest.cs) (revision e6e469cae0962a8a697a1b6fbf9753eefaa0eb19) @@ -26,12 +26,10 @@ using Core.Common.Base; using Core.Common.Controls.TreeView; using Core.Common.Gui.ContextMenu; -using Core.Common.Gui.TestUtil.ContextMenu; using Core.Common.TestUtil; using Core.Common.Utils.Reflection; using Core.Components.Gis.Data; using Core.Plugins.Map.Legend; -using Core.Plugins.Map.Properties; using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; @@ -267,7 +265,7 @@ var mapDataCollection = new MapDataCollection("test data"); var menuBuilderMock = mocks.StrictMock(); - menuBuilderMock.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock); + menuBuilderMock.Expect(mb => mb.AddImportItem()).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddSeparator()).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddPropertiesItem()).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.Build()).Return(null); @@ -286,128 +284,6 @@ // Expectancies will be asserted in TearDown() } - [Test] - public void ContextMenuStrip_Always_ContainsAddMapLayerMenuItem() - { - // Setup - const string expectedItemText = "&Voeg kaartlaag toe..."; - const string expectedItemTooltip = "Importeer een nieuwe kaartlaag en voeg deze toe."; - var mapDataCollection = new MapDataCollection("test data"); - - using (var treeViewControl = new TreeViewControl()) - { - contextMenuBuilderProvider.Expect(cmbp => cmbp.Get(mapDataCollection, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); - mocks.ReplayAll(); - - // Call - using (ContextMenuStrip contextMenu = info.ContextMenuStrip(mapDataCollection, null, treeViewControl)) - { - // Assert - TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0, expectedItemText, expectedItemTooltip, Resources.MapPlusIcon); - } - } - } - - [Test] - [RequiresSTA] - public void GivenFilePathIsSet_WhenShapeFileIsCorrupt_ThenNoMapDataViewIsAdded() - { - // Setup - string testFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "CorruptFile.shp"); - - DialogBoxHandler = (name, wnd) => - { - OpenFileDialogTester tester = new OpenFileDialogTester(wnd); - tester.OpenFile(testFilePath); - }; - - // When - var mapDataCollection = new MapDataCollection("test data"); - using (var treeViewControl = new TreeViewControl()) - { - contextMenuBuilderProvider.Expect(cmbp => cmbp.Get(mapDataCollection, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); - mocks.ReplayAll(); - - using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(mapDataCollection, null, treeViewControl)) - { - // When - Action action = () => contextMenuStrip.Items[0].PerformClick(); - - // Then - var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie.", - testFilePath); - TestHelper.AssertLogMessageIsGenerated(action, expectedMessage); - } - } - - mocks.VerifyAll(); - } - - [Test] - [RequiresSTA] - public void GivenFilePathIsSet_WhenShapeFileIsEmpty_ThenNoMapDataViewIsAdded() - { - // Setup - string testFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "EmptyFile.shp"); - - DialogBoxHandler = (name, wnd) => - { - OpenFileDialogTester tester = new OpenFileDialogTester(wnd); - tester.OpenFile(testFilePath); - }; - - // When - var mapDataCollection = new MapDataCollection("test data"); - using (var treeViewControl = new TreeViewControl()) - { - contextMenuBuilderProvider.Expect(cmbp => cmbp.Get(mapDataCollection, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); - mocks.ReplayAll(); - - using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(mapDataCollection, null, treeViewControl)) - { - // When - Action action = () => contextMenuStrip.Items[0].PerformClick(); - - // Then - var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': kon geen geometrieën vinden in dit bestand.", - testFilePath); - TestHelper.AssertLogMessageIsGenerated(action, expectedMessage); - } - } - } - - [Test] - [RequiresSTA] - public void GivenFilePathIsSet_WhenShapeFileIsValid_ThenMapDataViewIsAdded() - { - // Setup - string testFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Single_Point_with_ID.shp"); - - DialogBoxHandler = (name, wnd) => - { - OpenFileDialogTester tester = new OpenFileDialogTester(wnd); - tester.OpenFile(testFilePath); - }; - - // When - var mapDataCollection = new MapDataCollection("test data"); - using (var treeViewControl = new TreeViewControl()) - { - contextMenuBuilderProvider.Expect(cmbp => cmbp.Get(mapDataCollection, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); - mocks.ReplayAll(); - - using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(mapDataCollection, null, treeViewControl)) - { - // When - Action action = () => contextMenuStrip.Items[0].PerformClick(); - - // Then - string expectedMessage = "Het shapebestand is geïmporteerd."; - TestHelper.AssertLogMessageIsGenerated(action, expectedMessage); - } - } - } - public override void TearDown() { mapLegendView.Dispose();