Пример #1
0
        /// <summary>
        /// Determines if a folder can be renamed.
        /// </summary>
        /// <param name="oldFolderName">Name of an existing folder.</param>
        /// <param name="newFolderName">New name of the folder.</param>
        /// <returns>True if the folder can be renamed, i.e. if none of the new item names conflicts with an existing item name.</returns>
        public bool CanRenameFolder(string oldFolderName, string newFolderName)
        {
            ProjectFolder.ThrowExceptionOnInvalidFullFolderPath(oldFolderName);
            ProjectFolder.ThrowExceptionOnInvalidFullFolderPath(newFolderName);

            var oldList     = GetItemsInFolderAndSubfolders(oldFolderName);
            var itemHashSet = new HashSet <object>(oldList);

            int oldFolderNameLength = oldFolderName == null ? 0 : oldFolderName.Length;

            foreach (var item in oldList)
            {
                if (item is IProjectItem projItem)
                {
                    string oldName = projItem.Name;
                    string newName = (newFolderName ?? string.Empty) + oldName.Substring(oldFolderNameLength);

                    var coll = AltaxoDocument.GetCollectionForProjectItemType(projItem.GetType());
                    if (coll.ContainsAnyName(newName) && !itemHashSet.Contains(coll[newName]))
                    {
                        return(false);
                    }
                }
                else
                {
                    throw new NotImplementedException("Unknown item type encountered: " + item.GetType().ToString());
                }
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Move items in a list to another folder.
        /// </summary>
        /// <param name="list">List of items to move. Momentarily the item types <see cref="Altaxo.Data.DataTable"/>, <see cref="Altaxo.Graph.Gdi.GraphDocument"/> and <see cref="ProjectFolder"/></param> are supported.
        /// <param name="newFolderName">Name of the folder where to move the items into.</param>
        public void MoveItemsToFolder(IList <object> list, string newFolderName)
        {
            ProjectFolder.ThrowExceptionOnInvalidFullFolderPath(newFolderName);

            foreach (object item in list)
            {
                if (item is ProjectFolder projFolder)
                {
                    string moveToFolder = ProjectFolder.Combine(newFolderName, Main.ProjectFolder.GetFoldersLastFolderPart(projFolder.Name));
                    RenameFolder(projFolder.Name, moveToFolder);
                }
                else if (item is Altaxo.Main.Properties.ProjectFolderPropertyDocument propDoc)
                {
                    string newName = Main.ProjectFolder.Combine(newFolderName, Main.ProjectFolder.GetNamePart(propDoc.Name));
                    if (AltaxoDocument.ProjectFolderProperties.Contains(newName))
                    {
                        // Project folders are unique for the specific folder, we can not simply rename it to another name
                        // Thus I decided here to merge the moved property bag with the already existing property bag
                        var existingDoc = AltaxoDocument.ProjectFolderProperties[newName];
                        existingDoc.PropertyBagNotNull.MergePropertiesFrom(propDoc.PropertyBagNotNull, true);
                    }
                    else
                    {
                        propDoc.Name = newName;
                    }
                }
                else if (item is IProjectItem projItem)
                {
                    var coll    = AltaxoDocument.GetCollectionForProjectItemType(item.GetType());
                    var newName = Main.ProjectFolder.Combine(newFolderName, Main.ProjectFolder.GetNamePart(projItem.Name));
                    if (coll.ContainsAnyName(newName))
                    {
                        newName = coll.FindNewItemName(newName);
                    }
                    projItem.Name = newName;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Move the provided items from the old folder to a new folder. This is done by renaming all items in the given list by
        /// starting with the new folder name.
        /// </summary>
        /// <param name="oldFolderName">Name of the existing folder.</param>
        /// <param name="newFolderName">New name of the folder.</param>
        /// <param name="items">List of items that should be renamed.</param>
        public void MoveItemsToNewFolder(string oldFolderName, string newFolderName, IEnumerable <IProjectItem> items)
        {
            ProjectFolder.ThrowExceptionOnInvalidFullFolderPath(oldFolderName);
            ProjectFolder.ThrowExceptionOnInvalidFullFolderPath(newFolderName);

            // The algorithm tries to continuously rename items that could be renamed
            // If no more items could be renamed, the rest of the items is renamed with a unique generated name using the new name as basis

            // Suspend all items
            var suspendTokensOfProjectItems = items.Select(item => item.SuspendGetToken()).ToArray();

            var itemList = new List <IProjectItem>(items);

            SortItemsByDependencies(itemList); // items that have no dependencies should now be first in the list (thus also the first to be renamed)

            // first, we rename all items to unique names

            var oldNameDictionary = new Dictionary <IProjectItem, string>(); // stores the old names of the items

            foreach (var item in itemList)
            {
                oldNameDictionary[item] = item.Name;
                item.Name = Guid.NewGuid().ToString() + "\\"; // this name should be new, the backslash is to allow also folder property documents to be renamed.
            }

            int oldFolderNameLength = oldFolderName == null ? 0 : oldFolderName.Length;
            var itemsRenamed        = new HashSet <IProjectItem>();

            foreach (var item in itemList)
            {
                string oldName = oldNameDictionary[item];
                string newName = (newFolderName == null ? "" : newFolderName) + oldName.Substring(oldFolderNameLength);

                if (item is Main.Properties.ProjectFolderPropertyDocument propDoc)
                {
                    var coll = AltaxoDocument.ProjectFolderProperties;
                    if (!coll.ContainsAnyName(newName))
                    {
                        item.Name = newName;
                    }
                    else // we integrate the properties in the other properties
                    {
                        var oldProps     = coll[newName].PropertyBagNotNull;
                        var propsToMerge = propDoc.PropertyBagNotNull;
                        oldProps.MergePropertiesFrom(propsToMerge, false);
                    }
                }
                else // normal case
                {
                    var coll = AltaxoDocument.GetCollectionForProjectItemType(item.GetType());
                    if (!coll.ContainsAnyName(newName))
                    {
                        item.Name = newName;
                    }
                    else
                    {
                        item.Name = coll.FindNewItemName(newName);
                    }
                }
            } // end foreach item

            // Resume all items
            suspendTokensOfProjectItems.ForEachDo(token => token.Dispose());
        }