Ejemplo n.º 1
0
 private void View(SwagDataColumn swagDataColumn)
 {
     if (swagDataColumn.Parent != null && swagDataColumn.Parent is SwagDataTable)
     {
         SwagDataTable parent = (SwagDataTable)swagDataColumn.Parent;
         parent.SelectedColumn = swagDataColumn;
         View(parent);
     }
 }
Ejemplo n.º 2
0
 private void View(SwagDataTable swagDataTable)
 {
     if (swagDataTable.Parent != null && swagDataTable.Parent is SwagDataSet)
     {
         SwagDataSet parent = (SwagDataSet)swagDataTable.Parent;
         parent.SelectedChild = swagDataTable;
         View(parent);
     }
 }
Ejemplo n.º 3
0
        private static void SwagDataTablePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SwagDataGrid swagDataGrid = (SwagDataGrid)d;

            if (swagDataGrid != null && swagDataGrid.SwagDataTable != null && !swagDataGrid.SwagDataTable.IsInitialized)
            {
                SwagDataTable swagDataTable = swagDataGrid.SwagDataTable;
                InitSwagDataTable(swagDataTable);
            }
        }
Ejemplo n.º 4
0
        private static void SwagDataTable_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SwagDataTable swagDataTable = (SwagDataTable)sender;

            switch (e.PropertyName)
            {
            case "DataTable":
                swagDataTable.Columns.Clear();
                InitSwagDataTable(swagDataTable);
                break;
            }
        }
Ejemplo n.º 5
0
        private void SwagColumnHeader_FillEmptyInputClick(object sender, RoutedEventArgs e)
        {
            Button         btnFill        = (Button)sender;
            MenuItem       miParent       = (MenuItem)((MenuItem)((Grid)btnFill.Parent).Parent).Parent;
            SwagDataColumn swagDataColumn = (SwagDataColumn)btnFill.DataContext;
            SwagDataTable  swagDataTable  = swagDataColumn.SwagDataTable;
            ContextMenu    contextMenu    = DependencyObjectHelper.TryFindParent <ContextMenu>(btnFill);
            String         colName        = swagDataColumn.ColumnName;
            Type           targetType     = swagDataColumn.DataType;

            Grid   grid             = miParent.FindLogicalChild <Grid>("gridFillEmptyInput");
            String defaultValueText = grid.FindVisualChild <TextBox>().Text;

            SwagLogger.LogStart(this, "Fill Column with Input |col={Column}|inpt={Input}|", swagDataColumn.ColumnName, defaultValueText);

            if (defaultValueText != "")
            {
                #region Resolve defaultValue
                Object defaultValue = DBNull.Value;
                if (targetType.GetTypeCode() != TypeCode.String)
                {
                    try
                    {
                        defaultValue = Convert.ChangeType(defaultValueText, targetType);
                    }
                    catch
                    {
                        defaultValue = DBNull.Value;
                    }
                }
                else
                {
                    defaultValue = defaultValueText;
                }
                #endregion Resolve defaultValue

                swagDataTable.DelaySave = true;
                using (var scope = swagDataTable.GetFreezeListScope())
                {
                    foreach (DataRowView drv in swagDataTable.DataTable.DefaultView)
                    {
                        if (drv[colName] == null || drv[colName] == DBNull.Value || drv[colName].ToString() == "")
                        {
                            drv[colName] = defaultValue;
                        }
                    }
                }
                swagDataTable.DelaySave = false;
            }

            contextMenu.IsOpen = false;
            SwagLogger.LogEnd(this, "Fill Column with Input |col={Column}|inpt={Input}|", swagDataColumn.ColumnName, defaultValueText);
        }
Ejemplo n.º 6
0
        private void Search_OnSearch(object sender, RoutedEventArgs e)
        {
            SearchTextBox  searchTextBox  = (SearchTextBox)sender;
            SwagData       swagData       = (SwagData)((SwagTabItem)searchTextBox.DataContext).ViewModel;
            SwagDataResult swagDataResult = SwagDataTable.Search(searchTextBox.Text, searchTextBox.FilterMode,
                                                                 (sdc, sdr, searchValue, filterMode) =>
            {
                String compareTarget = sdr.DataRow[sdc.ColumnName].ToString();
                String compareValue  = searchValue;
                return(SearchHelper.Evaluate(compareTarget, compareValue, false, filterMode, false));
            });

            swagData.SwagDataResult = swagDataResult;
        }
Ejemplo n.º 7
0
        private void View(SwagDataColumn swagDataColumn)
        {
            DataGridColumn dataGridColumn = DataGrid.Columns.FirstOrDefault(c => c.Header.ToString() == swagDataColumn.ColumnName);
            SwagDataTable  swagDataTable  = swagDataColumn.SwagDataTable;

            DataGrid.ScrollIntoView(null, dataGridColumn);
            foreach (KeyValuePair <String, SwagDataColumn> kvp in swagDataTable.Columns)
            {
                kvp.Value.IsSelected = false;
            }
            swagDataColumn.IsSelected = true;
            Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
            {
                DataGrid.Focus();
            }));
        }
Ejemplo n.º 8
0
        private void View(SwagDataRowResult rowResult)
        {
            SwagDataColumn swagDataColumn = (SwagDataColumn)rowResult.Parent.SwagData;
            SwagDataRow    swagDataRow    = (SwagDataRow)rowResult.SwagData;
            DataRowView    drv            = drv = swagDataColumn.SwagDataTable.DataTable.DefaultView[swagDataColumn.SwagDataTable.DataTable.Rows.IndexOf(swagDataRow.DataRow)];;
            SwagDataTable  swagDataTable  = swagDataColumn.SwagDataTable;
            DataGridColumn dataGridColumn = DataGrid.Columns.FirstOrDefault(c => c.Header.ToString() == swagDataColumn.ColumnName);

            swagDataTable = swagDataColumn.SwagDataTable;

            Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
            {
                DataGridCellInfo cellInfo = new DataGridCellInfo(drv, dataGridColumn);
                DataGrid.ScrollIntoView(cellInfo.Item, cellInfo.Column);
                DataGrid.SelectedCells.Clear();
                DataGrid.SelectedCells.Add(cellInfo);
                DataGrid.CurrentCell = cellInfo;
                DataGrid.Focus();
            }));
        }
Ejemplo n.º 9
0
        private void SwagColumnHeader_SelectColumnValueClick(object sender, RoutedEventArgs e)
        {
            MenuItem             menuItem             = (MenuItem)sender;
            ContextMenu          contextMenu          = DependencyObjectHelper.TryFindParent <ContextMenu>(menuItem);
            FrameworkElement     placementTarget      = (FrameworkElement)contextMenu.PlacementTarget;
            DataGridColumnHeader dataGridColumnHeader = DependencyObjectHelper.TryFindParent <DataGridColumnHeader>(placementTarget);
            SwagDataColumn       swagDataColumn       = (SwagDataColumn)menuItem.DataContext;
            SwagDataTable        swagDataTable        = swagDataColumn.SwagDataTable;

            SwagLogger.LogStart(this, "Select Column |col={Column}|", swagDataColumn.ColumnName);

            DataGrid.SelectedCellsChanged -= DataGrid_SelectedCellsChanged;
            DataGrid.SelectedCells.Clear();
            foreach (DataRowView drv in swagDataTable.DataTable.DefaultView)
            {
                DataGrid.SelectedCells.Add(new DataGridCellInfo(drv, dataGridColumnHeader.Column));
            }
            DataGrid.SelectedCellsChanged += DataGrid_SelectedCellsChanged;
            DataGrid_SelectedCellsChanged(null, null);
            SwagLogger.LogStart(this, "Select Column |col={Column}|", swagDataColumn.ColumnName);
        }
Ejemplo n.º 10
0
        private void SwagColumnHeader_FillEmptyDefaultClick(object sender, RoutedEventArgs e)
        {
            MenuItem       menuItem       = (MenuItem)sender;
            SwagDataColumn swagDataColumn = (SwagDataColumn)menuItem.DataContext;
            SwagDataTable  swagDataTable  = swagDataColumn.SwagDataTable;
            String         colName        = swagDataColumn.ColumnName;
            Type           targetType     = swagDataColumn.DataType;

            SwagLogger.LogStart(this, "Fill Column with Default |col={Column}|", swagDataColumn.ColumnName);
            swagDataTable.DelaySave = true;
            using (var scope = swagDataTable.GetFreezeListScope())
            {
                foreach (DataRowView drv in swagDataTable.DataTable.DefaultView)
                {
                    if (drv[colName] == null || drv[colName] == DBNull.Value || drv[colName].ToString() == "")
                    {
                        drv[colName] = Activator.CreateInstance(targetType);
                    }
                }
            }
            swagDataTable.DelaySave = false;
        }
Ejemplo n.º 11
0
        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;
        }
Ejemplo n.º 12
0
        private async void SwagColumnHeader_ConvertClick(object sender, RoutedEventArgs e)
        {
            MenuItem       menuItem = (MenuItem)sender;
            MenuItem       miParent = (MenuItem)menuItem.Parent;
            SwagDataColumn originalSwagDataColumn = (SwagDataColumn)menuItem.DataContext;
            SwagDataTable  swagDataTable          = originalSwagDataColumn.SwagDataTable;
            Type           targetType             = (Type)menuItem.Tag;
            Grid           grid             = miParent.FindLogicalChild <Grid>("gridConvertOptions");
            Boolean        keepOriginal     = grid.FindVisualChild <CheckBox>().IsChecked ?? false;
            String         defaultValueText = grid.FindVisualChild <TextBox>().Text;
            DataTable      dt         = originalSwagDataColumn.SwagDataTable.DataTable;
            String         newColName = $"{originalSwagDataColumn.ColumnName}{targetType.Name}";

            swagDataTable.DelaySave = true;

            SwagLogger.LogStart(this, "Convert Column |col={Column}|", originalSwagDataColumn.ColumnName);

            Int32 count = 1;

            while (dt.Columns.Contains(newColName))
            {
                newColName = $"{originalSwagDataColumn.ColumnName}{count}{targetType.Name}";
                count++;
            }

            SwagDataColumn newSwagDataColumn = new SwagDataColumn()
            {
                ColumnName = newColName, DataType = targetType
            };

            swagDataTable.Columns.Add(newSwagDataColumn.ColumnName, newSwagDataColumn);
            SwagWindow.GlobalIsBusy = true;
            await Task.Run(() =>
            {
                Func <Type, String, object, object> convert = (type, input, defaultOutput) =>
                {
                    switch (type.Name)
                    {
                    case "Int32":
                        if (Int32.TryParse(input, out Int32 outInt32))
                        {
                            return(outInt32);
                        }
                        break;

                    case "Decimal":
                        if (Decimal.TryParse(input, out Decimal outDecimal))
                        {
                            return(outDecimal);
                        }
                        break;

                    case "DateTime":
                        if (DateTime.TryParse(input, out DateTime outDateTime))
                        {
                            return(outDateTime);
                        }
                        break;

                    case "TimeSpan":
                        if (TimeSpan.TryParse(input, out TimeSpan outTimeSpan))
                        {
                            return(outTimeSpan);
                        }
                        break;

                    case "String":
                    default:
                        return(input.ToString());
                    }
                    return(defaultOutput);
                };

                #region Resolve defaultValue
                Object defaultValue = DBNull.Value;
                if (targetType.GetTypeCode() != TypeCode.String)
                {
                    defaultValue = convert(targetType, defaultValueText, DBNull.Value);
                }
                else
                {
                    defaultValue = defaultValueText;
                }
                #endregion Resolve defaultValue

                #region Resolve Rows
                swagDataTable.DelaySave = true;
                using (var scope = swagDataTable.GetFreezeListScope())
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr[newColName] = convert(targetType, dr[$"{originalSwagDataColumn.ColumnName}"].ToString(), defaultValue);
                    }
                }
                swagDataTable.DelaySave = false;
                #endregion Resolve Rows
            });

            SwagWindow.GlobalIsBusy = false;
            if (!keepOriginal)
            {
                newSwagDataColumn.SetSequence(originalSwagDataColumn.ColSeq);
                originalSwagDataColumn.Remove();
                newSwagDataColumn.Rename(originalSwagDataColumn.ColumnName);
            }

            SwagLogger.LogEnd(this, "Convert Column |col={Column}|", originalSwagDataColumn.ColumnName);
        }