comparison Chronosv2/source/Presentation/Windows/Navigation/NavigationService.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.Diagnostics;
27 using System.Linq;
28 using System.Threading;
29 using System.Windows;
30 using System.Windows.Threading;
31 using Chronos.Extensions;
32 using Chronos.Presentation.Core.Navigation;
33 using Chronos.Presentation.Core.Services;
34 using Chronos.Presentation.Core.VirtualDesktops;
35 using Chronos.Presentation.Core.Windows;
36 using Chronos.Presentation.Windows.Controls;
37 using nRoute.Components;
38 using nRoute.Components.Composition;
39 using nRoute.Components.Messaging;
40 using nRoute.Components.Routing;
41 using nRoute.Navigation;
42 using nRoute.Services;
43 using nRoute.SiteMaps;
44 using nRoute.ViewServices;
45
46 namespace Chronos.Presentation.Windows.Navigation
47 {
48 /// <summary>
49 /// Contains methods to support navigation.
50 /// </summary>
51 [MapService(typeof(INavigationService),
52 InitializationMode=InitializationMode.OnDemand,
53 Lifetime=InstanceLifetime.Singleton)]
54 public sealed class NavigationService
55 : INavigationService, INavigationHandler
56 {
57 #region · Constructors ·
58
59 /// <summary>
60 /// Initializes a new instance of the <see cref="NavigationService"/> class
61 /// </summary>
62 public NavigationService()
63 {
64 // Set this as the default navigation handler
65 nRoute.Navigation.NavigationService.DefaultNavigationHandler = this;
66 }
67
68 #endregion
69
70 #region · Methods ·
71
72 /// <summary>
73 /// Performs the navigation to the given target
74 /// </summary>
75 /// <param name="target"></param>
76 public void Navigate(string target)
77 {
78 this.Navigate(NavigateMode.New, target, null);
79 }
80
81 /// <summary>
82 /// Performs the navigation to the given target
83 /// </summary>
84 /// <param name="target"></param>
85 public void Navigate(NavigateMode mode, string target)
86 {
87 this.Navigate(mode, target, null);
88 }
89
90 /// <summary>
91 /// Performs the navigation to the given target
92 /// </summary>
93 /// <param name="target"></param>
94 public void Navigate(string target, params object[] args)
95 {
96 this.Navigate(NavigateMode.New, target, args);
97 }
98
99 /// <summary>
100 /// Performs the navigation to the given target
101 /// </summary>
102 /// <param name="target"></param>
103 public void Navigate(NavigateMode mode, string target, params object[] args)
104 {
105 if (String.IsNullOrEmpty(target))
106 {
107 IShowMessageViewService showMessageService = ViewServiceLocator.GetViewService<IShowMessageViewService>();
108
109 showMessageService.ButtonSetup = DialogButton.Ok;
110 showMessageService.Caption = "Warning";
111 showMessageService.Text = "Option not mapped to a view.";
112
113 showMessageService.ShowMessage();
114
115 return;
116 }
117
118 Application.Current.Dispatcher.BeginInvoke
119 (
120 DispatcherPriority.Background,
121 new ThreadStart
122 (
123 delegate
124 {
125 ParametersCollection navParams = null;
126 string area = null;
127
128 if (args != null && args.Length > 0)
129 {
130 navParams = new ParametersCollection();
131 navParams.Add(NavigationParams.NavigationParamsKey, args);
132 }
133
134 var smnode = SiteMapService.SiteMap.RootNode.ChildNodes
135 .OfType<NavigationNode>()
136 .Traverse<NavigationNode>(node => ((node.ChildNodes) != null ? node.ChildNodes.OfType<NavigationNode>() : null))
137 .Where(n => n.Url == target).SingleOrDefault();
138
139 if (smnode != null)
140 {
141 area = smnode.SiteArea;
142 }
143
144 NavigationRequest request = new NavigationRequest(target, navParams, area, mode);
145 IDisposable navigationToken = nRoute.Navigation.NavigationService.Navigate(request);
146
147 if (navigationToken != null)
148 {
149 navigationToken.Dispose();
150 navigationToken = null;
151 }
152 }
153 )
154 );
155 }
156
157 #endregion
158
159 #region · INavigationHandler Members ·
160
161 void INavigationHandler.ProcessRequest(NavigationRequest request, Action<bool> requestCallback)
162 {
163 Debug.Assert(request != null, "request");
164 Debug.Assert(requestCallback != null, "requestCallback");
165
166 NavigatingCancelInfo info = new NavigatingCancelInfo(request);
167
168 if (!info.Cancel)
169 {
170 // change state
171 Channel<NavigatingCancelInfo>.Public.OnNext(info);
172
173 // Check if the navigation needs to be canceled
174 if (!info.Cancel)
175 {
176 requestCallback(true);
177 }
178 else
179 {
180 requestCallback(false);
181 }
182 }
183 else
184 {
185 requestCallback(false);
186 }
187 }
188
189 /// <summary>
190 /// Process the given <see cref="NavigationResponse"/> instance
191 /// </summary>
192 /// <param name="response"></param>
193 void INavigationHandler.ProcessResponse(NavigationResponse response)
194 {
195 // basic check
196 if (response == null)
197 {
198 throw new ArgumentNullException("response");
199 }
200
201 // we check if the navigation was successfull or not
202 if (response.Status != ResponseStatus.Success)
203 {
204 this.PublishNavigationFailedInfo(response.Request);
205
206 IShowMessageViewService showMessageService = ViewServiceLocator.GetViewService<IShowMessageViewService>();
207
208 showMessageService.ButtonSetup = DialogButton.Ok;
209 showMessageService.Caption = "Chronos - Error en la navegación";
210 showMessageService.Text =
211 ((response.Error != null) ? response.Error.Message : String.Format("No ha sido posible resolver la navegación solicitada ({0})", response.Request.RequestUrl));
212
213 showMessageService.ShowMessage();
214 }
215 else
216 {
217 Application.Current.Dispatcher.BeginInvoke
218 (
219 DispatcherPriority.Background,
220 new ThreadStart
221 (
222 () =>
223 {
224 WindowElement window = response.Content as WindowElement;
225
226 if (response.ResponseParameters != null &&
227 response.ResponseParameters.Count > 0)
228 {
229 ISupportNavigationLifecycle supporter = nRoute.Navigation.NavigationService.GetSupporter<ISupportNavigationLifecycle>(response.Content);
230
231 if (supporter != null)
232 {
233 supporter.Initialize(response.ResponseParameters);
234 }
235 }
236
237 this.PublishNavigatedInfo(window.Title, response);
238
239 if (response.Request.NavigationMode == NavigateMode.Modal)
240 {
241 ServiceLocator.GetService<IVirtualDesktopManager>().ShowDialog(window);
242 }
243 else
244 {
245 ServiceLocator.GetService<IVirtualDesktopManager>().Show(window);
246 }
247 }
248 )
249 );
250 }
251 }
252
253 #endregion
254
255 #region · Request Validation ·
256
257 /// <summary>
258 /// Validates the given <see cref="NavigationRequest"/> instance
259 /// </summary>
260 /// <param name="request"></param>
261 /// /// <returns>Returns as to if the request was validated or not, if not validated it woun't be processed.</returns>
262 private bool ValidateRequest(NavigationRequest request)
263 {
264 // basic check
265 if (request == null)
266 {
267 throw new ArgumentNullException("request");
268 }
269
270 // by default
271 return true;
272 }
273
274 #endregion
275
276 #region · Private Methods ·
277
278 private void OnNavigationCancelled()
279 {
280 Channel<NavigationFailedInfo>.Public.OnNext(new NavigationFailedInfo(null));
281 }
282
283 private void PublishNavigationFailedInfo(NavigationRequest request)
284 {
285 Channel<NavigationFailedInfo>.Public.OnNext(new NavigationFailedInfo(request));
286 }
287
288 private void PublishNavigatedInfo(string title, NavigationResponse response)
289 {
290 Channel<NavigatedInfo>.Public.OnNext
291 (
292 new NavigatedInfo
293 (
294 response.Request,
295 title,
296 (response.Content as WindowElement).Id
297 ),
298 true
299 );
300 }
301
302 #endregion
303 }
304 }