Exemple #1
0
        private void Load_Dest_From_Clipboard_Button_Click(object sender, RoutedEventArgs e)
        {
            String data = Clipboard.GetText();
            DataTableCsvStringConverter csvStringToDataTable = new DataTableCsvStringConverter();
            DataTableConvertParams      context = new DataTableConvertParams();

            context.FieldDelim = '\t';
            DataTable dt = csvStringToDataTable.ToDataTable(context, data);

            Dest.DataTable = dt;
        }
 private void Parse_Paste_TSV_txtPreviewExecuted(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
 {
     if (e.Command == ApplicationCommands.Paste)
     {
         String text = Clipboard.GetText();
         DataTableConvertParams cp = new DataTableConvertParams()
         {
             FieldDelim = '\t'
         };
         IDataTableConverter converter = new DataTableCsvStringConverter();
         DataTable           dt        = converter.ToDataTable(cp, text);
         SwagDataSet.AddDataTableCommand.Execute(dt);
         e.Handled = true;
     }
 }
        public static void InitSwagDataTable(SwagDataTable swagDataTable)
        {
            if (swagDataTable.DataTable == null) //Build DataTable
            {
                DataTable dt = new DataTable(swagDataTable.Display);
                foreach (KeyValuePair <String, SwagDataColumn> kvpCol in swagDataTable.Columns)
                {
                    kvpCol.Value.SwagDataTable = swagDataTable;
                    dt.Columns.Add(kvpCol.Value.DataColumn());
                }

                swagDataTable.DictRows.Clear();
                foreach (SwagData swagData in swagDataTable.Children)
                {
                    if (swagData is SwagDataRow swagDataRow)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (KeyValuePair <String, JToken> kvpField in swagDataRow.Value)
                        {
                            dr[kvpField.Key] = ((JValue)kvpField.Value).Value;
                        }
                        swagDataTable.DictRows.Add(dr, swagDataRow);
                        dt.Rows.Add(dr);
                        swagDataRow.DataRow = dr;
                    }
                }

                swagDataTable.DataTable = dt;
                swagDataTable.InitColumns();
            }
            else //Build From DataTable
            {
                #region Clear Columns and Rows for instance
                swagDataTable.Columns.Clear();
                swagDataTable.Children.Clear();
                #endregion Clear Columns and Rows for instance

                #region Add Columns and Rows for instance
                swagDataTable.InitColumns();
                foreach (DataColumn dc in swagDataTable.DataTable.Columns)
                {
                    SwagDataColumn sdc = new SwagDataColumn()
                    {
                        ColumnName = dc.ColumnName, DataType = dc.DataType
                    };
                    sdc.SwagDataTable  = swagDataTable;
                    sdc.DataTypeString = sdc.DataTypeString;
                    swagDataTable.Children.Add(sdc);
                    swagDataTable.Columns.Add(dc.ColumnName, sdc);
                }

                swagDataTable.DictRows.Clear();
                foreach (DataRow dr in swagDataTable.DataTable.Rows)
                {
                    SwagDataRow row = new SwagDataRow(dr);
                    row.Value           = row.Value;
                    row.ValueTypeString = row.ValueTypeString;
                    swagDataTable.Children.Add(row);
                    swagDataTable.DictRows.Add(row.DataRow, row);
                }
                #endregion Add Columns and Rows for instance
            }

            #region InitViews
            CollectionViewSource columnsVisibilitySource, columnsFilterSource;
            columnsVisibilitySource = new CollectionViewSource()
            {
                Source = swagDataTable.Columns
            };
            columnsFilterSource = new CollectionViewSource()
            {
                Source = swagDataTable.Columns
            };
            columnsFilterSource.View.Filter = (itm) =>
            {
                KeyValuePair <String, SwagDataColumn> kvp = (KeyValuePair <String, SwagDataColumn>)itm;
                return(kvp.Value.HasAppliedFilter);
            };
            swagDataTable.ColumnsVisibilityView = columnsVisibilitySource.View;
            swagDataTable.ColumnsFilterView     = columnsFilterSource.View;
            #endregion InitViews

            #region FilterCommand
            swagDataTable.FilterCommand = new RelayCommand(() =>
            {
                ICollectionView view = CollectionViewSource.GetDefaultView(swagDataTable.DataTable.DefaultView);
                if (view is BindingListCollectionView)      //Assuming you are DataView for now
                {
                    BindingListCollectionView bindingView = (BindingListCollectionView)view;
                    //https://stackoverflow.com/questions/9385489/why-errors-when-filters-datatable-with-collectionview
                    bindingView.CancelEdit();

                    String combinedFilter = "";
                    foreach (KeyValuePair <string, SwagDataColumn> kvp in swagDataTable.Columns)
                    {
                        if (kvp.Value.HasAppliedFilter)
                        {
                            String filterTemp = kvp.Value.AppliedFilter;
                            if (kvp.Value.AppliedFilter.Contains("'(Blanks)'"))
                            {
                                filterTemp = string.Format("({0} OR CONVERT([{1}], 'System.String') = '' OR [{1}] IS NULL)", kvp.Value.AppliedFilter, kvp.Key);
                            }
                            combinedFilter = string.Format("{0}{1} AND ", combinedFilter, filterTemp);
                        }
                    }

                    if (combinedFilter.EndsWith("AND "))
                    {
                        combinedFilter = combinedFilter.Substring(0, combinedFilter.Length - 4);
                    }

                    bindingView.CustomFilter = combinedFilter;
                }

                columnsFilterSource.View.Refresh();
            });
            #endregion FilterCommand

            #region ExportCommand
            swagDataTable.ExportCommand = new RelayCommand(() =>
            {
                IDataTableConverter converter  = null;
                string dialogFilter            = "";
                SwagTableExportType exportType = swagDataTable.Settings["Export"]["Type"].GetValue <SwagTableExportType>();
                switch (exportType)
                {
                case SwagTableExportType.Csv:
                    converter    = new DataTableCsvStringConverter();
                    dialogFilter = "CSV files (*.csv)|*.csv";
                    break;

                case SwagTableExportType.TSql_Command:
                    converter    = new DataTableTSqlCommandConverter();
                    dialogFilter = "SQL files (*.sql)|*.sql";
                    break;

                case SwagTableExportType.Sqlite:
                    SaveFileDialog sfd   = new SaveFileDialog();
                    sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    sfd.FileName         = Path.ChangeExtension(swagDataTable.DataTable.TableName, null);
                    sfd.Filter           = "SQLite files (*.db;*.sqlite)|*.db;*.sqlite";
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        if (sfd.ShowDialog() ?? false)
                        {
                            DataSetSqliteFileConverter dsConverter = new DataSetSqliteFileConverter();
                            DataSet ds = new DataSet();
                            ds.Tables.Add(swagDataTable.DataTable.Copy());
                            dsConverter.FromDataSet(null, ds, sfd.FileName);
                        }
                    }));
                    return;

                case SwagTableExportType.Sqlite_Command:
                    converter    = new DataTableSqliteCommandConverter();
                    dialogFilter = "SQLite command files (*.cmd)|*.cmd";
                    break;
                }

                Object output = converter.FromDataTableToObject(new DataTableConvertParams(), swagDataTable.DataTable.DefaultView.ToTable());

                switch (swagDataTable.Settings["Export"]["Destination"].GetValue <SwagTableDestinationType>())
                {
                case SwagTableDestinationType.Clipboard:
                    Clipboard.SetText(output.ToString());
                    break;

                case SwagTableDestinationType.File:
                    SaveFileDialog sfd   = new SaveFileDialog();
                    sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    sfd.FileName         = swagDataTable.DataTable.TableName;
                    sfd.Filter           = dialogFilter;

                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        if (sfd.ShowDialog() ?? false)
                        {
                            File.WriteAllText(sfd.FileName, output.ToString());
                        }
                    }));
                    break;

                case SwagTableDestinationType.New_Window:
                    Window window         = new Window();
                    TextBox textBox       = new TextBox();
                    textBox.AcceptsReturn = textBox.AcceptsTab = true;
                    textBox.Text          = output.ToString();
                    window.Content        = textBox;
                    window.Show();
                    break;
                }
            });
            #endregion ExportCommand

            #region ImportCommand
            swagDataTable.ImportCommand = new RelayCommand(() =>
            {
                IDataTableConverter converter = null;
                DataTableConvertParams cp     = new DataTableConvertParams();

                string dialogFilter = "";
                String inputText    = "";

                switch (swagDataTable.Settings["Import"]["Type"].GetValue <SwagTableImportType>())
                {
                case SwagTableImportType.Csv:
                    converter    = new DataTableCsvStringConverter();
                    dialogFilter = "CSV files (*.csv)|*.csv";
                    break;

                case SwagTableImportType.Tsv:
                    converter     = new DataTableCsvStringConverter();
                    cp.FieldDelim = '\t';
                    dialogFilter  = "TSV files (*.tsv)|*.tsv";
                    break;
                }

                switch (swagDataTable.Settings["Import"]["Source"].GetValue <SwagTableSourceType>())
                {
                case SwagTableSourceType.File:
                    OpenFileDialog ofd   = new OpenFileDialog();
                    ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    ofd.Filter           = dialogFilter;

                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        if (ofd.ShowDialog() ?? false)
                        {
                            inputText = File.ReadAllText(ofd.FileName);
                        }
                        else
                        {
                            return;
                        }
                    }));
                    break;

                case SwagTableSourceType.Clipboard:
                    inputText = Clipboard.GetText();
                    break;
                }

                DataTable dtInput       = converter.ToDataTable(cp, inputText);
                swagDataTable.DataTable = dtInput;
            });
            #endregion ImportCommand

            #region ApplyColumnVisibilityFilterCommand
            swagDataTable.ApplyColumnVisibilityFilterCommand =
                new RelayCommand(() =>
            {
                columnsVisibilitySource.View.Filter = (itm) =>
                {
                    KeyValuePair <String, SwagDataColumn> kvp = (KeyValuePair <String, SwagDataColumn>)itm;
                    return(SearchHelper.Evaluate(
                               kvp.Key,
                               swagDataTable.Settings["ColumnEditor"]["Visibility"]["Search"]["Text"].GetValue <string>(),
                               false,
                               swagDataTable.Settings["ColumnEditor"]["Visibility"]["Search"]["FilterMode"].GetValue <FilterMode>(),
                               false));
                };
            });
            #endregion ApplyColumnVisibilityFilterCommand

            #region ApplyColumnFiltersFilterCommand
            swagDataTable.ApplyColumnFiltersFilterCommand =
                new RelayCommand(() =>
            {
                columnsFilterSource.View.Filter = (itm) =>
                {
                    KeyValuePair <String, SwagDataColumn> kvp = (KeyValuePair <String, SwagDataColumn>)itm;
                    return(kvp.Value.HasAppliedFilter && (SearchHelper.Evaluate(
                                                              kvp.Key,
                                                              swagDataTable.Settings["ColumnEditor"]["Filters"]["Search"]["Text"].GetValue <string>(),
                                                              false,
                                                              swagDataTable.Settings["ColumnEditor"]["Filters"]["Search"]["FilterMode"].GetValue <FilterMode>(),
                                                              false) || SearchHelper.Evaluate(
                                                              kvp.Value.AppliedFilter,
                                                              swagDataTable.Settings["ColumnEditor"]["Filters"]["Search"]["Text"].GetValue <string>(),
                                                              false,
                                                              swagDataTable.Settings["ColumnEditor"]["Filters"]["Search"]["FilterMode"].GetValue <FilterMode>(),
                                                              false)));
                };
            });
            #endregion ApplyColumnFiltersFilterCommand

            #region ResetColumnsCommand
            swagDataTable.ResetColumnsCommand =
                new RelayCommand(() =>
            {
                swagDataTable.DelaySave        = true;
                columnsVisibilitySource.Source = columnsFilterSource.Source = swagDataTable.Columns;
                columnsVisibilitySource.View.Refresh();
                columnsFilterSource.View.Refresh();
                swagDataTable.OnPropertyChangedPublic("ColumnsView");
                swagDataTable.OnPropertyChangedPublic("ColumnCount");
                swagDataTable.DelaySave = false;
                var tempCols            = swagDataTable.Columns;
                swagDataTable.Columns   = null;
                swagDataTable.Columns   = tempCols;
            });
            #endregion ResetColumnsCommand

            #region InitSettings
            swagDataTable.Settings.TryAddChildSetting("ColumnEditor", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconFontAwesomeKind.EditSolid
            });
            swagDataTable.Settings["ColumnEditor"].TryAddChildSetting("Visibility", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconMaterialKind.Eye
            });
            swagDataTable.Settings["ColumnEditor"]["Visibility"].TryAddChildSetting("Search", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.Magnify
            });
            swagDataTable.Settings["ColumnEditor"]["Visibility"]["Search"].TryAddChildSetting("Text", new SwagSetting <String>()
            {
                Icon = PackIconBoxIconsKind.RegularText
            });
            swagDataTable.Settings["ColumnEditor"]["Visibility"]["Search"].TryAddChildSetting("FilterMode", new SwagSetting <FilterMode>()
            {
                SettingType = SettingType.DropDown, Value = FilterMode.CONTAINS, Icon = PackIconUniconsKind.Filter, ItemsSource = (FilterMode[])Enum.GetValues(typeof(FilterMode))
            });
            swagDataTable.Settings["ColumnEditor"].TryAddChildSetting("Filters", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconMaterialKind.Filter
            });
            swagDataTable.Settings["ColumnEditor"]["Filters"].TryAddChildSetting("Search", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.Magnify
            });
            swagDataTable.Settings["ColumnEditor"]["Filters"]["Search"].TryAddChildSetting("Text", new SwagSetting <String>()
            {
                Icon = PackIconBoxIconsKind.RegularText
            });
            swagDataTable.Settings["ColumnEditor"]["Filters"]["Search"].TryAddChildSetting("FilterMode", new SwagSetting <FilterMode>()
            {
                SettingType = SettingType.DropDown, Value = FilterMode.CONTAINS, Icon = PackIconUniconsKind.Filter, ItemsSource = (FilterMode[])Enum.GetValues(typeof(FilterMode))
            });
            swagDataTable.Settings.TryAddChildSetting("Search", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.Table, Icon2 = PackIconMaterialKind.Magnify
            });
            swagDataTable.Settings["Search"].TryAddChildSetting("Text", new SwagSetting <String>()
            {
                Icon = PackIconBoxIconsKind.RegularText
            });
            swagDataTable.Settings["Search"].TryAddChildSetting("FilterMode", new SwagSetting <FilterMode>()
            {
                SettingType = SettingType.DropDown, Value = FilterMode.CONTAINS, Icon = PackIconUniconsKind.Filter, ItemsSource = (FilterMode[])Enum.GetValues(typeof(FilterMode))
            });
            swagDataTable.Settings.TryAddChildSetting("Export", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.Export
            });
            swagDataTable.Settings["Export"].TryAddChildSetting("Type", new SwagSetting <SwagTableExportType>()
            {
                SettingType = SettingType.DropDown, Value = SwagTableExportType.Csv, Icon = PackIconMaterialKind.Export, Icon2 = PackIconFontAwesomeKind.ShapesSolid, ItemsSource = (SwagTableExportType[])Enum.GetValues(typeof(SwagTableExportType))
            });
            swagDataTable.Settings["Export"].TryAddChildSetting("Destination", new SwagSetting <SwagTableDestinationType>()
            {
                SettingType = SettingType.DropDown, Value = SwagTableDestinationType.Clipboard, Icon = PackIconMaterialKind.Target, ItemsSource = (SwagTableDestinationType[])Enum.GetValues(typeof(SwagTableDestinationType))
            });
            swagDataTable.Settings.TryAddChildSetting("Import", new SwagSettingGroup()
            {
                Icon = PackIconMaterialKind.Import
            });
            swagDataTable.Settings["Import"].TryAddChildSetting("Type", new SwagSetting <SwagTableImportType>()
            {
                SettingType = SettingType.DropDown, Value = SwagTableImportType.Tsv, Icon = PackIconMaterialKind.Import, Icon2 = PackIconFontAwesomeKind.ShapesSolid, ItemsSource = (SwagTableImportType[])Enum.GetValues(typeof(SwagTableImportType))
            });
            swagDataTable.Settings["Import"].TryAddChildSetting("Source", new SwagSetting <SwagTableSourceType>()
            {
                SettingType = SettingType.DropDown, Value = SwagTableSourceType.Clipboard, Icon = PackIconMaterialKind.SourceCommitStart, ItemsSource = (SwagTableSourceType[])Enum.GetValues(typeof(SwagTableSourceType))
            });
            #endregion InitSettings

            #region InitTabs
            SwagTabGroup tabs = new SwagTabGroup();
            tabs["ColumnEditor"] = new SwagTabGroup()
            {
                Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconFontAwesomeKind.EditSolid, Display = "Column Editor"
            };
            tabs["Search"] = new SwagTabItem()
            {
                Icon = PackIconMaterialKind.TextSearch
            };
            tabs["Export"] = new SwagTabItem()
            {
                Icon = PackIconMaterialKind.Table, Icon2 = PackIconMaterialKind.ArrowRightThick
            };
            tabs["Import"] = new SwagTabItem()
            {
                Icon = PackIconMaterialKind.Table, Icon2 = PackIconMaterialKind.ArrowLeftThick
            };
            tabs["Settings"] = new SwagTabItem()
            {
                Icon = PackIconMaterialKind.Cog
            };
            tabs["ColumnEditor"]["Visibility"] = new SwagTabItem()
            {
                Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconMaterialKind.Eye
            };
            tabs["ColumnEditor"]["Filters"] = new SwagTabItem()
            {
                Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconMaterialKind.Filter
            };
            //tabs["ColumnEditor"]["Add"] = new SwagTabItem() { Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconMaterialKind.Plus };
            //tabs["ColumnEditor"]["View"] = new SwagTabItem() { Icon = PackIconMaterialKind.TableColumn, Icon2 = PackIconMaterialKind.Magnify };
            swagDataTable.Tabs = tabs;
            SwagItemPreOrderIterator <SwagTabItem> iterator = tabs.CreateIterator();
            for (SwagTabItem tabItem = iterator.First(); !iterator.IsDone; tabItem = iterator.Next())
            {
                tabItem.ViewModel = swagDataTable;
            }
            //swagDataTable.Tabs.SwagItemChanged += _tabs_SwagItemChanged;
            //swagDataTable.Tabs.PropertyChangedExtended += _tabs_PropertyChangedExtended;
            #endregion InitTabs

            swagDataTable.PropertyChanged += SwagDataTable_PropertyChanged;
            swagDataTable.InitDataTable();
            swagDataTable.IsInitialized = true;
        }