/// <summary>
    /// Searchs for a appropriate control for a given controller and attaches the control to the controller.
    /// </summary>
    /// <param name="controller">The controller a control is searched for.</param>
    public void FindAndAttachControlTo(IMVCController controller)
    {
      // if the controller has 
      System.Type ct = controller.GetType();
      object[] viewattributes = ct.GetCustomAttributes(typeof(ExpectedTypeOfViewAttribute), false);

      if (viewattributes != null && viewattributes.Length > 0)
      {
        if (viewattributes.Length > 1)
        {
          Array.Sort(viewattributes);
        }

        foreach (ExpectedTypeOfViewAttribute attr in viewattributes)
        {
          Type[] controltypes = ReflectionService.GetNonAbstractSubclassesOf(new System.Type[] { typeof(System.Windows.Forms.Control), attr.TargetType });
          foreach (Type controltype in controltypes)
          {
            // test if the control has a special preference for a controller...
            object[] controlForAttributes = controltype.GetCustomAttributes(typeof(UserControlForControllerAttribute), false);
            if (controlForAttributes.Length > 0)
            {
              bool containsControllerType = false;
              foreach (UserControlForControllerAttribute controlForAttr in controlForAttributes)
              {
                if (ReflectionService.IsSubClassOfOrImplements(ct,controlForAttr.TargetType))
                {
                  containsControllerType = true;
                  break;
                }
              }
              if (!containsControllerType)
                continue; // then this view is not intended for our controller
            }

            // all seems ok, so we can try to create the control
            object controlinstance = System.Activator.CreateInstance(controltype);
            if (null == controlinstance)
              throw new ApplicationException(string.Format("Searching a control for controller of type {0}. Find control type {1}, but it was not possible to create a instance of this type.", ct, controltype));

            controller.ViewObject = controlinstance;

            if (controller.ViewObject == null)
              throw new ApplicationException(string.Format("Searching a control for controller of type {0}. Find control type {1}, but the controller did not accept this control.", ct, controltype));

            return;
          }
        }
      }
      else // Controller has no ExpectedTypeOfView attribute
      {
        System.Windows.Forms.UserControl control =
          (System.Windows.Forms.UserControl)ReflectionService.GetClassForClassInstanceByAttribute(
          typeof(UserControlForControllerAttribute),
          new System.Type[] { typeof(System.Windows.Forms.UserControl) },
          new object[] { controller });

        if (control == null)
          return;

        controller.ViewObject = control;
      }
    }
Esempio n. 2
0
        private static void CreateAndAttachControlOfType(IMVCController controller, System.Type controltype)
        {
            object controlinstance = System.Activator.CreateInstance(controltype);

            if (null == controlinstance)
            {
                throw new ApplicationException(string.Format("Searching a control for controller of type {0}. Find control type {1}, but it was not possible to create a instance of this type.", controller.GetType(), controltype));
            }
            controller.ViewObject = controlinstance;
        }
Esempio n. 3
0
        /// <summary>
        /// Searchs for a appropriate control for a given controller and attaches the control to the controller.
        /// </summary>
        /// <param name="controller">The controller a control is searched for.</param>
        public void FindAndAttachControlTo(IMVCController controller)
        {
            // if the controller has
            System.Type ct             = controller.GetType();
            object[]    viewattributes = ct.GetCustomAttributes(typeof(ExpectedTypeOfViewAttribute), false);

            if (viewattributes != null && viewattributes.Length > 0)
            {
                if (viewattributes.Length > 1)
                {
                    Array.Sort(viewattributes);
                }

                foreach (ExpectedTypeOfViewAttribute attr in viewattributes)
                {
                    Type[] controltypes = ReflectionService.GetNonAbstractSubclassesOf(new System.Type[] { typeof(System.Windows.Forms.Control), attr.TargetType });
                    foreach (Type controltype in controltypes)
                    {
                        // test if the control has a special preference for a controller...
                        object[] controlForAttributes = controltype.GetCustomAttributes(typeof(UserControlForControllerAttribute), false);
                        if (controlForAttributes.Length > 0)
                        {
                            bool containsControllerType = false;
                            foreach (UserControlForControllerAttribute controlForAttr in controlForAttributes)
                            {
                                if (ReflectionService.IsSubClassOfOrImplements(ct, controlForAttr.TargetType))
                                {
                                    containsControllerType = true;
                                    break;
                                }
                            }
                            if (!containsControllerType)
                            {
                                continue; // then this view is not intended for our controller
                            }
                        }

                        // all seems ok, so we can try to create the control
                        object controlinstance = System.Activator.CreateInstance(controltype);
                        if (null == controlinstance)
                        {
                            throw new ApplicationException(string.Format("Searching a control for controller of type {0}. Find control type {1}, but it was not possible to create a instance of this type.", ct, controltype));
                        }

                        controller.ViewObject = controlinstance;

                        if (controller.ViewObject == null)
                        {
                            throw new ApplicationException(string.Format("Searching a control for controller of type {0}. Find control type {1}, but the controller did not accept this control.", ct, controltype));
                        }

                        return;
                    }
                }
            }
            else // Controller has no ExpectedTypeOfView attribute
            {
                System.Windows.Forms.UserControl control =
                    (System.Windows.Forms.UserControl)ReflectionService.GetClassForClassInstanceByAttribute(
                        typeof(UserControlForControllerAttribute),
                        new System.Type[] { typeof(System.Windows.Forms.UserControl) },
                        new object[] { controller });

                if (control == null)
                {
                    return;
                }

                controller.ViewObject = control;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Searchs for a appropriate control for a given controller and attaches the control to the controller.
        /// </summary>
        /// <param name="controller">The controller a control is searched for.</param>
        /// <param name="guiControlType">Base type of the underlying Gui system.</param>
        private void InternalFindAndAttachControlUsingGuiType(IMVCController controller, System.Type guiControlType)
        {
            // if the controller has
            System.Type ct             = controller.GetType();
            object[]    viewattributes = ct.GetCustomAttributes(typeof(ExpectedTypeOfViewAttribute), false);
            if (null == viewattributes || viewattributes.Length == 0)
            {
                viewattributes = ct.GetCustomAttributes(typeof(ExpectedTypeOfViewAttribute), true);
            }

            bool isInvokeRequired = Current.Dispatcher.InvokeRequired;

            if (viewattributes != null && viewattributes.Length > 0)
            {
                if (viewattributes.Length > 1)
                {
                    Array.Sort(viewattributes);
                }

                foreach (ExpectedTypeOfViewAttribute attr in viewattributes)
                {
                    Type[] controltypes = ReflectionService.GetNonAbstractSubclassesOf(new System.Type[] { guiControlType, attr.TargetType });
                    if (controltypes.Length > 1)
                    {
                        // retrieve priorities for the types, and then sort controltypes using priority descending

                        var priorities = new int[controltypes.Length];
                        for (int i = 0; i < controltypes.Length; ++i)
                        {
                            object[] pattributes = controltypes[i].GetCustomAttributes(typeof(UserControlPriorityAttribute), false);
                            if (null != pattributes && pattributes.Length > 0)
                            {
                                priorities[i] = -((UserControlPriorityAttribute)pattributes[0]).Priority;
                            }
                        }
                        Array.Sort(priorities, controltypes); // the negation of priority (see above) ensures that the sorting is priority descending
                    }

                    foreach (Type controltype in controltypes)
                    {
                        // test if the control has a special preference for a controller...
                        object[] controlForAttributes = controltype.GetCustomAttributes(typeof(UserControlForControllerAttribute), false);
                        if (controlForAttributes.Length > 0)
                        {
                            bool containsControllerType = false;
                            foreach (UserControlForControllerAttribute controlForAttr in controlForAttributes)
                            {
                                if (ReflectionService.IsSubClassOfOrImplements(ct, controlForAttr.TargetType))
                                {
                                    containsControllerType = true;
                                    break;
                                }
                            }
                            if (!containsControllerType)
                            {
                                continue; // then this view is not intended for our controller
                            }
                        }

                        // all seems ok, so we can try to create the control

                        if (isInvokeRequired)
                        {
                            Current.Dispatcher.InvokeIfRequired(CreateAndAttachControlOfType, controller, controltype);
                        }
                        else
                        {
                            CreateAndAttachControlOfType(controller, controltype);
                        }
                        return;
                    }
                }
            }
            else // Controller has no ExpectedTypeOfView attribute
            {
                if (isInvokeRequired)
                {
                    Current.Dispatcher.InvokeIfRequired(CreateControlForControllerWithNoExpectedTypeOfViewAttribute, controller, guiControlType);
                }
                else
                {
                    CreateControlForControllerWithNoExpectedTypeOfViewAttribute(controller, guiControlType);
                }
            }
        }