示例#1
0
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            SwagLogger.Log(e.Exception, e.Exception.Message);

#if !DEBUG
            UIHelper.StringInputDialog(e.Exception.DeepMessage());
#endif

            e.Handled = true;
        }
        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);
        }
        private void SwagDataHeader_RenameClick(object sender, RoutedEventArgs e)
        {
            Button      btnRename   = (Button)sender;
            MenuItem    miParent    = (MenuItem)((MenuItem)((Grid)btnRename.Parent).Parent).Parent;
            SwagData    swagData    = (SwagData)btnRename.DataContext;
            ContextMenu contextMenu = DependencyObjectHelper.TryFindParent <ContextMenu>(btnRename);

            Grid   grid         = miParent.FindLogicalChild <Grid>("gridRename");
            String newColName   = grid.FindVisualChild <TextBox>().Text;
            String originalName = swagData.Display;

            SwagLogger.LogStart(this, "SwagData Rename |orig={Column}|new={NewColName}|", originalName, newColName);
            swagData.Rename(newColName);
            contextMenu.IsOpen = false;
            SwagLogger.LogEnd(this, "SwagData Rename |orig={Column}|new={NewColName}|", originalName, newColName);
        }
        public static void LoadFiles(this SwagDataSet swagDataSet, IEnumerable <String> files, IEnumerable <KeyValuePairViewModel <String, ParseViewModel> > parseMappers)
        {
            foreach (String file in files)
            {
                SwagLogger.LogStart(swagDataSet, "Load {file}", file);
                SwagData child = SwagDataHelper.FromFile(file, parseMappers);
                SwagLogger.LogEnd(swagDataSet, "Load {file}", file);

                if (child != null)
                {
                    swagDataSet.Children.Add(child);
                }
                else
                {
                    SwagLogger.Log("Load {file} did not yield data (unsupported extenstion).", file);
                }
            }
        }
        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);
        }
        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;
        }
        private void SwagColumnHeader_MoveClick(object sender, RoutedEventArgs e)
        {
            MenuItem       menuItem       = (MenuItem)sender;
            SwagDataColumn swagDataColumn = (SwagDataColumn)menuItem.DataContext;

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

            Int32 offSet         = Int32.Parse(menuItem.Tag.ToString());
            Int32 targetSequence = swagDataColumn.ColSeq + offSet;

            if (targetSequence < 0)
            {
                targetSequence = 0;
            }
            else if (targetSequence > swagDataColumn.SwagDataTable.Columns.Count - 1)
            {
                targetSequence = swagDataColumn.SwagDataTable.Columns.Count - 1;
            }
            swagDataColumn.SetSequence(targetSequence);
            swagDataColumn.SwagDataTable.ResetColumnsCommand.Execute(null);

            SwagLogger.LogEnd(this, "Move Column |col={Column}|", swagDataColumn.ColumnName);
        }
        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);
        }
        public SwagWindowSettingGroup GetWindowSettingGroupByName(String groupName)
        {
            SwagLogger.LogStart(this, "{Service} {Action}", "WindowSettingService", "GetWindowSettingGroupByName");

            SwagWindowSettingGroup windowSettings = _swagWindowSettingsGroupRepository.Get(sg => sg.Name == groupName, null).FirstOrDefault();

            if (windowSettings != null)
            {
                #region Load SwagSettingUnitOfWork
                _swagSettingGroupRepository.RecursiveLoadCollection(windowSettings, "Children");

                #region OLD 2 Changing type to derived WPF type (not needed because of usage of CollectionToViewConverter)
                //#region Get Groups
                //Stack<SwagOverFlow.ViewModels.SwagSettingGroup> groups = new Stack<SwagOverFlow.ViewModels.SwagSettingGroup>();
                //SwagItemPreOrderIterator<SwagSetting> iterator = storedSettings.CreateIterator();
                //for (SwagSetting setting = iterator.First(); !iterator.IsDone; setting = iterator.Next())
                //{
                //    switch (setting)
                //    {
                //        case SwagOverFlow.ViewModels.SwagSettingGroup group:
                //            if (setting != storedSettings)
                //            {
                //                groups.Push(group);
                //            }
                //            break;
                //    }
                //}
                //#endregion Get Groups

                //#region Resolve Groups
                ////Attaching and detaching needs to be in reverse order
                //while (groups.Count > 0)
                //{
                //    SwagOverFlow.ViewModels.SwagSettingGroup group = groups.Pop();
                //    SwagSettingGroup newGroup = new SwagSettingGroup(group);
                //    if (group.Parent != null)
                //    {
                //        group.Parent = null;
                //    }
                //    work.SettingGroups.Detach(group);
                //    work.SettingGroups.Attach(newGroup);
                //}
                //#endregion Resolve Groups

                //windowSettings = new SwagWindowSettingGroup(storedSettings);
                //work.SettingGroups.Detach(storedSettings);
                //work.SettingGroups.Attach(windowSettings);
                //work.SettingGroups.RecursiveLoadChildren(windowSettings);
                #endregion OLD 2 Changing type to derived WPF type (not needed because of usage of CollectionToViewConverter)

                #region OLD - Dynamically creates generic type (handled in Properties of SwagItemViewModel/SwagSettingView instead)
                //SwagItemPreOrderIterator<SwagItemViewModel> iterator = windowSettings.CreateIterator();
                //for (SwagItemViewModel swagItem = iterator.First(); !iterator.IsDone; swagItem = iterator.Next())
                //{
                //    SwagSettingViewModel swagSetting = (SwagSettingViewModel)swagItem;
                //    if (swagSetting.IconString != null)
                //    {
                //        Type iconType = JsonConvert.DeserializeObject<Type>(swagSetting.IconTypeString);
                //        swagSetting.Icon = (Enum)Enum.Parse(iconType, swagSetting.IconString);
                //    }

                //    if (swagSetting.ItemsSource != null)
                //    {
                //        Type itemsSourceType = JsonConvert.DeserializeObject<Type>(swagSetting.ItemsSourceTypeString);
                //        swagSetting.ItemsSource = JsonConvert.DeserializeObject(swagSetting.ItemsSource.ToString(), itemsSourceType);
                //    }

                //    if (!String.IsNullOrEmpty(swagSetting.ValueTypeString))
                //    {
                //        Type typeGenericTemplate = typeof(SwagSettingViewModel<>);
                //        Type valueType = JsonConvert.DeserializeObject<Type>(swagSetting.ValueTypeString);
                //        Type[] typeArgs = { valueType };
                //        Type typeGeneric = typeGenericTemplate.MakeGenericType(typeArgs);
                //        windowSettings.Descendants.Remove(swagSetting);
                //        work.Settings.Delete(swagSetting);

                //        SwagSettingViewModel newSetting = (SwagSettingViewModel)Activator.CreateInstance(typeGeneric, swagSetting);
                //        newSetting.Children = swagSetting.Children;

                //        if (newSetting.ItemsSource != null)
                //        {
                //            Type itemsSourceType = JsonConvert.DeserializeObject<Type>(newSetting.ItemsSourceTypeString);
                //            newSetting.ItemsSource = JsonConvert.DeserializeObject(newSetting.ItemsSource.ToString(), itemsSourceType);
                //        }

                //        swagSetting.Parent.Children.Remove(swagSetting);
                //        swagSetting.Parent.Children.Add(newSetting);

                //        if (valueType == typeof(Boolean))
                //        {
                //            newSetting.Value = Boolean.Parse(swagSetting.Value.ToString());
                //        }
                //        else if (valueType == typeof(String) && swagSetting.Value != null)
                //        {
                //            newSetting.Value = swagSetting.Value.ToString();
                //        }
                //        else if (swagSetting.Value != null)
                //        {
                //            newSetting.Value = JsonConvert.DeserializeObject(swagSetting.Value.ToString(), valueType);
                //        }

                //        work.Settings.Insert(newSetting);
                //    }
                //}
                #endregion OLD
                #endregion Load SwagSettingUnitOfWork

                SwagLogger.Log("{Service} {Action}", "WindowSettingService", "Loaded from database");
            }

            if (windowSettings == null)
            {
                #region Create SwagWindowSettingGroup
                windowSettings      = new SwagWindowSettingGroup(true);
                windowSettings.Name = windowSettings.AlternateId = groupName;
                _swagWindowSettingsGroupRepository.Insert(windowSettings);

                SwagItemPreOrderIterator <SwagSetting> iterator = windowSettings.CreateIterator();
                for (SwagSetting setting = iterator.First(); !iterator.IsDone; setting = iterator.Next())
                {
                    //Mark these properites as modified to have them save properly
                    setting.ObjValue        = setting.ObjValue;
                    setting.ValueTypeString = setting.ValueTypeString;
                    setting.Data            = setting.Data;
                }

                _context.SaveChanges();
                SwagLogger.Log("{Service} {Action}", "WindowSettingService", "Created then saved to database");
                #endregion Create SwagWindowSettingGroup
            }

            SwagLogger.LogEnd(this, "{Service} {Action}", "WindowSettingService", "GetWindowSettingGroupByName");
            return(windowSettings);
        }