//Refresh Data and Var grids from Refresh icon in main window
        //Refresh DataSet and Variable grid 25MAr2013
        public void RefreshBothGrids(DataSource ds) //A.
        {
            RefreshDataSet(ds);                     //Refresh DataGrid
            TabItem panel = GetTabItem(ds);         //04Sep2014

            if (panel == null)
            {
                return;                //fix for crash when for some reason the existing dataset tab is not found
            }
            //04Sep2014 TabItem panel = docGroup.SelectedItem as TabItem;//we dont want active tab. Instead we need tab that matches the title "Dataset1"
            DataPanel datapanel = panel.Content as DataPanel;

            //if (!ds.IsPaginationClicked) //if the refresh is called from refresh icon and not from pagination
            //{
            //    ds.StartColindex = 0;
            //    ds.EndColindex = 15;
            //    datapanel.InitializeDynamicColIndexes(); // then reset the start and end indexes (to 0 and 15)
            //}

            //31May2018
            //refresh datagrid tab. (RData files diff names but same dataframe name, so needs tab title refresh too)
            StackPanel sp        = panel.Header as StackPanel;
            string     sheetname = string.Empty;//29Apr2015

            if (ds.SheetName != null && ds.SheetName.Trim().Length > 0)
            {
                sheetname = "{" + ds.SheetName + "}";
            }
            Label lb = sp.Children[0] as Label;

            lb.ToolTip = ds.FileName;
            var tabtextcolor = new SolidColorBrush(Color.FromArgb(255, (byte)48, (byte)88, (byte)144));//Foreground="#FF1579DA"

            lb.Foreground = tabtextcolor;
            lb.Content    = Path.GetFileName(ds.FileName) + sheetname + " (" + ds.Name + ")";

            //datapanel.sortcolnames = sortcolnames; //11Apr2014
            //datapanel.sortorder = sortorder; //14Apr2014
            datapanel.sortasccolnames  = sortasccolnames;
            datapanel.sortdesccolnames = sortdesccolnames;
            datapanel.Variables        = ds.Variables; //Refresh Var grid
            datapanel.DisableEnableAllNavButtons();
            datapanel.arrangeVarGridCols();            // arranging col positions
            datapanel.RefreshStatusBar();


            //04Aug2014 Remove old dialogs if cols changed in dataset
            DataSource removedds = panel.Tag as DataSource;
            string     semiKey   = removedds.FileName + removedds.Name;

            RemoveOldSessionDialogsFromMemory(semiKey);
        }
        //Refresh DataSet
        public void RefreshDataSet(DataSource ds) //A.
        {
            TabItem panel = GetTabItem(ds);       //04Sep2014

            if (panel == null)
            {
                return;                //fix for crash when for some reason the existing dataset tab is not found
            }
            //04Sep2014 TabItem panel = docGroup.SelectedItem as TabItem;//we dont want active tab. Instead we need tab that matches the title "Dataset1"
            DataPanel datapanel = panel.Content as DataPanel;

            if (!ds.IsPaginationClicked) //if the refresh is called from refresh icon and not from pagination
            {
                ds.StartColindex = 0;
                ds.EndColindex   = 15;
                datapanel.InitializeDynamicColIndexes(); // then reset the start and end indexes (to 0 and 15)
            }

            ds.Changed = true;//False +ve cases:scrolling to next page, refreshing grid without modifying
            //IList list = new VirtualListDynamic(_analyticsService, ds);
            VirtualListDynamic vld = new VirtualListDynamic(_analyticsService, ds);

            vld.DataF = _analyticsService.GetDataFrame(ds);
            IList list = vld;

            panel.Tag    = ds; //panel.Tag = ds;
            datapanel.DS = ds; //sending reference
            //datapanel.sortcolnames = sortcolnames; //11Apr2014
            //datapanel.sortorder = sortorder; //14Apr2014
            datapanel.sortasccolnames  = sortasccolnames;
            datapanel.sortdesccolnames = sortdesccolnames;
            datapanel.Data             = list;
            //causing recursion datapanel.Variables = ds.Variables; //Refresh Var grid
            datapanel.DisableEnableAllNavButtons();
            //causing recursion datapanel.arrangeVarGridCols(); // arranging col positions
            datapanel.RefreshStatusBar();

            // comment following variable refresh, if dont want to update var grid from R datasets.
            // Arrangement changing. Calling the arrangeVarGridCols() will solve the arrangement problem.
            // but I guess ObservableCollection is causing problem and  throwing error for text and numeric cols
            // datapanel.Variables = ds.Variables; //uncommented for Compute 22Mar2013

            //05Mar2013 commented b'coz Tab header changed to normal header when added var in vargrid
            //panel.Header = Path.GetFileName(ds.FileName);
        }
        public void Load_Dataframe(DataSource ds)
        {
            logService.Info("Grid Loading started." + ds.Name);
            bool bo = false; //These two lines for testing refresh on new row addition. Remove them later.

            if (bo)
            {
                closeTab();
            }

            ///DocumentPanel panel = new DocumentPanel();
            //C1DockControlPanel c1panel = new C1DockControlPanel();//Anil
            StackPanel sp = new StackPanel(); //sp.Background = Brushes.Black;

            sp.Orientation = Orientation.Horizontal;
            //sp.HorizontalAlignment = HorizontalAlignment.Center;
            sp.VerticalAlignment = VerticalAlignment.Center;
            //sp.Margin = new Thickness(1, 1, 1, 1);

            Label lb           = new Label(); lb.FontWeight = FontWeights.DemiBold;
            var   tabtextcolor = new SolidColorBrush(Color.FromArgb(255, (byte)48, (byte)88, (byte)144));//Foreground="#FF1579DA"

            lb.Foreground = tabtextcolor;
            lb.Content    = Path.GetFileName(ds.FileName) + " (" + ds.Name + ")";//06Aug2012
            lb.ToolTip    = ds.FileName; lb.Margin = new Thickness(1, 1, 1, 1);
            //Button b = new Button(); b.Content="x";

            Image b = new Image(); b.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(b_MouseLeftButtonUp);

            b.ToolTip = "Close this dataset"; b.Height = 16.0; b.Width = 22.0;
            string packUri = "pack://application:,,,/BlueSky;component/Images/closetab.png";

            b.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;

            sp.Children.Add(lb);
            sp.Children.Add(b); //sp.Children.Add(b);

            TabItem panel = new TabItem();

            //panel.Margin = new Thickness(1, 1, 1, 1);
            b.Tag = panel;                //for remove
            ///////triggers///08Feb2013
            Binding bind = new Binding(); //for close image to hide for inactive tabs

            bind.Source    = panel;
            bind.Path      = new PropertyPath("IsSelected");
            bind.Converter = new BooleanToVisibilityConverter();
            b.SetBinding(Image.VisibilityProperty, bind);

            Style st     = new Style(typeof(TabItem));
            var   brush1 = new SolidColorBrush(Color.FromArgb(255, (byte)212, (byte)227, (byte)242));//#FFD4E3F2

            st.Setters.Add(new Setter()
            {
                Property = TabItem.BackgroundProperty, Value = brush1
            });

            Trigger tg = new Trigger {
                Property = TabItem.IsSelectedProperty, Value = true
            };
            var brush2 = new SolidColorBrush(Color.FromArgb(255, (byte)234, (byte)239, (byte)245));//#FFEAEFF5

            tg.Setters.Add(new Setter()
            {
                Property = TabItem.BackgroundProperty, Value = brush2
            });

            st.Triggers.Add(tg);

            panel.Style = st;
            ///triggers///

            DataPanel datapanel = new DataPanel();

            datapanel.sortasccolnames  = sortasccolnames;
            datapanel.sortdesccolnames = sortdesccolnames;

            //IList list = new VirtualListDynamic(_analyticsService, ds);
            VirtualListDynamic vld = new VirtualListDynamic(_analyticsService, ds);

            vld.DataF = _analyticsService.GetDataFrame(ds);
            IList list = vld;

            panel.Tag      = ds; //panel.Tag = ds;
            datapanel.DS   = ds; //sending reference
            datapanel.Data = list;

            datapanel.Variables = ds.Variables;

            datapanel.statusbar.Text = ""; // "No Split";//03Dec2013 Status bar
            datapanel.DisableEnableAllNavButtons();
            panel.Header  = sp;            ///Path.GetFileName(ds.FileName);
            panel.Content = datapanel;
            docGroup.Items.Add(panel);     //panel
            panel.IsSelected      = true;
            docGroup.SelectedItem = panel;

            ///layoutManager.Activate(panel);
            //docGroup.UpdateLayout();
            //panel.UpdateLayout();

            //Following focus will not work until the main window is active. Say you called this function
            //from some dialog window created in C#(not analysis dialog). And the dialog is still on top
            //of the app while datagrid window will be in back. So following line will have no effect.
            panel.Focus();//15Oct2015
            logService.Info("Grid Loading finished.");
        }
        public void LoadNewDataSet(DataSource ds)
        {
            logService.Info("Grid Loading started." + ds.Name);
            bool bo = false; //These two lines for testing refresh on new row addition. Remove them later.

            if (bo)
            {
                closeTab();
            }

            if (!DataSourceExists(ds))
            {
                ///DocumentPanel panel = new DocumentPanel();
                //C1DockControlPanel c1panel = new C1DockControlPanel();//Anil
                StackPanel sp = new StackPanel(); //sp.Background = Brushes.Red;
                sp.Orientation         = Orientation.Horizontal;
                sp.HorizontalAlignment = HorizontalAlignment.Center;
                sp.VerticalAlignment   = VerticalAlignment.Center;
                //sp.Margin = new Thickness(1, 1, 1, 1);

                string sheetname = string.Empty;//29Apr2015
                if (ds.SheetName != null && ds.SheetName.Trim().Length > 0)
                {
                    sheetname = "{" + ds.SheetName + "}";
                }
                Label lb           = new Label(); lb.FontWeight = FontWeights.DemiBold;
                var   tabtextcolor = new SolidColorBrush(Color.FromArgb(255, (byte)48, (byte)88, (byte)144)); //Foreground="#FF1579DA"
                lb.Foreground = tabtextcolor;
                lb.Content    = Path.GetFileName(ds.FileName) + sheetname + " (" + ds.Name + ")";             //06Aug2012
                lb.ToolTip    = ds.FileName + sheetname;
                lb.Margin     = new Thickness(1, 1, 1, 1);
                //Button b = new Button(); b.Content="x";
                Image b = new Image(); b.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(b_MouseLeftButtonUp);
                b.ToolTip = "Close this dataset"; b.Height = 16.0; b.Width = 22.0;

                string packUri = "pack://application:,,,/BlueSky;component/Images/closetab.png";
                b.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
                sp.Children.Add(lb);
                sp.Children.Add(b); //sp.Children.Add(b);

                TabItem panel = new TabItem();
                //panel.Margin = new Thickness(1, 1, 1, 1);
                b.Tag = panel;                //for remove
                ///////triggers///08Feb2013
                Binding bind = new Binding(); //for close image to hide for inactive tabs
                bind.Source    = panel;
                bind.Path      = new PropertyPath("IsSelected");
                bind.Converter = new BooleanToVisibilityConverter();
                b.SetBinding(Image.VisibilityProperty, bind);

                Style st     = new Style(typeof(TabItem));
                var   brush1 = new SolidColorBrush(Color.FromArgb(255, (byte)212, (byte)227, (byte)242));//#FFD4E3F2
                st.Setters.Add(new Setter()
                {
                    Property = TabItem.BackgroundProperty, Value = brush1
                });

                Trigger tg = new Trigger {
                    Property = TabItem.IsSelectedProperty, Value = true
                };
                var brush2 = new SolidColorBrush(Color.FromArgb(255, (byte)234, (byte)239, (byte)245));//#FFEAEFF5
                tg.Setters.Add(new Setter()
                {
                    Property = TabItem.BackgroundProperty, Value = brush2
                });

                st.Triggers.Add(tg);

                panel.Style = st;
                ///triggers///

                DataPanel datapanel = new DataPanel();
                //datapanel.sortcolnames = sortcolnames;//11Apr2014
                //datapanel.sortorder = sortorder; //14Apr2014
                datapanel.sortasccolnames  = sortasccolnames;
                datapanel.sortdesccolnames = sortdesccolnames;

                VirtualListDynamic vld = new VirtualListDynamic(_analyticsService, ds);
                vld.DataF = _analyticsService.GetDataFrame(ds);
                if (vld.DataF == null)
                {
                    return;                    //03Aug2015 When SaveAs fails and tries to load the dataset closing the current one.
                }
                IList list = vld;
                panel.Tag      = ds; //panel.Tag = ds;
                datapanel.DS   = ds; //sending reference
                datapanel.Data = list;

                datapanel.Variables = ds.Variables;

                datapanel.statusbar.Text = ""; // "No Split";//03Dec2013 Status bar
                datapanel.DisableEnableAllNavButtons();
                panel.Header = sp;             ///Path.GetFileName(ds.FileName);

                panel.Content = datapanel;
                docGroup.Items.Add(panel);//panel
                //panel.IsSelected = true;
                docGroup.SelectedItem = panel;
                //docGroup.SelectedIndex = docGroup.Items.Count - 1;
                //docGroup.Background = Brushes.Red; /color around open dataset
                ///layoutManager.Activate(panel);

                panel.Focus(); //15Oct2015

                //foreach (TabItem tbit in docGroup.Items)
                //{
                //    tbit.IsSelected = false;
                //}
                //panel.IsSelected = true;
                //docGroup.SelectedIndex = docGroup.Items.Count - 1;
            }
            logService.Info("Grid Loading finished.");
        }