Ejemplo n.º 1
0
 protected void RegisterViewModelEventHandlers(GenericViewModel viewModel)
 {
     // Register event handlers
     if (viewModel is ISavable)
     {
         (viewModel as ISavable).FileSaved += File_OnSaved;
     }
     if (viewModel is INotifyModified)
     {
         (viewModel as INotifyModified).Modified += File_OnModified;
     }
     viewModel.MenuItemRefreshRequested += OnMenuItemRefreshRequested;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a list of view controls that edit the given ViewModel.
        /// </summary>
        /// <param name="viewModel">ViewModel for which to find view controls</param>
        /// <param name="requestedTabTypes">Limits what types of iObjectControl should be returned.  If the iObjectControl is not of any type in this IEnumerable, it will not be used.  If empty or nothing, no constraints will be applied, which is not recommended because the iObjectControl could be made for a different environment (for example, a Windows Forms user control being used in a WPF environment).</param>
        /// <param name="appViewModel">Instance of the current application ViewModel.</param>
        /// <returns>An IEnumerable of object controls for the given model.</returns>
        /// <remarks>This version of <see cref="GetViewControls(Object, IEnumerable(Of Type), PluginManager)"/> searches for views, then finds paths to the model.</remarks>
        private static IEnumerable <IViewControl> GetViewControlsByView(object viewModel, IEnumerable <Type> RequestedTabTypes, ApplicationViewModel appViewModel, PluginManager manager)
        {
            if (viewModel == null)
            {
                throw (new ArgumentNullException(nameof(viewModel)));
            }

            var modelType   = viewModel.GetType().GetTypeInfo();
            var allTabs     = new List <IViewControl>();
            var objControls = GetViewControls(manager);

            foreach (var etab in objControls.Where(x => RequestedTabTypes.Any(y => ReflectionHelpers.IsOfType(x, y.GetTypeInfo()))).OrderBy(x => x.GetSortOrder(modelType, true)))
            {
                bool             isMatch          = false;
                GenericViewModel currentViewModel = null;

                //Check to see if the tab support the type of the given object
                var supportedTypes = etab.GetSupportedTypes();

                foreach (var typeInfo in supportedTypes)
                {
                    if (typeInfo.IsInterface)
                    {
                        //The target is an interface.  Check to see if there's a view model that implements it.
                        //Otherwise, check the model

                        //Get the view model for the model from the IOUI mangaer
                        var viewmodelsForModel = appViewModel.GetViewModelsForModel(viewModel);

                        //If there are none, and the model is a FileViewModel, get the view models that way
                        if (ReferenceEquals(viewmodelsForModel, null) && viewModel is FileViewModel)
                        {
                            viewmodelsForModel = (viewModel as FileViewModel).GetViewModels(appViewModel);
                        }

                        //If we still can't find anything, set viewModelsForModel to an empty enumerable
                        if (ReferenceEquals(viewmodelsForModel, null))
                        {
                            viewmodelsForModel = Array.Empty <GenericViewModel>();
                        }

                        // Of the view models that support the model, select the ones that the current view supports
                        var availableViewModels = viewmodelsForModel.Where(x => ReflectionHelpers.IsOfType(x, typeInfo));

                        if (availableViewModels != null && availableViewModels.Any())
                        {
                            // This view model fits the critera
                            var first = availableViewModels.First();
                            isMatch          = etab.SupportsObject(first);
                            currentViewModel = first;
                            if (isMatch)
                            {
                                break;
                            }
                        }
                        else if (ReflectionHelpers.IsOfType(viewModel, typeInfo))
                        {
                            // The model implements this interface
                            isMatch = etab.SupportsObject(viewModel);
                            if (isMatch)
                            {
                                break;
                            }
                        }
                    }
                    else if (ReflectionHelpers.IsOfType(viewModel, typeInfo))
                    {
                        // The model is the same type as the target
                        isMatch = etab.SupportsObject(viewModel);
                        if (isMatch)
                        {
                            break;
                        }
                    }
                    else if (ReflectionHelpers.IsOfType(typeInfo, typeof(GenericViewModel).GetTypeInfo()))
                    {
                        // The object control is targeting a view model

                        // First, check to see if there's any view models for this model (i.e., is this an open file?)
                        var viewmodelsForModel = appViewModel.GetViewModelsForModel(viewModel);

                        if (ReferenceEquals(viewmodelsForModel, null) && viewModel is FileViewModel)
                        {
                            viewmodelsForModel = (viewModel as FileViewModel).GetViewModels(appViewModel);
                        }

                        //If there are, check to see if the target view supports the view model
                        if (viewmodelsForModel != null)
                        {
                            var potentialViewModel = viewmodelsForModel.FirstOrDefault(x => ReflectionHelpers.IsOfType(x, typeInfo)) as GenericViewModel;
                            if (potentialViewModel != null)
                            {
                                //This view model supports our model
                                isMatch          = etab.SupportsObject(potentialViewModel);
                                currentViewModel = potentialViewModel;
                                if (isMatch)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                // This is a supported tab.  We're adding it!
                if (isMatch)
                {
                    // Create another instance of etab, since etab is our cached, search-only instance.
                    var tab = manager.CreateNewInstance(etab) as IViewControl;

                    // Set the appropriate object
                    if (currentViewModel != null)
                    {
                        //We have a view model that the view wants
                        tab.ViewModel = currentViewModel;
                    }
                    else if (tab.SupportsObject(viewModel))
                    {
                        // This model is what the view wants
                        tab.ViewModel = viewModel;
                    }

                    allTabs.Add(tab);
                }
            }

            var backupTabs = new List <IViewControl>();
            var notBackup  = new List <IViewControl>();

            //Sort the backup vs non-backup tabs
            foreach (var item in allTabs)
            {
                if (item.GetIsBackupControl())
                {
                    backupTabs.Add(item);
                }
                else
                {
                    notBackup.Add(item);
                }
            }

            //And use the non-backup ones if available
            if (notBackup.Count > 0)
            {
                return(notBackup);
            }
            else
            {
                var toUse = backupTabs.OrderBy(x => x.GetSortOrder(modelType, true)).FirstOrDefault();
                if (toUse == null)
                {
                    return(Array.Empty <IViewControl>());
                }
                else
                {
                    return(new[] { toUse });
                }
            }
        }