コード例 #1
0
        protected override void OnPopupOpened()
        {
            base.OnPopupOpened();

            // загружаем вид
            if (!_isSettingLoaded)
            {
                SaveRestoreLayoutHelper.RestoreLayout(this, FormComponents.MenuAndComponents);
                _isSettingLoaded = true;
            }

            // если перед открытием нужно дозапросить данные, то делаем это
            if (!string.IsNullOrEmpty(_displayValueForFilteringAfterPopupOpened))
            {
                var filter = _displayValueForFilteringAfterPopupOpened;
                _displayValueForFilteringAfterPopupOpened = null;

                RefreshDataByDisplayMemverValue(filter);

                // восстанавиливаем необходимость автопоиска данных
                IncrementalFiltering = _previosIncrementalFiltering;
            }
            else
            if (IsSimpleMode)
            {
                RefreshData(false);
            }

            if (PopupContentGridControl != null)
            {
                PopupContentGridControl.KeyUp += PopupContentGridControlOnKey;
            }
        }
コード例 #2
0
ファイル: Module.cs プロジェクト: zaharvsb52/MyWmsPro
        private static void InitSaveRestoreStrategies()
        {
            // учим как сохранять/восстанавливать Grid
            SaveRestoreLayoutHelper.RegisterSaveRestoreStrategy(typeof(GridDataControlBase), context =>
            {
                if (!context.ValidateAction())
                {
                    return;
                }
                var controlGrid = context.ProcessingObject as CustomGridControl;
                var controlTree = context.ProcessingObject as CustomTreeListControl;
                if (controlGrid == null && controlTree == null)
                {
                    return;
                }

                switch (context.ActionType)
                {
                case SaveRestoreActionType.Save:
                    foreach (var fileConfig in context.FileNames)
                    {
                        var vers           = fileConfig.Version == null ? new Version(0, 0, 0, 0) : new Version(fileConfig.Version.Major, fileConfig.Version.Minor, fileConfig.Version.Build, fileConfig.Version.Revision + 1);
                        fileConfig.Version = vers;
                    }
                    PrepareSaveFile(context);
                    foreach (var fileNames in context.FileNames)
                    {
                        fileNames.SetCurrentFileName();
                    }

                    if (context.FileNames.Length > 0)
                    {
                        var fl = new string[context.FileNames.Length];

                        for (var i = 0; i < fl.Length; i++)
                        {
                            fl[i] = context.FileNames[i].GetFullFileNameClient();
                            if (LayoutInfo.ContainsKey(fl[i]))
                            {
                                LayoutInfo.Remove(fl[i]);
                            }
                        }

                        if (controlGrid != null)
                        {
                            controlGrid.SaveLayoutToXml(fl);
                        }
                        else
                        {
                            controlTree.SaveLayoutToXml(fl);
                        }
                    }
                    break;

                case SaveRestoreActionType.Restore:
                    for (var i = 0; i < context.FileNames.Length; i++)
                    {
                        var fileNames      = context.FileNames[i];
                        var filenameClient = fileNames.CurrentFullFileName;

                        if (string.IsNullOrEmpty(filenameClient) || !File.Exists(filenameClient))
                        {
                            filenameClient = string.Empty;
                            if (context.FilesDb != null)
                            {
                                byte[] filedb;
                                var filenameBd = fileNames.GetFileNameBd();
                                if (context.FilesDb.ContainsKey(filenameBd) &&
                                    (filedb = context.FilesDb[filenameBd].Text) != null)
                                {
                                    fileNames.Version = context.FilesDb[filenameBd].Version;
                                    PrepareSaveFile(context.FileNames[i]);
                                    fileNames.SetCurrentFileName();
                                    filenameClient = fileNames.CurrentFullFileName;
                                    File.WriteAllBytes(filenameClient, filedb);
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(filenameClient))
                        {
                            if (LayoutInfo.ContainsKey(filenameClient))
                            {
                                var layout = LayoutInfo[filenameClient];
                                if (controlGrid != null)
                                {
                                    controlGrid.RestoreLayoutFromString(layout, i);
                                }
                                else
                                {
                                    controlTree.RestoreLayoutFromString(layout, i);
                                }
                            }
                            else
                            {
                                if (File.Exists(filenameClient))
                                {
                                    try
                                    {
                                        using (var fileStream = new FileStream(filenameClient, FileMode.Open))
                                        {
                                            using (var wr = new StreamReader(fileStream))
                                            {
                                                var layout = wr.ReadToEnd();
                                                if (controlGrid != null)
                                                {
                                                    controlGrid.RestoreLayoutFromString(layout, i);
                                                }
                                                else
                                                {
                                                    controlTree.RestoreLayoutFromString(layout, i);
                                                }
                                                LayoutInfo[filenameClient] = layout;
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        File.Delete(filenameClient);
                                        Log.Error(
                                            new OperationException(
                                                string.Format(ExceptionResources.RestoreLayoutBadFile,
                                                              filenameClient, Environment.NewLine), ex));
                                    }
                                }
                            }
                        }
                    }

                    break;

                case SaveRestoreActionType.Clear:
                    Clear(context);
                    break;

                default:
                    throw new DeveloperException("Unknown enum value.");
                }
            });


            // учим как сохранять/восстанавливать CustomDataLayoutControl
            SaveRestoreLayoutHelper.RegisterSaveRestoreStrategy(typeof(CustomDataLayoutControl), context =>
            {
                if (!context.ValidateAction())
                {
                    return;
                }
                var control = context.ProcessingObject as CustomDataLayoutControl;
                if (control == null)
                {
                    throw new DeveloperException("Strategy is not valid.");
                }

                switch (context.ActionType)
                {
                case SaveRestoreActionType.Save:
                    foreach (var fileConfig in context.FileNames)
                    {
                        var vers = fileConfig.Version == null
                                ? new Version(0, 0, 0, 0)
                                : new Version(fileConfig.Version.Major, fileConfig.Version.Minor,
                                              fileConfig.Version.Build, fileConfig.Version.Revision + 1);
                        fileConfig.Version = vers;
                    }
                    PrepareSaveFile(context);

                    foreach (var fileconfig in context.FileNames)
                    {
                        fileconfig.SetCurrentFileName();

                        var filenameClient = fileconfig.GetFullFileNameClient();
                        if (LayoutInfo.ContainsKey(filenameClient))
                        {
                            LayoutInfo.Remove(filenameClient);
                        }
                        using (var fileStream = new FileStream(filenameClient, FileMode.OpenOrCreate))
                        {
                            using (var wr = XmlWriter.Create(fileStream))
                            {
                                control.WriteToXML(wr);
                            }
                            fileStream.Flush();
                        }
                    }
                    break;

                case SaveRestoreActionType.Restore:
                    foreach (var fileConfig in context.FileNames)
                    {
                        string filenameClient = fileConfig.CurrentFullFileName;

                        if (string.IsNullOrEmpty(filenameClient) || !File.Exists(filenameClient))
                        {
                            filenameClient = string.Empty;
                            if (context.FilesDb != null)
                            {
                                byte[] filedb;
                                var filenameBd = fileConfig.GetFileNameBd();
                                if (context.FilesDb.ContainsKey(filenameBd) &&
                                    (filedb = context.FilesDb[filenameBd].Text) != null)
                                {
                                    fileConfig.Version = context.FilesDb[filenameBd].Version;
                                    PrepareSaveFile(fileConfig);
                                    fileConfig.SetCurrentFileName();
                                    filenameClient = fileConfig.CurrentFullFileName;
                                    File.WriteAllBytes(filenameClient, filedb);
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(filenameClient))
                        {
                            if (LayoutInfo.ContainsKey(filenameClient))
                            {
                                control.RestoreLayout(LayoutInfo[filenameClient]);
                            }
                            else
                            {
                                if (File.Exists(filenameClient))
                                {
                                    var needDelete = false;
                                    using (var fileStream = new FileStream(filenameClient, FileMode.Open))
                                    {
                                        using (var wr = new StreamReader(fileStream))
                                        {
                                            try
                                            {
                                                var layout = wr.ReadToEnd();
                                                control.RestoreLayout(layout);
                                                LayoutInfo[filenameClient] = layout;
                                            }
                                            catch (Exception ex)
                                            {
                                                needDelete = true;
                                                Log.Error(
                                                    new OperationException(
                                                        string.Format(ExceptionResources.RestoreLayoutBadFile,
                                                                      filenameClient, Environment.NewLine), ex));
                                            }
                                        }
                                    }
                                    if (needDelete)
                                    {
                                        File.Delete(filenameClient);
                                    }
                                }
                            }
                        }
                    }
                    break;

                case SaveRestoreActionType.Clear:
                    Clear(context);
                    break;

                default:
                    throw new DeveloperException("Unknown enum value.");
                }
            });

            // учим как сохранять/восстанавливать CustomLookUpEdit
            SaveRestoreLayoutHelper.RegisterSaveRestoreStrategy(typeof(CustomLookUpEdit), context =>
            {
                if (!context.ValidateAction())
                {
                    return;
                }
                var control = context.ProcessingObject as CustomLookUpEdit;
                if (control == null)
                {
                    throw new DeveloperException("Strategy is not valid.");
                }

                switch (context.ActionType)
                {
                case SaveRestoreActionType.Save:
                    if (control.PopupContentGridControl != null)
                    {
                        foreach (var fileConfig in context.FileNames)
                        {
                            var vers = fileConfig.Version == null
                                    ? new Version(0, 0, 0, 0)
                                    : new Version(fileConfig.Version.Major, fileConfig.Version.Minor,
                                                  fileConfig.Version.Build, fileConfig.Version.Revision + 1);
                            fileConfig.Version = vers;
                        }
                        PrepareSaveFile(context);
                        foreach (var fileConfig in context.FileNames)
                        {
                            fileConfig.SetCurrentFileName();
                            control.PopupContentGridControl.SaveLayoutToXml(fileConfig.CurrentFullFileName);
                        }
                    }
                    break;

                case SaveRestoreActionType.Restore:
                    foreach (var fileConfig in context.FileNames)
                    {
                        string filenameClient = fileConfig.CurrentFullFileName;

                        if (string.IsNullOrEmpty(filenameClient) || !File.Exists(filenameClient))
                        {
                            filenameClient = string.Empty;
                            if (context.FilesDb != null)
                            {
                                byte[] filedb;
                                var filenameBd = fileConfig.GetFileNameBd();
                                if (context.FilesDb.ContainsKey(filenameBd) &&
                                    (filedb = context.FilesDb[filenameBd].Text) != null)
                                {
                                    fileConfig.Version = context.FilesDb[filenameBd].Version;
                                    PrepareSaveFile(fileConfig);
                                    fileConfig.SetCurrentFileName();
                                    filenameClient = fileConfig.CurrentFullFileName;
                                    File.WriteAllBytes(filenameClient, filedb);
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(filenameClient) && File.Exists(filenameClient))
                        {
                            try
                            {
                                if (control.PopupContentGridControl != null)
                                {
                                    control.PopupContentGridControl.RestoreLayoutFromXml(filenameClient);
                                }
                            }
                            catch (Exception ex)
                            {
                                File.Delete(filenameClient);
                                Log.Error(new OperationException(string.Format(ExceptionResources.RestoreLayoutBadFile, filenameClient, Environment.NewLine), ex));
                            }
                        }
                    }
                    break;

                case SaveRestoreActionType.Clear:
                    Clear(context);
                    break;

                default:
                    throw new DeveloperException("Unknown enum value.");
                }
            });

            // учим как сохранять/восстанавливать CustomBarManager
            SaveRestoreLayoutHelper.RegisterSaveRestoreStrategy(typeof(CustomBarManager), context =>
            {
                if (!context.ValidateAction())
                {
                    return;
                }
                var control = context.ProcessingObject as CustomBarManager;
                if (control == null)
                {
                    throw new DeveloperException("Strategy is not valid.");
                }

                switch (context.ActionType)
                {
                case SaveRestoreActionType.Save:
                    foreach (var fileConfig in context.FileNames)
                    {
                        var vers           = fileConfig.Version == null ? new Version(0, 0, 0, 0) : new Version(fileConfig.Version.Major, fileConfig.Version.Minor, fileConfig.Version.Build, fileConfig.Version.Revision + 1);
                        fileConfig.Version = vers;
                    }
                    PrepareSaveFile(context);
                    var count = context.FileNames.Length - 1;
                    for (var i = 0; i <= count; i++)
                    {
                        var fileConfig = context.FileNames[i];
                        fileConfig.SetCurrentFileName();
                        var filenameClient = fileConfig.CurrentFullFileName;
                        control.SaveLayoutToXml(filenameClient, i == count && count > 0);     //Если файл один, то нет глобальных настроек
                        if (LayoutInfo.ContainsKey(filenameClient))
                        {
                            LayoutInfo.Remove(filenameClient);
                        }
                    }
                    break;

                case SaveRestoreActionType.Restore:
                    foreach (var fileConfig in context.FileNames)
                    {
                        var filenameClient = fileConfig.CurrentFullFileName;

                        if (string.IsNullOrEmpty(filenameClient) || !File.Exists(filenameClient))
                        {
                            filenameClient = string.Empty;
                            if (context.FilesDb != null)
                            {
                                byte[] filedb;
                                var filenameBd = fileConfig.GetFileNameBd();
                                if (context.FilesDb.ContainsKey(filenameBd) &&
                                    (filedb = context.FilesDb[filenameBd].Text) != null)
                                {
                                    fileConfig.Version = context.FilesDb[filenameBd].Version;
                                    PrepareSaveFile(fileConfig);
                                    fileConfig.SetCurrentFileName();
                                    filenameClient = fileConfig.CurrentFullFileName;
                                    File.WriteAllBytes(filenameClient, filedb);
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(filenameClient))
                        {
                            if (LayoutInfo.ContainsKey(filenameClient))
                            {
                                control.RestoreLayoutFromString(LayoutInfo[filenameClient]);
                            }
                            else
                            {
                                if (File.Exists(filenameClient))
                                {
                                    try
                                    {
                                        //control.RestoreLayoutFromXml(filenameClient);
                                        using (var fileStream = new FileStream(filenameClient, FileMode.Open))
                                        {
                                            using (var wr = new StreamReader(fileStream))
                                            {
                                                var layout = wr.ReadToEnd();
                                                control.RestoreLayoutFromString(layout);
                                                LayoutInfo[filenameClient] = layout;
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        File.Delete(filenameClient);
                                        Log.Error(
                                            new OperationException(
                                                string.Format(ExceptionResources.RestoreLayoutBadFile,
                                                              filenameClient,
                                                              Environment.NewLine), ex));
                                    }
                                }
                            }
                        }
                    }
                    break;

                case SaveRestoreActionType.Clear:
                    Clear(context);
                    break;

                default:
                    throw new DeveloperException("Unknown enum value.");
                }
            });

            // учим как сохранять/восстанавливать Window
            SaveRestoreLayoutHelper.RegisterSaveRestoreStrategy(typeof(Window), context =>
            {
                if (!context.ValidateAction())
                {
                    return;
                }
                var control = context.ProcessingObject as Window;
                if (control == null)
                {
                    throw new DeveloperException("Strategy is not valid.");
                }

                switch (context.ActionType)
                {
                case SaveRestoreActionType.Save:
                    foreach (var fileConfig in context.FileNames)
                    {
                        var vers           = fileConfig.Version == null ? new Version(0, 0, 0, 0) : new Version(fileConfig.Version.Major, fileConfig.Version.Minor, fileConfig.Version.Build, fileConfig.Version.Revision + 1);
                        fileConfig.Version = vers;
                    }
                    for (var i = 0; i < context.FileNames.Length; i++)
                    {
                        if ((i == 0 && (context.FormComponents & FormComponents.FormSize) == FormComponents.FormSize) ||
                            (i == 1 && (context.FormComponents & FormComponents.FormPosition) == FormComponents.FormPosition))
                        {
                            PrepareSaveFile(context.FileNames[i]);
                        }
                    }
                    foreach (var fileNames in context.FileNames)
                    {
                        fileNames.SetCurrentFileName();
                    }

                    if (context.FileNames.Length > 0)
                    {
                        var saveRestoreLayoutWindowHelper = new SaveRestoreLayoutWindowHelper();
                        if ((context.FormComponents & FormComponents.FormSize) == FormComponents.FormSize)
                        {
                            var file = context.FileNames[0].GetFullFileNameClient();
                            saveRestoreLayoutWindowHelper.SaveToXmlWindowSize(file, control);
                        }
                        if ((context.FormComponents & FormComponents.FormPosition) == FormComponents.FormPosition && context.FileNames.Length >= 1)
                        {
                            var file = context.FileNames[1].GetFullFileNameClient();
                            saveRestoreLayoutWindowHelper.SaveToXmlWindowPosition(file, control);
                        }
                    }
                    break;

                case SaveRestoreActionType.Restore:
                    for (var i = 0; i < context.FileNames.Length; i++)
                    {
                        if ((i == 0 && (context.FormComponents & FormComponents.FormSize) == FormComponents.FormSize) ||
                            (i == 1 && (context.FormComponents & FormComponents.FormPosition) == FormComponents.FormPosition))
                        {
                            var fileNames      = context.FileNames[i];
                            var filenameClient = fileNames.CurrentFullFileName;

                            if (string.IsNullOrEmpty(filenameClient) || !File.Exists(filenameClient))
                            {
                                filenameClient = string.Empty;
                                if (context.FilesDb != null)
                                {
                                    byte[] filedb;
                                    var filenameBd = fileNames.GetFileNameBd();
                                    if (context.FilesDb.ContainsKey(filenameBd) &&
                                        (filedb = context.FilesDb[filenameBd].Text) != null)
                                    {
                                        fileNames.Version = context.FilesDb[filenameBd].Version;
                                        PrepareSaveFile(context.FileNames[i]);
                                        fileNames.SetCurrentFileName();
                                        filenameClient = fileNames.CurrentFullFileName;
                                        File.WriteAllBytes(filenameClient, filedb);
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(filenameClient) && File.Exists(filenameClient))
                            {
                                var saveRestoreLayoutWindowHelper = new SaveRestoreLayoutWindowHelper();
                                try
                                {
                                    if (i == 0)
                                    {
                                        saveRestoreLayoutWindowHelper.LoadFromXmlWindowSize(filenameClient, control);
                                    }
                                    else if (i == 1)
                                    {
                                        saveRestoreLayoutWindowHelper.LoadFromXmlWindowPosition(filenameClient,
                                                                                                control);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    File.Delete(filenameClient);
                                    Log.Error(
                                        new OperationException(
                                            string.Format(ExceptionResources.RestoreLayoutBadFile,
                                                          filenameClient, Environment.NewLine), ex));
                                }
                            }
                        }
                    }
                    break;

                case SaveRestoreActionType.Clear:
                    for (var i = 0; i < context.FileNames.Length; i++)
                    {
                        if ((i == 0 && (context.FormComponents & FormComponents.FormSize) == FormComponents.FormSize) ||
                            (i == 1 && (context.FormComponents & FormComponents.FormPosition) == FormComponents.FormPosition))
                        {
                            Clear(context.FileNames[i], context.FilesDb);
                        }
                    }
                    break;

                default:
                    throw new DeveloperException("Unknown enum value.");
                }
            });
        }