private void ButtonOK_Click( object sender, EventArgs e )
        {
            Result = new List<ShipGroupData.ViewColumnData>( ColumnView.Rows.Count - 1 );

            for ( int i  = 1; i < ColumnView.Rows.Count; i++ ) {

                var row = ColumnView.Rows[i];
                var r = new ShipGroupData.ViewColumnData( (string)row.Tag );
                r.DisplayIndex = row.Index - 1;
                r.Visible = (bool)row.Cells[ColumnView_Visible.Index].Value;
                r.AutoSize = (bool)row.Cells[ColumnView_AutoSize.Index].Value;
                r.Width = Convert.ToInt32( row.Cells[ColumnView_Width.Index].Value );

                Result.Add( r );
            }

            ScrollLockColumnCount = (int)ScrLkColumnCount.Value;

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        private void MenuMember_CreateGroup_Click( object sender, EventArgs e )
        {
            var ships = GetSelectedShipID();
            if ( ships.Count() == 0 )
                return;

            using ( var dialog = new DialogTextInput( "グループの追加", "グループ名を入力してください:" ) ) {

                if ( dialog.ShowDialog( this ) == System.Windows.Forms.DialogResult.OK ) {

                    var group = KCDatabase.Instance.ShipGroup.Add();

                    group.Name = dialog.InputtedText.Trim();

                    for ( int i = 0; i < ShipView.Columns.Count; i++ ) {
                        var newdata = new ShipGroupData.ViewColumnData( ShipView.Columns[i] );
                        if ( SelectedTab == null )
                            newdata.Visible = true;		//初期状態では全行が非表示のため
                        group.ViewColumns.Add( ShipView.Columns[i].Name, newdata );
                    }

                    // 艦船ID == 0 を作成(空リストを作る)
                    group.Expressions.Expressions.Add( new ExpressionList( false, true, false ) );
                    group.Expressions.Expressions[0].Expressions.Add( new ExpressionData( ".MasterID", ExpressionData.ExpressionOperator.Equal, 0 ) );
                    group.Expressions.Compile();

                    group.AddInclusionFilter( ships );

                    TabPanel.Controls.Add( CreateTabLabel( group.GroupID ) );

                }

            }
        }
        private void MenuGroup_Add_Click( object sender, EventArgs e )
        {
            using ( var dialog = new DialogTextInput( "グループを追加", "グループ名を入力してください:" ) ) {

                if ( dialog.ShowDialog( this ) == System.Windows.Forms.DialogResult.OK ) {

                    var group = KCDatabase.Instance.ShipGroup.Add();

                    group.Name = dialog.InputtedText.Trim();

                    for ( int i = 0; i < ShipView.Columns.Count; i++ ) {
                        var newdata = new ShipGroupData.ViewColumnData( ShipView.Columns[i] );
                        if ( SelectedTab == null )
                            newdata.Visible = true;		//初期状態では全行が非表示のため
                        group.ViewColumns.Add( ShipView.Columns[i].Name, newdata );
                    }

                    TabPanel.Controls.Add( CreateTabLabel( group.GroupID ) );

                }

            }
        }
        private void FormShipGroup_Load( object sender, EventArgs e )
        {
            ShipGroupManager groups = KCDatabase.Instance.ShipGroup;

            // 空(≒初期状態)の時、おなじみ全所属艦を追加
            if ( groups.ShipGroups.Count == 0 ) {

                var group = KCDatabase.Instance.ShipGroup.Add();
                group.Name = "全所属艦";

                for ( int i = 0; i < ShipView.Columns.Count; i++ ) {
                    var newdata = new ShipGroupData.ViewColumnData( ShipView.Columns[i] );
                    if ( SelectedTab == null )
                        newdata.Visible = true;		//初期状態では全行が非表示のため
                    group.ViewColumns.Add( ShipView.Columns[i].Name, newdata );
                }
            }

            foreach ( var g in groups.ShipGroups.Values ) {
                TabPanel.Controls.Add( CreateTabLabel( g.GroupID ) );
            }

            //*/
            {
                int columnCount = ShipView.Columns.Count;
                for ( int i = 0; i < columnCount; i++ ) {
                    ShipView.Columns[i].Visible = false;
                }
            }
            //*/

            ConfigurationChanged();

            APIObserver o = APIObserver.Instance;

            o.APIList["api_port/port"].ResponseReceived += APIUpdated;
            o.APIList["api_get_member/ship2"].ResponseReceived += APIUpdated;
            o.APIList["api_get_member/ship_deck"].ResponseReceived += APIUpdated;

            Utility.Configuration.Instance.ConfigurationChanged += ConfigurationChanged;

            IsRowsUpdating = false;
            Icon = ResourceManager.ImageToIcon( ResourceManager.Instance.Icons.Images[(int)ResourceManager.IconContent.FormShipGroup] );
        }
        /// <summary>
        /// 指定したタブのグループのShipViewを作成します。
        /// </summary>
        /// <param name="target">作成するビューのグループデータ</param>
        private void BuildShipView( ImageLabel target )
        {
            if ( target == null )
                return;

            ShipGroupData group = KCDatabase.Instance.ShipGroup[(int)target.Tag];

            IsRowsUpdating = true;
            ShipView.SuspendLayout();

            UpdateMembers( group );

            ShipView.Rows.Clear();

            var ships = group.MembersInstance;
            var rows = new List<DataGridViewRow>( ships.Count() );

            foreach ( ShipData ship in ships ) {

                if ( ship == null ) continue;

                DataGridViewRow row = CreateShipViewRow( ship );
                rows.Add( row );

            }

            for ( int i = 0; i < rows.Count; i++ )
                rows[i].Tag = i;

            ShipView.Rows.AddRange( rows.ToArray() );

            // 設定に抜けがあった場合補充
            if ( group.ViewColumns == null ) {
                group.ViewColumns = new Dictionary<string, ShipGroupData.ViewColumnData>();
            }
            if ( ShipView.Columns.Count != group.ViewColumns.Count ) {
                foreach ( DataGridViewColumn column in ShipView.Columns ) {

                    if ( !group.ViewColumns.ContainsKey( column.Name ) ) {
                        var newdata = new ShipGroupData.ViewColumnData( column );
                        newdata.Visible = true;		//初期状態でインビジだと不都合なので

                        group.ViewColumns.Add( newdata.Name, newdata );
                    }
                }
            }

            ApplyViewData( group );
            ApplyAutoSort( group );

            ShipView.ResumeLayout();
            IsRowsUpdating = false;

            //status bar
            if ( KCDatabase.Instance.Ships.Count > 0 ) {
                Status_ShipCount.Text = string.Format( "所属: {0}隻", group.Members.Count );
                Status_LevelTotal.Text = string.Format( "合計Lv: {0}", group.MembersInstance.Where( s => s != null ).Sum( s => s.Level ) );
                Status_LevelAverage.Text = string.Format( "平均Lv: {0:F2}", group.Members.Count > 0 ? group.MembersInstance.Where( s => s != null ).Average( s => s.Level ) : 0 );
            }
        }
        private void MenuMember_CreateFromSelection_Click( object sender, EventArgs e )
        {
            if ( ShipView.Rows.GetRowCount( DataGridViewElementStates.Selected ) == 0 )
                return;

            using ( var dialog = new DialogTextInput( "選択範囲から固定グループを作成", "グループ名を入力してください:" ) ) {

                if ( dialog.ShowDialog( this ) == System.Windows.Forms.DialogResult.OK ) {

                    var group = KCDatabase.Instance.ShipGroup.Add();

                    group.Name = dialog.InputtedText.Trim();

                    for ( int i = 0; i < ShipView.Columns.Count; i++ ) {
                        var newdata = new ShipGroupData.ViewColumnData( ShipView.Columns[i] );
                        if ( SelectedTab == null )
                            newdata.Visible = true;		//初期状態では全行が非表示のため
                        group.ViewColumns.Add( ShipView.Columns[i].Name, newdata );
                    }

                    group.Expressions.Expressions.Add( new ExpressionList( false, true, false ) );

                    var exp = group.Expressions.Expressions[0];
                    foreach ( int id in ShipView.SelectedRows.Cast<DataGridViewRow>().Select( r => (int)r.Cells[ShipView_ID.Index].Value ) ) {
                        exp.Expressions.Add( new ExpressionData( ".MasterID", ExpressionData.ExpressionOperator.Equal, id ) );
                    }

                    TabPanel.Controls.Add( CreateTabLabel( group.GroupID ) );

                }

            }
        }