Ejemplo n.º 1
0
        /// <summary>Creates a new <see cref="JsonDocumentModel"/> based on a given schema file path. </summary>
        /// <param name="schemaPath">The schema file path. </param>
        /// <param name="dispatcher">The UI dispatcher. </param>
        /// <returns>The <see cref="JsonDocumentModel"/>. </returns>
        public static Task <JsonDocumentModel> CreateAsync(string schemaPath, IDispatcher dispatcher)
        {
            return(Task.Run(() =>
            {
                var schema = JsonSchema4.FromJson(File.ReadAllText(schemaPath, Encoding.UTF8));
                var data = JsonObjectModel.FromSchema(schema);

                var document = new JsonDocumentModel();
                document.Initialize(data, dispatcher);
                return document;
            }));
        }
Ejemplo n.º 2
0
        /// <summary>Creates a new <see cref="JsonDocumentModel"/> based on a given schema file path. </summary>
        /// <param name="schemaPath">The schema file path. </param>
        /// <param name="dispatcher">The UI dispatcher. </param>
        /// <returns>The <see cref="JsonDocumentModel"/>. </returns>
        public static Task <JsonDocumentModel> CreateAsync(string schemaPath, IDispatcher dispatcher)
        {
            return(Task.Run(() =>
            {
                var schema = JsonSchema4.FromFileAsync(schemaPath).GetAwaiter().GetResult();
                var data = JsonObjectModel.FromSchema(schema);

                var document = new JsonDocumentModel();
                document.Initialize(data, dispatcher);
                return document;
            }));
        }
Ejemplo n.º 3
0
        /// <summary>Loads a <see cref="JsonDocumentModel"/> from a file path and schema path. </summary>
        /// <param name="filePath">The file path. </param>
        /// <param name="schemaPath">The schema path. </param>
        /// <param name="dispatcher">The UI dispatcher. </param>
        /// <returns>The <see cref="JsonDocumentModel"/>. </returns>
        public static Task <JsonDocumentModel> LoadAsync(string filePath, string schemaPath, IDispatcher dispatcher)
        {
            return(Task.Run(() =>
            {
                var schema = JsonSchema4.FromFileAsync(schemaPath).GetAwaiter().GetResult();
                var data = JsonObjectModel.FromJson(File.ReadAllText(filePath, Encoding.UTF8), schema);

                var document = new JsonDocumentModel();
                document.Initialize(data, dispatcher);
                document.FilePath = filePath;
                document.SchemaPath = schemaPath;
                return document;
            }));
        }
Ejemplo n.º 4
0
        /// <summary>Creates a new <see cref="JsonDocumentModel"/> based on a given schema file path. </summary>
        /// <param name="schemaPath">The schema file path. </param>
        /// <param name="dispatcher">The UI dispatcher. </param>
        /// <returns>The <see cref="JsonDocumentModel"/>. </returns>
        public static Task<JsonDocumentModel> CreateAsync(string schemaPath, IDispatcher dispatcher)
        {
            return Task.Run(() =>
            {
                var schema = JsonSchema4.FromJson(File.ReadAllText(schemaPath, Encoding.UTF8));
                var data = JsonObjectModel.FromSchema(schema);

                var document = new JsonDocumentModel();
                document.Initialize(data, dispatcher);
                return document;
            });
        }
Ejemplo n.º 5
0
 private async Task SaveDocumentSchemaAsAsync(JsonDocumentModel document)
 {
     var fileName = Path.GetFileNameWithoutExtension(document.FilePath) + ".schema.json";
     var result = await Messenger.Default.SendAsync(new SaveJsonDocumentMessage(fileName));
     if (result.Success)
     {
         await RunTaskAsync(async token =>
         {
             await Task.Run(() => File.WriteAllText(result.Result, document.Data.Schema.ToJson()), token);
         });
     }
 }
Ejemplo n.º 6
0
        private async Task SaveDocumentAsync(JsonDocumentModel document, bool saveAs)
        {
            if (!document.HasFileLocation || saveAs)
            {
                var fileName = document.HasFileLocation ?
                    Path.GetFileNameWithoutExtension(document.FilePath) + ".json" :
                    Strings.DefaultFileName + ".json";

                var result = await Messenger.Default.SendAsync(new SaveJsonDocumentMessage(fileName));
                if (result.Success)
                {
                    AddRecentFile(result.Result);
                    document.FilePath = result.Result;
                }
                else
                    return;
            }

            await RunTaskAsync(async token =>
            {
                await document.SaveAsync(true);
            });
        }
Ejemplo n.º 7
0
 private Task SaveDocumentAsAsync(JsonDocumentModel document)
 {
     return SaveDocumentAsync(document, true);
 }
Ejemplo n.º 8
0
 private async Task ValidateDocumentAsync(JsonDocumentModel document)
 {
     var errors = await document.Data.ValidateAsync();
     if (errors.Length == 0)
     {
         await Messenger.Default.SendAsync(
             new TextMessage(string.Format(Strings.MessageValidDocumentText, document.DisplayTitle),
                 Strings.MessageValidDocumentTitle));
     }
     else
     {
         await Messenger.Default.SendAsync(
             new TextMessage(
                 string.Format(Strings.MessageNotValidDocumentText, document.DisplayTitle, errors),
                 Strings.MessageNotValidDocumentTitle));
     }
 }
Ejemplo n.º 9
0
        private void RemoveDocument(JsonDocumentModel document)
        {
            Documents.Remove(document);

            document.UndoRedoManager.PropertyChanged -= UndoRedoManagerOnPropertyChanged;
        }
Ejemplo n.º 10
0
        private void AddDocument(JsonDocumentModel document)
        {
            Documents.Add(document);
            SelectedDocument = document;

            document.UndoRedoManager.PropertyChanged += UndoRedoManagerOnPropertyChanged;
        }
Ejemplo n.º 11
0
        /// <summary>Opens a document from a given file name. </summary>
        /// <param name="fileName">The file name. </param>
        /// <param name="isReadOnly">The value indicating whether the document can be changed. </param>
        /// <returns>The task. </returns>
        public async Task OpenDocumentAsync(string fileName, bool isReadOnly)
        {
            var existingDocument = Documents.SingleOrDefault(d => d.FilePath == fileName);
            if (existingDocument != null)
                SelectedDocument = existingDocument;
            else
            {
                await RunTaskAsync(async token =>
                {
                    var schemaPath = JsonDocumentModel.GetDefaultSchemaPath(fileName);
                    if (!File.Exists(schemaPath))
                    {
                        var result = await Messenger.Default.SendAsync(new OpenJsonDocumentMessage(Strings.OpenJsonSchemaDocumentDialog));
                        if (!result.Success)
                            return;

                        schemaPath = result.Result;
                    }

                    var document = await JsonDocumentModel.LoadAsync(fileName, schemaPath, ServiceLocator.Default.Resolve<IDispatcher>());
                    document.IsReadOnly = isReadOnly;

                    AddDocument(document);
                    AddRecentFile(fileName);
                });
            }
        }
Ejemplo n.º 12
0
        /// <summary>Closes the given document and saves it if needed. </summary>
        /// <param name="document">The document to close. </param>
        /// <returns>The task. </returns>
        public async Task<bool> CloseDocumentAsync(JsonDocumentModel document)
        {
            if (document.UndoRedoManager.CanUndo)
            {
                var message = new TextMessage(string.Format(Strings.MessageSaveDocumentText, document.DisplayTitle),
                    Strings.MessageSaveDocumentTitle, MessageButton.YesNoCancel);

                var result = await Messenger.Default.SendAsync(message);
                if (result.Result == MessageResult.Cancel)
                    return false;

                if (result.Result == MessageResult.Yes)
                    await SaveDocumentAsync(document);
            }

            RemoveDocument(document);
            return true;
        }