CurrentStatusWindow.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » CCTrayLib » Presentation » 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 » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » CCTrayLib » Presentation » CurrentStatusWindow.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.CruiseControl.CCTrayLib.Monitoring;
using ThoughtWorks.CruiseControl.Remote;
using System.Diagnostics;

namespace ThoughtWorks.CruiseControl.CCTrayLib.Presentation{
    public partial class CurrentStatusWindow : Form
    {
        private IProjectMonitor projectToMonitor;
        private ProjectStatusSnapshot snapshot;
        private Exception currentError;
        private Stopwatch loadStopwatch = new Stopwatch();
        private List<string> oldKeys = new List<string>();
        private List<string> newKeys = new List<string>();

        public CurrentStatusWindow(IProjectMonitor projectToMonitor)
        {
            InitializeComponent();
            ProjectToMonitor = projectToMonitor;
        }

        public IProjectMonitor ProjectToMonitor
        {
            get { return projectToMonitor; }
            set
            {
                // Nice big logic check to make sure nothing is missed out
                bool changeMonitor = ((value != null) && (value.Detail != null) && (value.Detail.Configuration != null));
                if (changeMonitor)
                {
                    if ((projectToMonitor == null) ||
                        (projectToMonitor.Detail == null) ||
                        (projectToMonitor.Detail.Configuration == null))
                    {
                        changeMonitor = true;
                    }
                    else
                    {
                        changeMonitor = (projectToMonitor.Detail.Configuration != value.Detail.Configuration);
                    }
                }

                // Now we can do the actual monitor change
                if (changeMonitor)
                {
                    Text = string.Format("Current Status for {0} [{1}]",
                        value.Detail.ProjectName,
                        value.Detail.ServerName);
                    projectToMonitor = value;
                    statusExplorer.Nodes.Clear();
                    RefreshStatus();
                }
            }
        }

        private void RefreshStatus()
        {
            // If the background worker is not busy, then start the refresh, otherwise there is already
            // a refresh under way, so there is no need to start another
            if (!displayWorker.IsBusy)
            {
                // Update the display and then start the background worker
                refreshTimer.Enabled = false;
                currentStatus.Text = "Loading status...";
                currentError = null;
                loadStopwatch.Reset();
                loadStopwatch.Start();
                displayWorker.RunWorkerAsync();
            }
        }

        private void refreshTimer_Tick(object sender, EventArgs e)
        {
            RefreshStatus();
        }

        private void refreshCommand_Click(object sender, EventArgs e)
        {
            RefreshStatus();
        }

        private void displayWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                ProjectStatusSnapshot newSnapshot = projectToMonitor.RetrieveSnapshot();
                snapshot = newSnapshot;
            }
            catch (Exception error)
            {
                currentError = error;
            }
        }

        private void displayWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            loadStopwatch.Stop();
            currentStatus.Text = string.Format("Status loaded ({0:0.00}s)",
                Convert.ToDouble(loadStopwatch.ElapsedMilliseconds) / 1000);
            if (currentError == null)
            {
                // Update the display
                AddToExplorer(snapshot, statusExplorer.Nodes, 0);

                // Remove any old items
                foreach (string key in oldKeys)
                {
                    if (!newKeys.Contains(key))
                    {
                        TreeNode[] oldNodes = statusExplorer.Nodes.Find(key, true);
                        foreach (TreeNode oldNode in oldNodes)
                        {
                            oldNode.Remove();
                        }
                    }
                }
                if (statusExplorer.SelectedNode == null)
                {
                    statusExplorer.SelectedNode = statusExplorer.Nodes[0];
                }
                else
                {
                    DisplayStatusItem(statusExplorer.SelectedNode.Tag as ItemStatus);
                }

                // Update the lists
                oldKeys = newKeys;
                newKeys = new List<string>();
            }
            else
            {
                // Tell the user an error occurred and what sort of error
                MessageBox.Show("Unable to refresh status: " + currentError.Message,
                    "Refresh Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            refreshTimer.Enabled = true;
        }

        private void AddToExplorer(ItemStatus value, TreeNodeCollection nodes, int position)
        {
            TreeNode node = null;

            // First attempt to find a node with a matching name
            foreach (TreeNode nodeToCheck in nodes)
            {
                if (object.Equals((nodeToCheck.Tag as ItemStatus), value))
                {
                    node = nodeToCheck;
                    break;
                }
            }

            string nodeKey = value.Identifier.ToString();
            bool newNode = false;
            if (node == null)
            {
                // Need to add a whole new node
                node = nodes.Insert(position, nodeKey, value.Name);
                newNode = true;
            }
            newKeys.Add(nodeKey);

            // Update the node
            node.Tag = value;
            node.Text = value.Name;
            node.ImageKey = value.Status.ToString();
            node.SelectedImageKey = node.ImageKey;

            // Update all the child items
            int childPosition = 0;
            foreach (ItemStatus childItem in value.ChildItems)
            {
                AddToExplorer(childItem, node.Nodes, childPosition++);
            }
            if (newNode) node.Expand();
        }

        private void statusExplorer_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ItemStatus status = e.Node.Tag as ItemStatus;
            DisplayStatusItem(status);
        }

        private void DisplayStatusItem(ItemStatus status)
        {
            statusDetails.SelectedObject = new StatusItemDisplay(status);
            int progress = 0;
            Color color = Color.Gray;

            // Calculate the progress bar
            switch (status.Status)
            {
                case ItemBuildStatus.CompletedSuccess:
                    progress = 100;
                    color = Color.Green;
                    break;
                case ItemBuildStatus.CompletedFailed:
                    progress = 100;
                    color = Color.Red;
                    break;
                case ItemBuildStatus.Cancelled:
                    progress = 100;
                    break;
                case ItemBuildStatus.Running:
                    // TODO: Caculdate the progress percentage
                    progress = 50;
                    break;
            }

            // Update the progress bar
            statusProgress.Value = (progress > 100) ? 100 : progress;
            statusProgress.ForeColor = color;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.