Index: Core/Common/src/Core.Common.Gui/Forms/ViewHost/DocumentViewController.cs
===================================================================
diff -u -rfb0a99ea46697bf627c6e7c788d7461af91522ea -r8eb9e190d7762dc253fb7691774f0378aaf41132
--- Core/Common/src/Core.Common.Gui/Forms/ViewHost/DocumentViewController.cs (.../DocumentViewController.cs) (revision fb0a99ea46697bf627c6e7c788d7461af91522ea)
+++ Core/Common/src/Core.Common.Gui/Forms/ViewHost/DocumentViewController.cs (.../DocumentViewController.cs) (revision 8eb9e190d7762dc253fb7691774f0378aaf41132)
@@ -25,19 +25,15 @@
using System.Windows.Forms;
using Core.Common.Controls.Views;
using Core.Common.Gui.Plugin;
-using Core.Common.Gui.Properties;
using Core.Common.Util.Reflection;
-using log4net;
namespace Core.Common.Gui.Forms.ViewHost
{
///
- /// Class responsible for finding a view given some data-object.
+ /// Class responsible for finding a view given some data object.
///
- public class DocumentViewController : IDocumentViewController
+ public class DocumentViewController : IDocumentViewController, IDisposable
{
- private static readonly ILog log = LogManager.GetLogger(typeof(DocumentViewController));
-
private readonly IViewHost viewHost;
private readonly ViewInfo[] viewInfos;
private readonly IWin32Window dialogParent;
@@ -56,11 +52,24 @@
this.viewInfos = viewInfos.ToArray();
this.dialogParent = dialogParent;
+ DefaultViewTypes = new Dictionary();
+
viewHost.ViewClosed += ViewHostOnViewClosed;
}
- public IDictionary DefaultViewTypes { get; } = new Dictionary();
+ ///
+ /// Gets the default view types registered for data object types, that can be used to
+ /// automatically resolve a particular view when multiple candidates are available.
+ ///
+ /// The keys in this dictionary are the object types and the values the
+ /// corresponding view types.
+ public IDictionary DefaultViewTypes { get; }
+ public void Dispose()
+ {
+ viewHost.ViewClosed -= ViewHostOnViewClosed;
+ }
+
public bool OpenViewForData(object data, bool alwaysShowDialog = false)
{
if (data == null)
@@ -71,7 +80,6 @@
ViewInfo[] viewInfoList = FilterOnInheritance(GetViewInfosFor(data)).ToArray();
if (viewInfoList.Length == 0)
{
- log.DebugFormat(Resources.DocumentViewController_OpenViewForData_No_view_registered_for_0_, data);
return false;
}
@@ -124,36 +132,6 @@
return viewInfos.Where(vi => data.GetType().Implements(vi.DataType) && vi.AdditionalDataCheck(data));
}
- private void ViewHostOnViewClosed(object sender, ViewChangeEventArgs viewChangeEventArgs)
- {
- object data = openedViewLookup.Where(kv => ReferenceEquals(kv.Value, viewChangeEventArgs.View))
- .Select(kv => kv.Key)
- .FirstOrDefault();
-
- if (data != null)
- {
- openedViewLookup.Remove(data);
- }
- }
-
- private bool ShouldRemoveViewForData(IView view, object data)
- {
- ViewInfo viewInfo = viewInfos.FirstOrDefault(vi => vi.ViewType == view.GetType());
-
- return viewInfo != null
- && (Equals(viewInfo.GetViewData(data), view.Data) ||
- viewInfo.CloseForData(view, data));
- }
-
- private Type GetDefaultViewType(object dataObject)
- {
- Type selectionType = dataObject.GetType();
-
- return DefaultViewTypes.ContainsKey(selectionType)
- ? DefaultViewTypes[selectionType]
- : null;
- }
-
private static IEnumerable FilterOnInheritance(IEnumerable compatibleStandaloneViewInfos)
{
ViewInfo[] viewInfos = compatibleStandaloneViewInfos.ToArray();
@@ -195,41 +173,36 @@
return view;
}
- private ViewInfo GetViewInfoUsingDialog(object data, IEnumerable viewInfoList)
+ private bool ShouldRemoveViewForData(IView view, object data)
{
- Type defaultViewTypeForData = GetDefaultViewTypeForData(data);
- string defaultViewName = defaultViewTypeForData != null
- ? viewInfoList.First(vi => vi.ViewType == defaultViewTypeForData).Description
- : null;
+ ViewInfo viewInfo = viewInfos.FirstOrDefault(vi => vi.ViewType == view.GetType());
- Dictionary viewTypeDictionary = viewInfoList.ToDictionary(vi => vi.Description ?? vi.ViewType.Name);
- using (var viewSelector = new SelectViewDialog(dialogParent)
- {
- DefaultViewName = defaultViewName,
- Items = viewTypeDictionary.Keys.ToList()
- })
- {
- if (viewSelector.ShowDialog() != DialogResult.OK)
- {
- return null;
- }
+ return viewInfo != null
+ && (Equals(viewInfo.GetViewData(data), view.Data) ||
+ viewInfo.CloseForData(view, data));
+ }
- ViewInfo selectedViewInfo = viewTypeDictionary[viewSelector.SelectedItem];
+ private void ViewHostOnViewClosed(object sender, ViewChangeEventArgs viewChangeEventArgs)
+ {
+ object data = openedViewLookup.Where(kv => ReferenceEquals(kv.Value, viewChangeEventArgs.View))
+ .Select(kv => kv.Key)
+ .FirstOrDefault();
- if (viewSelector.DefaultViewName == null)
- {
- ClearDefaultView(data);
- }
- else
- {
- ViewInfo defaultViewInfo = viewTypeDictionary[viewSelector.DefaultViewName];
- SetDefaultView(defaultViewInfo.ViewType, data);
- }
-
- return selectedViewInfo;
+ if (data != null)
+ {
+ openedViewLookup.Remove(data);
}
}
+ private Type GetDefaultViewType(object dataObject)
+ {
+ Type selectionType = dataObject.GetType();
+
+ return DefaultViewTypes.ContainsKey(selectionType)
+ ? DefaultViewTypes[selectionType]
+ : null;
+ }
+
private void ClearDefaultView(object data)
{
Type selectedItemType = data.GetType();
@@ -260,5 +233,40 @@
return DefaultViewTypes.ContainsKey(selectionType) ? DefaultViewTypes[selectionType] : null;
}
+
+ private ViewInfo GetViewInfoUsingDialog(object data, IEnumerable viewInfoList)
+ {
+ Type defaultViewTypeForData = GetDefaultViewTypeForData(data);
+ string defaultViewName = defaultViewTypeForData != null
+ ? viewInfoList.First(vi => vi.ViewType == defaultViewTypeForData).Description
+ : null;
+
+ Dictionary viewTypeDictionary = viewInfoList.ToDictionary(vi => vi.Description ?? vi.ViewType.Name);
+ using (var viewSelector = new SelectViewDialog(dialogParent)
+ {
+ DefaultViewName = defaultViewName,
+ Items = viewTypeDictionary.Keys.ToList()
+ })
+ {
+ if (viewSelector.ShowDialog() != DialogResult.OK)
+ {
+ return null;
+ }
+
+ ViewInfo selectedViewInfo = viewTypeDictionary[viewSelector.SelectedItem];
+
+ if (viewSelector.DefaultViewName == null)
+ {
+ ClearDefaultView(data);
+ }
+ else
+ {
+ ViewInfo defaultViewInfo = viewTypeDictionary[viewSelector.DefaultViewName];
+ SetDefaultView(defaultViewInfo.ViewType, data);
+ }
+
+ return selectedViewInfo;
+ }
+ }
}
}
\ No newline at end of file
Index: Core/Common/src/Core.Common.Gui/Forms/ViewHost/IDocumentViewController.cs
===================================================================
diff -u -r67284323e2785c651633d9c52049ba12a9c70e6a -r8eb9e190d7762dc253fb7691774f0378aaf41132
--- Core/Common/src/Core.Common.Gui/Forms/ViewHost/IDocumentViewController.cs (.../IDocumentViewController.cs) (revision 67284323e2785c651633d9c52049ba12a9c70e6a)
+++ Core/Common/src/Core.Common.Gui/Forms/ViewHost/IDocumentViewController.cs (.../IDocumentViewController.cs) (revision 8eb9e190d7762dc253fb7691774f0378aaf41132)
@@ -19,7 +19,6 @@
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
-using System;
using System.Collections.Generic;
using Core.Common.Gui.Plugin;
@@ -42,14 +41,6 @@
public interface IDocumentViewController
{
///
- /// Gets the default view types registered for data object types, that can be used to
- /// automatically resolve a particular view when multiple candidates are available.
- ///
- /// The keys in this dictionary are the object types and the values the
- /// corresponding view types.
- IDictionary DefaultViewTypes { get; }
-
- ///
/// Opens a view for .
///
/// The data to open a view for.
Index: Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs
===================================================================
diff -u -rac96d7c315129af851634ed5a4a6800b59ede718 -r8eb9e190d7762dc253fb7691774f0378aaf41132
--- Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs (.../GuiCoreTest.cs) (revision ac96d7c315129af851634ed5a4a6800b59ede718)
+++ Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs (.../GuiCoreTest.cs) (revision 8eb9e190d7762dc253fb7691774f0378aaf41132)
@@ -902,8 +902,8 @@
Assert.AreEqual(1, gui.ViewHost.ToolViews.Count(v => v is MessageWindow));
Assert.IsNotNull(gui.DocumentViewController);
- CollectionAssert.IsEmpty(gui.DocumentViewController.DefaultViewTypes);
}
+
mocks.VerifyAll();
}