/// <summary> /// Gets the string key for a <see cref="Tool"/>. /// </summary> /// <param name="tool">The <see cref="Tool"/> to get the key for.</param> /// <returns>The key for the <paramref name="tool"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="tool"/> is null.</exception> protected static string GetToolKey(Tool tool) { if (tool == null) throw new ArgumentNullException("tool"); return tool.GetType().FullName; }
/// <summary> /// Removes a <see cref="Tool"/> from this <see cref="ToolStateManager"/>. /// </summary> /// <param name="tool">The <see cref="Tool"/> to add.</param> /// <returns>True if the <paramref name="tool"/> was successfully removed; false if it was not in the collection.</returns> /// <exception cref="ArgumentNullException"><paramref name="tool"/> is null.</exception> public bool Remove(Tool tool) { if (tool == null) throw new ArgumentNullException("tool"); var key = GetToolKey(tool); return _tools.Remove(key); }
/// <summary> /// Adds a <see cref="Tool"/> to this <see cref="ToolStateManager"/>. If settings already exist for the /// <paramref name="tool"/>, they will be applied automatically. Only one instance of each type can be added. /// </summary> /// <param name="tool">The <see cref="Tool"/> to add.</param> /// <exception cref="ArgumentNullException"><paramref name="tool"/> is null.</exception> /// <exception cref="ArgumentException">The <paramref name="tool"/>, or a different instance of the same class, is already /// in the collection.</exception> public void Add(Tool tool) { if (tool == null) throw new ArgumentNullException("tool"); var key = GetToolKey(tool); _tools.Add(key, tool); ApplyToolSettings(tool); }
/// <summary> /// Reloads or resets the settings for a specific <see cref="Tool"/>. /// </summary> /// <param name="tool">The <see cref="Tool"/> to reset the settings of.</param> void ApplyToolSettings(Tool tool) { var key = GetToolKey(tool); IValueReader s; if (!_toolSettings.TryGetValue(key, out s)) { // No settings provided, reset the tool tool.ResetState(); } else { // Load from settings try { tool.ReadState(s); } catch (Exception ex) { const string errmsg = "Failed to restore the settings for tool `{0}`. Resetting tool to default. Exception: {1}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, tool, ex); Debug.Fail(string.Format(errmsg, tool, ex)); // When loading from settings fails, reset to default values tool.ResetState(); } } }
/// <summary> /// Initializes a new instance of the <see cref="ToolBarItemDropDownButton"/> class. /// </summary> /// <param name="tool">The <see cref="Tool"/> the control is for.</param> /// <exception cref="ArgumentNullException"><paramref name="tool"/> is null.</exception> public ToolBarItemDropDownButton(Tool tool) { if (tool == null) throw new ArgumentNullException("tool"); _tool = tool; Initialize(); }
/// <summary> /// Initializes a new instance of the <see cref="ToolBarItemProgressBar"/> class. /// </summary> /// <param name="tool">The <see cref="Tool"/> the control is for.</param> /// <exception cref="ArgumentNullException"><paramref name="tool"/> is null.</exception> public ToolBarItemProgressBar(Tool tool) { if (tool == null) throw new ArgumentNullException("tool"); _tool = tool; Initialize(); }
/// <summary> /// Tries to get the <see cref="ToolStripItem"/> for a <see cref="Tool"/>. /// </summary> /// <param name="tool">The tool to get the <see cref="ToolStripItem"/> for.</param> /// <returns>The <see cref="ToolStripItem"/> for the <paramref name="tool"/>, or null if unable to get it.</returns> protected static ToolStripItem TryGetToolStripItem(Tool tool) { if (tool == null) { const string errmsg = "Tool is null."; if (log.IsErrorEnabled) log.ErrorFormat(errmsg); Debug.Fail(errmsg); return null; } if (tool.ToolBarControl == null) { const string errmsg = "Tool `{0}` has null ToolBarControl."; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, tool); Debug.Fail(string.Format(errmsg, tool)); return null; } if (!(tool.ToolBarControl is ToolStripItem)) { const string errmsg = "Tool `{0}` ToolBarControl `{1}` is not of the expected type ToolStripItem (is type: `{2}`."; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, tool, tool.ToolBarControl, tool.ToolBarControl.GetType()); Debug.Fail(string.Format(errmsg, tool, tool.ToolBarControl, tool.ToolBarControl.GetType())); return null; } return tool.ToolBarControl as ToolStripItem; }
/// <summary> /// Removes a <see cref="Tool"/> from its <see cref="ToolBar"/>. /// </summary> /// <param name="tool">The <see cref="Tool"/> to remove from its <see cref="ToolBar"/>.</param> public static void RemoveFromToolBar(Tool tool) { if (!tool.ToolBarControl.IsOnToolBar) return; var c = TryGetToolStripItem(tool); if (c == null) return; var tb = GetToolBar(tool.ToolBarVisibility); if (tb == null) return; Debug.Assert(tb.Items.Contains(c)); tb.Items.Remove(c); Debug.Assert(!tb.Items.Contains(c)); }
/// <summary> /// General <see cref="ToolStripItem"/> initialization. /// </summary> /// <param name="t">The <see cref="Tool"/>.</param> /// <param name="c">The <see cref="ToolStripItem"/>.</param> static void InitializeGeneral(Tool t, ToolStripItem c) { c.Text = t.Name; c.Name = t.Name; c.AutoToolTip = true; c.ToolTipText = t.Name; }
/// <summary> /// Creates the <see cref="IToolBarControl"/> for a <see cref="Tool"/>. /// </summary> /// <param name="tool">The <see cref="Tool"/> to create the control for.</param> /// <param name="controlType">The type of control.</param> /// <returns>The <see cref="IToolBarControl"/> for the <paramref name="tool"/> using the given /// <paramref name="controlType"/>, or null if the <paramref name="controlType"/> is /// <see cref="ToolBarControlType.None"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="tool"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="controlType"/> does not contain a defined value of the /// <see cref="ToolBarControlType"/> enum.</exception> public static IToolBarControl CreateToolControl(Tool tool, ToolBarControlType controlType) { if (tool == null) throw new ArgumentNullException("tool"); if (!EnumHelper<ToolBarControlType>.IsDefined(controlType)) throw new ArgumentOutOfRangeException("controlType"); // Create the control ToolStripItem c; switch (controlType) { case ToolBarControlType.None: return null; case ToolBarControlType.Button: c = new ToolBarItemButton(tool); break; case ToolBarControlType.ComboBox: c = new ToolBarItemComboBox(tool); break; case ToolBarControlType.DropDownButton: c = new ToolBarItemDropDownButton(tool); break; case ToolBarControlType.Label: c = new ToolBarItemLabel(tool); break; case ToolBarControlType.ProgressBar: c = new ToolBarItemProgressBar(tool); break; case ToolBarControlType.SplitButton: c = new ToolBarItemSplitButton(tool); break; case ToolBarControlType.TextBox: c = new ToolBarItemTextBox(tool); break; default: const string errmsg = "ToolBarControlType `{0}` is not supported - could not create control for tool `{1}`."; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, controlType, tool); Debug.Fail(string.Format(errmsg, controlType, tool)); return null; } return (IToolBarControl)c; }
/// <summary> /// Adds a <see cref="Tool"/> to its <see cref="ToolBar"/>. /// </summary> /// <param name="tool">The <see cref="Tool"/> to add to its <see cref="ToolBar"/>.</param> public static void AddToToolBar(Tool tool) { // Make sure the tool can be shown in a toolbar if (!tool.CanShowInToolbar) return; // Don't do anything if already on the toolbar if (tool.ToolBarControl.IsOnToolBar) return; // Get the tool's ToolStripItem var c = TryGetToolStripItem(tool); if (c == null) return; // Get the toolbar for the tool var tb = GetToolBar(tool.ToolBarVisibility); if (tb == null) return; Debug.Assert(!tb.Items.Contains(c)); // Add the tool to the ToolBar tb.Items.Add(c); Debug.Assert(tb.Items.Contains(c)); }