PageRenderTime 32ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/Controls/ErrorToolbar.cs

#
C# | 232 lines | 158 code | 49 blank | 25 comment | 31 complexity | 95929eca6f8fca509673655e1fd0747f MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You may
  3// obtain a copy of the license at http://nunit.org
  4// ****************************************************************
  5
  6using System;
  7using System.Collections.Generic;
  8using System.Text;
  9using System.Windows.Forms;
 10using System.Collections;
 11using System.Drawing;
 12using System.Diagnostics;
 13using NUnit.UiException.Properties;
 14
 15namespace NUnit.UiException.Controls
 16{
 17    /// <summary>
 18    /// A specialization of a ToolStrip to show instances of IErrorDisplay.
 19    /// </summary>
 20    public class ErrorToolbar :
 21        ToolStrip,
 22        IEnumerable
 23    {
 24        public event EventHandler SelectedRendererChanged;
 25
 26        private List<IErrorDisplay> _displays;
 27
 28        private ToolStripItem _separator;
 29        private int _selection;
 30
 31        public ErrorToolbar()
 32        {
 33            _displays = new List<IErrorDisplay>();
 34
 35            _separator = CreateDefaultItem("-", null, null);
 36            Items.Add(_separator);
 37
 38            _selection = -1;
 39            
 40            BackgroundImage = Resources.ImageErrorBrowserHeader;
 41            BackgroundImageLayout = ImageLayout.Tile;
 42
 43            return;
 44        }
 45
 46        /// <summary>
 47        /// Create and configure a ToolStripButton.
 48        /// </summary>
 49        public static ToolStripButton NewStripButton(
 50            bool canCheck, string text, Image image, EventHandler onClick)
 51        {
 52            ToolStripButton button;
 53
 54            button = new ToolStripButton(text, image, onClick);
 55            button.CheckOnClick = canCheck;
 56            button.Image = image;
 57            button.ImageScaling = ToolStripItemImageScaling.None;
 58            button.TextImageRelation = TextImageRelation.ImageBeforeText;
 59            button.DisplayStyle = ToolStripItemDisplayStyle.Image;
 60
 61            return (button);
 62        }
 63
 64        /// <summary>
 65        /// Gets the count of IErrorDisplay instances.
 66        /// </summary>
 67        public int Count
 68        {
 69            get { return (_displays.Count); }
 70        }
 71
 72        /// <summary>
 73        /// Gets the display at the given index.
 74        /// </summary>
 75        public IErrorDisplay this[int index]
 76        {
 77            get { return (_displays[index]); }
 78        }
 79
 80        /// <summary>
 81        /// Gets or sets the IErrorDisplay to be selected.
 82        /// </summary>
 83        public IErrorDisplay SelectedDisplay
 84        {
 85            get {
 86                if (_selection == -1)
 87                    return (null);
 88                return ((IErrorDisplay)Items[_selection].Tag); 
 89            }
 90            set
 91            {
 92                int index = IndexOf(value);
 93
 94                UiExceptionHelper.CheckFalse(index == -1 && value != null,
 95                    "Cannot select unregistered display.", "SelectedDisplay");
 96
 97                if (index == _selection)
 98                    return;
 99
100                _selection = index;
101                SetOrUnsetCheckedFlag(_selection);
102                ShowOrHideOptionItems(_selection);
103                
104                if (SelectedRendererChanged != null)
105                    SelectedRendererChanged(this, new EventArgs());
106
107                return;
108            }
109        }
110
111        /// <summary>
112        /// Register a new IErrorDisplay in the toolbar.
113        /// </summary>
114        public void Register(IErrorDisplay display)
115        {
116            ToolStripItem item;
117            int sepIndex;
118
119            UiExceptionHelper.CheckNotNull(display, "display");
120            UiExceptionHelper.CheckNotNull(display.PluginItem, "display.PluginItem");
121
122            item = display.PluginItem;
123            item.Tag = display;
124            item.Click += new EventHandler(item_Click);
125
126            _displays.Add(display);
127            sepIndex = Items.IndexOf(_separator);
128            Items.Insert(sepIndex, item);
129
130            if (display.OptionItems != null)
131            {
132                ToolStripItem[] array = display.OptionItems;
133                foreach (ToolStripItem value in array)
134                {
135                    value.Visible = false;
136                    Items.Add(value);
137                }
138            }
139
140            if (_displays.Count == 1)
141                SelectedDisplay = display;
142
143            return;
144        }
145
146        /// <summary>
147        /// Clears all IErrorDisplay in the toolbar.
148        /// </summary>
149        public void Clear()
150        {
151            _displays.Clear();
152            Items.Clear();
153            Items.Add(_separator);
154
155            return;
156        }        
157
158        private void ShowOrHideOptionItems(int selectedIndex)
159        {
160            int index;
161
162            foreach (IErrorDisplay item in _displays)
163            {
164                if ((index = IndexOf(item)) == -1)                     
165                    continue;
166
167                if (item.OptionItems == null)
168                    continue;
169
170                foreach (ToolStripItem stripItem in item.OptionItems)
171                    stripItem.Visible = (index == selectedIndex);
172            }
173
174            return;
175        }
176
177        private void SetOrUnsetCheckedFlag(int selectedIndex)
178        {
179            int index;
180
181            foreach (IErrorDisplay item in _displays)
182            {
183                index = IndexOf(item);
184                if (index == -1)
185                    continue;
186                item.PluginItem.Checked = (index == selectedIndex);
187            }
188
189            return;
190        }
191
192        private int IndexOf(IErrorDisplay renderer)
193        {
194            int i;
195
196            if (renderer == null)
197                return (-1);
198
199            for (i = 0; i < Items.Count; ++i)
200                if (object.ReferenceEquals(Items[i].Tag, renderer))
201                    return (i);
202
203            return (-1);
204        }       
205
206        private void item_Click(object sender, EventArgs e)
207        {
208            ToolStripItem item = sender as ToolStripItem;
209            IErrorDisplay renderer;
210
211            if (item == null || item.Tag == null)
212                return;
213
214            renderer = item.Tag as IErrorDisplay;
215            if (renderer == null)
216                return;
217
218            SelectedDisplay = renderer;
219
220            return;
221        }                
222
223        #region IEnumerable Membres
224
225        public IEnumerator GetEnumerator()
226        {
227            return (_displays.GetEnumerator());
228        }
229
230        #endregion
231    }      
232}