private IdentKey CreateMenuAndSubscribe(ICommand command, string parent) { BarSubItem parentItem = null; if (!string.Equals(parent, _rootBar.BarName)) { parentItem = FindItemByName(parent); if (parentItem == null) { throw new InvalidOperationException("Parent item not find."); } } BarItem item = null; BarItemLink link = null; BarItemInvoker invoker = null; IdentKey key = new IdentKey(); int lastValidIndex = LastIndex; try { item = BarItemFactory.CreateItem(command.CommandType); item.Tag = key; _barManager.Items.Add(item); item.Glyph = command.Image; item.Appearance.BackColor = Color.Transparent; if (parentItem == null) { link = _rootBar.ItemLinks.Insert(LastIndex, item); LastIndex++; } else { link = parentItem.ItemLinks.Insert(parentItem.ItemLinks.Count, item); } invoker = BarItemInvokerFactory.CreateItemInvoker(link, command); _Commands.Add(key, new CommandData(invoker, parentItem)); return(key); } catch (Exception ex) { ClearResource(parentItem, item, link, invoker, key, lastValidIndex); throw ex; } }
private void UnSubscribeAndRemoveMenu(IdentKey key) { if (key == null) { throw new ArgumentNullException("Key can not be null."); } CommandData data = null; if (!_Commands.TryGetValue(key, out data)) { throw new InvalidOperationException("Command with such key is not found."); } ICommand command = data.Invoker.Command; BarItemInvoker invoker = data.Invoker; BarSubItem parentItem = data.ParentItem; BarItemLink link = invoker.BarItemLink; BarItem item = link.Item; if (command.CommandType == CommandType.Group) { BarSubItem groupItem = item as BarSubItem; List <BarItemLink> itemLinks = new List <BarItemLink>(); foreach (BarItemLink itemLink in groupItem.ItemLinks) { itemLinks.Add(itemLink); } foreach (BarItemLink itemLink in itemLinks) { BarItem i = itemLink.Item; IdentKey itemKey = i.Tag as IdentKey; if (itemKey != null) { UnSubscribeAndRemoveMenu(itemKey); } } } ClearResource(parentItem, item, link, invoker, key, parentItem == null ? LastIndex-- : LastIndex); }
public static BarItemInvoker CreateItemInvoker(BarItemLink link, ICommand command) { BarItemInvoker result = null; switch (command.CommandType) { case CommandType.Button: result = new BarItemInvoker(link, command); break; case CommandType.CheckButton: result = new CheckedBarItemInvoker((BarCheckItemLink)link, command); break; case CommandType.Group: result = new BarItemInvoker(link, command); break; default: throw new NotSupportedException(string.Format("BarItemInvokerFactory. Command type = {0} not supported.", command.CommandType.ToString())); } return(result); }
public CommandData(BarItemInvoker invoker, BarSubItem parent) { Invoker = invoker; ParentItem = parent; }
private void ClearResource(BarSubItem parentItem, BarItem item, BarItemLink link, BarItemInvoker invoker, IdentKey key, int lastValidIndex) { LastIndex = lastValidIndex; try { _Commands.Remove(key); } catch { } try { if (invoker != null) { invoker.Dispose(); } } catch { } try { if (link != null) { if (parentItem == null) { _rootBar.ItemLinks.Remove(link); } else { parentItem.ItemLinks.Remove(link); } } } catch { } try { if (item != null) { _barManager.Items.Remove(item); } } catch { } }