// 2008.08.30 YZ Add plugin name and plugin directory column
// 2008.09.13 YZ Fixed plugin sorter
// 2008.11.12 YZ Add plugin version column
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace freetrain.framework.plugin
{
	/// <summary>
	/// PluginListDialog の概要の説明です。
	/// </summary>
	public class PluginListDialog : System.Windows.Forms.Form
	{
#region YZ_20080913_DELETE
#region YZ_20080830_ADDED
//      private int clickColumnNo = -1;                                         // Click column no
#endregion
#endregion

        private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListView list;
		private System.Windows.Forms.ColumnHeader titleColumn;
		private System.Windows.Forms.ColumnHeader authorColumn;
		private System.Windows.Forms.ColumnHeader linkColumn;
		private System.Windows.Forms.ImageList images;
        private ColumnHeader pluginName;
        private ColumnHeader pluginDir;
        private ColumnHeader versionColumn;
		private System.ComponentModel.IContainer components;

		public PluginListDialog()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
            //

#region YZ_20080913_ADDED
            this.list.ListViewItemSorter = new ListViewItemComparer();
#endregion
        }

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if( disposing && components != null)
				components.Dispose();
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
        this.components = new System.ComponentModel.Container();
        System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
            "test"},0,System.Drawing.SystemColors.WindowText,System.Drawing.SystemColors.Window,new System.Drawing.Font("MS UI Gothic",9F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(128))));
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PluginListDialog));
        this.panel1 = new System.Windows.Forms.Panel();
        this.button1 = new System.Windows.Forms.Button();
        this.list = new System.Windows.Forms.ListView();
        this.titleColumn = new System.Windows.Forms.ColumnHeader();
        this.authorColumn = new System.Windows.Forms.ColumnHeader();
        this.linkColumn = new System.Windows.Forms.ColumnHeader();
        this.pluginName = new System.Windows.Forms.ColumnHeader();
        this.pluginDir = new System.Windows.Forms.ColumnHeader();
        this.images = new System.Windows.Forms.ImageList(this.components);
        this.versionColumn = new System.Windows.Forms.ColumnHeader();
        this.panel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // panel1
        // 
        this.panel1.Controls.Add(this.button1);
        this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
        this.panel1.Location = new System.Drawing.Point(0,225);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(424,50);
        this.panel1.TabIndex = 0;
        // 
        // button1
        // 
        this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
        this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.button1.Location = new System.Drawing.Point(309,10);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(105,30);
        this.button1.TabIndex = 0;
        this.button1.Text = "&OK";
        // 
        // list
        // 
        this.list.AllowColumnReorder = true;
        this.list.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.titleColumn,
            this.authorColumn,
            this.versionColumn,
            this.linkColumn,
            this.pluginName,
            this.pluginDir});
        this.list.Dock = System.Windows.Forms.DockStyle.Fill;
        this.list.FullRowSelect = true;
        listViewItem1.StateImageIndex = 0;
        this.list.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem1});
        this.list.Location = new System.Drawing.Point(0,0);
        this.list.Name = "list";
        this.list.Size = new System.Drawing.Size(424,225);
        this.list.SmallImageList = this.images;
        this.list.TabIndex = 1;
        this.list.UseCompatibleStateImageBehavior = false;
        this.list.View = System.Windows.Forms.View.Details;
        this.list.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.list_ColumnClick);
        // 
        // titleColumn
        // 
        this.titleColumn.Text = "名前";
        this.titleColumn.Width = 141;
        // 
        // authorColumn
        // 
        this.authorColumn.Text = "製作者";
        // 
        // linkColumn
        // 
        this.linkColumn.Text = "連絡先";
        this.linkColumn.Width = 207;
        // 
        // pluginName
        // 
        this.pluginName.Text = "プラグイン名";
        this.pluginName.Width = 140;
        // 
        // pluginDir
        // 
        this.pluginDir.Text = "プラグインディレクトリ";
        this.pluginDir.Width = 200;
        // 
        // images
        // 
        this.images.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("images.ImageStream")));
        this.images.TransparentColor = System.Drawing.Color.Transparent;
        this.images.Images.SetKeyName(0,"");
        // 
        // versionColumn
        // 
        this.versionColumn.Text = "Ver.";
        // 
        // PluginListDialog
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(6,15);
        this.ClientSize = new System.Drawing.Size(424,275);
        this.Controls.Add(this.list);
        this.Controls.Add(this.panel1);
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "PluginListDialog";
        this.Text = "インストールされているプラグイン";
        this.Load += new System.EventHandler(this.PluginListDialog_Load);
        this.panel1.ResumeLayout(false);
        this.ResumeLayout(false);

		}
		#endregion

		private void PluginListDialog_Load(object sender, System.EventArgs e) {
			// populate the list
			list.Items.Clear();
			foreach( Plugin p in Core.plugins ) {
#region YZ_20080830_MODIFIED 
//              list.Items.Add( new ListViewItem(
//      			new string[]{ p.title, p.author, p.homepage }, 0 ));
#region YZ_20081112_MODIFIED 
//              list.Items.Add( new ListViewItem(
//					new string[]{ p.title, p.author, p.homepage, p.name, p.dirName }, 0 ));
                list.Items.Add( new ListViewItem(
					new string[]{ p.title, p.author, p.version, p.homepage, p.name, p.dirName }, 0 ));
#endregion
#endregion
            }
		}

#region YZ_20080830_ADDED
        private void list_ColumnClick(object sender,ColumnClickEventArgs e) {

#region YZ_20080913_MODIFIED
//          if (clickColumnNo != e.Column) {
//              this.list.ListViewItemSorter = new ListViewItemComparer(e.Column, 1);
//              clickColumnNo = e.Column;
//          } else {
//              this.list.ListViewItemSorter = new ListViewItemComparer(e.Column, 2);
//              clickColumnNo = -1;
//          }
//      }
                                                                                // Set sort column no
            ((ListViewItemComparer)this.list.ListViewItemSorter).columnNo = e.Column;
            this.list.Sort();                                                   // Sort
        }
#endregion

#endregion
	}

#region YZ_20080830_ADDED
    class ListViewItemComparer : IComparer
    {
        private int  col;                                                       // Column no                
        private byte _order;                                                    // Sort order

        // Default constructor
        public ListViewItemComparer()
        {
            col    = 0;                                                         // Initialize column no
            _order = 1;                                                         // Initialize sort order
        }

#region YZ_20080913_ADDED
        /// <summary>Sort column no</summary>
        public int columnNo {
            get { return col; }
            set {
                if (col == value) {                                             // If column no equal prev column no
                    if (_order == 1) {                                          // If order is ascending
                        _order = 2;                                             // Set order to descending
                    } else {
                        _order = 1;                                             // Set order to ascending
                    }
                } else {
                    col = value;                                                // Set column no
                    _order = 1;                                                 // Set order to ascending
                }
            }
        }
#endregion

#region YZ_20080913_DELETE
//      public ListViewItemComparer(int column)
//      {
//          col    = column;                                                    // Set column no
//          _order = 1;                                                         // Initialize sort order
//      }

//      public ListViewItemComparer(int column, byte order)
//      {
//          col    = column;                                                    // Set column no
//          _order = order;                                                     // Set sort order
//      }
#endregion

        public int Compare(object x, object y)
        {
            if (_order == 1) {                                                  // If order is ascending
                                                                                // Comapre for ascending sort
                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
            } else {                                                            // If order is descending
                                                                                // Comapre for descending sort
                return String.Compare(((ListViewItem)y).SubItems[col].Text,((ListViewItem)x).SubItems[col].Text);
            }
        }
    }
#endregion
}
