Пример #1
0
            internal static void RegisterAdapter(CommandAdapterBase <TInvoker> adapter, IUIElement key)
            {
                List <CommandAdapterBase <TInvoker> > invokers;

                // if this is the first time we've seen this key, create a list for the adapters
                if (adapters.TryGetValue(key, out invokers) == false)
                {
                    invokers = new List <CommandAdapterBase <TInvoker> >();
                    adapters.Add(key, invokers);
                }

                // store a reference to the adapter in the list
                invokers.Add(adapter);

                // also store the key using the adapter as the key
                adapterKey.Add(adapter, key);

                Command command = adapter.Command;

                if (null != command)
                {
                    // add command and verify element state
                    AddCommand(command, key);
                }

                // verify/update the state of the key
                VerifyCommandState(key);
            }
Пример #2
0
            private static void VerifyCommandState(IUIElement logicalElement)
            {
                List <Command> commands;
                CommandStatus  status            = CommandStatus.Unavailable;
                Command        commandWithStatus = null;

                // if we have commands associated with the element...
                if (elementCommands.TryGetValue(logicalElement, out commands))
                {
                    for (int i = 0, len = commands.Count; i < len; i++)
                    {
                        Command cmd = commands[i];

                        if (cmd.Status == CommandStatus.Disabled)
                        {
                            status            = CommandStatus.Disabled;
                            commandWithStatus = cmd;
                        }
                        else if (cmd.Status == CommandStatus.Enabled)
                        {
                            status = CommandStatus.Enabled;
                            break;
                        }
                    }
                }

                logicalElement.UpdateState(status);

                // invoke a method on the command adapters so
                // derived command adapters can react to the change
                List <CommandAdapterBase <TInvoker> > cmdAdapters;

                if (adapters.TryGetValue(logicalElement, out cmdAdapters))
                {
                    for (int i = 0, len = cmdAdapters.Count; i < len; i++)
                    {
                        CommandAdapterBase <TInvoker> cmdAdapter = cmdAdapters[i];

                        cmdAdapter.OnInvokerStateChanged();
                    }
                }
            }
Пример #3
0
            internal static void NotifyCommandChanged(CommandAdapterBase <TInvoker> adapter, Command oldCommand, Command newCommand)
            {
                IUIElement logicalElement;

                // we only need to worry about it if, the adapter has been associated with an invoker/element
                if (adapterKey.TryGetValue(adapter, out logicalElement))
                {
                    if (null != oldCommand)
                    {
                        RemoveCommand(oldCommand, logicalElement);
                    }

                    if (null != newCommand)
                    {
                        AddCommand(newCommand, logicalElement);
                    }

                    VerifyCommandState(logicalElement);
                }
            }
Пример #4
0
            internal static void UnregisterAdapter(CommandAdapterBase <TInvoker> adapter, IUIElement key)
            {
                List <CommandAdapterBase <TInvoker> > invokers;

                if (adapters.TryGetValue(key, out invokers) == false)
                {
                    throw new InvalidOperationException(Properties.Resources.UnrecognizedAdapterKey);
                }

                Debug.Assert(invokers.Contains(adapter), "The specified adapter is not associated with the specified key.");

                // if this is the last invoker...
                if (invokers.Count == 1)
                {
                    if (invokers[0] != adapter)
                    {
                        throw new InvalidOperationException(Properties.Resources.InvalidAdapterForKey);
                    }

                    // remove the key as well
                    adapters.Remove(key);
                }
                else
                {
                    invokers.Remove(adapter);
                }

                // remove the dictionary which relates the adapter to the key
                adapterKey.Remove(adapter);

                Command command = adapter.Command;

                if (null != command)
                {
                    // add command and verify element state
                    RemoveCommand(command, key);

                    // verify/update the state of the key
                    VerifyCommandState(key);
                }
            }