PackagesListForm.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 » PackagesListForm.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 System.Diagnostics;
using ThoughtWorks.CruiseControl.Remote;
using System.IO;

namespace ThoughtWorks.CruiseControl.CCTrayLib.Presentation{
    public partial class PackagesListForm : Form
    {
        private IProjectMonitor project;
        private Exception currentError;
        private Stopwatch loadStopwatch = new Stopwatch();
        private PackageDetails[] packages;
        private string downloadLocation;

        public PackagesListForm(IProjectMonitor project)
        {
            InitializeComponent();
            Project = project;
        }

        public IProjectMonitor Project
        {
            get { return project; }
            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 ((project == null) ||
                        (project.Detail == null) ||
                        (project.Detail.Configuration == null))
                    {
                        changeMonitor = true;
                    }
                    else
                    {
                        changeMonitor = (project.Detail.Configuration != value.Detail.Configuration);
                    }
                }

                // Now we can do the actual monitor change
                if (changeMonitor)
                {
                    Text = string.Format("Available Packages for {0} [{1}]",
                        value.Detail.ProjectName,
                        value.Detail.ServerName);
                    project = value;
                    packageList.Items.Clear();
                    RefreshList();
                }
            }
        }

        private void RefreshList()
        {
            // 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 (!listLoader.IsBusy)
            {
                // Update the display and then start the background worker
                statusLabel.Text = "Loading status...";
                currentError = null;
                loadStopwatch.Reset();
                loadStopwatch.Start();
                listLoader.RunWorkerAsync();
            }
        }

        private void listLoader_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                packages = project.RetrievePackageList();
            }
            catch (Exception error)
            {
                currentError = error;
            }
        }

        private void listLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            loadStopwatch.Stop();
            statusLabel.Text = string.Format("Package list loaded ({0:0.00}s)",
                Convert.ToDouble(loadStopwatch.ElapsedMilliseconds) / 1000);
            if (currentError == null)
            {
                // Update the display
                packageList.Items.Clear();
                foreach (PackageDetails package in packages)
                {
                    ListViewItem item = new ListViewItem(new string[] {
                        package.Name,
                        package.BuildLabel,
                        package.DateTime.ToString("F"),
                        package.NumberOfFiles.ToString(),
                        FormatSize(package.Size)
                    });
                    item.Tag = package;
                    packageList.Items.Add(item);
                }
            }
            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);
            }
        }

        private string FormatSize(long size)
        {
            double workingSize = size;
            if (size > 1048576)
            {
                workingSize = workingSize / 1048576;
                return string.Format("{0:0.00}Mb", workingSize);
            }
            else if (size > 1024)
            {
                workingSize = workingSize / 1024;
                return string.Format("{0:0.00}Kb", workingSize);
            }
            else
            {
                return string.Format("{0}b", workingSize);
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void downloadButton_Click(object sender, EventArgs e)
        {
            DownloadPackages();
        }

        private void packageList_DoubleClick(object sender, EventArgs e)
        {
            DownloadPackages();
        }

        private void DownloadPackages()
        {
            if (packageList.SelectedItems.Count == 0)
            {
                MessageBox.Show("You have not selected any packages to download",
                    "Download Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "Select the location to download the packages to:";
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    downloadLocation = dialog.SelectedPath;
                    List<PackageDetails> downloadList = new List<PackageDetails>();
                    foreach (ListViewItem item in packageList.SelectedItems)
                    {
                        downloadList.Add(item.Tag as PackageDetails);
                    }
                    packages = downloadList.ToArray();
                    statusLabel.Text = string.Format("Downloading packages (0 of {0})", downloadList.Count);
                    currentError = null;
                    downloader.RunWorkerAsync();
                }
            }
        }

        private void downloader_DoWork(object sender, DoWorkEventArgs e)
        {
            int count = 0;
            foreach (PackageDetails package in packages)
            {
                Stream outputStream = File.Create(Path.Combine(downloadLocation, package.Name + ".zip"));
                try
                {
                    IFileTransfer fileTransfer = project.RetrieveFileTransfer(package.FileName);
                    fileTransfer.Download(outputStream);
                }
                finally
                {
                    outputStream.Flush();
                    outputStream.Close();
                }

                downloader.ReportProgress(count++);
            }
        }

        private void downloader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            statusLabel.Text = "All packages downloaded";
            if (currentError != null)
            {
                // Tell the user an error occurred and what sort of error
                MessageBox.Show("Unable to download all packages: " + currentError.Message,
                    "Download Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Process folderDisplay = new Process();
                folderDisplay.StartInfo = new ProcessStartInfo(downloadLocation);
                folderDisplay.Start();
            }
        }

        private void downloader_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            statusLabel.Text = string.Format("Downloading packages ({1} of {0})", 
                packages.Length,
                e.ProgressPercentage);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.