Click here to Skip to main content
15,895,011 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.8K   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 System.Globalization;
using BSE.Platten.Tunes.Properties;

namespace BSE.Platten.Tunes
{
    public partial class TreeViewFavorites : UserControl
    {
        #region Enumerationen

        public enum TreeViewListEvent
        {
            AddPlayList,
            DeletePlaylist,
            PlayTracks,
            PlayRandomTracks
        }

        #endregion

        #region Events

        public event EventHandler<TreeViewFavoritesEventArgs> ListClick;
        public event EventHandler<TreeViewFavoritesEventArgs> PlayListEvent;

        #endregion

        #region FieldsPrivate

        private TreeViewFavoritesChildNode m_nodeMySingleCharts;
        private TreeViewFavoritesChildNode m_nodeMyAlbumCharts;
        private TreeViewFavoritesChildNode m_nodeMyPlaylists;
        private TreeViewFavoritesChildNode m_selectedNode;

        #endregion

        #region MethodsPublic

        public TreeViewFavorites()
        {
            InitializeComponent();
            Image imgCharts = global::BSE.Platten.Tunes.Properties.Resources.Interpret16;
            Image imgTitle = global::BSE.Platten.Tunes.Properties.Resources.Album16;
            Image imgPlaylists = global::BSE.Platten.Tunes.Properties.Resources.wiedergabe16;
            this.m_imgListFavorites.Images.AddRange(new Image[] {
                imgCharts,
                imgTitle,
                imgPlaylists});
            this.m_trvFavorites.ImageList = this.m_imgListFavorites;
            InitializeNodes();
        }

        public void SetFavorites(CFavorite[] favorites, ListMode listMode)
        {
            switch (listMode)
            {
                case ListMode.Singlecharts:
                    this.m_nodeMySingleCharts.Nodes.Clear();
                    this.m_nodeMySingleCharts.Nodes.AddRange(GetTreeNodesForMyList(favorites, listMode));
                    break;
                case ListMode.Albumcharts:
                    this.m_nodeMyAlbumCharts.Nodes.Clear();
                    this.m_nodeMyAlbumCharts.Nodes.AddRange(GetTreeNodesForMyList(favorites, listMode));
                    break;
            }
        }

        public void SetPlayLists(BSE.Platten.BO.CPlaylist[] playLists)
        {
            this.m_nodeMyPlaylists.Nodes.Clear();
            this.m_nodeMyPlaylists.Nodes.AddRange(GetTreeNodesForPlayLists(playLists, ListMode.Playlists));
            this.m_nodeMyPlaylists.ExpandAll();
        }

        public void SetPlayLists(BSE.Platten.BO.CPlaylist[] playLists, BSE.Platten.BO.CPlaylist selectedPlayList)
        {
            this.SetPlayLists(playLists);
            if (selectedPlayList != null)
            {
                foreach (TreeViewFavoritesChildNode treeNode in this.m_nodeMyPlaylists.Nodes)
                {
                    if (treeNode.Text == selectedPlayList.Name)
                    {
                        treeNode.EnsureVisible();
                        this.m_trvFavorites.SelectedNode = treeNode;
                    }
                }
            }
        }

        #endregion

        #region MethodsProtected
        #endregion

        #region MethodsPrivate

        private void InitializeNodes()
        {
            this.m_nodeMySingleCharts = new TreeViewFavoritesChildNode();
            this.m_nodeMySingleCharts.ListMode = ListMode.Singlecharts;
            this.m_nodeMySingleCharts.Parentchild = ParentChild.Parent;
            this.m_nodeMySingleCharts.Text = Resources.IDS_TreeViewFavoritesMySingleChartsText;
            this.m_nodeMySingleCharts.CanExpand = true;
            this.m_nodeMySingleCharts.ImageIndex = 0;
            this.m_nodeMySingleCharts.SelectedImageIndex = 0;
            this.m_trvFavorites.Nodes.Add(this.m_nodeMySingleCharts);


            this.m_nodeMyAlbumCharts = new TreeViewFavoritesChildNode();
            this.m_nodeMyAlbumCharts.ListMode = ListMode.Albumcharts;
            this.m_nodeMyAlbumCharts.Parentchild = ParentChild.Parent;
            this.m_nodeMyAlbumCharts.Text = Resources.IDS_TreeViewFavoritesMyAlbumChartsText;
            this.m_nodeMyAlbumCharts.CanExpand = true;
            this.m_nodeMyAlbumCharts.ImageIndex = 0;
            this.m_nodeMyAlbumCharts.SelectedImageIndex = 0;
            this.m_trvFavorites.Nodes.Add(this.m_nodeMyAlbumCharts);

            this.m_nodeMyPlaylists = new TreeViewFavoritesChildNode();
            this.m_nodeMyPlaylists.ListMode = ListMode.Playlists;
            this.m_nodeMyPlaylists.Parentchild = ParentChild.Parent;
            this.m_nodeMyPlaylists.Text = Resources.IDS_TreeViewFavoritesMyPlaylistsText;
            this.m_nodeMyPlaylists.CanExpand = true;
            this.m_nodeMyPlaylists.ImageIndex = 2;
            this.m_nodeMyPlaylists.SelectedImageIndex = 2;
            this.m_trvFavorites.Nodes.Add(this.m_nodeMyPlaylists);
        }

        private static TreeViewFavoritesChildNode[] GetTreeNodesForMyList(CFavorite[] favorites, ListMode listMode)
        {
            int iCountFavorites = favorites.Length;
            TreeViewFavoritesChildNode[] treeNodes = new TreeViewFavoritesChildNode[iCountFavorites];
            if (iCountFavorites > 0)
            {
                for (int i = 0; i < iCountFavorites; i++)
                {
                    treeNodes[i] = new TreeViewFavoritesChildNode();
                    treeNodes[i].ListMode = listMode;
                    treeNodes[i].Parentchild = ParentChild.Child;
                    treeNodes[i].Id = favorites[i].TitelId;
                    treeNodes[i].CanExpand = false;
                    treeNodes[i].ImageIndex = 1;
                    treeNodes[i].SelectedImageIndex = 1;
                    if (listMode == ListMode.Singlecharts)
                    {
                        treeNodes[i].Text = string.Format(
                            CultureInfo.CurrentUICulture,
                            "({0}) {1} - {2}",
                            favorites[i].Count,favorites[i].Interpret,favorites[i].Title);
                    }
                    if (listMode == ListMode.Albumcharts)
                    {
                        treeNodes[i].Text = string.Format(
                            CultureInfo.CurrentUICulture,
                            "({0}) {1} - {2}",
                            favorites[i].Count,favorites[i].Interpret,favorites[i].Album);
                    }
                }
            }
            return treeNodes;
        }

        private static TreeViewFavoritesChildNode[] GetTreeNodesForPlayLists(CPlaylist[] playLists, ListMode listMode)
        {
            int iCountPlayLists = playLists.Length;
            TreeViewFavoritesChildNode[] treeNodes = new TreeViewFavoritesChildNode[iCountPlayLists];
            if (iCountPlayLists > 0)
            {
                for (int i = 0; i < iCountPlayLists; i++)
                {
                    treeNodes[i] = new TreeViewFavoritesChildNode();
                    treeNodes[i].ListMode = listMode;
                    treeNodes[i].Parentchild = ParentChild.Child;
                    treeNodes[i].Id = playLists[i].Id;
                    treeNodes[i].CanExpand = false;
                    treeNodes[i].CanDrop = true;
                    treeNodes[i].ImageIndex = 2;
                    treeNodes[i].SelectedImageIndex = 2;
                    if (listMode == ListMode.Playlists)
                    {
                        treeNodes[i].Text = playLists[i].Name;
                    }
                }
            }
            return treeNodes;
        }

        private void FavoritesAfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeViewFavoritesChildNode treeNode = e.Node as TreeViewFavoritesChildNode;
            if (treeNode != null)
            {
                if ((this.m_selectedNode != null) && this.m_selectedNode.Equals(treeNode) == true)
                {
                    return;
                }
                if (treeNode.Parentchild == ParentChild.Child)
                {
                    if (ListClick != null)
                    {
                        ListClick(this, new TreeViewFavoritesEventArgs(treeNode.Id, treeNode.ListMode));
                    }
                }
            }
        }

        private void FavoritesNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeViewFavoritesChildNode treeNode = e.Node as TreeViewFavoritesChildNode;
            if (treeNode == null)
            {
                return;
            }
            this.m_selectedNode = treeNode;
            if (e.Button == MouseButtons.Left)
            {
                if (treeNode.Parentchild == ParentChild.Child)
                {
                    if (ListClick != null)
                    {
                        ListClick(this, new TreeViewFavoritesEventArgs(treeNode.Id, treeNode.ListMode));
                    }
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (!treeNode.Bounds.Contains(new Point(e.X, e.Y)))
                {
                    return;
                }

                this.m_ctxMenu.Items.Clear();

                switch (treeNode.ListMode)
                {
                    case ListMode.Playlists:
                        if (treeNode.Parentchild == ParentChild.Parent)
                        {
                            ToolStripMenuItem mnuAddPlayList = new ToolStripMenuItem();
                            mnuAddPlayList.Text = Resources.IDS_TreeViewFavoritesMenuAddPlaylist;
                            mnuAddPlayList.Image = Resources.NewPlaylist16;
                            mnuAddPlayList.Click += new System.EventHandler(NewPlayListClick);

                            this.m_ctxMenu.Items.AddRange(new ToolStripItem[]
                                {
                                    mnuAddPlayList
                                });

                            this.m_ctxMenu.Show(
                                this.m_trvFavorites,
                                new Point(treeNode.Bounds.X, treeNode.Bounds.Y + treeNode.Bounds.Height));

                        }
                        else if (treeNode.Parentchild == ParentChild.Child)
                        {
                            ToolStripMenuItem mnuPlay = new ToolStripMenuItem();
                            mnuPlay.Text = Resources.IDS_TreeViewFavoritesMenuPlay;
                            mnuPlay.Image = Resources.wiedergabe16;
                            mnuPlay.Click += new System.EventHandler(PlayClick);

                            ToolStripMenuItem mnuPlayRandom = new ToolStripMenuItem();
                            mnuPlayRandom.Text = Resources.IDS_TreeViewFavoritesMenuPlayRandom;
                            mnuPlayRandom.Image = Resources.Shuffle_icon;
                            mnuPlayRandom.Click += new System.EventHandler(PlayRandomClick);

                            ToolStripSeparator mnuSeparator = new ToolStripSeparator();

                            ToolStripMenuItem mnuDeletePlayList = new ToolStripMenuItem();
                            mnuDeletePlayList.Text = Resources.IDS_TreeViewFavoritesMenuDeletePlaylist;
                            mnuDeletePlayList.Image = Resources.delete;
                            mnuDeletePlayList.Click += new System.EventHandler(DeletePlayListClick);

                            this.m_ctxMenu.Items.AddRange(new ToolStripItem[]
                                {
                                    mnuPlay,
                                    mnuPlayRandom,
                                    mnuSeparator,
                                    mnuDeletePlayList
                                });
                            this.m_ctxMenu.Show(
                                this.m_trvFavorites,
                                new Point(treeNode.Bounds.X, treeNode.Bounds.Y + treeNode.Bounds.Height));
                        }
                        break;
                }
            }
        }

        private void NewPlayListClick(object sender, EventArgs e)
        {
            if (PlayListEvent != null)
            {
                PlayListEvent(this, new TreeViewFavoritesEventArgs(TreeViewListEvent.AddPlayList));
            }
        }

        private void DeletePlayListClick(object sender, EventArgs e)
        {
            if (PlayListEvent != null)
            {
                PlayListEvent(this, new TreeViewFavoritesEventArgs(TreeViewListEvent.DeletePlaylist));
            }
        }

        private void PlayClick(object sender, EventArgs e)
        {
            if (PlayListEvent != null)
            {
                PlayListEvent(this, new TreeViewFavoritesEventArgs(TreeViewListEvent.PlayTracks));
            }
        }

        private void PlayRandomClick(object sender, EventArgs e)
        {
            if (PlayListEvent != null)
            {
                PlayListEvent(this, new TreeViewFavoritesEventArgs(TreeViewListEvent.PlayRandomTracks));
            }
        }

        #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