1 using System;
  2 using System.Collections;
  3 using System.IO;
  4 using System.Windows;
  5 
  6 namespace FileExplorer
  7 {
  8     public class DirectoryNode : Node
  9     {
 10         public nothrow DirectoryNode(const string& name_) : base(name_)isRootDirectory(false)
 11         {
 12         }
 13         public override nothrow bool IsRootDirectory() const
 14         {
 15             return isRootDirectory;
 16         }
 17         public nothrow void SetRootDirectory()
 18         {
 19             isRootDirectory = true;
 20         }
 21         public nothrow void AddDirectory(DirectoryNode* directory)
 22         {
 23             directory->SetParent(this);
 24             directories.Add(UniquePtr<DirectoryNode>(directory));
 25         }
 26         public nothrow void AddFile(FileNode* file)
 27         {
 28             file->SetParent(this);
 29             files.Add(UniquePtr<FileNode>(file));
 30         }
 31         public override nothrow string ImageName() const
 32         {
 33             return "folder_closed.bitmap";
 34         }
 35         public override nothrow string ExpandedImageName() const
 36         {
 37             return "folder_opened.bitmap";
 38         }
 39         public override nothrow TreeViewNode* ToTreeViewNode(bool createChilrenImageList* imageList)
 40         {
 41             UniquePtr<TreeViewNode> treeViewNodePtr(new TreeViewNode(Name()));
 42             TreeViewNode* node = treeViewNodePtr.Get();
 43             SetTreeViewNode(node);
 44             if (imageList != null)
 45             {
 46                 int imageIndex = imageList->GetImageIndex(ImageName());
 47                 if (imageIndex != -1)
 48                 {
 49                     node->SetImageIndex(imageIndex);
 50                 }
 51                 int expandedImageIndex = imageList->GetImageIndex(ExpandedImageName());
 52                 if (expandedImageIndex != -1)
 53                 {
 54                     node->SetExpandedImageIndex(expandedImageIndex);
 55                 }
 56             }
 57             return treeViewNodePtr.Release();
 58         }
 59         public override Control* CreateView(ImageList* imageList)
 60         {
 61             Clear();
 62             string directoryPath = DirectoryPath();
 63             List<string> directoryNames = Directory.GetDirectories(directoryPath);
 64             Sort(directoryNames.Begin()directoryNames.End());
 65             for (const string& directoryName : directoryNames)
 66             {
 67                 AddDirectory(new DirectoryNode(Path.GetFileName(directoryName)));
 68             }
 69             List<string> fileNames = Directory.GetFiles(directoryPath);
 70             Sort(fileNames.Begin()fileNames.End());
 71             for (const string& fileName : fileNames)
 72             {
 73                 AddFile(new FileNode(Path.GetFileName(fileName)));
 74             }
 75             ListView* listView = new ListView(ListViewCreateParams(ListViewControlCreateParams(ControlCreateParams().Defaults())).Defaults());
 76             MainWindow* mainWindow = GetMainWindow();
 77             if (mainWindow != null)
 78             {
 79                 mainWindow->AddListViewEventHandlers(listView);
 80             }
 81             listView->SetData(this);
 82             listView->SetDoubleBuffered();
 83             listView->SetImageList(imageList);
 84             listView->AddColumn("Name"400);
 85             TreeViewNode* treeViewNode = GetTreeViewNode();
 86             if (treeViewNode == null)
 87             {
 88                 Node* parent = Parent();
 89                 if (parent != null)
 90                 {
 91                     treeViewNode = parent->GetTreeViewNode();
 92                 }
 93             }
 94             if (treeViewNode != null)
 95             {
 96                 treeViewNode->RemoveChildren();
 97             }
 98             for (const UniquePtr<DirectoryNode>& directory : directories)
 99             {
100                 ListViewItem* directoryItem = listView->AddItem();
101                 directory->SetData(directoryItemimageList);
102                 if (treeViewNode != null)
103                 {
104                     treeViewNode->AddChild(directory->ToTreeViewNode(falseimageList));
105                 }
106             }
107             for (const UniquePtr<FileNode>& file : files)
108             {
109                 ListViewItem* fileItem = listView->AddItem();
110                 file->SetData(fileItemimageList);
111             }
112             if (treeViewNode != null)
113             {
114                 treeViewNode->Select();
115                 TreeView* treeView = treeViewNode->GetTreeView();
116                 if (treeView != null)
117                 {
118                     treeView->Invalidate();
119                 }
120             }
121             return listView;
122         }
123         protected override nothrow string DirectoryPath()
124         {
125             string parentDirectoryPath;
126             Node* p = Parent();
127             if (p != null)
128             {
129                 parentDirectoryPath = p->DirectoryPath();
130             }
131             if (!parentDirectoryPath.IsEmpty())
132             {
133                 return Path.Combine(parentDirectoryPathName());
134             }
135             else
136             {
137                 return Name();
138             }
139         }
140         private void Clear()
141         {
142             directories.Clear();
143             files.Clear();
144         }
145         private bool isRootDirectory;
146         private List<UniquePtr<DirectoryNode>> directories;
147         private List<UniquePtr<FileNode>> files;
148     }
149 }