Click here to Skip to main content
15,891,423 members
Articles / Programming Languages / C#

AidaNet : Network resources inventory

Rate me:
Please Sign up or sign in to vote.
4.13/5 (8 votes)
7 Mar 20044 min read 60.9K   1.2K   23  
XPath script to analyze multiple XML files.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
using System.Xml ; // XmlTextReader 
using System.Xml.XPath ;
using System.Threading ;
using ListViewHelper;     // a sort by column ListView
                
// Aida XML result                 
// computer name       : Report/Page[Title="Report"]/Item[Title="Computer"]/Value
// Operating System    : Report/Page[Title="Report"]/Item[Title="Operating System"]/Value
// All installed prog  : //Report/Page[Title="Installed Programs"]//Device

namespace AidaNet
{

	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{

      // applications variables
      // -----------------------------------------------------------------------------

      string Folder ;
      public ListViewSorter filterSorter ;
      public ListViewSorter fileSorter ;
      
      // -----------------------------------------------------------------------------

      private System.Windows.Forms.ColumnHeader columnHeader1;
      private System.Windows.Forms.ColumnHeader columnHeader2;
      private System.Windows.Forms.ColumnHeader columnHeader3;
      private System.Windows.Forms.ImageList imageList1;
      private System.Windows.Forms.MainMenu mainMenu1;
      private System.Windows.Forms.MenuItem menuItem1;
      private System.Windows.Forms.MenuItem menuClose;
      private System.Windows.Forms.ComboBox comboFilter;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.ListView filterView;
      private System.Windows.Forms.ListView fileView;
      private System.Windows.Forms.Label LabelSelectCount;
      private System.Windows.Forms.Label LabelResultCount;
      private System.Windows.Forms.Panel panelTop;
      private System.Windows.Forms.Panel panelLeft;
      private System.Windows.Forms.Panel panelTitleFiles;
      private System.Windows.Forms.Panel panelBottom;
      private System.Windows.Forms.Panel panelCombo;
      private System.Windows.Forms.MenuItem menuAnalyse;
      private System.Windows.Forms.ColumnHeader DetailcolumnHeader;
      private System.Windows.Forms.ListView DetailView;
      private System.Windows.Forms.Splitter splitterH;
      private System.Windows.Forms.Splitter splitterV;
      private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
         // up and down pictures are not displayed if we use XP style
         // Application.EnableVisualStyles();  // use XP style
         
         InitializeComponent();  

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.components = new System.ComponentModel.Container();
         System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
         System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("");
         this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
         this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
         this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
         this.imageList1 = new System.Windows.Forms.ImageList(this.components);
         this.mainMenu1 = new System.Windows.Forms.MainMenu();
         this.menuItem1 = new System.Windows.Forms.MenuItem();
         this.menuAnalyse = new System.Windows.Forms.MenuItem();
         this.menuClose = new System.Windows.Forms.MenuItem();
         this.comboFilter = new System.Windows.Forms.ComboBox();
         this.label1 = new System.Windows.Forms.Label();
         this.filterView = new System.Windows.Forms.ListView();
         this.fileView = new System.Windows.Forms.ListView();
         this.panelTop = new System.Windows.Forms.Panel();
         this.panelCombo = new System.Windows.Forms.Panel();
         this.LabelSelectCount = new System.Windows.Forms.Label();
         this.panelLeft = new System.Windows.Forms.Panel();
         this.splitterH = new System.Windows.Forms.Splitter();
         this.panelBottom = new System.Windows.Forms.Panel();
         this.panelTitleFiles = new System.Windows.Forms.Panel();
         this.LabelResultCount = new System.Windows.Forms.Label();
         this.splitterV = new System.Windows.Forms.Splitter();
         this.DetailView = new System.Windows.Forms.ListView();
         this.DetailcolumnHeader = new System.Windows.Forms.ColumnHeader();
         this.panelTop.SuspendLayout();
         this.panelCombo.SuspendLayout();
         this.panelLeft.SuspendLayout();
         this.panelBottom.SuspendLayout();
         this.panelTitleFiles.SuspendLayout();
         this.SuspendLayout();
         // 
         // columnHeader1
         // 
         this.columnHeader1.Text = "Name";
         this.columnHeader1.Width = 200;
         // 
         // columnHeader2
         // 
         this.columnHeader2.Text = "Attribute";
         this.columnHeader2.Width = 100;
         // 
         // imageList1
         // 
         this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
         this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
         this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
         // 
         // mainMenu1
         // 
         this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                  this.menuItem1});
         // 
         // menuItem1
         // 
         this.menuItem1.Index = 0;
         this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                  this.menuAnalyse,
                                                                                  this.menuClose});
         this.menuItem1.Text = "&File";
         // 
         // menuAnalyse
         // 
         this.menuAnalyse.Index = 0;
         this.menuAnalyse.Text = "&Analyse";
         this.menuAnalyse.Click += new System.EventHandler(this.menuAnalyse_Click);
         // 
         // menuClose
         // 
         this.menuClose.Index = 1;
         this.menuClose.Text = "&Close";
         this.menuClose.Click += new System.EventHandler(this.menuClose_Click);
         // 
         // comboFilter
         // 
         this.comboFilter.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.comboFilter.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
         this.comboFilter.Location = new System.Drawing.Point(80, 0);
         this.comboFilter.MaxDropDownItems = 20;
         this.comboFilter.Name = "comboFilter";
         this.comboFilter.Size = new System.Drawing.Size(421, 21);
         this.comboFilter.Sorted = true;
         this.comboFilter.TabIndex = 1;
         this.comboFilter.SelectedValueChanged += new System.EventHandler(this.comboFilter_SelectedValueChanged);
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(8, 4);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(64, 16);
         this.label1.TabIndex = 10;
         this.label1.Text = "Select filter";
         // 
         // filterView
         // 
         this.filterView.AllowColumnReorder = true;
         this.filterView.Dock = System.Windows.Forms.DockStyle.Fill;
         this.filterView.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.filterView.FullRowSelect = true;
         this.filterView.HideSelection = false;
         this.filterView.Location = new System.Drawing.Point(0, 48);
         this.filterView.Name = "filterView";
         this.filterView.Size = new System.Drawing.Size(509, 184);
         this.filterView.TabIndex = 2;
         this.filterView.View = System.Windows.Forms.View.Details;
         this.filterView.SelectedIndexChanged += new System.EventHandler(this.filterView_SelectedIndexChanged);
         // 
         // fileView
         // 
         this.fileView.Dock = System.Windows.Forms.DockStyle.Fill;
         this.fileView.FullRowSelect = true;
         this.fileView.HideSelection = false;
         this.fileView.Location = new System.Drawing.Point(0, 30);
         this.fileView.MultiSelect = false;
         this.fileView.Name = "fileView";
         this.fileView.Size = new System.Drawing.Size(509, 371);
         this.fileView.TabIndex = 1;
         this.fileView.View = System.Windows.Forms.View.Details;
         this.fileView.SelectedIndexChanged += new System.EventHandler(this.fileView_SelectedIndexChanged);
         // 
         // panelTop
         // 
         this.panelTop.Controls.Add(this.filterView);
         this.panelTop.Controls.Add(this.panelCombo);
         this.panelTop.Dock = System.Windows.Forms.DockStyle.Top;
         this.panelTop.Location = new System.Drawing.Point(0, 0);
         this.panelTop.Name = "panelTop";
         this.panelTop.Size = new System.Drawing.Size(509, 232);
         this.panelTop.TabIndex = 15;
         // 
         // panelCombo
         // 
         this.panelCombo.Controls.Add(this.LabelSelectCount);
         this.panelCombo.Controls.Add(this.label1);
         this.panelCombo.Controls.Add(this.comboFilter);
         this.panelCombo.Dock = System.Windows.Forms.DockStyle.Top;
         this.panelCombo.Location = new System.Drawing.Point(0, 0);
         this.panelCombo.Name = "panelCombo";
         this.panelCombo.Size = new System.Drawing.Size(509, 48);
         this.panelCombo.TabIndex = 12;
         // 
         // LabelSelectCount
         // 
         this.LabelSelectCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.LabelSelectCount.Location = new System.Drawing.Point(8, 27);
         this.LabelSelectCount.Name = "LabelSelectCount";
         this.LabelSelectCount.Size = new System.Drawing.Size(490, 14);
         this.LabelSelectCount.TabIndex = 11;
         this.LabelSelectCount.Text = "Filter selection.";
         // 
         // panelLeft
         // 
         this.panelLeft.Controls.Add(this.splitterH);
         this.panelLeft.Controls.Add(this.panelBottom);
         this.panelLeft.Controls.Add(this.panelTop);
         this.panelLeft.Dock = System.Windows.Forms.DockStyle.Fill;
         this.panelLeft.Location = new System.Drawing.Point(0, 0);
         this.panelLeft.Name = "panelLeft";
         this.panelLeft.Size = new System.Drawing.Size(509, 633);
         this.panelLeft.TabIndex = 1;
         // 
         // splitterH
         // 
         this.splitterH.BackColor = System.Drawing.SystemColors.GrayText;
         this.splitterH.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
         this.splitterH.Dock = System.Windows.Forms.DockStyle.Top;
         this.splitterH.Location = new System.Drawing.Point(0, 232);
         this.splitterH.Name = "splitterH";
         this.splitterH.Size = new System.Drawing.Size(509, 3);
         this.splitterH.TabIndex = 19;
         this.splitterH.TabStop = false;
         // 
         // panelBottom
         // 
         this.panelBottom.Controls.Add(this.fileView);
         this.panelBottom.Controls.Add(this.panelTitleFiles);
         this.panelBottom.Dock = System.Windows.Forms.DockStyle.Fill;
         this.panelBottom.Location = new System.Drawing.Point(0, 232);
         this.panelBottom.Name = "panelBottom";
         this.panelBottom.Size = new System.Drawing.Size(509, 401);
         this.panelBottom.TabIndex = 18;
         // 
         // panelTitleFiles
         // 
         this.panelTitleFiles.Controls.Add(this.LabelResultCount);
         this.panelTitleFiles.Dock = System.Windows.Forms.DockStyle.Top;
         this.panelTitleFiles.Location = new System.Drawing.Point(0, 0);
         this.panelTitleFiles.Name = "panelTitleFiles";
         this.panelTitleFiles.Size = new System.Drawing.Size(509, 30);
         this.panelTitleFiles.TabIndex = 0;
         // 
         // LabelResultCount
         // 
         this.LabelResultCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.LabelResultCount.Location = new System.Drawing.Point(8, 8);
         this.LabelResultCount.Name = "LabelResultCount";
         this.LabelResultCount.Size = new System.Drawing.Size(490, 14);
         this.LabelResultCount.TabIndex = 12;
         this.LabelResultCount.Text = "Corresponding files.";
         // 
         // splitterV
         // 
         this.splitterV.BackColor = System.Drawing.SystemColors.GrayText;
         this.splitterV.Dock = System.Windows.Forms.DockStyle.Right;
         this.splitterV.Location = new System.Drawing.Point(509, 0);
         this.splitterV.Name = "splitterV";
         this.splitterV.Size = new System.Drawing.Size(3, 633);
         this.splitterV.TabIndex = 5;
         this.splitterV.TabStop = false;
         // 
         // DetailView
         // 
         this.DetailView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                     this.DetailcolumnHeader});
         this.DetailView.Dock = System.Windows.Forms.DockStyle.Right;
         this.DetailView.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
                                                                                   listViewItem1});
         this.DetailView.Location = new System.Drawing.Point(512, 0);
         this.DetailView.MultiSelect = false;
         this.DetailView.Name = "DetailView";
         this.DetailView.Size = new System.Drawing.Size(192, 633);
         this.DetailView.TabIndex = 1;
         this.DetailView.View = System.Windows.Forms.View.Details;
         // 
         // DetailcolumnHeader
         // 
         this.DetailcolumnHeader.Text = "Detail";
         this.DetailcolumnHeader.Width = 500;
         // 
         // Form1
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(704, 633);
         this.Controls.Add(this.panelLeft);
         this.Controls.Add(this.splitterV);
         this.Controls.Add(this.DetailView);
         this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
         this.Menu = this.mainMenu1;
         this.Name = "Form1";
         this.Text = "Aida Net";
         this.panelTop.ResumeLayout(false);
         this.panelCombo.ResumeLayout(false);
         this.panelLeft.ResumeLayout(false);
         this.panelBottom.ResumeLayout(false);
         this.panelTitleFiles.ResumeLayout(false);
         this.ResumeLayout(false);

      }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}


      // -----------------------------------------------------------------------------
      // sample use : gridCtrl.GetCell(1,1).Image = createBitmap(GetType() , "image1.bmp"); // typeof(Form1)
      public Bitmap createBitmap (Type type, string resource)
      {
         Stream stream ;
         stream = type.Module.Assembly.GetManifestResourceStream(type.Module.Assembly.GetName().Name+"."+resource); 
         if (stream == null)
         {
            // TTrace.Debug.SendObject ("ResourceNotFound : " + resource , type );
            
            throw new ArgumentException("ResourceNotFound", resource);
 
         }
         Bitmap result = new Bitmap(stream) ;
         return result ; 
      } 


      
      // -----------------------------------------------------------------------------
      private void menuClose_Click(object sender, System.EventArgs e)
      {
         Close() ;      
      }

      // -----------------------------------------------------------------------------

      private void MsgBox (string message, string caption) 
      {
         MessageBoxButtons buttons = MessageBoxButtons.OK;
         MessageBox.Show(this, message, caption, buttons) ;
      }

      private void MsgBox (string message) 
      {
         MessageBoxButtons buttons = MessageBoxButtons.OK;
         MessageBox.Show(this, message, "", buttons) ;
      }

      // -----------------------------------------------------------------------------
      // Load all XML files
      public int fileID ;
      private void menuAnalyse_Click(object sender, System.EventArgs e)
      {
         ProgressForm pf = new ProgressForm() ;
         pf.Show() ;

         Cursor oldCur = this.Cursor ;
         this.Cursor = Cursors.WaitCursor ;
         
         try
         {
            // initialize ListView sort method

            filterSorter = new ListViewSorter (filterView) ;
            filterView.ListViewItemSorter =  filterSorter ;
            filterSorter.TableArrows = imageList1 ;

            fileSorter = new ListViewSorter (fileView) ;
            fileView.ListViewItemSorter = fileSorter ;
            fileSorter.TableArrows = imageList1 ;
            
            // open the AidaNet xml file and a navigator for queries
            //----------------------------------------------------------

            // Other ways...
            //XmlTextReader reader = new XmlTextReader("..\\..\\AidaNet.xml");
            //XmlDocument xmlDoc = new XmlDocument();
            ////Load the file into the XmlDocument
            //xmlDoc.Load(reader);
            ////Close  the connection to the file.
            //reader.Close();

            XPathDocument ScriptDoc;
            if (File.Exists ("AidaNet.xml"))                  // normal runtime : same folder
               ScriptDoc = new XPathDocument ("AidaNet.xml");
            else if (File.Exists ("..\\..\\AidaNet.xml"))     // debug mode
               ScriptDoc = new XPathDocument ("..\\..\\AidaNet.xml");
            else
               throw new Exception ("AidaNet.xml not found in current path") ;

            XPathNavigator ScriptNav = ScriptDoc.CreateNavigator() ;

            // Script : get the Path to the xml folder
            //----------------------------------------

            XPathNodeIterator ScriptIterator = ScriptNav.Select ("//FolderPath") ;
            if (ScriptIterator.MoveNext() == false)
               return ;

            Folder = ScriptIterator.Current.Value ;

            // Script : Get all aliases and groups
            // -----------------------------------

            // the script must contain a UNIQUE root "Alias" tag under Config. 
            ScriptIterator = ScriptNav.Select ("Config/Alias") ;

            // read the root alias info and recursive add children
            recurReadAliases (statics.RootAliases, ScriptIterator) ;

            // get all groups, file grid and detail grid
            ReadGroups (ScriptNav) ;
            ReadFileGrid (ScriptNav) ;
            ReadDetailGrid (ScriptNav) ;

            // load xml files
            //-------------------------------------
            string[] entries = System.IO.Directory.GetFiles(Folder,"*.xml" ) ;  

            if (entries.Length == 0)
               MsgBox ("No xml files in the specified folder", Folder) ;
            
            fileID = 0 ;
            ArrayList AllfilesAlias = new ArrayList();
            pf.progressBar.Maximum = entries.Length ;
            pf.progressBar.Value = 0 ;
            pf.progressBar.Visible = true ;

            foreach (string str in entries) 
            {
               pf.progressBar.Value = fileID ;

               // open xml file
               XPathDocument CurrentXML = new XPathDocument (str);
               XPathNavigator FileNav = CurrentXML.CreateNavigator() ;

               // execute Alias for the current file.
               statics.RootAliases.path = "." ;
               XPathNodeIterator FileIterator = FileNav.Select (".") ;
               recurExecAliases (statics.RootAliases, FileIterator) ;
               fileID++ ;
            }
            FillCombo(); // fill the combo with Aliases and groups
         } 
         finally 
         {
            this.Cursor = oldCur ;
            pf.Close() ;
         }
         comboFilter.Focus();        
      }


      // -----------------------------------------------------------------------------
      // recursive load aliases script 
      // <Alias Name="OS_LANG" Path="Item[Title='OS Language']/Value" Title="OS Language"/>

      void recurReadAliases (Alias ParentAlias, XPathNodeIterator ParentIterator) 
      {
         // stop recursive call when no more element
         while (ParentIterator.MoveNext() == true)
         {
            Alias CurrentAlias = new Alias() ;

            CurrentAlias.path  = ParentIterator.Current.GetAttribute ("Path","") ;
            CurrentAlias.name  = ParentIterator.Current.GetAttribute ("Name","") ;
            CurrentAlias.title = ParentIterator.Current.GetAttribute ("Title","") ;

            // add the alias to the Parent Alias and to the hashTable
            ParentAlias.subAliases.Add (CurrentAlias) ;

            // don't add Alias without name
            if (CurrentAlias.name.Length != 0)
               statics.HashAliases.Add (CurrentAlias.name, CurrentAlias) ; 

            // get sub Aliases
            XPathNodeIterator CurrentScriptIterator = ParentIterator.Current.Select ("Alias") ;
            recurReadAliases (CurrentAlias , CurrentScriptIterator) ;
         }
      }

      // -----------------------------------------------------------------------------
      
      //<DetailGrid>
      //   <Field>PC_NAME</Field>
      //   <Field>USER_NAME</Field>
      //   <Field>RAM</Field>
      //   <Field>CPU_FULL</Field>
      //</DetailGrid>
      void ReadDetailGrid (XPathNavigator ScriptNav)
      {
         XPathNodeIterator ScriptIterator = ScriptNav.Select ("Config/DetailGrid") ;

         if (ScriptIterator.MoveNext() == false)
            return ;  // script problem.

         //fileView
         ScriptIterator = ScriptIterator.Current.Select ("Field") ;
         while (ScriptIterator.MoveNext() == true)
         {
            string str = ScriptIterator.Current.Value  ;
            Alias alias = (Alias) statics.HashAliases[str] ; // Key : Alias name, Value : Alias object
            if (alias != null) 
               statics.Details.Add (alias) ;   // Add to array for later.
            else 
            {
               Group group = (Group) statics.HashGroups[str] ; // Key : group name , Value : Group object
               if (group != null)
                  statics.Details.Add (group) ;
            } // else : error , not an alias nor a group.
         }
      }      

      // -----------------------------------------------------------------------------
      
      // <FileGrid>
      //    <Col>PC_NAME</Col>
      //    <Col>USER_NAME</Col>
      //    <Col>RAM</Col>
      //    <Col>CPU_FULL</Col>
      // </FileGrid>

      void ReadFileGrid (XPathNavigator ScriptNav)
      {
         XPathNodeIterator ScriptIterator = ScriptNav.Select ("Config/FileGrid") ;

         if (ScriptIterator.MoveNext() == false)
            return ;  // script problem.

         //fileView
         ScriptIterator = ScriptIterator.Current.Select ("Col") ;
         while (ScriptIterator.MoveNext() == true)
         {
            string aliasName = ScriptIterator.Current.Value ;
            Alias alias = (Alias) statics.HashAliases[aliasName] ; // Key : Alias name, Value : Alias object
            if (alias != null) 
            {
               // Add to array for later.
               statics.FileAliases.Add (alias) ;

               // Add columns
               fileView.Columns.Add (alias.title, 20, HorizontalAlignment.Left);
            }
         }
         fileSorter.ResizeColumns () ;
      }      
            
      // -----------------------------------------------------------------------------

      // <Group Name="CPU" Title="Processor detail">
      //    <Alias Name="CPU_MANUFACT"/>
      //    <Alias Name="CPU_TYPE"/>
      //    <Alias Name="CPU_CLOCK"/>
      // </Group>
      void ReadGroups (XPathNavigator ScriptNav)
      {
         // get all groups, don't need to know where are defined groups
         // m group can define aliases not in the same level.
         // moving group node just before or after specified aliases is
         // just for xml read abilities.
         XPathNodeIterator ScriptIterator = ScriptNav.Select ("//Group") ;

         while (ScriptIterator.MoveNext() == true)
         {
            string groupName = ScriptIterator.Current.GetAttribute ("Name","") ;
            Group group = new Group(groupName) ;
            group.title = ScriptIterator.Current.GetAttribute ("Title","");
            statics.HashGroups.Add (groupName, group) ;

            // add aliases

            XPathNodeIterator AliasIterator ;

            // get MasterGrid "Col" element
            AliasIterator = ScriptIterator.Current.Select ("Alias") ;
            while (AliasIterator.MoveNext() == true)
            {
               string aliasName = AliasIterator.Current.GetAttribute ("Name","") ;
               Alias alias = (Alias) statics.HashAliases [aliasName] ;

               if (alias == null) 
               {
                  MsgBox ("Error in group " + group.name + 
                     " : Alias " + aliasName + " don't exist") ;
                  continue ;
               }

               if (alias.group != null)
               {
                  MsgBox ("Error in group " + group.name + 
                     " : Alias " + aliasName + " already in group " + alias.group.name) ;
                  continue ; 
               }

               group.groupAliases.Add (alias) ;
               alias.group = group ;
            }
         }      
      }

      // -----------------------------------------------------------------------------
      // recursive load aliases script 

      void recurExecAliases (Alias alias, XPathNodeIterator ParentIterator) 
      {
         // execute the Alias path to the current file Iterator
         XPathNodeIterator PathIterator  = ParentIterator.Current.Select (alias.path) ;
         while (PathIterator.MoveNext() == true)
         {
            // only leaf node store values.
            if (alias.subAliases.Count == 0)
            {
               // get value
               string str = PathIterator.Current.Value ;
               
               alias.AddValue (str,fileID ) ;

            } 
            else  // not a leaf : recur execute all sub aliases on the curent iterator
            {
               foreach (Alias subAlias in alias.subAliases)
                  recurExecAliases (subAlias, PathIterator) ;
            }
         }
      }
      
      // -----------------------------------------------------------------------------
      
      // fill the combo with Aliases and groups
      void FillCombo ()   
      {
         // Hashtable. Key : Alias name , Value : alias object
         foreach (DictionaryEntry AliasDict in statics.HashAliases)
         {
            Alias alias = (Alias) AliasDict.Value ;
            if (alias.name.Length == 0)
               continue ;

            if (alias.valueRefs.Count == 0)
               continue ;

            comboFilter.Items.Add (alias) ;
         }

         // Hashtable. Key : group name , Value : Group
         foreach (DictionaryEntry groupDict in statics.HashGroups)
         {
            Group group = (Group) groupDict.Value ;
            comboFilter.Items.Add (group) ;
         }
      }
         
      // -----------------------------------------------------------------------------
      // user select a filter. comboFilter.SelectedItem is an alias or a group
      private void comboFilter_SelectedValueChanged(object sender, System.EventArgs e)
      {
         Cursor oldCur = this.Cursor ;
         this.Cursor = Cursors.WaitCursor ;
         filterSorter.SortBy (null, SortOrder.None) ;
         fileSorter.SortBy (null, SortOrder.None) ;
         fileView.BeginUpdate() ;
         filterView.BeginUpdate() ;
         fileView.Items.Clear() ;   // clear only contant
         filterView.Clear() ;  // clear columns and items
         
         try
         {
            if (comboFilter.SelectedItem is Alias)
            {
               Alias alias = (Alias) comboFilter.SelectedItem ;

               filterView.Columns.Add (alias.title, 20, HorizontalAlignment.Left);
               filterView.Columns.Add ("Count", 50, HorizontalAlignment.Left);

               // valueRefs : ArrayList
               foreach (ValueRef vref in alias.valueRefs)
                  if (vref.FileRefs.Count != 0)
                  {
                     // first col
                     ListViewItem item = filterView.Items.Add (vref.ItemValue,0) ;
                     // filterView_SelectedIndexChanged can know the ID list of this item
                     item.Tag = vref.FileRefs ;

                     // add count col
                     item.SubItems.Add (vref.FileRefs.Count.ToString());
                  }
            }
            else if (comboFilter.SelectedItem is Group) 
            {
               Group group = (Group) comboFilter.SelectedItem ;

               // add all group alias
               foreach (Alias alias in group.groupAliases)
                  filterView.Columns.Add (alias.title, 20, HorizontalAlignment.Left);

               // add count col
               filterView.Columns.Add ("Count", 50, HorizontalAlignment.Left);

               // print all groupvalue for the same group
               foreach (GroupValue groupvalue in group.groupValues)
               {
                  ListViewItem item = null ;
                  // get each values ordered by alias
                  foreach (Alias alias in group.groupAliases)
                  {
                     // AliasValues : Hashtable : Key = Alias Object , Value = string 
                     string Value = (string) groupvalue.AliasValues[alias] ;
                     // first col must be added in filterView.Items
                     if (item == null) 
                     {
                        item = filterView.Items.Add (Value,0) ;
                        // filterView_SelectedIndexChanged can know the ID list of this item
                        item.Tag = groupvalue.FileRefs ;  
                     }
                     else  // other columns must be added in item.SubItems
                     {  
                        item.SubItems.Add (Value);
                     }
                  }
                  // add count col
                  item.SubItems.Add (groupvalue.FileRefs.Count.ToString());

               }
            }
            filterSorter.ResizeColumns (); 
         } 
         finally 
         {
            fileView.EndUpdate () ;
            filterView.EndUpdate () ;
                     
            //filterSorter.SortBy (filterView.Columns[0], true) ;
            LabelSelectCount.Text = "Filter selection (" + filterView.Items.Count + ")" ;
            this.Cursor = oldCur ;
         }

      }

      // -----------------------------------------------------------------------------
      // user click on a alias ou group value
      // show files in the second grid.
      // ListViewItem.Tag are ArrayList of integer (file reference)
      private void filterView_SelectedIndexChanged(object sender, System.EventArgs e)
      {
         Cursor oldCur = this.Cursor ;
         this.Cursor = Cursors.WaitCursor ;
         fileView.BeginUpdate() ;
         fileView.Sorting = SortOrder.None ;
         try
         {
            fileView.Items.Clear() ;  // clear columns and items 
            // Count is zero when the user change selection
            if (filterView.SelectedItems.Count == 0)
               return ;
           
            foreach (ListViewItem filter_LVI in filterView.SelectedItems)
            {
               ArrayList FileRefs  = (ArrayList) filter_LVI.Tag ;

               foreach (int ID in FileRefs)
               {
                  // detect if the file is not already in the fileView.Items objects
                  bool alreadyInFileView = false ;
                  foreach (ListViewItem file_LVI in fileView.Items)
                     if ((int) file_LVI.Tag == ID) 
                     {
                        alreadyInFileView = true ;
                        break ;
                     }
                  // if already in file view, continue with another fileid
                  if (alreadyInFileView == true)
                     continue ;

                  ListViewItem item = null ;
                  // FileAliases : Arraylist
                  foreach (Alias alias in statics.FileAliases)
                  {
                     string Value = "" ;

                     // locate the ValueRef having the ID
                     // alias.valueRefs : ArrayList
                     foreach (ValueRef vref in alias.valueRefs)
                     {
                        if (vref.FileRefs.Contains (ID))
                        {
                           Value = vref.ItemValue ;
                           break ;
                        }
                     }
                     // when found, add the ValueRef.ItemValue in the column
                     // first col must be added in filterView.Items
                     if (item == null) 
                     {
                        item = fileView.Items.Add (Value) ;    //  ,0
                        item.Tag = ID ; // fileView_SelectedIndexChanged can know file ID of this item
                     }
                     else    // other columns must be added in item.SubItems
                     {  
                        item.SubItems.Add (Value);
                     }
                  }
               }
            }
         } 
         finally 
         {
            fileView.EndUpdate() ;
            
            LabelResultCount.Text = "Corresponding files (" + fileView.Items.Count + ")" ;
            this.Cursor = oldCur ;
         }
         fileSorter.ResizeColumns () ; 
         fileView.Items[0].Selected = true ;
         
         //DetailView.Items.Clear() ;
      }

      // -----------------------------------------------------------------------------
      // user select a file.  fileView.SelectedItems[0].Tag if the ID of the selected item
      private void fileView_SelectedIndexChanged(object sender, System.EventArgs e)
      {
         // Count is zero when the user change selection
         if (fileView.SelectedItems.Count == 0)
            return ;
         
         DetailView.Items.Clear() ;

         int fileID = (int) fileView.SelectedItems[0].Tag ;

         // display all informations for that file, using the "DetailGrid" xml script
         foreach (object obj in statics.Details)
         {
            if (obj is Alias) 
            {
               Alias alias = (Alias) obj ;
               // show Alias title
               ListViewItem lv = new ListViewItem (alias.title) ;
               lv.Font = new Font(lv.Font, FontStyle.Bold | FontStyle.Underline);
               DetailView.Items.Add (lv);

               // show Values for that fileID

               // valueRefs : ArrayList
               foreach (ValueRef vref in alias.valueRefs)
                  if (vref.FileRefs.Contains (fileID))
                     DetailView.Items.Add ("   " + vref.ItemValue);   

            } 
            else if (obj is Group) 
            {
               Group group = (Group) obj ;

               // show Group title + sub alias titles
               string fullTitle ;
               fullTitle = group.title + " (" ;
               
               bool first = true ;
               // ArrayList groupAliases ;   // Alias [] 
               foreach (Alias alias in group.groupAliases)
               {
                  if (first) {
                     fullTitle += alias.title ;
                     first = false ;
                  } else {
                     fullTitle += ", " + alias.title ;
                  }
               }
               fullTitle += ")" ;
               

               ListViewItem lv = new ListViewItem (fullTitle) ;
               lv.Font = new Font(lv.Font, FontStyle.Bold | FontStyle.Underline);
               DetailView.Items.Add (lv);
               
               // show groups for the file ID
               foreach (GroupValue groupvalue in group.groupValues)
               {
                  if (groupvalue.FileRefs.Contains (fileID))
                  {
                     string fullValue = "" ;
                     // get each values ordered by alias
                     foreach (Alias alias in group.groupAliases)
                     {
                        // AliasValues : Hashtable : Key = Alias Object , Value = string 
                        string Value = (string) groupvalue.AliasValues[alias] ;

                        if (fullValue.Length == 0) 
                           fullValue = Value ;
                        else 
                           fullValue += ", " + Value ;
                     }
                     DetailView.Items.Add ("   " + fullValue);   

                  }   // FileRefs contain the file id  
               }      // group value
            }         // Alias or Group
            DetailView.Items.Add ("" );  // add empty line after each title/values
         }            // next detail item
      }
      // -----------------------------------------------------------------------------


   } // class end
} // namespace end

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions