/// <summary> /// Opens the view associated to the provided view-model /// </summary> /// <param name="viewModel"> /// The <see cref="IPanelViewModel"/> for which the associated view needs to be opened /// </param> /// <param name="useRegionManager"> /// A value indicating whether handling the opening of the view shall be handled by the region manager. In case this region manager does not handle /// this it will be event-based using the <see cref="CDPMessageBus"/>. /// </param> /// <remarks> /// The data context of the view is the <see cref="IPanelViewModel"/> /// </remarks> public void Open(IPanelViewModel viewModel, bool useRegionManager) { if (viewModel == null) { throw new ArgumentNullException(nameof(viewModel), "The IPanelViewModel may not be null"); } if (useRegionManager) { this.Open(viewModel); } else { IPanelView view; this.ViewModelViewPairs.TryGetValue(viewModel, out view); string regionName = string.Empty; if (view == null) { var lazyView = this.GetViewType(viewModel); regionName = lazyView.Metadata.Region; var parameters = new object[] { true }; view = Activator.CreateInstance(lazyView.Value.GetType(), parameters) as IPanelView; view.DataContext = viewModel; this.ViewModelViewPairs.Add(viewModel, view); } var openPanelEvent = new NavigationPanelEvent(viewModel, view, PanelStatus.Open, regionName); CDPMessageBus.Current.SendMessage(openPanelEvent); } }
/// <summary> /// removes the view and view-model from the <see cref="ViewModelViewPairs"/> and send a panel close event /// </summary> /// <param name="panelViewModel"> /// The <see cref="IPanelViewModel"/> that needs to be cleaned up /// </param> /// <param name="panelView"> /// The <see cref="IPanelView"/> that needs to be cleaned up /// </param> private void CleanUpPanelsAndSendCloseEvent(IPanelViewModel panelViewModel, IPanelView panelView) { this.ViewModelViewPairs.Remove(panelViewModel); var closePanelEvent = new NavigationPanelEvent(panelViewModel, panelView, PanelStatus.Closed); CDPMessageBus.Current.SendMessage(closePanelEvent); panelView.DataContext = null; panelViewModel.Dispose(); }
/// <summary> /// Re-opens an exisiting View associated to the provided view-model, or opens a new View /// Re-opening is done by sending a <see cref="CDPMessageBus"/> event. /// This event can be handled by more specific code, for example in the addin, where some /// ViewModels should not close at all. For those viewmodels visibility is toggled on every /// <see cref="NavigationPanelEvent"/> event that has <see cref="PanelStatus.Open"/> set. /// </summary> /// <param name="viewModel"> /// The <see cref="IPanelViewModel"/> for which the associated view needs to be opened, or closed /// </param> public void OpenExistingOrOpenInAddIn(IPanelViewModel viewModel) { if (this.AddInViewModelViewPairs.TryGetValue(viewModel, out var view)) { var openPanelEvent = new NavigationPanelEvent(viewModel, view, PanelStatus.Open); CDPMessageBus.Current.SendMessage(openPanelEvent); } else { this.OpenInAddIn(viewModel); } }
/// <summary> /// Removes a <see cref="IScriptPanelViewModel"/> from the <see cref="CollectionScriptPanelViewModels"/> and its information stored in the <see cref="PathScriptingFiles"/>. /// </summary> /// <param name="navigationPanelEvent"> The payload of the event that is being handled.</param> private void HandleClosedPanel(NavigationPanelEvent navigationPanelEvent) { var scriptPanelViewModel = (IScriptPanelViewModel)navigationPanelEvent.ViewModel; var header = scriptPanelViewModel.Caption; if (this.PathScriptingFiles.ContainsKey(header)) { this.PathScriptingFiles.Remove(header); } this.CollectionScriptPanelViewModels.Remove(scriptPanelViewModel); }
private void HandleOpenPanel(NavigationPanelEvent navigationPanelEvent) { logger.Debug("Opening Panel {0}", navigationPanelEvent.ViewModel); try { if (!(navigationPanelEvent.View is UIElement uiElement)) { return; } var identifier = navigationPanelEvent.ViewModel.Identifier; var taskPaneExists = this.customTaskPanes.TryGetValue(identifier, out var identifiableCustomTaskPane); if (taskPaneExists) { identifiableCustomTaskPane.CustomTaskPane.Visible = !identifiableCustomTaskPane.CustomTaskPane.Visible; } else { var title = navigationPanelEvent.ViewModel.Caption; var dockPosition = navigationPanelEvent.ViewModel.TargetName.ToDockPosition(); logger.Trace("Create new Task Pane with title {0}", title); var taskPane = this.TaskPaneFactory.CreateCTP("CDP4AddinCE.TaskPaneWpfHostControl", title); taskPane.DockPosition = dockPosition; taskPane.Width = 300; taskPane.Visible = true; if (taskPane is CustomTaskPane customTaskPane) { customTaskPane.VisibleStateChangeEvent += this.CustomTaskPane_VisibleStateChangeEvent; } if (taskPane.ContentControl is TaskPaneWpfHostControl wpfHostControl) { wpfHostControl.SetContent(uiElement); } identifiableCustomTaskPane = new IdentifiableCustomTaskPane(identifier, taskPane); this.customTaskPanes.Add(identifier, identifiableCustomTaskPane); } } catch (Exception ex) { var innerExceptionMessage = ex.InnerException != null ? ex.InnerException.Message : string.Empty; logger.Fatal(ex, $"handle open panel failed: {ex.Message} - {innerExceptionMessage}"); } }
/// <summary> /// Finalizes the <see cref="IPanelViewModel"/> on close /// </summary> /// <param name="panelViewModel"> /// The <see cref="IPanelViewModel"/> that needs to be cleaned up /// </param> private void CleanUpPanelsAndSendCloseEvent(IPanelViewModel panelViewModel) { var closePanelEvent = new NavigationPanelEvent(panelViewModel, null, PanelStatus.Closed); CDPMessageBus.Current.SendMessage(closePanelEvent); panelViewModel.Dispose(); // unregister from filter string service this.filterStringService.UnregisterFromService(panelViewModel); this.OptimizeMemoryUsage(); }
public void VerifyThatHandleClosedPanelWorks() { var avalonEditor = new TextEditor(); var panel1 = new Mock <ScriptPanelViewModel>("panel 1", this.scriptingProxy.Object, "*.lua", this.openSessions, true); panel1.As <IPanelViewModel>(); panel1.As <IScriptPanelViewModel>().SetupProperty(x => x.Caption, "panel 1"); panel1.SetupProperty(x => x.AvalonEditor, avalonEditor); var panel2 = new Mock <ScriptPanelViewModel>("panel 2", this.scriptingProxy.Object, "*.lua", this.openSessions, true); panel2.As <IPanelViewModel>(); panel2.As <IScriptPanelViewModel>().SetupProperty(x => x.Caption, "panel 2"); panel2.SetupProperty(x => x.AvalonEditor, avalonEditor); var panel3 = new Mock <ScriptPanelViewModel>("panel 3", this.scriptingProxy.Object, "*.lua", this.openSessions, true); panel3.As <IPanelViewModel>(); panel3.As <IScriptPanelViewModel>().SetupProperty(x => x.Caption, "panel 3"); panel3.SetupProperty(x => x.AvalonEditor, avalonEditor); this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Add(panel1.Object); this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Add(panel2.Object); this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Add(panel3.Object); this.scriptingEngineRibbonPageGroupViewModel.PathScriptingFiles.Add("panel 1", "path panel 1"); this.scriptingEngineRibbonPageGroupViewModel.PathScriptingFiles.Add("panel 3", "path panel 3"); var panelView = new Mock <IPanelView>(); var navigationPanelEvent = new NavigationPanelEvent(panel3.Object, panelView.Object, PanelStatus.Closed); CDPMessageBus.Current.SendMessage(navigationPanelEvent); Assert.AreEqual(this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Count, 2); Assert.AreEqual(this.scriptingEngineRibbonPageGroupViewModel.PathScriptingFiles.Count, 1); Assert.IsTrue(this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Contains(panel1.Object)); Assert.IsTrue(this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Contains(panel2.Object)); Assert.IsFalse(this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Contains(panel3.Object)); Assert.IsTrue(this.scriptingEngineRibbonPageGroupViewModel.PathScriptingFiles.ContainsKey("panel 1")); Assert.IsFalse(this.scriptingEngineRibbonPageGroupViewModel.PathScriptingFiles.ContainsKey("panel 3")); navigationPanelEvent = new NavigationPanelEvent(panel1.Object, panelView.Object, PanelStatus.Closed); CDPMessageBus.Current.SendMessage(navigationPanelEvent); Assert.AreEqual(this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Count, 1); Assert.AreEqual(this.scriptingEngineRibbonPageGroupViewModel.PathScriptingFiles.Count, 0); Assert.IsTrue(this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Contains(panel2.Object)); Assert.IsFalse(this.scriptingEngineRibbonPageGroupViewModel.CollectionScriptPanelViewModels.Contains(panel1.Object)); Assert.IsFalse(this.scriptingEngineRibbonPageGroupViewModel.PathScriptingFiles.ContainsKey("panel 1")); }
/// <summary> /// removes the view and view-model from the <see cref="ViewModelViewPairs"/> and send a panel close event /// </summary> /// <param name="panelViewModel"> /// The <see cref="IPanelViewModel"/> that needs to be cleaned up /// </param> /// <param name="panelView"> /// The <see cref="IPanelView"/> that needs to be cleaned up /// </param> private void CleanUpPanelsAndSendCloseEvent(IPanelViewModel panelViewModel, IPanelView panelView) { this.ViewModelViewPairs.Remove(panelViewModel); var closePanelEvent = new NavigationPanelEvent(panelViewModel, panelView, PanelStatus.Closed); CDPMessageBus.Current.SendMessage(closePanelEvent); panelView.DataContext = null; panelViewModel.Dispose(); // unregister from filter string service this.filterStringService.UnregisterFromService(panelView); }
/// <summary> /// Handles the <see cref="NavigationPanelEvent"/> with <see cref="PanelStatus.Open"/> /// </summary> /// <param name="navigationPanelEvent"> /// The event that carries the view, view-model combination and <see cref="PanelStatus"/> /// </param> private void HandleOpenPanel(NavigationPanelEvent navigationPanelEvent) { logger.Debug("Opening Panel {0}", navigationPanelEvent.ViewModel); try { var uiElement = navigationPanelEvent.View as UIElement; if (uiElement != null) { var identifier = navigationPanelEvent.ViewModel.Identifier; IdentifiableCustomTaskPane identifiableCustomTaskPane = null; var taskPaneExists = this.customTaskPanes.TryGetValue(identifier, out identifiableCustomTaskPane); if (taskPaneExists) { identifiableCustomTaskPane.CustomTaskPane.Visible = !identifiableCustomTaskPane.CustomTaskPane.Visible; } else { var title = navigationPanelEvent.ViewModel.Caption; var dockPosition = navigationPanelEvent.RegionName.ToDockPosition(); logger.Trace("Create new Task Pane with title {0}", title); var taskPane = this.TaskPaneFactory.CreateCTP("CDP4AddinCE.TaskPaneWpfHostControl", title); taskPane.DockPosition = dockPosition; taskPane.Width = 300; taskPane.Visible = true; var wpfHostControl = taskPane.ContentControl as TaskPaneWpfHostControl; if (wpfHostControl != null) { wpfHostControl.SetContent(uiElement); } identifiableCustomTaskPane = new IdentifiableCustomTaskPane(identifier, taskPane); this.customTaskPanes.Add(identifier, identifiableCustomTaskPane); } } } catch (Exception ex) { var innerExceptionMessage = ex.InnerException != null ? ex.InnerException.Message : string.Empty; logger.Fatal(ex, string.Format("handle open panel failed: {0} - {1}", ex.Message, innerExceptionMessage)); } }
/// <summary> /// Opens the view associated with the <see cref="IPanelViewModel"/> in the AddIn /// </summary> /// <param name="viewModel">The <see cref="IPanelViewModel"/> to open</param> public void OpenInAddIn(IPanelViewModel viewModel) { var viewType = this.GetViewType(viewModel); var parameters = new object[] { true }; var view = Activator.CreateInstance(viewType, parameters) as IPanelView; if (view != null) { view.DataContext = viewModel; this.AddInViewModelViewPairs.Add(viewModel, view); // register for Filter Service this.filterStringService.RegisterForService(viewModel); } var openPanelEvent = new NavigationPanelEvent(viewModel, view, PanelStatus.Open); CDPMessageBus.Current.SendMessage(openPanelEvent); }
/// <summary> /// Handles the <see cref="NavigationPanelEvent"/> with <see cref="PanelStatus.Closed"/> /// </summary> /// <param name="navigationPanelEvent"> /// The event that carries the view, view-model combination and <see cref="PanelStatus"/> /// </param> private void HandleClosePanel(NavigationPanelEvent navigationPanelEvent) { logger.Debug("Closing Panel {0}", navigationPanelEvent.ViewModel); try { var identifier = navigationPanelEvent.ViewModel.Identifier; IdentifiableCustomTaskPane identifiableCustomTaskPane = null; var taskPaneExists = this.customTaskPanes.TryGetValue(identifier, out identifiableCustomTaskPane); if (taskPaneExists) { identifiableCustomTaskPane.CustomTaskPane.Visible = false; identifiableCustomTaskPane.Dispose(); this.customTaskPanes.Remove(identifier); } } catch (Exception ex) { logger.Fatal(ex, "handle close panel failed"); } }