FolderBrowserModel.cs :  » GUI » TreeViewAdv » SampleApp » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » TreeViewAdv 
TreeViewAdv » SampleApp » FolderBrowserModel.cs
#pragma warning disable 67  // Event never used

using System;
using System.Collections.Generic;


using Aga.Controls.Tree;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Threading;

namespace SampleApp{
  public class FolderBrowserModel: ITreeModel
  {
    private BackgroundWorker _worker;
    private List<BaseItem> _itemsToRead;
    private Dictionary<string, List<BaseItem>> _cache = new Dictionary<string, List<BaseItem>>();

    public FolderBrowserModel()
    {
      _itemsToRead = new List<BaseItem>();

      _worker = new BackgroundWorker();
      _worker.WorkerReportsProgress = true;
      _worker.DoWork += new DoWorkEventHandler(ReadFilesProperties);
      _worker.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
    }

    void ReadFilesProperties(object sender, DoWorkEventArgs e)
    {
      while(_itemsToRead.Count > 0)
      {
        BaseItem item = _itemsToRead[0];
        _itemsToRead.RemoveAt(0);

        Thread.Sleep(50); //emulate time consuming operation
        if (item is FolderItem)
        {
          DirectoryInfo info = new DirectoryInfo(item.ItemPath);
          item.Date = info.CreationTime;
        }
        else if (item is FileItem)
        {
          FileInfo info = new FileInfo(item.ItemPath);
          item.Size = info.Length;
          item.Date = info.CreationTime;
          if (info.Extension.ToLower() == ".ico")
          {
            Icon icon = new Icon(item.ItemPath);
            item.Icon = icon.ToBitmap();
          }
          else if (info.Extension.ToLower() == ".bmp")
          {
            item.Icon = new Bitmap(item.ItemPath);
          }
        }
        _worker.ReportProgress(0, item);
      }
    }

    void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
      OnNodesChanged(e.UserState as BaseItem);
    }

    private TreePath GetPath(BaseItem item)
    {
      if (item == null)
        return TreePath.Empty;
      else
      {
        Stack<object> stack = new Stack<object>();
        while (item != null)
        {
          stack.Push(item);
          item = item.Parent;
        }
        return new TreePath(stack.ToArray());
      }
    }

    public System.Collections.IEnumerable GetChildren(TreePath treePath)
    {
      List<BaseItem> items = null;
      if (treePath.IsEmpty())
      {
        if (_cache.ContainsKey("ROOT"))
          items = _cache["ROOT"];
        else
        {
          items = new List<BaseItem>();
          _cache.Add("ROOT", items);
          foreach (string str in Environment.GetLogicalDrives())
            items.Add(new RootItem(str, this));
        }
      }
      else
      {
        BaseItem parent = treePath.LastNode as BaseItem;
        if (parent != null)
        {
          if (_cache.ContainsKey(parent.ItemPath))
            items = _cache[parent.ItemPath];
          else
          {
            items = new List<BaseItem>();
            try
            {
              foreach (string str in Directory.GetDirectories(parent.ItemPath))
                items.Add(new FolderItem(str, parent, this));
              foreach (string str in Directory.GetFiles(parent.ItemPath))
              {
                FileItem item = new FileItem(str, parent, this);
                items.Add(item);
              }
            }
            catch (IOException)
            {
              return null;
            }
            _cache.Add(parent.ItemPath, items);
            _itemsToRead.AddRange(items);
            if (!_worker.IsBusy)
              _worker.RunWorkerAsync();
          }
        }
      }
      return items;
    }

    public bool IsLeaf(TreePath treePath)
    {
      return treePath.LastNode is FileItem;
    }

    public event EventHandler<TreeModelEventArgs> NodesChanged;
    internal void OnNodesChanged(BaseItem item)
    {
      if (NodesChanged != null)
      {
        TreePath path = GetPath(item.Parent);
        NodesChanged(this, new TreeModelEventArgs(path, new object[] { item }));
      }
    }

    public event EventHandler<TreeModelEventArgs> NodesInserted;
    public event EventHandler<TreeModelEventArgs> NodesRemoved;
    public event EventHandler<TreePathEventArgs> StructureChanged;
    public void OnStructureChanged()
    {
      if (StructureChanged != null)
        StructureChanged(this, new TreePathEventArgs());
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.