private void buttonField_Click(object sender, EventArgs e)
 {
     BaseUtils.ConditionLists res = HistoryFilterHelpers.ShowDialog(FindForm(), fieldfilter, discoveryform, "Journal: Filter out fields".T(EDTx.UserControlJournalGrid_JHF));
     if (res != null)
     {
         fieldfilter = res;
         PutSetting(dbFieldFilter, fieldfilter.GetJSON());
         HistoryChanged(current_historylist);
     }
 }
        private void AddEntry(HistoryEntry he)
        {
            bool add = he.IsJournalEventInEventFilter(GetSetting(dbFilter, "All"));

            if (!add)
            {
                ftotalevents++;
                UpdateToolTipsForFilter();
            }

            if (add && !HistoryFilterHelpers.FilterHistory(he, fieldfilter, discoveryform.Globals))
            {
                add = false;
                ftotalfilters++;
                UpdateToolTipsForFilter();
            }

            if (add)
            {
                var sst = new BaseUtils.StringSearchTerms(textBoxSearch.Text, searchterms);
                var row = CreateHistoryRow(he, sst);     // we might be filtered out by search
                if (row != null)
                {
                    dataGridViewJournal.Rows.Insert(0, row);
                }
                else
                {
                    add = false;
                }
            }

            if (add)                                // its been added, we have at least 1 row visible, at row 0
            {
                var filter = (TravelHistoryFilter)comboBoxTime.SelectedItem ?? TravelHistoryFilter.NoFilter;

                if (filter.MaximumNumberOfItems != null)        // this one won't remove the latest one
                {
                    for (int r = dataGridViewJournal.Rows.Count - 1; r >= filter.MaximumNumberOfItems; r--)
                    {
                        dataGridViewJournal.Rows.RemoveAt(r);
                    }
                }

                if (checkBoxCursorToTop.Checked) // Move focus to first row
                {
                    dataGridViewJournal.ClearSelection();
                    dataGridViewJournal.SetCurrentAndSelectAllCellsOnRow(0);       // its the current cell which needs to be set, moves the row marker as well
                    FireChangeSelection();
                }
            }
        }
        private void Display(HistoryList hl, bool disablesorting)
        {
            todo.Clear();           // ensure in a quiet state
            queuedadds.Clear();
            todotimer.Stop();

            if (hl == null)     // just for safety
            {
                return;
            }

            this.dataGridViewJournal.Cursor = Cursors.WaitCursor;
            buttonExtExcel.Enabled          = buttonFilter.Enabled = buttonField.Enabled = false;

            current_historylist = hl;

            Tuple <long, int> pos = CurrentGridPosByJID();

            SortOrder sortorder = dataGridViewJournal.SortOrder;
            int       sortcol   = dataGridViewJournal.SortedColumn?.Index ?? -1;

            if (sortcol >= 0 && disablesorting)
            {
                dataGridViewJournal.Columns[sortcol].HeaderCell.SortGlyphDirection = SortOrder.None;
                sortcol = -1;
            }

            var filter = (TravelHistoryFilter)comboBoxTime.SelectedItem ?? TravelHistoryFilter.NoFilter;

            List <HistoryEntry> result = filter.Filter(hl.EntryOrder());

            fdropdown = hl.Count - result.Count();

            result = HistoryList.FilterByJournalEvent(result, GetSetting(dbFilter, "All"), out ftotalevents);
            result = HistoryFilterHelpers.FilterHistory(result, fieldfilter, discoveryform.Globals, out ftotalfilters);

            dataGridViewJournal.Rows.Clear();
            rowsbyjournalid.Clear();

            dataGridViewJournal.Columns[0].HeaderText = EDDConfig.Instance.GetTimeTitle();

            List <HistoryEntry[]> chunks = new List <HistoryEntry[]>();

            int chunksize = 500;

            for (int i = 0; i < result.Count; i += chunksize, chunksize = 2000)
            {
                HistoryEntry[] chunk = new HistoryEntry[i + chunksize > result.Count ? result.Count - i : chunksize];

                result.CopyTo(i, chunk, 0, chunk.Length);
                chunks.Add(chunk);
            }

            var sst = new BaseUtils.StringSearchTerms(textBoxSearch.Text, searchterms);

            System.Diagnostics.Stopwatch swtotal = new System.Diagnostics.Stopwatch(); swtotal.Start();

            //int lrowno = 0;

            foreach (var chunk in chunks)
            {
                todo.Enqueue(() =>
                {
                    //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start();

                    List <DataGridViewRow> rowstoadd = new List <DataGridViewRow>();

                    foreach (var item in chunk)
                    {
                        var row = CreateHistoryRow(item, sst);
                        if (row != null)
                        {
                            //row.Cells[2].Value = (lrowno++).ToString() + " " + item.Journalid + " " + (string)row.Cells[2].Value;
                            rowstoadd.Add(row);
                        }
                    }

                    dataGridViewJournal.Rows.AddRange(rowstoadd.ToArray()); // much faster to send in one chunk

                    // System.Diagnostics.Debug.WriteLine("J Chunk Load in " + sw.ElapsedMilliseconds);

                    if (dataGridViewJournal.MoveToSelection(rowsbyjournalid, ref pos, false))
                    {
                        FireChangeSelection();
                    }
                });
            }

            todo.Enqueue(() =>
            {
                System.Diagnostics.Debug.WriteLine(BaseUtils.AppTicks.TickCount + " JG TOTAL TIME " + swtotal.ElapsedMilliseconds);

                UpdateToolTipsForFilter();

                if (dataGridViewJournal.MoveToSelection(rowsbyjournalid, ref pos, true))
                {
                    FireChangeSelection();
                }

                if (sortcol >= 0)
                {
                    dataGridViewJournal.Sort(dataGridViewJournal.Columns[sortcol], (sortorder == SortOrder.Descending) ? ListSortDirection.Descending : ListSortDirection.Ascending);
                    dataGridViewJournal.Columns[sortcol].HeaderCell.SortGlyphDirection = sortorder;
                }

                this.dataGridViewJournal.Cursor = Cursors.Default;
                buttonExtExcel.Enabled          = buttonFilter.Enabled = buttonField.Enabled = true;

                while (queuedadds.Count > 0)              // finally, dequeue any adds added
                {
                    System.Diagnostics.Debug.WriteLine("JG Dequeue paused adds");
                    AddEntry(queuedadds.Dequeue());
                }
            });

            todotimer.Start();
        }