Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.2K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using BSE.Platten.BO;
using BSE.Platten.Common;
using BSE.Platten.Audio;

namespace BSE.Platten.Tunes
{
    public partial class History : UserControl
    {
        #region Delegates

        private delegate void delegateUpdateHistory(SortableCollection<CTrack> trackCollection);

        #endregion

        #region Events

        public event EventHandler<AlbumSelectEventArgs> AlbumSelecting;

        #endregion

        #region FieldsPrivate

        private CEnvironment m_environment;

        #endregion

        #region Properties

        public CEnvironment Environment
        {
            get { return this.m_environment; }
            set { this.m_environment = value; }
        }

        #endregion

        #region MethodsPublic

        public History()
        {
            InitializeComponent();
            this.m_grdHistory.AutoGenerateColumns = false;
            this.m_grdHistory.AlternatingRowsDefaultCellStyle.BackColor = BSE.Platten.Common.BSEColors.AlternatingBackColor;
            this.m_grdHistory.DataSource = new SortableCollection<CTrack>();
        }

        public void UpdateHistory(SortableCollection<CTrack> trackCollection)
        {
            if (trackCollection != null)
            {
                try
                {
                    this.Invoke(new delegateUpdateHistory(this.UpdateDataGridView),
                        new object[] { trackCollection });
                }
                catch (Exception exception)
                {
                    GlobalizedMessageBox.Show(this,exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public void UpdateHistory(CTrack currentTrack, BSE.Platten.Audio.PlayMode eMode)
        {
            if ((this.m_environment != null) && (currentTrack != null))
            {
                HistoryUpdateData updateData = new HistoryUpdateData();
                updateData.Environment = this.m_environment;
                updateData.HistoryData = new HistoryData();
                updateData.HistoryData.UserName = this.m_environment.UserName;
                updateData.HistoryData.TitelId = currentTrack.TitelId;
                updateData.HistoryData.LiedId = currentTrack.LiedId;
                updateData.HistoryData.AppId = (int)eMode;
                if (eMode == PlayMode.Playlist)
                {
                    updateData.HistoryData.AppId = (int)PlayMode.Song;
                }
                updateData.HistoryData.PlayedAt = DateTime.Now;

                m_backgroundWorkerUpdate.RunWorkerAsync(updateData);
            }
        }
        #endregion

        #region MethodsProtected

        protected void OnAlbumSelecting(AlbumSelectEventArgs e)
        {
            if (AlbumSelecting != null)
            {
                AlbumSelecting(this, e);
            }
        }

        #endregion

        #region MethodsPrivate

        private void UpdateDataGridView(SortableCollection<CTrack> trackCollection)
        {
            if (trackCollection != null)
            {
                this.m_grdHistory.DataSource = trackCollection;
            }
        }
        
        private void GridMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DataGridView.HitTestInfo hitTestInfo = this.m_grdHistory.HitTest(e.X, e.Y);
                if (hitTestInfo.RowIndex >= 0)
                {
                    CTrack track = this.m_grdHistory.Rows[hitTestInfo.RowIndex].DataBoundItem as CTrack;
                    if (track != null)
                    {
                        PlayListDragDropData playListDragDropData = new PlayListDragDropData();
                        if (track.LiedId == 0)
                        {
                            playListDragDropData.Id = track.TitelId;
                            playListDragDropData.PlayListDragDropMode = PlayList.PlayListDragDropMode.Albums;
                        }
                        if (track.LiedId > 0)
                        {
                            playListDragDropData.Id = track.LiedId;
                            playListDragDropData.PlayListDragDropMode = PlayList.PlayListDragDropMode.PlayList;
                        }
                        BSE.Windows.Forms.CDraggedListViewObjects draggedListViewObjects
                            = new BSE.Windows.Forms.CDraggedListViewObjects(new BSE.Windows.Forms.ListView(DragDropEffects.Move));
                        draggedListViewObjects.DragObjects.Add(playListDragDropData);
                        BSE.Windows.Forms.CDataObject dataObject = new BSE.Windows.Forms.CDataObject(draggedListViewObjects);

                        this.m_grdHistory.DoDragDrop(dataObject, DragDropEffects.Copy);
                    }
                }
            }
        }
        
        private void GridMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (this.m_grdHistory.SelectedRows.Count != 0)
                {
                    this.m_ctxHistory.Show(this.m_grdHistory, new Point(e.X, e.Y));
                }
            }
        }
        
        private void GridKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
            {
                if (this.m_grdHistory.SelectedRows.Count != 0)
                {
                    SelectAlbumClick(sender, e);
                }
            }
        }
        
        private void GridResize(object sender, EventArgs e)
        {
            
            foreach (DataGridViewColumn dataGridViewColumn in this.m_grdHistory.Columns)
            {
                if (dataGridViewColumn.Equals(this.m_clmnTitle) == true)
                {
                    if (dataGridViewColumn.Width != 0)
                    {
                        int iTotalColumnWidth = 0;
                        int iVerticalScrollBarWidth = 0;

                        foreach (Control dataGridControl in this.m_grdHistory.Controls)
                        {
                            if (dataGridControl is VScrollBar)
                            {
                                if (dataGridControl.Visible)
                                {
                                    iVerticalScrollBarWidth = SystemInformation.VerticalScrollBarWidth;
                                }
                            }
                        }

                        foreach (DataGridViewColumn col in this.m_grdHistory.Columns)
                        {
                            iTotalColumnWidth += col.Width;
                        }

                        dataGridViewColumn.Width += this.m_grdHistory.Width
                            - iTotalColumnWidth
                            - iVerticalScrollBarWidth
                            - dataGridViewColumn.DividerWidth;

                        if (dataGridViewColumn.Width < 1)
                        {
                            dataGridViewColumn.Width = 1;
                        }
                    }
                }
            }
        }
        
        private void SelectAlbumClick(object sender, EventArgs e)
        {
            DataGridViewRow dataGridViewRow = this.m_grdHistory.SelectedRows[0];
            CTrack track = dataGridViewRow.DataBoundItem as CTrack;
            if (track != null)
            {
                OnAlbumSelecting(new AlbumSelectEventArgs(track.TitelId));
            }
        }
        private void BackgroundWorkerUpdateDoWork(object sender, DoWorkEventArgs e)
        {
            HistoryUpdateData updateData = e.Argument as HistoryUpdateData;
            if ((updateData != null) && (updateData.Environment != null))
            {
                CTunesBusinessObject tunesBusinessObject = new CTunesBusinessObject(
                    updateData.Environment.GetConnectionString());
                try
                {
                    e.Result = tunesBusinessObject.GetHistoryTrackCollection(
                        updateData.HistoryData);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        private void BackgroundWorkerUpdateRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                GlobalizedMessageBox.Show(this, e.Error.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                SortableCollection<CTrack> tracksCollection = e.Result as SortableCollection<CTrack>;
                this.m_grdHistory.Invoke(
                    new delegateUpdateHistory(this.UpdateDataGridView),
                    new object[] { tracksCollection });
            }
        }

        internal class HistoryUpdateData
        {
            #region FieldsPrivate
            private BSE.Platten.BO.CEnvironment m_environment;
            private BSE.Platten.BO.HistoryData m_historyData;
            #endregion

            #region Properties
            public BSE.Platten.BO.CEnvironment Environment
            {
                get { return this.m_environment; }
                set { this.m_environment = value; }
            }
            public BSE.Platten.BO.HistoryData HistoryData
            {
                get { return this.m_historyData; }
                set { this.m_historyData = value; }
            }
            #endregion
        }

        #endregion
    }
}

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
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions