comparison Chronosv2/source/Presentation/ViewModel/NavigationViewModel.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 09d18d6e5f40
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 Chronos.Presentation.Core.Navigation;
27 using Chronos.Presentation.Core.ViewModel;
28 using Chronos.Presentation.Core.VirtualDesktops;
29 using NLog;
30 using nRoute.Components;
31 using nRoute.Navigation;
32
33 namespace Chronos.Presentation.ViewModel
34 {
35 /// <summary>
36 /// base class for navigation based viewmodel iumplementations
37 /// </summary>
38 public abstract class NavigationViewModel
39 : ClosableViewModel, INavigationViewModel
40 {
41 #region · Logger ·
42
43 private static Logger Logger = LogManager.GetCurrentClassLogger();
44
45 #endregion
46
47 #region · Fields ·
48
49 #region · Commands ·
50
51 private ActionCommand newWindowCommand;
52 private ActionCommand restoreCommand;
53
54 #endregion
55
56 #endregion
57
58 #region · INavigationViewModel Commands ·
59
60 /// <summary>
61 /// Gets the open new window command
62 /// </summary>
63 public ActionCommand NewWindowCommand
64 {
65 get
66 {
67 if (this.newWindowCommand == null)
68 {
69 this.newWindowCommand = new ActionCommand
70 (
71 () => OnOpenNewWindow(),
72 () => CanOpenNewWindow()
73 );
74 }
75
76 return this.newWindowCommand;
77 }
78 }
79
80 /// <summary>
81 /// Gets the restore command
82 /// </summary>
83 public ActionCommand RestoreCommand
84 {
85 get
86 {
87 if (this.restoreCommand == null)
88 {
89 this.restoreCommand = new ActionCommand
90 (
91 () => OnRestore(),
92 () => CanRestore()
93 );
94 }
95
96 return this.restoreCommand;
97 }
98 }
99
100 #endregion
101
102 #region · INavigationViewModel Properties ·
103
104 /// <summary>
105 /// Gets the navigation route
106 /// </summary>
107 public virtual string NavigationRoute
108 {
109 get { return String.Empty; }
110 }
111
112 /// <summary>
113 /// Gets a value indicating if the viewmodel has available relations
114 /// </summary>
115 public virtual bool HasRelations
116 {
117 get { return false; }
118 }
119
120 #endregion
121
122 #region · Constructors ·
123
124 /// <summary>
125 /// Initializes a new instance of the <see cref="NavigationViewModel"/> class.
126 /// </summary>
127 protected NavigationViewModel()
128 : base()
129 {
130 }
131
132 #endregion
133
134 #region · Methods ·
135
136 public override void Close()
137 {
138 this.newWindowCommand = null;
139 this.restoreCommand = null;
140
141 base.Close();
142 }
143
144 #endregion
145
146 #region · Navigation Methods ·
147
148 /// <summary>
149 /// Performs the navigation to the given target
150 /// </summary>
151 /// <param name="target"></param>
152 protected void Navigate(string target)
153 {
154 this.Navigate(NavigateMode.New, target, null);
155 }
156
157 /// <summary>
158 /// Performs the navigation to the given target
159 /// </summary>
160 /// <param name="target"></param>
161 protected void Navigate(NavigateMode mode, string target)
162 {
163 this.Navigate(mode, target, null);
164 }
165
166 /// <summary>
167 /// Performs the navigation to the given target
168 /// </summary>
169 /// <param name="target"></param>
170 protected void Navigate(string target, params object[] args)
171 {
172 this.Navigate(NavigateMode.New, target, args);
173 }
174
175 /// <summary>
176 /// Performs the navigation to the given target
177 /// </summary>
178 /// <param name="target"></param>
179 protected void Navigate(NavigateMode mode, string target, params object[] args)
180 {
181 this.GetService<INavigationService>().Navigate(mode, target, args);
182 }
183
184 #endregion
185
186 #region · ISupportNavigationLifecycle Members ·
187
188 /// <summary>
189 /// Closings the specified confirm callback.
190 /// </summary>
191 /// <param name="confirmCallback">The confirm callback.</param>
192 void ISupportNavigationLifecycle.Closing(System.Action<bool> confirmCallback)
193 {
194 this.OnClosing(confirmCallback);
195 }
196
197 /// <summary>
198 /// Initializes the specified request parameters.
199 /// </summary>
200 /// <param name="requestParameters">The request parameters.</param>
201 void ISupportNavigationLifecycle.Initialize(ParametersCollection requestParameters)
202 {
203 this.OnInitialize(requestParameters);
204 }
205
206 #endregion
207
208 #region · ISupportNavigationState Members ·
209
210 /// <summary>
211 /// Restores the state.
212 /// </summary>
213 /// <param name="state">The state.</param>
214 void ISupportNavigationState.RestoreState(ParametersCollection state)
215 {
216 this.OnRestoreState(state);
217 }
218
219 /// <summary>
220 /// Saves the state.
221 /// </summary>
222 /// <returns></returns>
223 ParametersCollection ISupportNavigationState.SaveState()
224 {
225 return this.OnSaveState();
226 }
227
228 #endregion
229
230 #region · Command Actions ·
231
232 protected virtual bool CanOpenNewWindow()
233 {
234 return !String.IsNullOrEmpty(this.NavigationRoute);
235 }
236
237 protected virtual void OnOpenNewWindow()
238 {
239 Logger.Debug("Abrir una nueva ventana '{0}'", this.GetType());
240
241 this.GetService<INavigationService>().Navigate(this.NavigationRoute);
242 }
243
244 protected virtual bool CanRestore()
245 {
246 return true;
247 }
248
249 protected virtual void OnRestore()
250 {
251 Logger.Debug("Restaurar ventana '{0}'", this.GetType());
252
253 this.GetService<IVirtualDesktopManager>().Restore(this.Id);
254 }
255
256 #endregion
257
258 #region · Protected Methods ·
259
260 /// <summary>
261 /// Called when closing.
262 /// </summary>
263 /// <param name="confirmCallback">The confirm callback.</param>
264 protected virtual void OnClosing(Action<bool> confirmCallback)
265 {
266 confirmCallback(false);
267 }
268
269 /// <summary>
270 /// Called when the window is going to be initialized after navigation.
271 /// </summary>
272 /// <param name="requestParameters">The request parameters.</param>
273 protected virtual void OnInitialize(ParametersCollection requestParameters)
274 {
275 }
276
277 /// <summary>
278 /// Called when restore state.
279 /// </summary>
280 /// <param name="state">The state.</param>
281 protected virtual void OnRestoreState(ParametersCollection state)
282 {
283 }
284
285 /// <summary>
286 /// Called when save state.
287 /// </summary>
288 /// <returns></returns>
289 protected virtual ParametersCollection OnSaveState()
290 {
291 return null;
292 }
293
294 #endregion
295 }
296 }