예제 #1
0
        private void CreateFolderButtonClick(object sender, EventArgs e)
        {
            using (var pb = new FavoritesFolderForm(string.Empty, string.Empty, true))
                if (pb.ShowDialog(this) == DialogResult.OK)
                {
                    var activeFolder = _folderView.ActiveNode as FavoritesFolder;
                    if (activeFolder == null || pb.CreateAsRoot)
                    {
                        activeFolder = _favManager.RootFolder;
                    }

                    // TODO: Действия в AddFolder и Reload, приводят к тому,
                    // что добавление первой папки вызывает искючение при установке свойства Nodes дерева.
                    _favManager.AddFolder(pb.FolderName, pb.FolderComment, activeFolder);
                    _favManager.Reload();

                    var folders = _favManager.RootFolder;
                    folders.ShowLinks = false;

                    _folderView.VirtualListSize = folders.Count;
                    _folderView.Nodes           = folders;
                }
        }
예제 #2
0
        private void GridDragDrop(object sender, DragEventArgs e)
        {
            var effect = DragDropEffects.None;

            if (sender == _grid)
            {
                int dummy;
                var point = _grid.PointToClient(new Point(e.X, e.Y));

                ITreeNode activeNode;
                _grid.GetNodeAndCellIndexAt(point.X, point.Y, out activeNode, out dummy);
                if (activeNode == null)
                {
                    activeNode = _favManager.RootFolder;
                }

                var activeEntry = activeNode as IFavoritesEntry;
                if (activeEntry != null)
                {
                    var activeFolder = activeEntry.IsContainer ? (FavoritesFolder)activeEntry : (FavoritesFolder)activeEntry.Parent;
                    if (e.Data.GetDataPresent(typeof(TreeNodeBag)))
                    {
                        var dropNodes = (TreeNodeBag)e.Data.GetData(typeof(TreeNodeBag));
                        if (dropNodes != null && dropNodes.Count > 0)
                        {
                            var select = LastActiveNode ?? _grid.ActiveNode;
                            if (dropNodes.IsAssignableFrom(typeof(IFavoritesEntry)))
                            {
                                var copy        = DragDropHelper.GetDropEffect(e.KeyState, true) == DragDropEffects.Copy;
                                var failedItems = new List <IFavoritesEntry>();
                                ProgressWorker.Run(_serviceManager, false,
                                                   pi =>
                                {
                                    pi.SetProgressText(SR.Favorites.MovingItems);
                                    // При одновременном перемещении контейнера и его элементов,
                                    // элементы оказываются не в "своём" контейнере, а в том, где отпустили мышь.
                                    // Так и задумано
                                    foreach (IFavoritesEntry entry in dropNodes)
                                    {
                                        if (copy)
                                        {
                                            Debug.Assert(entry is FavoritesLink, "entry is FavoritesLink");
                                            var link = (FavoritesLink)entry;
                                            if (!_favManager.AddMessageLink(link.MessageId, link.Comment, activeFolder))
                                            {
                                                failedItems.Add(entry);
                                            }
                                        }
                                        else if (!entry.Move(activeFolder))
                                        {
                                            failedItems.Add(entry);
                                        }
                                    }
                                },
                                                   () =>
                                {
                                    _favManager.Reload();

                                    if (failedItems.Count > 0)
                                    {
                                        var failedMessages = new StringBuilder();
                                        failedMessages.Append(Environment.NewLine);
                                        foreach (var entry in failedItems)
                                        {
                                            var itemText = string.IsNullOrEmpty(entry.Comment)
                                                                                                        ? (entry is FavoritesLink
                                                                                                                ? ((FavoritesLink)entry).Link
                                                                                                                : ((FavoritesFolder)entry).Name)
                                                                                                        : entry.Comment;
                                            failedMessages.AppendFormat(" \"{0}\"{1}", itemText, Environment.NewLine);
                                        }
                                        var message = String.Format(SR.Favorites.ItemsExists, failedMessages, activeFolder.Name);
                                        MessageBox.Show(this, message,
                                                        FavoritesFeature.Instance.Info,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    }

                                    effect = (dropNodes.Count > failedItems.Count) ? (copy ? DragDropEffects.Copy : DragDropEffects.Move) : DragDropEffects.None;
                                });
                            }
                            else if (dropNodes.IsAssignableFrom(typeof(IMsg)))
                            {
                                var failedItems =
                                    dropNodes
                                    .Cast <IMsg>()
                                    .Where(
                                        message =>
                                        !_favManager.AddMessageLink(message.ID, message.Subject, activeFolder))
                                    .ToList();

                                if (failedItems.Count > 0)
                                {
                                    var failedMessages = new StringBuilder();
                                    failedMessages.Append(Environment.NewLine);
                                    foreach (var message in failedItems)
                                    {
                                        failedMessages.AppendFormat("{0} - \"{1}\"{2}",
                                                                    message.ID, message.Subject, Environment.NewLine);
                                    }
                                    var messageText = String.Format(SR.Favorites.ItemsExists, failedMessages, activeFolder.Name);
                                    MessageBox.Show(this, messageText,
                                                    FavoritesFeature.Instance.Info,
                                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }                                //if

                                effect = (dropNodes.Count > failedItems.Count) ? DragDropEffects.Link : DragDropEffects.None;
                            }                            //if

                            if (select != null)
                            {
                                _grid.EnsureVisible(select, false);
                                if (_grid.ActiveNode != select)
                                {
                                    _grid.ActiveNode = select;
                                }
                            } //if
                        }     //if
                    }         //if
                }             //if
            }                 //if

            e.Effect = effect;
        }