comparison Chronosv2/source/Presentation/Widgets/Navigator/NavigatorWidgetViewModel.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 /*
2 The MIT License
3
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 using System;
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.Linq;
29 using System.Windows.Input;
30 using Chronos.Extensions;
31 using Chronos.Presentation.Core.Navigation;
32 using Chronos.Presentation.ViewModel;
33 using Chronos.Presentation.Windows;
34 using nRoute.Components;
35 using nRoute.Services;
36 using nRoute.SiteMaps;
37
38 namespace Chronos.Presentation.Widgets
39 {
40 /// <summary>
41 /// Navigator Widget ViewModel
42 /// </summary>
43 /// <remarks>
44 /// http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
45 /// </remarks>
46 public sealed class NavigatorWidgetViewModel
47 : WidgetViewModel
48 {
49 #region · Fields ·
50
51 private readonly ReadOnlyCollection<NavigationNode> functions;
52 private readonly SiteMapNode rootOption;
53 private List<NavigationNode> filteredFunctions;
54 private string filterText;
55 private ICommand navigateToCommand;
56
57 #endregion
58
59 #region · Commands ·
60
61 /// <summary>
62 /// Gets the navigation command
63 /// </summary>
64 public ICommand NavigateToCommand
65 {
66 get
67 {
68 if (this.navigateToCommand == null)
69 {
70 this.navigateToCommand = new ActionCommand<string>
71 (
72 x => OnNavigateTo(x),
73 x => CanNavigate()
74 );
75 }
76
77 return this.navigateToCommand;
78 }
79 }
80
81 #endregion
82
83 #region · Properties ·
84
85 #region · FirstGeneration ·
86
87 /// <summary>
88 /// Returns a read-only collection containing the first application option
89 /// in the tree, to which the TreeView can bind.
90 /// </summary>
91 public ReadOnlyCollection<NavigationNode> Functions
92 {
93 get
94 {
95 if (String.IsNullOrEmpty(this.filterText))
96 {
97 return this.functions;
98 }
99 else
100 {
101 return new ReadOnlyCollection<NavigationNode>(this.filteredFunctions);
102 }
103 }
104 }
105
106 #endregion
107
108 #region · FilterText ·
109
110 /// <summary>
111 /// Gets/sets a fragment of the name to filter for.
112 /// </summary>
113 public string FilterText
114 {
115 get { return this.filterText; }
116 set
117 {
118 if (value != filterText)
119 {
120 this.filterText = value;
121 this.PerformFilter();
122 }
123 }
124 }
125
126 #endregion
127
128 #endregion
129
130 #region · Constructor ·
131
132 /// <summary>
133 /// Initializes a new instance of the <see cref="NavigatorWidget"/> class.
134 /// </summary>
135 public NavigatorWidgetViewModel()
136 : base()
137 {
138 if (!DesignMode.IsInDesignMode)
139 {
140 this.rootOption = SiteMapService.SiteMap.RootNode;
141 this.filteredFunctions = new List<NavigationNode>();
142 this.functions = new ReadOnlyCollection<NavigationNode>(this.rootOption.ChildNodes.OfType<NavigationNode>().ToArray());
143 }
144 }
145
146 #endregion
147
148 #region · Command Actions ·
149
150 /// <summary>
151 /// Returns a value indcating whether the navigation command can be executed
152 /// </summary>
153 /// <returns></returns>
154 private bool CanNavigate()
155 {
156 return this.filteredFunctions != null && this.filteredFunctions.Count == 1;
157 }
158
159 /// <summary>
160 /// Handles the navigation command
161 /// </summary>
162 private void OnNavigateTo(string url)
163 {
164 ServiceLocator.GetService<INavigationService>().Navigate(url);
165 }
166
167 #endregion
168
169 #region · Search Logic ·
170
171 private void PerformFilter()
172 {
173 this.VerifyMatchingOptionEnumerator();
174 }
175
176 private void VerifyMatchingOptionEnumerator()
177 {
178 // Clear current matching options
179 this.filteredFunctions.Clear();
180
181 // Perform filter if needed
182 if (!String.IsNullOrEmpty(this.filterText))
183 {
184 this.filteredFunctions.AddRange(this.FindMatches(this.filterText));
185 }
186
187 // Notify changes
188 this.NotifyPropertyChanged(() => Functions);
189 }
190
191 private List<NavigationNode> FindMatches(string filterText)
192 {
193 var nodes = from node in this.rootOption
194 .ChildNodes
195 .OfType<NavigationNode>()
196 .Traverse<NavigationNode>(node => ((node.ChildNodes) != null ? node.ChildNodes.OfType<NavigationNode>() : null))
197 where node.Title.StartsWith(filterText, StringComparison.OrdinalIgnoreCase) && (node.ChildNodes == null || node.ChildNodes.Count == 0)
198 select node;
199
200 return nodes.ToList();
201 }
202
203 #endregion
204 }
205 }