-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBreadcrumbTreeItemViewModel.cs
417 lines (362 loc) · 15.7 KB
/
BreadcrumbTreeItemViewModel.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
namespace BreadcrumbTestLib.ViewModels.Breadcrumbs
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BreadcrumbTestLib.ViewModels.Interfaces;
using BreadcrumbTestLib.ViewModels.Breadcrumbs.TreeSelectors;
using BreadcrumbTestLib.ViewModels.Base;
using WSF.Interfaces;
using System;
using BmLib.Interfaces;
using BmLib.Enums;
using BreadcrumbTestLib.Models;
using System.Windows.Input;
using WSF;
using WSF.IDs;
using WSF.Enums;
/// <summary>
/// Class implements a ViewModel to manage a sub-tree of a Breadcrumb control.
///
/// This sub-tree includes
/// - a specific item (see Header property),
/// - a SelectedItem (see Selection property), and
/// - a list of items below this item (<see cref="BreadcrumbTreeItemHelperViewModel{VM}"/>).
/// </summary>
public class BreadcrumbTreeItemViewModel : ViewModelBase,
ISupportBreadcrumbTreeItemViewModel<BreadcrumbTreeItemViewModel, IDirectoryBrowser>,
IBreadcrumbTreeItemViewModel
{
#region fields
/// <summary>
/// Log4net logger facility for this class.
/// </summary>
protected static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private IDirectoryBrowser _dir;
private string _header;
private BreadcrumbTreeItemViewModel _rootNode, _parentNode;
// Instance of the root viewmodel - reference is used to invoke central tree related (navigational) methods
private IRoot<IDirectoryBrowser> _Root;
private ICommand _DropDownItemSelectedCommand;
private ICommand _BreadcrumbTreeTreeItemClickCommand;
#endregion fields
#region constructors
/// <summary>
/// Class constructor from an available parentNode and directory model
/// to recurse down on a given structure.
/// </summary>
/// <param name="dir"></param>
/// <param name="parentNode"></param>
internal BreadcrumbTreeItemViewModel(IDirectoryBrowser dir,
BreadcrumbTreeItemViewModel parentNode,
IRoot<IDirectoryBrowser> root
)
{
Logger.InfoFormat("FullName '{0}'", (dir != null ? dir.FullName : "(null)"));
_Root = root;
_dir = dir;
// If parentNode == null => Parent of Root is null
_rootNode = parentNode == null ? this : parentNode._rootNode;
_parentNode = parentNode;
Header = (dir != null ? _dir.Label : string.Empty );
Func<bool, object, Task<IEnumerable<BreadcrumbTreeItemViewModel>>> loadAsyncFunc = (isLoaded, parameter) => Task.Run(() =>
{
try
{
var subItemsList = Browser.GetChildItems(_dir.FullName, null, SubItemFilter.NameOnly, true);
// FullName has preference for directory file system path over SpecialId
// (if an item has both)
// -> this is useful here to no relist SpecialItems below Desktop or other special items
return subItemsList.Select(d => new BreadcrumbTreeItemViewModel(d, this, _Root));
}
catch (Exception exp)
{
Logger.Error(exp);
return new List<BreadcrumbTreeItemViewModel>();
}
});
this.Entries = new BreadcrumbTreeItemHelperViewModel<BreadcrumbTreeItemViewModel>(loadAsyncFunc);
this.Selection = new TreeSelectorViewModel<BreadcrumbTreeItemViewModel, IDirectoryBrowser>
(_dir, this, this.Entries);
}
#endregion constructors
#region properties
/// <summary>
/// Gets a property that holds all other properties on whether
/// or not this entry is selected or not and so forth.
/// </summary>
public ITreeSelector<BreadcrumbTreeItemViewModel, IDirectoryBrowser> Selection { get; protected set; }
/// <summary>
/// Gets a structure that contains all root items that are located on level 0.
/// See <see cref="Entries.All"/> collection for more details.
/// </summary>
public IBreadcrumbTreeItemHelperViewModel<BreadcrumbTreeItemViewModel> Entries { get; protected set; }
/// <summary>
/// Gets the name of the Breadcrumb node (item).
/// </summary>
public string Header
{
get
{
return _header;
}
protected set
{
if (_header != value)
{
_header = value;
NotifyPropertyChanged(() => Header);
}
}
}
bool IOverflown.IsOverflown
{
get
{
if (Selection != null)
return Selection.IsOverflowed;
return false;
}
}
/// <summary>
/// Gets a command that can change the navigation target of the currently
/// selected location towards a new location. This command is typically
/// executed when:
/// 1) Any other than the root drop down triangle is opened,
/// 2) An entry in the list drop down is selected and
/// 3) The control is now deactivating its previous selection and
/// 4) needs to navigate towards the new selected item.
///
/// Expected command parameter:
/// Array of length 1 with an object of type <see cref="BreadcrumbTreeItemViewModel"/>
/// object[1] = {new <see cref="BreadcrumbTreeItemViewModel"/>() }
/// </summary>
public ICommand DropDownItemSelectedCommand
{
get
{
if (_DropDownItemSelectedCommand == null)
{
_DropDownItemSelectedCommand = new RelayCommand<object>(async (param) =>
{
var parArray = param as object[];
if (parArray == null)
return;
if (parArray.Length <= 0)
return;
// Limitation of command is currently only 1 LOCATION PARAMETER being processed
var selectedFolder = parArray[0] as BreadcrumbTreeItemViewModel;
if (selectedFolder == null)
return;
if (_Root.IsBrowsing == true) // Selection change originates from viewmodel
return; // So, let ignore this one since its browsing anyways...
Logger.InfoFormat("selectedFolder {0}", selectedFolder);
await _Root.NavigateToScheduledAsync(selectedFolder.GetModel(),
"BreadcrumbTreeItemViewModel.ItemSelectionChanged",
HintDirection.Down, this);
}
//// Partial rollback from commit on 2018-10-30:
//// https://github.com/Dirkster99/bm/commit/4ccb72b2ef6e500175cc99c63b37e2fa1d608e5a
//// Not needed and causes timing problems since other elements in the tree receive the event
//// ,(param) =>
//// {
//// if (_Root.IsBrowsing == true)
//// return false;
////
//// return true;
//// }
);
}
return _DropDownItemSelectedCommand;
}
}
/// <summary>
/// Gets a command that can be execute to make this item the currently
/// selected item in the breadcrumb control.
///
/// Use Case: User clicks a visible item in the list of breadcrumbs.
/// </summary>
public ICommand BreadcrumbTreeTreeItemClickCommand
{
get
{
if (_BreadcrumbTreeTreeItemClickCommand == null)
{
_BreadcrumbTreeTreeItemClickCommand = new RelayCommand<object>(async (param) =>
{
if (_Root.IsBrowsing == true) // Selection change originates from viewmodel
return; // So, let ignore this one since its browsing anyways...
Logger.InfoFormat("selectedFolder {0}", this);
await _Root.NavigateToScheduledAsync(this.GetModel(),
"BreadcrumbTreeItemViewModel.BreadcrumbTreeTreeItemClickCommand",
HintDirection.Up, this);
}
//// Partial rollback from commit on 2018-10-30:
//// https://github.com/Dirkster99/bm/commit/4ccb72b2ef6e500175cc99c63b37e2fa1d608e5a
//// Not needed and causes timing problems since other elements in the tree receive the event
//// ,(param) =>
//// {
//// if (_Root.IsBrowsing == true)
//// return false;
////
//// return true;
//// }
);
}
return _BreadcrumbTreeTreeItemClickCommand;
}
}
/// <summary>
/// Get Known FolderId or file system Path for this folder.
///
/// That is:
/// 1) A knownfolder GUID (if it exists) is shown
/// here as default preference over
///
/// 2) A storage location (if it exists) in the filesystem
/// </summary>
public string ItemPath
{
get
{
if (_dir == null)
return string.Empty;
if (string.IsNullOrEmpty(_dir.SpecialPathId) == false)
return _dir.SpecialPathId;
return _dir.PathFileSystem;
}
}
/// <summary>
/// Gets the name of this folder (without its root path component).
/// </summary>
public string ItemName
{
get
{
if (_dir != null)
return _dir.Name;
return string.Empty;
}
}
/// <summary>
/// Gets an optional pointer to the default icon resource used when the folder is created.
/// This is a null-terminated Unicode string in this form:
///
/// Module name, Resource ID
/// or null is this information is not available.
/// </summary>
public string IconResourceId
{
get
{
if (_dir != null)
return _dir.IconResourceId;
return null;
}
}
#endregion properties
#region methods
/// <summary>
/// Output Header string to hint about the object instance of this object.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("location '{0}' Header '{1}'",
(_dir != null ? _dir.FullName : "(null)"),
(this.Header != null ? this.Header : "(null)"));
}
/// <summary>
/// Gets all root items below the desktop item and makes them available
/// in the <see cref="Entries"/> collection. The first available item
/// in the retrieved collection (eg: 'PC') is selected.
///
/// This method should only be called on a root item of the breadcrumb tree to
/// initialize/construct a valid root of the tree(!!!)
/// </summary>
/// <param name="location"></param>
/// <param name="rootSelector"></param>
/// <returns></returns>
internal BreadcrumbTreeItemViewModel InitRoot(IDirectoryBrowser location)
{
try
{
// Find all entries below desktop
_dir = location;
Header = _dir.Label;
var models = Browser.GetChildItems(_dir.PathShell, null, SubItemFilter.NameOnly, true)
// (filter out recycle bin and control panel entries since its not that useful...)
.Where(d => string.Compare(d.SpecialPathId, KF_ParseName_IID.RecycleBinFolder, true) != 0)
.Where(d => string.Compare(d.PathRAW, KF_ParseName_IID.ControlPanelFolder, true) != 0);
var viewmodels = models.Select(d => new BreadcrumbTreeItemViewModel(d, this, _Root)).ToArray();
// and insert desktop sub-entries into Entries property
Entries.SetEntries(UpdateMode.Replace, viewmodels);
if (Entries.All.Count() > 0)
{
// All items imidiately under root are by definition root items
foreach (var item in Entries.All)
item.Selection.IsRoot = true;
var firstRootItem = Entries.All.First();
firstRootItem.Selection.IsSelected = true;
return firstRootItem;
}
}
catch
{
}
return null;
}
/// <summary>
/// Gets the model that represents this item for internal navigational
/// and debug puroses only(!).
/// </summary>
/// <returns></returns>
internal IDirectoryBrowser GetModel()
{
return _dir;
}
/// <summary>
/// Compares a given parse name with the parse names known in this object's model.
///
/// Considers case insensitive string matching for:
/// 1> SpecialPathId
/// 1.2> PathRAW (if SpecialPathId fails and CLSID may have been used to create this)
///
/// 3> PathFileSystem
/// </summary>
/// <param name="parseName">True is a matching parse name was found and false if not.</param>
/// <returns></returns>
internal bool EqualsParseName(string parseName)
{
return _dir.EqualsParseName(parseName);
}
/// <summary>
/// Goes through all Parent items of this item and gets a collection of viewmodel
/// items that represent the path towards this item in the the tree.
/// </summary>
/// <returns></returns>
internal IList<BreadcrumbTreeItemViewModel> GetPathItems(bool bReverseItems = true)
{
var parentNode = _parentNode;
var list = new List<BreadcrumbTreeItemViewModel>();
list.Add(this);
while (parentNode != null)
{
list.Add(parentNode);
parentNode = parentNode._parentNode;
}
if (bReverseItems == true)
list.Reverse();
return list;
}
internal bool EqualsLocation(IDirectoryBrowser location)
{
return _dir.Equals(location);
}
public IParent GetParent()
{
return _parentNode;
}
#endregion
}
}