void diagramIO_DiagramFileAdded(object sender, DiagramFileAddedEventArgs e) { Task.Factory.StartNew(async() => { // Make sure a preview doesn't already exist for the file and make sure the current directory still matches. bool previewExists = PreviewDiagrams.Select(pd => pd.Diagram.File).Contains(e.NewDiagramFile, FileComparer); if (!previewExists && e.NewDiagramFile.Directory?.FullName.Trim('\\') == DiagramLocation.FullName.Trim('\\')) { var newlyAddedDiagram = await _diagramIO.ReadAsync(e.NewDiagramFile); if (newlyAddedDiagram != null) { PreviewDiagrams.Add(_previewDiagramFactory(newlyAddedDiagram)); } } }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler); }
public DesignTimeDiagramExplorer() { DiagramLocation = GetDesignTimeDiagramPath(); foreach (var diagramFile in DiagramLocation.EnumerateFiles("*.puml")) { var imagePath = Path.Combine(diagramFile.DirectoryName, Path.GetFileNameWithoutExtension(diagramFile.Name) + ".png"); PreviewDiagrams.Add( new PreviewDiagramViewModel( new Diagram { Content = File.ReadAllText(diagramFile.FullName), File = diagramFile, ImageFile = new FileInfo(imagePath) }) { ImagePreview = BitmapFrame.Create(new Uri(imagePath)) }); } }
private async Task AddNewDiagramAsync(Uri newDiagramUri) { string newFilePath = newDiagramUri.LocalPath; if (String.IsNullOrEmpty(Path.GetExtension(newFilePath))) { newFilePath += FileExtension; } var newDiagram = new Diagram { File = new FileInfo(newFilePath), Content = String.Format(NewDiagramTemplate, Path.GetFileNameWithoutExtension(newFilePath) + ".png") }; _diagramLocation.Value = new DirectoryInfo(Path.GetDirectoryName(newFilePath)); var progress = _notifications.StartProgress(false); try { await _diagramIO.SaveAsync(newDiagram, false); var refreshedDiagram = await _diagramIO.ReadAsync(newDiagram.File); var preview = PreviewDiagrams.SingleOrDefault(p => FileComparer.Equals(p.Diagram.File, refreshedDiagram.File)); if (preview == null) { preview = _previewDiagramFactory(refreshedDiagram); PreviewDiagrams.Add(preview); } CurrentPreviewDiagram = preview; OnOpenPreviewRequested(preview); progress.Report(ProgressUpdate.Completed(string.Empty)); } catch (Exception e) { progress.Report(ProgressUpdate.Failed(e)); } }
private async Task LoadDiagramsAsync(DirectoryInfo directory) { PreviewDiagrams.Clear(); if (directory == null || !directory.Exists) { return; } using (var cts = new CancellationTokenSource()) { IsLoadingDiagrams = true; var progress = _notifications.StartProgress(); progress.Report(new ProgressUpdate { PercentComplete = 0, Message = Resources.Progress_LoadingDiagrams }); CancelLoadDiagramsCommand = new CancelTaskCommand(cts); // Capture diagrams as they are read for a more responsive UI. var readProgress = new Progress <ReadDiagramsProgress>(p => p.Diagram.Apply(d => PreviewDiagrams.Add(_previewDiagramFactory(d)))); // Report progress to UI by passing up progress data. progress.Wrap(readProgress, p => new ProgressUpdate { PercentComplete = (int?)(p.ProcessedDiagramCount / (double)p.TotalDiagramCount * 100), Message = String.Format(Resources.Progress_LoadingFile, p.ProcessedDiagramCount, p.TotalDiagramCount) }); try { await _diagramIO.ReadDiagramsAsync(directory, readProgress, cts.Token); progress.Report(ProgressUpdate.Completed(Resources.Progress_DiagramsLoaded)); } catch (OperationCanceledException) { progress.Report(ProgressUpdate.Completed(Resources.Progress_DiagramLoadCanceled)); } catch (Exception e) { progress.Report(ProgressUpdate.Failed(e)); } finally { CancelLoadDiagramsCommand = null; IsLoadingDiagrams = false; } } }