Index: Core/Components/src/Core.Components.DotSpatial.Forms/Views/WmtsLocationControl.cs =================================================================== diff -u -rcfb8cddfe064c4aa4a1843846968554c918ba58f -r25b9b0e94d71419d3a2f2f92c810f81e1b90a628 --- Core/Components/src/Core.Components.DotSpatial.Forms/Views/WmtsLocationControl.cs (.../WmtsLocationControl.cs) (revision cfb8cddfe064c4aa4a1843846968554c918ba58f) +++ Core/Components/src/Core.Components.DotSpatial.Forms/Views/WmtsLocationControl.cs (.../WmtsLocationControl.cs) (revision 25b9b0e94d71419d3a2f2f92c810f81e1b90a628) @@ -24,7 +24,11 @@ using System.Linq; using System.Windows.Forms; using Core.Common.Controls.Views; +using Core.Components.DotSpatial.Forms.IO; using Core.Components.DotSpatial.Forms.Properties; +using Core.Components.DotSpatial.Layer.BruTile.Configurations; +using log4net; +using BaseResources = Core.Common.Base.Properties.Resources; namespace Core.Components.DotSpatial.Forms.Views { @@ -33,15 +37,17 @@ /// public partial class WmtsLocationControl : UserControl, IView { + private static readonly ILog log = LogManager.GetLogger(typeof(WmtsLocationControl)); private readonly List wmtsConnectionInfos; - private IEnumerable capabilities; + private List capabilities; /// /// Creates a new instance of . /// public WmtsLocationControl() { wmtsConnectionInfos = new List(); + capabilities = new List(); InitializeComponent(); InitializeDataGridView(); @@ -57,7 +63,7 @@ } set { - capabilities = value as IEnumerable; + capabilities = value as List; UpdateDataGridViewDataSource(); } } @@ -100,9 +106,20 @@ true); } + private void UpdateDataGridViewDataSource(IEnumerable wmtsCapabilities) + { + capabilities.Clear(); + foreach (var wmtsCapability in wmtsCapabilities) + { + capabilities.Add(new WmtsCapabilityRow(wmtsCapability)); + } + + UpdateDataGridViewDataSource(); + } + private void UpdateDataGridViewDataSource() { - dataGridViewControl.SetDataSource(capabilities?.ToArray()); + dataGridViewControl.SetDataSource(capabilities); } #endregion @@ -125,6 +142,8 @@ urlLocationComboBox.ValueMember = nameof(WmtsConnectionInfo.Url); urlLocationComboBox.SelectedItem = selectedItem; + + UpdateConnectToButton(); } #endregion @@ -133,10 +152,35 @@ private void InitializeEventHandlers() { + UpdateConnectToButton(); + connectToButton.Click += ConnectToButtonOnClick; addLocationButton.Click += AddLocationButtonOnClick; editLocationButton.Click += EditLocationButtonOnClick; } + private void UpdateConnectToButton() + { + connectToButton.Enabled = wmtsConnectionInfos.Any(); + } + + private void ConnectToButtonOnClick(object sender, EventArgs e) + { + var selectedWmtsConnectionInfo = urlLocationComboBox.SelectedItem as WmtsConnectionInfo; + + try + { + IEnumerable wmtsCapabilities = WmtsCapabilityFactory.GetWmtsCapabilities(selectedWmtsConnectionInfo?.Url).ToArray(); + UpdateDataGridViewDataSource(wmtsCapabilities); + } + catch (CannotFindTileSourceException exception) + { + Form controlForm = FindForm(); + MessageBox.Show(controlForm, exception.Message, BaseResources.Error, MessageBoxButtons.OK, + MessageBoxIcon.Error); + log.Error(exception.Message, exception); + } + } + private void AddLocationButtonOnClick(object sender, EventArgs eventArgs) { Form controlForm = FindForm(); Index: Core/Components/src/Core.Components.Gis/Core.Components.Gis.csproj =================================================================== diff -u -r4b32064801ece7ab70357a2a942e21f113abc679 -r25b9b0e94d71419d3a2f2f92c810f81e1b90a628 --- Core/Components/src/Core.Components.Gis/Core.Components.Gis.csproj (.../Core.Components.Gis.csproj) (revision 4b32064801ece7ab70357a2a942e21f113abc679) +++ Core/Components/src/Core.Components.Gis/Core.Components.Gis.csproj (.../Core.Components.Gis.csproj) (revision 25b9b0e94d71419d3a2f2f92c810f81e1b90a628) @@ -85,6 +85,7 @@ ResXFileCodeGenerator Resources.Designer.cs + Designer Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/Views/WmtsLocationControlTest.cs =================================================================== diff -u -rcfb8cddfe064c4aa4a1843846968554c918ba58f -r25b9b0e94d71419d3a2f2f92c810f81e1b90a628 --- Core/Components/test/Core.Components.DotSpatial.Forms.Test/Views/WmtsLocationControlTest.cs (.../WmtsLocationControlTest.cs) (revision cfb8cddfe064c4aa4a1843846968554c918ba58f) +++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/Views/WmtsLocationControlTest.cs (.../WmtsLocationControlTest.cs) (revision 25b9b0e94d71419d3a2f2f92c810f81e1b90a628) @@ -19,14 +19,20 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Core.Common.Controls.DataGrid; using Core.Common.Controls.Views; +using Core.Common.TestUtil; using Core.Components.DotSpatial.Forms.Views; +using Core.Components.DotSpatial.Layer.BruTile.Configurations; +using Core.Components.DotSpatial.TestUtil; +using Core.Components.Gis.Data; using NUnit.Extensions.Forms; using NUnit.Framework; +using Rhino.Mocks; namespace Core.Components.DotSpatial.Forms.Test.Views { @@ -47,7 +53,7 @@ // Assert Assert.IsInstanceOf(control); Assert.IsInstanceOf(control); - Assert.IsNull(control.Data); + Assert.IsInstanceOf>(control.Data); } } @@ -144,7 +150,7 @@ // Setup using (var view = new WmtsLocationControl()) { - var capabilityRows = Enumerable.Empty(); + var capabilityRows = new List(); // Call view.Data = capabilityRows; @@ -353,6 +359,75 @@ } [Test] + public void GivenWmtsLocationControlAndConnectClicked_WhenValidDataFromUrl_ThenDataGridUpdated() + { + // Given + WmtsMapData backgroundMapData = WmtsMapData.CreateDefaultPdokMapData(); + + using (new UseCustomTileSourceFactoryConfig(backgroundMapData)) + using (Form form = ShowValidWmtsLocationControl()) + { + form.Show(); + + var connectToButton = new ButtonTester("connectToButton", form); + + // When + connectToButton.Click(); + + // Then + var dataGridViewControl = (DataGridViewControl) new ControlTester("dataGridViewControl", form).TheObject; + var rows = dataGridViewControl.Rows; + Assert.AreEqual(1, rows.Count); + + var cells = rows[0].Cells; + Assert.AreEqual(4, cells.Count); + Assert.AreEqual("brtachtergrondkaart(EPSG:28992)", cells[mapLayerIdColumnIndex].FormattedValue); + Assert.AreEqual("image/png", cells[mapLayerFormatColumnIndex].FormattedValue); + Assert.AreEqual("Stub schema", cells[mapLayerTitleColumnIndex].FormattedValue); + Assert.AreEqual("EPSG:28992", cells[mapLayerCoordinateSystemColumnIndex].FormattedValue); + } + } + + [Test] + public void GivenWmtsLocationControlAndConnectClicked_WhenCannotFindTileSourceException_ThenErrorMessageShownAndLogGenerated() + { + // Given + const string exceptionMessage = "fail"; + + var mockRepository = new MockRepository(); + var tileFactory = mockRepository.StrictMock(); + tileFactory.Expect(tf => tf.GetWmtsTileSources(null)).IgnoreArguments().Throw(new CannotFindTileSourceException(exceptionMessage)); + mockRepository.ReplayAll(); + + string messageBoxTitle = null; + string messageBoxText = null; + DialogBoxHandler = (formName, wnd) => + { + var messageBox = new MessageBoxTester(wnd); + messageBoxTitle = messageBox.Title; + messageBoxText = messageBox.Text; + messageBox.ClickOk(); + }; + + using (new UseCustomTileSourceFactoryConfig(tileFactory)) + using (Form form = ShowValidWmtsLocationControl()) + { + form.Show(); + + var connectToButton = new ButtonTester("connectToButton", form); + + // When + Action action = () => connectToButton.Click(); + + // Then + TestHelper.AssertLogMessageWithLevelIsGenerated(action, Tuple.Create(exceptionMessage, LogLevelConstant.Error)); + Assert.AreEqual("Fout", messageBoxTitle); + Assert.AreEqual(exceptionMessage, messageBoxText); + } + mockRepository.VerifyAll(); + } + + [Test] public void Dispose_DisposedAlreadyCalled_DoesNotThrowException() { // Call @@ -384,5 +459,22 @@ form.Controls.Add(control); return form; } + + private static Form ShowValidWmtsLocationControl() + { + var form = new Form(); + var control = new WmtsLocationControl(); + form.Controls.Add(control); + + var comboBox = (ComboBox) new ComboBoxTester("urlLocationComboBox", form).TheObject; + comboBox.DataSource = new List + { + new WmtsConnectionInfo("PDOK achtergrondkaart", "https://geodata.nationaalgeoregister.nl/wmts/top10nlv2?VERSION=1.0.0&request=GetCapabilities") + }; + + var connectToButton = (Button) new ButtonTester("connectToButton", form).TheObject; + connectToButton.Enabled = true; + return form; + } } } \ No newline at end of file