Exemple #1
0
        public MenuAdapter(ContextMenu menu, WebActionNode model, ActionDispatcher actionDispatcher)
            : base(menu)
        {
            _menu = menu;

            if (model is WebDropDownButtonAction)
            {
                //TODO (CR May 2010): there's probably a more generic way to do this
                WebDropDownButtonAction a = model as WebDropDownButtonAction;
                foreach (WebActionNode node in a.DropDownActions)
                {
                    MenuItem menuItem = BuildMenuItem(node, actionDispatcher);
                    if (menuItem != null)
                    {
                        _menu.Items.Add(menuItem);
                    }
                }
            }
            else
            {
                foreach (WebActionNode node in model.Children)
                {
                    MenuItem menuItem = BuildMenuItem(node, actionDispatcher);
                    if (menuItem != null)
                    {
                        _menu.Items.Add(menuItem);
                    }
                }
            }
        }
Exemple #2
0
 public MenuItemBinding(WebAction action, ActionDispatcher dispatcher, MenuItem item)
 {
     _actionItem       = action;
     _actionDispatcher = dispatcher;
     _actionDispatcher.Register(action.Identifier, this);
     item.Click += OnItemClick;
     Item        = item;
 }
Exemple #3
0
        private void ReleaseMenuItem(MenuItem item, bool disposing)
        {
            var binding = item.Tag as MenuItemBinding;

            if (binding != null)
            {
                if (disposing)
                {
                    binding.Dispose();
                }
                item.Tag = null;
            }

            foreach (MenuItem child in item.Items)
            {
                ReleaseMenuItem(child, disposing);
            }
        }
Exemple #4
0
        private static MenuItem BuildActionMenuItem(WebActionNode subNode, ActionDispatcher dispatcher)
        {
            WebAction actionNode = subNode as WebAction;

            MenuItem item = new MenuItem
            {
                IsEnabled  = actionNode.Enabled,
                IsChecked  = (actionNode is WebClickAction) && (actionNode as WebClickAction).Checked,
                Visibility = actionNode.DesiredVisiblility
            };

            var binding = new MenuItemBinding(actionNode, dispatcher, item);

            binding.SetLabel(actionNode.Label);
            binding.SetIcon();

            item.Tag = binding;

            return(item);
        }
Exemple #5
0
 internal static void CreateTest(this IMenu menu, int height, int depth)
 {
     menu.Items.Clear();
     for (int h = 0; h < height; ++h)
     {
         var item = new MenuItem(){Header = String.Format("Item {0}", h+1) };
         item.Click += (sender, e) => MessageBox.Show(item.Header.ToString());
         menu.Items.Add(item);
         if (h % 2 == 0)
         {
             if (depth > 0)
             {
                 var l = depth == 1 ? 10 : height;
                 CreateTest(item, l, depth - 1);
             }
         }
         else
         {
             item.IsChecked = true;
         }
     }
 }
Exemple #6
0
 public MenuItemBinding(WebAction action, ActionDispatcher dispatcher, MenuItem item)
 {
     _actionItem = action;
     _actionDispatcher = dispatcher;
     _actionDispatcher.Register(action.Identifier, this);
     item.Click += OnItemClick;
     Item = item;
 }
Exemple #7
0
        private static MenuItem BuildActionMenuItem(WebActionNode subNode, ActionDispatcher dispatcher)
        {
            WebAction actionNode = subNode as WebAction;

            MenuItem item = new MenuItem
            {
                IsEnabled = actionNode.Enabled,
                IsChecked = (actionNode is WebClickAction) && (actionNode as WebClickAction).Checked,
                Visibility = actionNode.DesiredVisiblility
            };

            var binding = new MenuItemBinding(actionNode, dispatcher, item);

            binding.SetLabel(actionNode.Label);
            binding.SetIcon();

            item.Tag = binding;

            return item;
        }
Exemple #8
0
        private static MenuItem BuildMenuItem(WebActionNode node, ActionDispatcher dispatcher)
        {
            MenuItem thisMenu = null;
            try
            {

                if (node.Children != null)
                {
                    thisMenu = new MenuItem { Header = node.LocalizedText };

                    foreach (WebActionNode subNode in node.Children)
                    {
                        if (subNode.Children == null || subNode.Children.Count == 0)
                        {
                            MenuItem menuItem = BuildActionMenuItem(subNode, dispatcher);

                            if (menuItem != null)
                            {
                                if (menuItem.IsChecked)
                                    thisMenu.IsChecked = true;

                                if (menuItem != null)
                                    thisMenu.Items.Add(menuItem);
                            }

                        }
                        else
                        {
                            MenuItem menuItem = BuildMenuItem(subNode, dispatcher);
                            if (menuItem != null)
                            {
                                if (menuItem.IsChecked)
                                    thisMenu.IsChecked = true;

                                thisMenu.Items.Add(menuItem);
                            }
                        }
                    }

                    // Don't show the menu if it has no children
                    thisMenu.Visibility = node.DesiredVisiblility;
                }
                else
                {
                    WebAction actionNode = node as WebAction;
                    thisMenu = BuildActionMenuItem(actionNode, dispatcher);
                }

                return thisMenu;
            }
            catch (Exception ex)
            {
                // When an error happens here we need as much technical details as possible to diagnose the problem
                // Also, it's better to capture the error in English
                var itemName = node.LocalizedText;
                if (node is WebActionNode)
                    itemName = (node as WebActionNode).LocalizedText;
                if (node is WebAction)
                    itemName = (node as WebAction).Label;

                var message = new StringBuilder();
                message.AppendFormat("An expected error has occurred when building menu item labelled: {0}", itemName);
                message.AppendLine();
                message.AppendLine(string.Format("Details: {0}", ex.Message));
                message.AppendLine("Stack:");
                message.AppendLine(ex.StackTrace);

                var newEx = new Exception(message.ToString(), ex);
                throw newEx;
            }
        }
Exemple #9
0
        private void ReleaseMenuItem(MenuItem item, bool disposing)
        {
            var binding = item.Tag as MenuItemBinding;
            if (binding != null)
            {
                if (disposing)
                    binding.Dispose();
                item.Tag = null;
            }

            foreach (MenuItem child in item.Items)
                ReleaseMenuItem(child, disposing);    
        }
Exemple #10
0
        private static MenuItem BuildMenuItem(WebActionNode node, ActionDispatcher dispatcher)
        {
            MenuItem thisMenu = null;

            try
            {
                if (node.Children != null)
                {
                    thisMenu = new MenuItem {
                        Header = node.LocalizedText
                    };

                    foreach (WebActionNode subNode in node.Children)
                    {
                        if (subNode.Children == null || subNode.Children.Count == 0)
                        {
                            MenuItem menuItem = BuildActionMenuItem(subNode, dispatcher);

                            if (menuItem != null)
                            {
                                if (menuItem.IsChecked)
                                {
                                    thisMenu.IsChecked = true;
                                }

                                if (menuItem != null)
                                {
                                    thisMenu.Items.Add(menuItem);
                                }
                            }
                        }
                        else
                        {
                            MenuItem menuItem = BuildMenuItem(subNode, dispatcher);
                            if (menuItem != null)
                            {
                                if (menuItem.IsChecked)
                                {
                                    thisMenu.IsChecked = true;
                                }

                                thisMenu.Items.Add(menuItem);
                            }
                        }
                    }

                    // Don't show the menu if it has no children
                    thisMenu.Visibility = node.DesiredVisiblility;
                }
                else
                {
                    WebAction actionNode = node as WebAction;
                    thisMenu = BuildActionMenuItem(actionNode, dispatcher);
                }

                return(thisMenu);
            }
            catch (Exception ex)
            {
                // When an error happens here we need as much technical details as possible to diagnose the problem
                // Also, it's better to capture the error in English
                var itemName = node.LocalizedText;
                if (node is WebActionNode)
                {
                    itemName = (node as WebActionNode).LocalizedText;
                }
                if (node is WebAction)
                {
                    itemName = (node as WebAction).Label;
                }

                var message = new StringBuilder();
                message.AppendFormat("An expected error has occurred when building menu item labelled: {0}", itemName);
                message.AppendLine();
                message.AppendLine(string.Format("Details: {0}", ex.Message));
                message.AppendLine("Stack:");
                message.AppendLine(ex.StackTrace);

                var newEx = new Exception(message.ToString(), ex);
                throw newEx;
            }
        }