private void DeleteItem(FolderTreeViewItem item) { if (item.IsFolder) { if (Directory.Exists(item.Path)) { EditorFileUtility.DeleteToTrash(item.Path); } if (item.parent != null) { item.parent.children.Remove(item); } } else { if (File.Exists(item.Path)) { EditorFileUtility.DeleteToTrash(item.Path); } if (item.parent != null) { (item.parent as FolderTreeViewItem).FileList.Remove(item); } } }
private void CreateTxtFile(FolderTreeViewItem parentItem = null) { var dataContainer = m_FolderGridViewGroup.GetDataContainer(); var gridView = m_FolderGridViewGroup.GetGridView(); if (parentItem == null) { if (!m_TreeView.isSearching) { parentItem = m_TreeView.data.FindItem(m_TreeView.state.selectedIDs[0]) as FolderTreeViewItem; } else { parentItem = m_TreeView.data.root as FolderTreeViewItem; } } var folderItem = new FileGridItem(); folderItem.IsFolder = false; folderItem.ParentId = parentItem.id; folderItem.Id = dataContainer.GetAutoID(); var newPath = EditorFileUtility.GetNewFile(parentItem.Path, "New Txt", "txt"); folderItem.Path = newPath; var fi = new System.IO.FileInfo(newPath); File.Create(newPath).Close(); folderItem.DisplayName = Path.GetFileNameWithoutExtension(fi.Name); //添加到TreeView var child = new FolderTreeViewItem(); child.Path = fi.FullName.Replace("/", "\\");; child.IsFolder = false; child.id = folderItem.Id; child.displayName = folderItem.DisplayName; child.parent = parentItem; child.SetConfigSource(m_WindowConfigSource); if (parentItem.FileList == null) { parentItem.FileList = new List <FolderTreeViewItem>(); } parentItem.FileList.Add(child); m_FolderGridViewGroup.GetGridViewDataSource().ItemList.Add(folderItem); SortGridViewItem(); m_FolderGridViewGroup.IsCreatingItem = true; gridView.SetSelection(new[] { folderItem.Id }, false); gridView.BeginRename(0); }
private void CreateFolder(FolderTreeViewItem parentItem = null) { var dataContainer = m_FolderGridViewGroup.GetDataContainer(); var gridView = m_FolderGridViewGroup.GetGridView(); if (parentItem == null) { if (!m_TreeView.isSearching) { parentItem = m_TreeView.data.FindItem(m_TreeView.state.selectedIDs[0]) as FolderTreeViewItem; } else { parentItem = m_TreeView.data.root as FolderTreeViewItem; } } var folderItem = new FolderGridItem(); folderItem.IsFolder = true; folderItem.ParentId = parentItem.id; folderItem.Id = dataContainer.GetAutoID(); var newPath = EditorFileUtility.CreateNewFolder(parentItem.Path, "New Folder"); folderItem.Path = newPath; var di = new DirectoryInfo(newPath); folderItem.DisplayName = di.Name; //重命名完成后添加到TreeView中 m_FolderGridViewGroup.RenameEndWhenCreatingAction = null; m_FolderGridViewGroup.RenameEndWhenCreatingAction += () => { //添加到TreeView var child = new FolderTreeViewItem(); child.Path = di.FullName.Replace("/", "\\");; child.IsFolder = true; child.id = folderItem.Id; child.displayName = di.Name; child.parent = parentItem; child.SetConfigSource(m_WindowConfigSource); parentItem.AddChild(child); }; m_FolderGridViewGroup.GetGridViewDataSource().ItemList.Add(folderItem); SortGridViewItem(); m_FolderGridViewGroup.IsCreatingItem = true; gridView.SetSelection(new[] { folderItem.Id }, false); gridView.BeginRename(0); }
protected void GetNewRootPathContent(FolderTreeViewItem item) { DirectoryInfo dirInfo = new DirectoryInfo(item.Path); foreach (var di in dirInfo.GetDirectories()) { if ((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } var child = new FolderTreeViewItem(); child.Path = di.FullName.Replace("/", "\\");; child.IsFolder = true; child.id = GetAutoID(); child.displayName = di.Name; child.parent = item; child.SetConfigSource(ConfigSource); item.AddChild(child); GetNewRootPathContent(child); } foreach (var fi in dirInfo.GetFiles()) { if ((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } foreach (var extName in ExtNames) { if (extName.ToLower() == fi.Extension.ToLower()) { var child = new FolderTreeViewItem(); child.Path = fi.FullName.Replace("/", "\\");; child.IsFolder = false; child.id = GetAutoID(); child.displayName = Path.GetFileNameWithoutExtension(fi.Name); child.parent = item; child.SetConfigSource(ConfigSource); if (item.FileList == null) { item.FileList = new List <FolderTreeViewItem>(); } item.FileList.Add(child); break; } } } }
public void UpdateValidItems() { if (RootItem != null) { if (!Directory.Exists(RootItem.Path)) { RootItem = null; autoId = 0; } else { var change = false; UpdateItemInfo(RootItem, ref change); if (change && UpdateItemChangedAction != null) { UpdateItemChangedAction(); } } } }
private void GetSearchItems(string searchText, FolderTreeViewItem item, List <GridItem> itemList) { if (item != m_DataContainer.RootItem) { if (item.displayName.ToLower().Contains(searchText)) { var gridItem = new FolderGridItem(); gridItem.Id = item.id; gridItem.DisplayName = item.displayName; gridItem.Path = item.Path; gridItem.ParentId = item.parent.id; gridItem.IsFolder = true; itemList.Add(gridItem); } } if (item.hasChildren) { foreach (var child in item.children) { GetSearchItems(searchText, child as FolderTreeViewItem, itemList); } } if (item.FileList != null) { foreach (var child in item.FileList) { if (child.displayName.ToLower().Contains(searchText)) { var gridItem = new FileGridItem(); gridItem.Id = child.id; gridItem.DisplayName = child.displayName; gridItem.Path = child.Path; gridItem.ParentId = item.id; gridItem.IsFolder = false; itemList.Add(gridItem); } } } }
public void Reparent(FolderTreeViewItem parent) { if (IsFolder) { DirectoryInfo di = new DirectoryInfo(Path); var targetPath = System.IO.Path.Combine(parent.Path, displayName).Replace("/", "\\"); if (targetPath != Path) { di.MoveTo(targetPath); Path = targetPath; ReparentChildren(); } } else { System.IO.FileInfo fi = new System.IO.FileInfo(Path); var targetPath = System.IO.Path.Combine(parent.Path, System.IO.Path.GetFileName(Path)).Replace("/", "\\"); if (targetPath != Path) { fi.MoveTo(targetPath); Path = targetPath; } } }
private void DuplicateItemGridView() { var selectedIdList = m_GridView.GetSelection(); if (selectedIdList.Length == 0) { return; } List <int> idList = new List <int>(); try { m_TreeView.EndNameEditing(true); m_GridView.EndRename(false); foreach (var id in selectedIdList) { var item = m_GridViewDataSource.GetItemByIndex(m_GridViewDataSource.GetItemIndexByItemId(id)) as FolderGridItem; FolderTreeViewItem treeItem = null; FolderTreeViewItem parentTreeItem = null; if (item.IsFolder) { treeItem = m_TreeView.FindItem(item.Id) as FolderTreeViewItem; parentTreeItem = treeItem.parent as FolderTreeViewItem; } else { parentTreeItem = m_TreeView.FindItem(item.ParentId) as FolderTreeViewItem; foreach (var child in parentTreeItem.FileList) { if (child.id == item.Id) { treeItem = child; break; } } } if (item.IsFolder) { var newPath = EditorFileUtility.GetNewFolder(item.Path); FileUtil.CopyFileOrDirectory(item.Path, newPath); var newItem = JsonReader.Deserialize(JsonWriter.Serialize(treeItem, new JsonWriterSettings() { MaxDepth = Int32.MaxValue }), true) as FolderTreeViewItem; newItem.id = m_FolderTreeViewGroup.GetDataContainer().GetAutoID(); newItem.FileList = null; newItem.children = null; idList.Add(newItem.id); newItem.Path = newPath; newItem.displayName = new DirectoryInfo(newPath).Name; var newGridItem = JsonReader.Deserialize(JsonWriter.Serialize(item, new JsonWriterSettings() { MaxDepth = Int32.MaxValue }), item.GetType(), true) as FolderGridItem; newGridItem.Id = newItem.id; newGridItem.Path = newItem.Path; newGridItem.DisplayName = newItem.displayName; m_GridViewDataSource.ItemList.Add(newGridItem); parentTreeItem.AddChild(newItem); var comparator = new AlphanumComparator.AlphanumComparator(); parentTreeItem.children.Sort((viewItem, treeViewItem) => { return(comparator.Compare(viewItem.displayName, treeViewItem.displayName)); }); } else { var newPath = EditorFileUtility.GetNewFile(item.Path); FileUtil.CopyFileOrDirectory(item.Path, newPath); var newItem = JsonReader.Deserialize(JsonWriter.Serialize(treeItem, new JsonWriterSettings() { MaxDepth = Int32.MaxValue }), true) as FolderTreeViewItem; newItem.id = m_FolderTreeViewGroup.GetDataContainer().GetAutoID(); idList.Add(newItem.id); newItem.Path = newPath; newItem.displayName = Path.GetFileNameWithoutExtension(newPath); parentTreeItem.FileList.Add(newItem); var newGridItem = JsonReader.Deserialize(JsonWriter.Serialize(item, new JsonWriterSettings() { MaxDepth = Int32.MaxValue }), item.GetType(), true) as FolderGridItem; newGridItem.Id = newItem.id; newGridItem.Path = newItem.Path; newGridItem.DisplayName = newItem.displayName; m_GridViewDataSource.ItemList.Add(newGridItem); } } m_GridView.SetSelection(idList.ToArray(), false); } catch (Exception e) { Debug.LogError("复制item出错:" + e); } finally { m_FolderTreeViewGroup.GetDataContainer().UpdateValidItems(); m_TreeView.data.RefreshData(); UpdateGridViewContent(); if (m_ConfigSource != null) { m_ConfigSource.SetConfigDirty(); } } }
protected void UpdateItemInfo(FolderTreeViewItem item, ref bool change) { if (item.hasChildren) { for (int i = item.children.Count - 1; i >= 0; --i) { var child = item.children[i] as FolderTreeViewItem; if (child == null || !Directory.Exists(child.Path) || !child.Path.StartsWith(item.Path)) { change = true; item.children.RemoveAt(i); } } } if (item.FileList != null) { for (int i = item.FileList.Count - 1; i >= 0; --i) { var child = item.FileList[i]; if (!File.Exists(child.Path)) { change = true; item.FileList.RemoveAt(i); } } } DirectoryInfo dirInfo = new DirectoryInfo(item.Path); foreach (var di in dirInfo.GetDirectories()) { if ((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } if (item.hasChildren) { bool exist = false; foreach (var childItem in item.children) { var folderItem = childItem as FolderTreeViewItem; if (folderItem != null && folderItem.Path == di.FullName) { exist = true; break; } } if (exist) { continue; } } var child = new FolderTreeViewItem(); child.Path = di.FullName.Replace("/", "\\");; child.IsFolder = true; child.id = GetAutoID(); child.displayName = di.Name; child.parent = item; child.SetConfigSource(ConfigSource); item.AddChild(child); change = true; } foreach (var fi in dirInfo.GetFiles()) { if ((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } foreach (var extName in ExtNames) { if (extName.ToLower() == fi.Extension.ToLower()) { if (item.FileList != null) { bool exist = false; foreach (var childItem in item.FileList) { var fileItem = childItem as FolderTreeViewItem; if (fileItem != null && fileItem.Path == fi.FullName) { exist = true; break; } } if (exist) { break; } } var child = new FolderTreeViewItem(); child.Path = fi.FullName.Replace("/", "\\");; child.IsFolder = false; child.id = GetAutoID(); child.displayName = Path.GetFileNameWithoutExtension(fi.Name); child.parent = item; child.SetConfigSource(ConfigSource); if (item.FileList == null) { item.FileList = new List <FolderTreeViewItem>(); } item.FileList.Add(child); change = true; break; } } } if (item.hasChildren) { var comparator = new AlphanumComparator.AlphanumComparator(); item.children.Sort((viewItem, treeViewItem) => { return(comparator.Compare(viewItem.displayName, treeViewItem.displayName)); }); } if (item.hasChildren) { foreach (var child in item.children) { UpdateItemInfo(child as FolderTreeViewItem, ref change); } } }