Ejemplo n.º 1
0
        public override object CreateInstance()
        {
            ActionType ct       = ActionType.Normal;
            bool       isArray  = false;
            bool       custom   = false;
            bool       isAction = false;

            foreach (string p in type.Split('|'))
            {
                switch (p)
                {
                case "check":
                    ct = ActionType.Check;
                    if (isAction)
                    {
                        throw new InvalidOperationException("Action type specified twice.");
                    }
                    isAction = true;
                    break;

                case "radio":
                    ct = ActionType.Radio;
                    if (isAction)
                    {
                        throw new InvalidOperationException("Action type specified twice.");
                    }
                    isAction = true;
                    break;

                case "normal":
                    ct = ActionType.Normal;
                    if (isAction)
                    {
                        throw new InvalidOperationException("Action type specified twice.");
                    }
                    isAction = true;
                    break;

                case "custom":
                    if (widget == null)
                    {
                        throw new InvalidOperationException("Widget type not specified in custom command.");
                    }
                    custom = true;
                    break;

                case "array":
                    isArray = true;
                    break;

                default:
                    throw new InvalidOperationException("Unknown command type: " + p);
                }
            }

            if (isAction && custom)
            {
                throw new InvalidOperationException("Invalid command type combination: " + type);
            }

            Command cmd;

            if (custom)
            {
                if (isArray)
                {
                    throw new InvalidOperationException("Array custom commands are not allowed.");
                }

                CustomCommand ccmd = new CustomCommand();
                ccmd.Text       = label;
                ccmd.WidgetType = Addin.GetType(widget);
                if (ccmd.WidgetType == null)
                {
                    throw new InvalidOperationException("Could not find command type '" + widget + "'.");
                }
                cmd = ccmd;
            }
            else
            {
                if (widget != null)
                {
                    throw new InvalidOperationException("Widget type can only be specified for custom commands.");
                }

                ActionCommand acmd = new ActionCommand();
                acmd.ActionType   = ct;
                acmd.CommandArray = isArray;

                if (defaultHandler != null)
                {
                    acmd.SetDefaultHandlerTypeInfo(Addin, defaultHandler);
                }

                cmd = acmd;
            }

            cmd.Id   = ParseCommandId(this);
            cmd.Text = StringParserService.Parse(BrandingService.BrandApplicationName(label));
            if (!String.IsNullOrWhiteSpace(_displayName))
            {
                cmd.DisplayName = StringParserService.Parse(BrandingService.BrandApplicationName(_displayName));
            }
            if ((_description != null) && (_description.Length > 0))
            {
                cmd.Description = BrandingService.BrandApplicationName(_description);
            }
            cmd.Description = cmd.Description;

            if (icon != null)
            {
                cmd.Icon = GetStockId(Addin, icon);
            }

            var keyBinding = Platform.IsMac ? macShortcut : shortcut;

            if (Platform.IsWindows && !string.IsNullOrEmpty(winShortcut))
            {
                keyBinding = winShortcut;
            }
            string[] splittedKeys = (keyBinding ?? "").Split(' ');

            cmd.AccelKey = KeyBindingManager.FixChordSeparators(KeyBindingManager.CanonicalizeBinding(splittedKeys[0]));
            if (splittedKeys.Length > 1)
            {
                cmd.AlternateAccelKeys = splittedKeys.Skip(1).Select(key => KeyBindingManager.FixChordSeparators(key)).ToArray();
            }

            cmd.DisabledVisible = disabledVisible;

            // Assign the category of the command
            CommandCategoryCodon cat = Parent as CommandCategoryCodon;

            if (cat != null)
            {
                cmd.Category = cat.Name;
            }

            return(cmd);
        }
Ejemplo n.º 2
0
        public override object CreateInstance()
        {
            ActionType ct       = ActionType.Normal;
            bool       isArray  = false;
            bool       custom   = false;
            bool       isAction = false;

            foreach (string p in type.Split('|'))
            {
                switch (p)
                {
                case "check":
                    ct = ActionType.Check;
                    if (isAction)
                    {
                        throw new InvalidOperationException("Action type specified twice.");
                    }
                    isAction = true;
                    break;

                case "radio":
                    ct = ActionType.Radio;
                    if (isAction)
                    {
                        throw new InvalidOperationException("Action type specified twice.");
                    }
                    isAction = true;
                    break;

                case "normal":
                    ct = ActionType.Normal;
                    if (isAction)
                    {
                        throw new InvalidOperationException("Action type specified twice.");
                    }
                    isAction = true;
                    break;

                case "custom":
                    if (widget == null)
                    {
                        throw new InvalidOperationException("Widget type not specified in custom command.");
                    }
                    custom = true;
                    break;

                case "array":
                    isArray = true;
                    break;

                default:
                    throw new InvalidOperationException("Unknown command type: " + p);
                }
            }

            if (isAction && custom)
            {
                throw new InvalidOperationException("Invalid command type combination: " + type);
            }

            Command cmd;

            if (custom)
            {
                if (isArray)
                {
                    throw new InvalidOperationException("Array custom commands are not allowed.");
                }

                CustomCommand ccmd = new CustomCommand();
                ccmd.Text       = label;
                ccmd.WidgetType = Addin.GetType(widget);
                if (ccmd.WidgetType == null)
                {
                    throw new InvalidOperationException("Could not find command type '" + widget + "'.");
                }
                cmd = ccmd;
            }
            else
            {
                if (widget != null)
                {
                    throw new InvalidOperationException("Widget type can only be specified for custom commands.");
                }

                ActionCommand acmd = new ActionCommand();
                acmd.ActionType   = ct;
                acmd.CommandArray = isArray;

                if (defaultHandler != null)
                {
                    acmd.SetDefaultHandlerTypeInfo(Addin, defaultHandler);
                }

                cmd = acmd;
            }

            cmd.Id   = ParseCommandId(this);
            cmd.Text = StringParserService.Parse(label);
            if ((_description != null) && (_description.Length > 0))
            {
                cmd.Description = _description;
            }
            cmd.Description = cmd.Description;

            if (icon != null)
            {
                cmd.Icon = GetStockId(Addin, icon);
            }

            cmd.AccelKey = KeyBindingManager.CanonicalizeBinding(PropertyService.IsMac? macShortcut : shortcut);

            cmd.DisabledVisible = disabledVisible;

            // Assign the category of the command
            CommandCategoryCodon cat = Parent as CommandCategoryCodon;

            if (cat != null)
            {
                cmd.Category = cat.Name;
            }

            return(cmd);
        }