-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewModel.cs
110 lines (93 loc) · 3.96 KB
/
ViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using DevExpress.Diagram.Core;
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Xml.Serialization;
namespace MDI_Diagram {
public class ViewModel : ViewModelBase {
int? documentCount = null;
public ViewModel() {
SelectedDiagramStencils = new StencilCollection() { "BasicShapes" };
DiagramTheme = DiagramThemes.Office;
}
IDocumentManagerService DocumentManagerService => GetService<IDocumentManagerService>();
IOpenFileDialogService OpenFileDialogService => GetService<IOpenFileDialogService>();
ISaveFileDialogService SaveFileDialogService => GetService<ISaveFileDialogService>();
IMessageBoxService MessageBoxService => GetService<IMessageBoxService>();
[Command]
public void CreateDocument(DocumentViewModel vm = null) {
if (!documentCount.HasValue)
documentCount = DocumentManagerService.Documents.Count();
IDocument doc = DocumentManagerService.FindDocumentById(documentCount);
if (doc == null) {
if (vm == null)
vm = new DocumentViewModel();
vm.Name = $"Document {documentCount}";
doc = DocumentManagerService.CreateDocument(vm);
doc.Id = documentCount;
documentCount++;
}
doc.Show();
}
[Command]
public void OnLoaded() {
CreateDocument();
}
[Command]
public void OpenMany() {
try {
if (OpenFileDialogService.ShowDialog()) {
var serializer = new XmlSerializer(typeof(DiagramLayoutWrapper));
var wrapper = serializer.Deserialize(OpenFileDialogService.File.OpenText()) as DiagramLayoutWrapper;
if (wrapper != null)
foreach (var diagram in wrapper.Diagrams)
CreateDocument(new DocumentViewModel() { Diagram = diagram });
}
} catch (Exception ex) {
MessageBoxService.ShowMessage(ex.Message, "Error", MessageButton.OK, MessageIcon.Error);
}
}
[Command]
public void SaveAll() {
if (SaveFileDialogService.ShowDialog()) {
var wrapper = new DiagramLayoutWrapper();
foreach (var doc in DocumentManagerService.Documents)
if (doc.Content is DocumentViewModel vm)
wrapper.Diagrams.Add(vm.Save());
var serializer = new XmlSerializer(typeof(DiagramLayoutWrapper));
serializer.Serialize(SaveFileDialogService.OpenFile(), wrapper);
}
}
public DiagramTool ActiveTool { get => GetValue<DiagramTool>(); set => SetValue(value); }
public DiagramTheme DiagramTheme { get => GetValue<DiagramTheme>(); set => SetValue(value); }
public ObservableCollection<DocumentViewModel> Documents { get; set; }
public StencilCollection SelectedDiagramStencils {
get => GetValue<StencilCollection>();
set {
if (value.Count > 0) {
SetValue(value);
}
}
}
}
public class DocumentViewModel : ViewModelBase, IDocumentContent {
IDiagramService DiagramService => GetService<IDiagramService>();
public void OnClose(CancelEventArgs e) { }
public void OnDestroy() { }
[Command]
public void OnLoaded() {
if (!string.IsNullOrEmpty(Diagram))
DiagramService?.Restore(Diagram);
}
public string Save() {
return DiagramService?.Save();
}
public string Diagram { get; set; }
public IDocumentOwner DocumentOwner { get; set; }
public string Name { get; set; }
public object Title => Name;
}
}