Changed from object to generic <T> for TreeView

This commit is contained in:
tznind
2020-12-16 12:04:46 +00:00
parent 5014d8ce61
commit dc184ba469
3 changed files with 194 additions and 139 deletions

View File

@@ -14,7 +14,20 @@ namespace UICatalog.Scenarios {
[ScenarioCategory ("TopLevel")]
class TreeViewFileSystem : Scenario {
TreeView _treeView;
/// <summary>
/// A tree view where nodes are files and folders
/// </summary>
TreeView<FileSystemInfo> _treeViewFiles;
/// <summary>
/// A tree view where nodes are <see cref="ITreeNode"/>
/// </summary>
TreeView _treeViewNodes;
/// <summary>
/// Currently showing tree view (either <see cref="_treeViewFiles"/> or <see cref="_treeViewNodes"/>)
/// </summary>
ITreeView _treeView;
public override void Setup ()
{
@@ -35,22 +48,39 @@ namespace UICatalog.Scenarios {
Top.Add (menu);
var statusBar = new StatusBar (new StatusItem [] {
new StatusItem(Key.F2, "~F2~ Add Root Drives", () => AddRootDrives()),
new StatusItem(Key.F3, "~F3~ Remove Root Object", () => RemoveRoot()),
new StatusItem(Key.F4, "~F4~ Clear Objects", () => ClearObjects()),
new StatusItem(Key.F5, "~F5~ Simple Tree", () => AddSimpleTree()),
new StatusItem(Key.F2, "~F2~ File Tree", () => SwitchToFileTree()),
new StatusItem(Key.F3, "~F3~ Clear Objects", () => ClearObjects()),
new StatusItem(Key.F4, "~F4~ Simple Tree", () => SwitchToSimpleTree()),
new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
});
Top.Add (statusBar);
_treeView = new TreeView () {
_treeViewFiles = new TreeView<FileSystemInfo> () {
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
};
// setup delegates
_treeViewFiles.TreeBuilder = new DelegateTreeBuilder<FileSystemInfo>(
// Determines how to compute children of any given branch
GetChildren,
// As a shortcut to enumerating half the file system, tell tree that all directories are expandable (even if they turn out to be empty later on)
(o)=>o is DirectoryInfo
);
// Determines how to represent objects as strings on the screen
_treeViewFiles.AspectGetter = FileSystemAspectGetter;
_treeViewNodes = new TreeView() {
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
};
string root = System.IO.Path.GetPathRoot(Environment.CurrentDirectory);
if(root == null)
@@ -58,9 +88,6 @@ namespace UICatalog.Scenarios {
MessageBox.ErrorQuery(10,5,"Error","Unable to determine file system root","ok");
return;
}
Win.Add (_treeView);
}
@@ -75,34 +102,19 @@ namespace UICatalog.Scenarios {
_treeView.ShowExpandableSymbol = !_treeView.ShowExpandableSymbol;
_treeView.SetNeedsDisplay();
}
/// <summary>
/// Sets up children getter delegates that return subfolders/files from directories
/// </summary>
private void SetupFileSystemDelegates ()
private void SwitchToSimpleTree ()
{
_treeView.TreeBuilder = new DelegateTreeBuilder(
Win.Remove (_treeViewFiles);
Win.Add(_treeViewNodes);
_treeView = _treeViewNodes;
// Determines how to compute children of any given branch
GetChildren,
// As a shortcut to enumerating half the file system, tell tree that all directories are expandable (even if they turn out to be empty later on)
(o)=>o is DirectoryInfo
);
// Determines how to represent objects as strings on the screen
_treeView.AspectGetter = AspectGetter;
}
private void AddSimpleTree ()
{
ClearObjects();
// Set builder to serve children of ITreeNode objects
_treeView.TreeBuilder = new TreeNodeBuilder();
// Add 2 root nodes with simple set of subfolders
_treeView.AddObject(CreateSimpleRoot());
_treeView.AddObject(CreateSimpleRoot());
_treeViewNodes.AddObject(CreateSimpleRoot());
_treeViewNodes.AddObject(CreateSimpleRoot());
}
private ITreeNode CreateSimpleRoot ()
@@ -168,24 +180,21 @@ namespace UICatalog.Scenarios {
private void ClearObjects()
{
_treeView.ClearObjects();
_treeView?.ClearObjects();
}
private void AddRootDrives()
private void SwitchToFileTree()
{
SetupFileSystemDelegates();
// switch trees
Win.Remove(_treeViewNodes);
Win.Add (_treeViewFiles);
_treeView = _treeViewFiles;
_treeView.AddObjects(DriveInfo.GetDrives().Select(d=>d.RootDirectory));
}
private void RemoveRoot()
{
if(_treeView.SelectedObject == null)
MessageBox.ErrorQuery(10,5,"Error","No object selected","ok");
else {
_treeView.Remove(_treeView.SelectedObject);
}
ClearObjects();
_treeViewFiles.AddObjects(DriveInfo.GetDrives().Select(d=>d.RootDirectory));
}
private IEnumerable<object> GetChildren(object model)
private IEnumerable<FileSystemInfo> GetChildren(FileSystemInfo model)
{
// If it is a directory it's children are all contained files and dirs
if(model is DirectoryInfo d) {
@@ -195,14 +204,16 @@ namespace UICatalog.Scenarios {
.OrderBy(a=>a is DirectoryInfo ? 0:1)
.ThenBy(b=>b.Name);
}
catch(SystemException ex) {
return new []{ex};
catch(SystemException) {
// Access violation or other error getting the file list for directory
return Enumerable.Empty<FileSystemInfo>();
}
}
return new object[0];
return Enumerable.Empty<FileSystemInfo>();;
}
private string AspectGetter(object model)
private string FileSystemAspectGetter(FileSystemInfo model)
{
if(model is DirectoryInfo d)
return d.Name;