示例#1
0
        /// <summary>
        /// Changes the user's current set of selected components to the components in the given array.  If the array is null or doesn't contain any components, this will select the top level component in the designer.
        /// </summary>
        void ISelectionService.SetSelectedComponents(ICollection components, SelectionTypes selectionType)
        {
            bool fToggle  = (selectionType & SelectionTypes.Toggle) == SelectionTypes.Toggle;
            bool fPrimary = (selectionType & SelectionTypes.Primary) == SelectionTypes.Primary;
            bool fAdd     = (selectionType & SelectionTypes.Add) == SelectionTypes.Add;
            bool fRemove  = (selectionType & SelectionTypes.Remove) == SelectionTypes.Remove;
            bool fReplace = (selectionType & SelectionTypes.Replace) == SelectionTypes.Replace;
            bool fAuto    = !(fToggle | fAdd | fRemove | fReplace);

            // We always want to allow NULL arrays coming in.
            if (components == null)
            {
                components = new object[0];
            }
            // If toggle, replace, remove or add are not specifically specified, infer them from  the state of the modifer keys.  This creates the "Auto" selection type for us by default.
            if (fAuto)
            {
                fToggle = (Control.ModifierKeys & (Keys.Control | Keys.Shift)) > 0;
                fAdd   |= System.Windows.Forms.Control.ModifierKeys == System.Windows.Forms.Keys.Shift;
                // If we are in auto mode, and if we are toggling or adding new controls, then cancel out the primary flag.
                if (fToggle || fAdd)
                {
                    fPrimary = false;
                }
            }

            // This flag is true if we changed selection and should therefore raise a selection change event.
            bool fChanged = false;
            // Handle the click case
            object requestedPrimary = null;
            int    primaryIndex;

            if (fPrimary && 1 == components.Count)
            {
                foreach (object o in components)
                {
                    requestedPrimary = o;
                    if (o == null)
                    {
                        throw new ArgumentNullException("components");
                    }
                    break;
                }
            }

            if (requestedPrimary != null && _selection != null && (primaryIndex = _selection.IndexOf(requestedPrimary)) != -1)
            {
                if (primaryIndex != 0)
                {
                    object tmp = _selection[0];
                    _selection[0]            = _selection[primaryIndex];
                    _selection[primaryIndex] = tmp;
                    fChanged = true;
                }
            }
            else
            {
                // 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 && !fAdd && !fRemove)
                {
                    if (_selection != null)
                    {
                        object[] selections = new object[_selection.Count];
                        _selection.CopyTo(selections, 0);
                        // Yucky and N^2, but even with several hundred components this should be fairly fast
                        foreach (object item in selections)
                        {
                            bool remove = true;
                            foreach (object comp in components)
                            {
                                if (comp == null)
                                {
                                    throw new ArgumentNullException("components");
                                }
                                if (object.ReferenceEquals(comp, item))
                                {
                                    remove = false;
                                    break;
                                }
                            }

                            if (remove)
                            {
                                RemoveSelection(item);
                                fChanged = true;
                            }
                        }
                    }
                }

                // Now select / toggle the components.
                foreach (object comp in components)
                {
                    if (comp == null)
                    {
                        throw new ArgumentNullException("components");
                    }

                    if (_selection != null && _selection.Contains(comp))
                    {
                        if (fToggle || fRemove)
                        {
                            RemoveSelection(comp);
                            fChanged = true;
                        }
                    }
                    else if (!fRemove)
                    {
                        AddSelection(comp);
                        fChanged = true;
                    }
                }
            }

            // Notify that our selection has changed
            if (fChanged)
            {
                //Set the SelectionInformation
                if (_selection.Count > 0)
                {
                    _statusCommandUI.SetStatusInformation(_selection[0] as Component);
                }
                else
                {
                    _statusCommandUI.SetStatusInformation(Rectangle.Empty);
                }
                OnSelectionChanged();
            }
        }