Beispiel #1
0
        /// <inheritdoc />
        public void ActivateAllElements(string region)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (string.IsNullOrWhiteSpace(region))
            {
                throw new ArgumentException("Parameter is an empty string.", nameof(region));
            }

            if (!this.RegionDictionary.ContainsKey(region))
            {
                throw new RegionNotFoundException(region);
            }

            object container = this.RegionDictionary[region]
                               .Item1;

            IRegionAdapter adapter = this.RegionDictionary[region]
                                     .Item2;

            List <object> elements = adapter.Get(container);

            foreach (object element in elements)
            {
                adapter.Activate(container, element);
            }

            adapter.Sort(container);
        }
Beispiel #2
0
        /// <inheritdoc />
        public void ClearElements(string region)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (string.IsNullOrWhiteSpace(region))
            {
                throw new ArgumentException("Parameter is an empty string.", nameof(region));
            }

            if (!this.RegionDictionary.ContainsKey(region))
            {
                throw new RegionNotFoundException(region);
            }

            object container = this.RegionDictionary[region]
                               .Item1;

            IRegionAdapter adapter = this.RegionDictionary[region]
                                     .Item2;

            adapter.Clear(container);
            adapter.Sort(container);
        }
Beispiel #3
0
        /// <inheritdoc />
        public bool Navigate(string region, object element)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (string.IsNullOrWhiteSpace(region))
            {
                throw new ArgumentException("Parameter is an empty string.", nameof(region));
            }

            if (!this.RegionDictionary.ContainsKey(region))
            {
                throw new RegionNotFoundException(region);
            }

            object container = this.RegionDictionary[region]
                               .Item1;

            IRegionAdapter adapter = this.RegionDictionary[region]
                                     .Item2;

            bool result = adapter.Navigate(container, element);

            adapter.Sort(container);

            return(result);
        }
Beispiel #4
0
        public void set_up()
        {
            var adapters      = new IRegionAdapter[] { new ToolbarTrayAdapter() };
            var regionFactory = new RegionFactory(adapters);

            _regionManager = new RegionManager(regionFactory);
        }
Beispiel #5
0
        /// <inheritdoc />
        public void ActivateElement(string region, object element)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            if (string.IsNullOrWhiteSpace(region))
            {
                throw new ArgumentException("Parameter is an empty string.", nameof(region));
            }

            if (!this.RegionDictionary.ContainsKey(region))
            {
                throw new RegionNotFoundException(region);
            }

            this.AddElement(region, element);

            object container = this.RegionDictionary[region]
                               .Item1;

            IRegionAdapter adapter = this.RegionDictionary[region]
                                     .Item2;

            adapter.Activate(container, element);
            adapter.Sort(container);
        }
Beispiel #6
0
        /// <inheritdoc />
        public List <object> GetElementsOfRegion(string region)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (string.IsNullOrWhiteSpace(region))
            {
                throw new ArgumentException("Parameter is an empty string.", nameof(region));
            }

            if (!this.RegionDictionary.ContainsKey(region))
            {
                throw new RegionNotFoundException(region);
            }

            object container = this.RegionDictionary[region]
                               .Item1;

            IRegionAdapter adapter = this.RegionDictionary[region]
                                     .Item2;

            return(adapter.Get(container));
        }
        /// <summary>
        /// Method that will create the region, by calling the right <see cref="IRegionAdapter"/>.
        /// </summary>
        /// <param name="targetElement">The target element that will host the <see cref="IRegion"/>.</param>
        /// <param name="regionName">Name of the region.</param>
        /// <returns>The created <see cref="IRegion"/></returns>
        protected virtual IRegion CreateRegion(DependencyObject targetElement, string regionName)
        {
            if (targetElement == null)
            {
                throw new ArgumentNullException(nameof(targetElement));
            }

            try
            {
                // Build the region
                IRegionAdapter regionAdapter = this._regionAdapterMappings.GetMapping(targetElement.GetType());
                IRegion        region        = regionAdapter.Initialize(targetElement, regionName);

                return(region);
            }
            catch (Exception ex)
            {
                throw new RegionCreationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              "An exception occurred while creating a region with name '{0}'. The exception was: {1}.",
                              regionName,
                              ex),
                          ex);
            }
        }
Beispiel #8
0
        private void set_up()
        {
            var adapters      = new IRegionAdapter[] { new TabControlAdapter() };
            var regionFactory = new RegionFactory(adapters);

            _regionManager = new RegionManager(regionFactory);
        }
Beispiel #9
0
        public void set_up()
        {
            var adapters      = new IRegionAdapter[] { new StatusBarAdapter() };
            var regionFactory = new RegionFactory(adapters);

            _regionManager = new RegionManager(regionFactory);
        }
Beispiel #10
0
        public void set_up()
        {
            var adapters      = new IRegionAdapter[] { new ItemsControlAdapter() };
            var regionFactory = new RegionFactory(adapters);

            _regionManager = new RegionManager(regionFactory);
        }
        /// <summary>
        ///     <see cref="IRegionConfigurator.RegisterMapping" />
        /// </summary>
        public void RegisterMapping(Type controlType, IRegionAdapter regionAdapter)
        {
            var regionAdapterMApping = ServiceLocator.Current.GetInstance <RegionAdapterMappings>();

            if (regionAdapterMApping != null)
            {
                regionAdapterMApping.RegisterMapping(controlType, regionAdapter);
            }
        }
Beispiel #12
0
        /// <inheritdoc />
        public void AddRegion(string region, object container)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (string.IsNullOrWhiteSpace(region))
            {
                throw new ArgumentException("Parameter is an empty string.", nameof(region));
            }

            Trace.TraceInformation($"Adding region {region} to: {container.GetType().Name}");

            List <Tuple <int, IRegionAdapter> > adapters = new List <Tuple <int, IRegionAdapter> >();
            Type containerType = container.GetType();

            foreach (IRegionAdapter currentAdapter in this.Adapters)
            {
                if (currentAdapter.IsCompatibleContainer(containerType, out int inheritanceDepth))
                {
                    adapters.Add(new Tuple <int, IRegionAdapter>(inheritanceDepth, currentAdapter));
                }
            }

            adapters.Sort((x, y) => x.Item1.CompareTo(y.Item1));

            if (adapters.Count == 0)
            {
                throw new NotSupportedException($"No region adapter supports the container type {containerType.Name}");
            }

            IRegionAdapter adapter = adapters[0]
                                     .Item2;

            Trace.TraceInformation($"Used region adapter for region{region} at {container.GetType().Name}: {adapter.GetType().Name}");

            if (this.RegionDictionary.ContainsKey(region))
            {
                if (ReferenceEquals(container, this.RegionDictionary[region]
                                    .Item1) && adapter.Equals(this.RegionDictionary[region]
                                                              .Item2))
                {
                    return;
                }

                this.RegionDictionary.Remove(region);
            }

            this.RegionDictionary.Add(region, new Tuple <object, IRegionAdapter>(container, adapter));
        }
Beispiel #13
0
        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            var mappings = base.ConfigureRegionAdapterMappings();

            IRegionAdapter adapter = ServiceLocator.Current.GetInstance <RibbonRegionAdapter>();

            mappings.RegisterMapping(typeof(Ribbon), adapter);

            return(mappings);
        }
Beispiel #14
0
        /// <summary>
        /// Attaches a region to an object and adds it to the region manager.
        /// </summary>
        /// <param name="regionTarget">The object to adapt. This is typically a container (i.e a control).</param>
        /// <param name="regionName">The name of the region to register.</param>
        /// <exception cref="ArgumentException">When regions collection already has a region registered using <paramref name="regionName"/>.</exception>
        public void AttachNewRegion(object regionTarget, string regionName)
        {
            if (Regions.ContainsKey(regionName))
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.RegionNameExistsException, regionName));

            IRegionAdapter regionAdapter = regionAdapterMappings.GetMapping(regionTarget.GetType());
            IRegion region = regionAdapter.Initialize(regionTarget);

            Regions.Add(regionName, region);
        }
Beispiel #15
0
        /// <summary>
        /// Registers the mapping between a type and an adapter.
        /// </summary>
        /// <param name="controlType">The type of the control.</param>
        /// <param name="adapter">The adapter to use with the <paramref name="controlType"/> type.</param>
        /// <exception cref="ArgumentNullException">When any of <paramref name="controlType"/> or <paramref name="adapter"/> are <see langword="null" />.</exception>
        /// <exception cref="InvalidOperationException">If a mapping for <paramref name="controlType"/> already exists.</exception>
        public void RegisterMapping(Type controlType, IRegionAdapter adapter)
        {
            if (controlType == null)
                throw new ArgumentNullException(nameof(controlType));

            if (adapter == null)
                throw new ArgumentNullException(nameof(adapter));

            if (mappings.ContainsKey(controlType))
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                  Resources.MappingExistsException, controlType.Name));

            mappings.Add(controlType, adapter);
        }
        /// <summary>
        /// Method that will create the region, by calling the right <see cref="IRegionAdapter"/>.
        /// </summary>
        /// <param name="targetElement">The target element that will host the <see cref="IRegion"/>.</param>
        /// <param name="regionName">Name of the region.</param>
        /// <returns>The created <see cref="IRegion"/></returns>
        protected virtual IRegion CreateRegion(DependencyObject targetElement, string regionName)
        {
            try
            {
                // Build the region
                IRegionAdapter regionAdapter = this.regionAdapterMappings.GetMapping(targetElement.GetType());
                IRegion        region        = regionAdapter.Initialize(targetElement, regionName);

                return(region);
            }
            catch (Exception ex)
            {
                throw new RegionCreationException(string.Format(CultureInfo.CurrentCulture, Resources.RegionCreationException, regionName, ex), ex);
            }
        }
Beispiel #17
0
        static void CreateRegionWithRegionAdapter(DependencyObject element, string regionName)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            //If I'm in design mode the main window is not set
            if (Application.Current == null ||
                Application.Current.MainWindow == null)
            {
                return;
            }
            var shellView = ServiceLocator.Current.GetInstance <ShellView>();

            if (shellView == null)
            {
                throw new ArgumentNullException("AvalonDockRegion.shellView");
            }
            shellView.Loaded += (sender, e) =>
            {
                //try
                {
                    if (ServiceLocator.Current == null)
                    {
                        return;
                    }

                    // Build the region
                    var mappings = ServiceLocator.Current.GetInstance <RegionAdapterMappings>();
                    if (mappings == null)
                    {
                        return;
                    }
                    IRegionAdapter regionAdapter = mappings.GetMapping(element.GetType());
                    if (regionAdapter == null)
                    {
                        return;
                    }

                    regionAdapter.Initialize(element, regionName);
                }
                //catch (Exception ex)
                //{
                //    throw new RegionCreationException(string.Format("Unable to create region {0}", regionName), ex);
                //}
            };
        }
Beispiel #18
0
        /// <inheritdoc />
        public void AddAdapter(IRegionAdapter regionAdapter)
        {
            if (regionAdapter == null)
            {
                throw new ArgumentNullException(nameof(regionAdapter));
            }

            if (this.Adapters.Contains(regionAdapter))
            {
                return;
            }

            Trace.TraceInformation($"Adding region adapter: {regionAdapter.GetType().Name}");

            this.Adapters.Add(regionAdapter);
        }
Beispiel #19
0
 public void RegisterMapping(Type controlType, IRegionAdapter adapter)
 {
     if (controlType == null)
     {
         throw new ArgumentNullException("controlType");
     }
     if (adapter == null)
     {
         throw new ArgumentNullException("adapter");
     }
     if (mappings.ContainsKey(controlType))
     {
         throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                           Resources.MappingExistsException, controlType.Name));
     }
     mappings.Add(controlType, adapter);
 }
Beispiel #20
0
        /// <summary>
        /// Method that will create the region, by calling the right <see cref="IRegionAdapter"/>.
        /// </summary>
        /// <param name="targetElement">The target element that will host the <see cref="IRegion"/>.</param>
        /// <param name="regionName">Name of the region.</param>
        /// <returns>The created <see cref="IRegion"/></returns>
        protected virtual IRegion CreateRegion(DependencyObject targetElement, string regionName)
        {
            if (targetElement == null)
            {
                throw new ArgumentNullException("targetElement");
            }
            try
            {
                // Build the region
                IRegionAdapter regionAdapter = this.regionAdapterMappings.GetMapping(targetElement.GetType());
                IRegion        region        = regionAdapter.Initialize(targetElement, regionName);

                return(region);
            }
            catch (Exception ex)
            {
                throw new RegionCreationException(string.Format(CultureInfo.CurrentCulture, Application.Current.FindResource("RegionCreationException").ToString(), regionName, ex), ex);
            }
        }
Beispiel #21
0
        /// <inheritdoc />
        public void RemoveAdapter(IRegionAdapter regionAdapter)
        {
            if (regionAdapter == null)
            {
                throw new ArgumentNullException(nameof(regionAdapter));
            }

            Trace.TraceInformation($"Removing region adapter: {regionAdapter.GetType().Name}");

            foreach (KeyValuePair <string, Tuple <object, IRegionAdapter> > region in this.RegionDictionary)
            {
                if (ReferenceEquals(regionAdapter, region.Value.Item2))
                {
                    throw new InvalidOperationException("The specified region adapter is still in use.");
                }
            }

            this.Adapters.Remove(regionAdapter);
        }
        static void CreateRegion(LayoutAnchorable element, string regionName)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            //If I'm in design mode the main window is not set
            if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
            {
                return;
            }

            try
            {
                if (Microsoft.Practices.ServiceLocation.ServiceLocator.Current == null)
                {
                    return;
                }

                // Build the region
                var mappings = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance <RegionAdapterMappings>();
                if (mappings == null)
                {
                    return;
                }
                IRegionAdapter regionAdapter = mappings.GetMapping(element.GetType());
                if (regionAdapter == null)
                {
                    return;
                }

                regionAdapter.Initialize(element, regionName);
            }
            catch (Exception ex)
            {
                throw new RegionCreationException(string.Format("Unable to create region {0}", regionName), ex);
            }
        }
Beispiel #23
0
        /// <summary>
        /// Registers the mapping between a type and an adapter.
        /// </summary>
        /// <param name="controlType">The type of the control.</param>
        /// <param name="adapter">The adapter to use with the <paramref name="controlType"/> type.</param>
        /// <exception cref="ArgumentNullException">When any of <paramref name="controlType"/> or <paramref name="adapter"/> are <see langword="null" />.</exception>
        /// <exception cref="InvalidOperationException">If a mapping for <paramref name="controlType"/> already exists.</exception>
        public void RegisterMapping(Type controlType, IRegionAdapter adapter)
        {
            if (controlType == null)
            {
                throw new ArgumentNullException(nameof(controlType));
            }

            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            if (_mappings.ContainsKey(controlType))
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              "Mapping with the given type is already registered: {0}.",
                              controlType.Name));
            }

            _mappings.Add(controlType, adapter);
        }
 public Navigator(IUnityContainer container, IRegionAdapter regionAdapter)
 {
     this.container     = container;
     this.regionAdapter = regionAdapter;
 }
Beispiel #25
0
 /// <summary>
 /// Registers the mapping between a type and an adapter.
 /// </summary>
 /// <typeparam name="TControl">The type of the control</typeparam>
 public void RegisterMapping <TControl>(IRegionAdapter adapter)
 {
     RegisterMapping(typeof(TControl), adapter);
 }
Beispiel #26
0
        /// <summary>
        /// Initializes new instance of <see cref="RegionFactory"/>.
        /// </summary>
        /// <param name="regionAdapters">A collection of region adapters that will be used by this <see cref="RegionFactory"/>.</param>
        /// <exception cref="ArgumentNullException">When <paramref name="regionAdapters"/> is null.</exception>
        public RegionFactory(IRegionAdapter[] regionAdapters)
        {
            if (regionAdapters == null) throw new ArgumentNullException("regionAdapters");

            _regionAdapters = regionAdapters.ToArray();
        }