Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C#

CIMTool for Windows Management Instrumentation - Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
22 Feb 2013CPOL6 min read 37.3K   7.6K   31  
Use WMI to retrieve information about your system and genrate classes for easy WMI development.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace System.Windows.Controls.WpfPropertyGrid
{
  /// <summary>
  /// Special grid entry that provides information about property category and gives access to underlying properties.
  /// </summary>
  public class CategoryItem : GridEntry
  {    
    #region Properties

    /// <summary>
    /// Gets or sets the attribute the category was created with.
    /// </summary>
    /// <value>The attribute.</value>
    public Attribute Attribute { get; set; }
        
    #region IsExpanded
    private bool _isExpanded = true;
    /// <summary>
    /// Gets or sets a value indicating whether this category is expanded.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this category is expanded; otherwise, <c>false</c>.
    /// </value>
    public bool IsExpanded
    {
      get { return _isExpanded; }
      set
      {
        if (_isExpanded == value) return;
        _isExpanded = value;
        OnPropertyChanged("IsExpanded");        
      }
    } 
    #endregion

    #region Properties
    private readonly GridEntryCollection<PropertyItem> _properties = new GridEntryCollection<PropertyItem>();
    /// <summary>
    /// Get all the properties in the category.
    /// </summary>
    /// <value></value>
    /// <returns>
    /// An enumerable collection of all the properties in the category.
    /// </returns>
    public ReadOnlyObservableCollection<PropertyItem> Properties
    {
      get { return new ReadOnlyObservableCollection<PropertyItem>(_properties); }
    } 
    #endregion

    /// <summary>
    /// Gets the <see cref="WpfPropertyGrid.PropertyItem"/> with the specified property name.
    /// </summary>
    /// <value></value>
    public PropertyItem this[string propertyName]
    {
      get { return _properties[propertyName]; }
    }
        
    private IComparer<PropertyItem> _comparer = new PropertyItemComparer();
    /// <summary>
    /// Gets or sets the comparer used to sort properties.
    /// </summary>
    /// <value>The comparer. </value>
    public IComparer<PropertyItem> Comparer
    {
      get { return _comparer; }
      set
      {
        if (_comparer == value) return;
        _comparer = value;
        _properties.Sort(_comparer);
        OnPropertyChanged("Comparer");        
      }
    }
    
    private bool _hasVisibleProperties;
    /// <summary>
    /// Gets or sets a value indicating whether this instance has visible properties.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance has visible properties; otherwise, <c>false</c>.
    /// </value>
    public bool HasVisibleProperties
    {
      get { return _hasVisibleProperties; }
      private set
      {
        if (_hasVisibleProperties == value) return;
        _hasVisibleProperties = value;        
        OnPropertyChanged("HasVisibleProperties");
      }
    }
    #endregion

    #region ctor
    /// <summary>
    /// Initializes a new instance of the <see cref="CategoryItem"/> class.
    /// </summary>
    /// <param name="owner">The owner.</param>
    /// <param name="name">The name.</param>
    public CategoryItem(PropertyGrid owner, string name)
    {
      if (owner == null) throw new ArgumentNullException("owner");
      if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
      
      Owner = owner;
      Name = name;    
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="CategoryItem"/> class.
    /// </summary>
    /// <param name="owner">The owner.</param>
    /// <param name="category">The category.</param>
    public CategoryItem(PropertyGrid owner, CategoryAttribute category)
      : this(owner, category.Category)
    {
      Attribute = category;
    }

    #endregion

    static readonly Func<PropertyItem, bool> IsVisibleProperty = prop => prop.IsBrowsable && prop.MatchesFilter;

    /// <summary>
    /// Adds the property.
    /// </summary>
    /// <param name="property">The property.</param>
    public void AddProperty(PropertyItem property)
    {
      if (property == null) throw new ArgumentNullException("property");
      if (_properties.Contains(property)) throw new ArgumentException("Cannot add a duplicated property " + property.Name);
     
      int index = _properties.BinarySearch(property, _comparer);
      if (index < 0)
        index = ~index;

      _properties.Insert(index, property);

      if (property.IsBrowsable)
        HasVisibleProperties = true;
      else
        HasVisibleProperties = _properties.Any(IsVisibleProperty);

      property.BrowsableChanged += PropertyBrowsableChanged;
    }
        
    private void PropertyBrowsableChanged(object sender, EventArgs e)
    {
      HasVisibleProperties = _properties.Any(IsVisibleProperty);
    }

    /// <summary>
    /// Checks whether the entry matches the filtering predicate.
    /// </summary>
    /// <param name="predicate">The filtering predicate.</param>
    /// <returns>
    /// 	<c>true</c> if entry matches predicate; otherwise, <c>false</c>.
    /// </returns>   
    public override bool MatchesPredicate(PropertyFilterPredicate predicate)
    {
      return _properties.All(property => property.MatchesPredicate(predicate));      
    }

    /// <summary>
    /// Applies the filter for the entry.
    /// </summary>
    /// <param name="filter">The filter.</param>
    public override void ApplyFilter(PropertyFilter filter)
    {
      bool propertiesMatch = false;
      foreach (var entry in Properties)
      {
        if (PropertyMatchesFilter(filter, entry))
          propertiesMatch = true;
      }

      MatchesFilter = propertiesMatch;

      HasVisibleProperties = _properties.Any(IsVisibleProperty);

      if (propertiesMatch && !IsExpanded)
        IsExpanded = true;

      OnFilterApplied(filter);      
    }
               
    private static bool PropertyMatchesFilter(PropertyFilter filter, PropertyItem entry)
    {
      entry.ApplyFilter(filter);
      return entry.MatchesFilter;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance AS.

Specializing in integrated operations and high performance computing solutions.

I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.

Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...

A few Highlights:

  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI


More information about what I do for a living can be found at: harlinn.com or LinkedIn

You can contact me at espen@harlinn.no

Comments and Discussions