-
Notifications
You must be signed in to change notification settings - Fork 321
/
FileViewModel.cs
176 lines (154 loc) · 3.15 KB
/
FileViewModel.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
namespace AvalonDock.MVVMTestApp
{
using System.IO;
using System.Windows.Input;
using System.Windows.Media;
class FileViewModel : PaneViewModel
{
#region fields
private static ImageSourceConverter ISC = new ImageSourceConverter();
private string _filePath = null;
private string _textContent = string.Empty;
private bool _isDirty = false;
private RelayCommand _saveCommand = null;
private RelayCommand _saveAsCommand = null;
private RelayCommand _closeCommand = null;
#endregion fields
#region constructors
/// <summary>
/// Class constructor from file path.
/// </summary>
/// <param name="filePath"></param>
public FileViewModel(string filePath)
{
FilePath = filePath;
Title = FileName;
//Set the icon only for open documents (just a test)
IconSource = ISC.ConvertFromInvariantString(@"pack://application:,,/Images/document.png") as ImageSource;
}
/// <summary>
/// Default class constructor
/// </summary>
public FileViewModel()
{
IsDirty = true;
Title = FileName;
}
#endregion constructors
#region Properties
public string FilePath
{
get => _filePath;
set
{
if (_filePath != value)
{
_filePath = value;
RaisePropertyChanged(nameof(FilePath));
RaisePropertyChanged(nameof(FileName));
RaisePropertyChanged(nameof(Title));
if (File.Exists(_filePath))
{
_textContent = File.ReadAllText(_filePath);
ContentId = _filePath;
}
}
}
}
public string FileName
{
get
{
if (FilePath == null)
return "Noname" + (IsDirty ? "*" : "");
return System.IO.Path.GetFileName(FilePath) + (IsDirty ? "*" : "");
}
}
public string TextContent
{
get => _textContent;
set
{
if (_textContent != value)
{
_textContent = value;
RaisePropertyChanged(nameof(TextContent));
IsDirty = true;
}
}
}
public bool IsDirty
{
get => _isDirty;
set
{
if (_isDirty != value)
{
_isDirty = value;
RaisePropertyChanged(nameof(IsDirty));
RaisePropertyChanged(nameof(FileName));
}
}
}
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand((p) => OnSave(p), (p) => CanSave(p));
}
return _saveCommand;
}
}
public ICommand SaveAsCommand
{
get
{
if (_saveAsCommand == null)
{
_saveAsCommand = new RelayCommand((p) => OnSaveAs(p), (p) => CanSaveAs(p));
}
return _saveAsCommand;
}
}
public ICommand CloseCommand
{
get
{
if (_closeCommand == null)
{
_closeCommand = new RelayCommand((p) => OnClose(), (p) => CanClose());
}
return _closeCommand;
}
}
#endregion Properties
#region methods
private bool CanClose()
{
return true;
}
private void OnClose()
{
Workspace.This.Close(this);
}
private bool CanSave(object parameter)
{
return IsDirty;
}
private void OnSave(object parameter)
{
Workspace.This.Save(this, false);
}
private bool CanSaveAs(object parameter)
{
return IsDirty;
}
private void OnSaveAs(object parameter)
{
Workspace.This.Save(this, true);
}
#endregion methods
}
}