///     Sets the given selection object to be the primary selection.
        internal void SetPrimarySelection(SampleSelectionItem sel) {
            if (sel != primarySelection) {
                if (null != primarySelection) {
                    primarySelection.Primary = false;
                }

                primarySelection = sel;
            }

            if (null != primarySelection) {
                primarySelection.Primary = true;
            }
        }
        ///     Called anytime the selection has changed.  We update our UI for the selection, and
        ///     then we fire a change event.
        private void OnSelectionChanged() {
            if (batchMode) {
                selectionChanged = true;
            }
            else {
                selectionChanged = false;
                
                // Raise SelectionChanging event - this should be moved to OnSelectionChanging()
				// if the implementation requires listening to changING events.
                if (SelectionChanging != null) {
                    try {
                        SelectionChanging(this, EventArgs.Empty);;
                    }
                    catch(Exception) {
                        // It is an error to ever throw in this event.
                        Debug.Fail("Exception thrown in selectionChanging event");
                    }
                }

                if (need_clear_selection)
                {
                    need_clear_selection = false;
                    selectionsByComponent.Clear();
                    primarySelection = null;                    
                }

                if (SelectionChanged != null) {
                    try {
                        SelectionChanged(this, EventArgs.Empty);;
                    }
                    catch(Exception) {
                        // It is an error to ever throw in this event.
                        Debug.Fail("Exception thrown in selectionChanging event");
                    }
                }
                
                
                OnSelectionContentsChanged();
            }
        }
 ///     Removes the given selection from our selection list
 private void RemoveSelection(SampleSelectionItem s) {
     selectionsByComponent.Remove(s.Component);
     s.Dispose();
 }
        ///     Disposes the entire selection manager.
        internal void Dispose() {
            // We've got to be careful here.  We're one of the last things to go away when
            // a form is being torn down, and we've got to be wary if things have pulled out
            // already.
            host.RemoveService(typeof(ISelectionService));
            host.TransactionOpened -= new EventHandler(this.DesignerHost_TransactionOpened);
            host.TransactionClosed -= new DesignerTransactionCloseEventHandler(this.DesignerHost_TransactionClosed);
            if (host.InTransaction) {
                DesignerHost_TransactionClosed(host, new DesignerTransactionCloseEventArgs(true));
            }
            host.LoadComplete -= new EventHandler(this.DesignerHost_LoadComplete);

            IComponentChangeService cs = (IComponentChangeService)host.GetService(typeof(IComponentChangeService));
            if (cs != null) {
                cs.ComponentAdded -= new ComponentEventHandler(this.DesignerHost_ComponentAdd);
                cs.ComponentRemoved -= new ComponentEventHandler(this.DesignerHost_ComponentRemove);
                cs.ComponentChanged -= new ComponentChangedEventHandler(this.DesignerHost_ComponentChanged);
            }

            SampleSelectionItem[] sels = new SampleSelectionItem[selectionsByComponent.Values.Count];
            selectionsByComponent.Values.CopyTo(sels, 0);
            
            for (int i = 0; i < sels.Length; i++) {
                sels[i].Dispose();
            }

            selectionsByComponent.Clear();
            primarySelection = null;
        }
 ///     Adds the given selection to our selection list.
 private void AddSelection(SampleSelectionItem sel) {
     selectionsByComponent[sel.Component] = sel;
 }
		/// Select the given components with the given SelectionType.
        void ISelectionService.SetSelectedComponents(ICollection components, SelectionTypes selectionType) {
            bool fToggle = false;
            bool fControl = false;
            bool fClick  = false;
            bool fChanged = false;  // did we actually change something?

            // Catch a null input list
            if (components == null){
                components = new Component[0];
            }

            if ((selectionType & SelectionTypes.Normal) == SelectionTypes.Normal
                || (selectionType & SelectionTypes.Click) == SelectionTypes.Click) {

                fControl = ((Control.ModifierKeys & Keys.Control) == Keys.Control);

                // Only toggle when we are affecting a single control, and
                // when we are handling the "mouse" state events (i.e. up/down
                // used to show/hide the selection).
                //
                fToggle = ((Control.ModifierKeys & Keys.Control) != 0 || (Control.ModifierKeys & Keys.Shift) != 0)
                          && components.Count == 1
                          && (selectionType & SelectionTypes.MouseUp) != SelectionTypes.MouseUp;
            }

            if ((selectionType & SelectionTypes.Click) == SelectionTypes.Click) {
                fClick = true;
            }


            // If we are replacing the selection, only remove the ones that are not in our new list.
            // We also handle the special case here of having a singular component selected that's
            // already selected.  In this case we just move it to the primary selection.
            //
            if (!fToggle && !fControl) {
                object firstSelection = null;
                foreach(object o in components) {
                    firstSelection = o;
                    break;
                }
                
                if (fClick && 1 == components.Count && ((ISelectionService)this).GetComponentSelected(firstSelection)) {
                    SampleSelectionItem oldPrimary = primarySelection;
                    SetPrimarySelection((SampleSelectionItem)selectionsByComponent[firstSelection]);
                    if (oldPrimary != primarySelection) {
                        fChanged = true;
                    }
                }
                else {
                    SampleSelectionItem[] selections = new SampleSelectionItem[selectionsByComponent.Values.Count];
                    selectionsByComponent.Values.CopyTo(selections, 0);                    

                    // Even with several hundred components this should be fairly fast
                    foreach(SampleSelectionItem item in selections) {
                        bool remove = true;
                    
                        foreach(object comp in components) {
                            if (comp == item.Component) {
                                remove = false;
                                break;
                            }
                        }
                        
                        if (remove) {
                            RemoveSelection(item);
                            fChanged = true;
                        }
                    }
                }
            }

            SampleSelectionItem primarySel = null;
            int selectedCount = selectionsByComponent.Count;

            // Now do the selection.
            foreach(Component comp in components) {
                if (comp != null) {
                    SampleSelectionItem  s = (SampleSelectionItem)selectionsByComponent[comp];
    
                    if (null == s) {
                        s = new SampleSelectionItem(this, comp);
                        AddSelection(s);

                        if (fControl || fToggle) {
                            primarySel = s;
                        }

                        fChanged = true;
                    }
                    else {
                        if (fToggle) {
                            // Remove the selection from our list.
                            RemoveSelection(s);
                            fChanged = true;
                        }
                    }
                }
            }

            if (primarySel != null) {
                SetPrimarySelection(primarySel);
            }

            // Now notify that our selection has changed
            if (fChanged) {
                OnSelectionChanged();
            }
        }
 internal void Dispose()
 {
     this.host.RemoveService(typeof(ISelectionService));
     this.host.TransactionOpened -= new EventHandler(this.DesignerHost_TransactionOpened);
     this.host.TransactionClosed -= new DesignerTransactionCloseEventHandler(this.DesignerHost_TransactionClosed);
     if (this.host.InTransaction)
     {
         this.DesignerHost_TransactionClosed(this.host, new DesignerTransactionCloseEventArgs(true));
     }
     this.host.LoadComplete -= new EventHandler(this.DesignerHost_LoadComplete);
     IComponentChangeService cs = (IComponentChangeService) this.host.GetService(typeof(IComponentChangeService));
     if (cs != null)
     {
         cs.ComponentAdded -= new ComponentEventHandler(this.DesignerHost_ComponentAdd);
         cs.ComponentRemoved -= new ComponentEventHandler(this.DesignerHost_ComponentRemove);
         cs.ComponentChanged -= new ComponentChangedEventHandler(this.DesignerHost_ComponentChanged);
     }
     SampleSelectionItem[] sels = new SampleSelectionItem[this.selectionsByComponent.Values.Count];
     this.selectionsByComponent.Values.CopyTo(sels, 0);
     for (int i = 0; i < sels.Length; i++)
     {
         sels[i].Dispose();
     }
     this.selectionsByComponent.Clear();
     this.primarySelection = null;
 }
 void ISelectionService.SetSelectedComponents(ICollection components, SelectionTypes selectionType)
 {
     bool fToggle = false;
     bool fControl = false;
     bool fClick = false;
     bool fChanged = false;
     if (components == null)
     {
         components = new Component[0];
     }
     if (((selectionType & SelectionTypes.Auto) == SelectionTypes.Auto) || ((selectionType & SelectionTypes.Click) == SelectionTypes.Click))
     {
         fControl = (Control.ModifierKeys & Keys.Control) == Keys.Control;
         fToggle = ((((Control.ModifierKeys & Keys.Control) != Keys.None) || ((Control.ModifierKeys & Keys.Shift) != Keys.None)) && (components.Count == 1)) && ((selectionType & SelectionTypes.MouseUp) != SelectionTypes.MouseUp);
     }
     if ((selectionType & SelectionTypes.Click) == SelectionTypes.Click)
     {
         fClick = true;
     }
     if (!fToggle && !fControl)
     {
         object firstSelection = null;
         foreach (object o in components)
         {
             firstSelection = o;
             break;
         }
         if ((fClick && (1 == components.Count)) && ((ISelectionService) this).GetComponentSelected(firstSelection))
         {
             SampleSelectionItem oldPrimary = this.primarySelection;
             this.SetPrimarySelection((SampleSelectionItem) this.selectionsByComponent[firstSelection]);
             if (oldPrimary != this.primarySelection)
             {
                 fChanged = true;
             }
         }
         else
         {
             SampleSelectionItem[] selections = new SampleSelectionItem[this.selectionsByComponent.Values.Count];
             this.selectionsByComponent.Values.CopyTo(selections, 0);
             foreach (SampleSelectionItem item in selections)
             {
                 bool remove = true;
                 foreach (object comp in components)
                 {
                     if (comp == item.Component)
                     {
                         remove = false;
                         break;
                     }
                 }
                 if (remove)
                 {
                     this.RemoveSelection(item);
                     fChanged = true;
                 }
             }
         }
     }
     SampleSelectionItem primarySel = null;
     int selectedCount = this.selectionsByComponent.Count;
     foreach (Component comp in components)
     {
         if (comp != null)
         {
             SampleSelectionItem s = (SampleSelectionItem) this.selectionsByComponent[comp];
             if (null == s)
             {
                 s = new SampleSelectionItem(this, comp);
                 this.AddSelection(s);
                 if (fControl || fToggle)
                 {
                     primarySel = s;
                 }
                 fChanged = true;
             }
             else if (fToggle)
             {
                 this.RemoveSelection(s);
                 fChanged = true;
             }
         }
     }
     if (primarySel != null)
     {
         this.SetPrimarySelection(primarySel);
     }
     if (fChanged)
     {
         this.OnSelectionChanged();
     }
 }