Mercurial > silverbladetech
comparison Chronosv2/source/Presentation/ViewModel/ClosableViewModel.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 System.Collections.Generic; | |
27 using System.ComponentModel; | |
28 using Chronos.Presentation.Core.ViewModel; | |
29 using Chronos.Presentation.Core.VirtualDesktops; | |
30 using NLog; | |
31 using nRoute.Components; | |
32 using nRoute.Components.Messaging; | |
33 | |
34 namespace Chronos.Presentation.ViewModel | |
35 { | |
36 /// <summary> | |
37 /// Base class for closeable view models | |
38 /// </summary> | |
39 public abstract class ClosableViewModel | |
40 : ViewModelBase, IClosableViewModel | |
41 { | |
42 #region · NotifyPropertyChanged Cached Instances · | |
43 | |
44 private static readonly PropertyChangedEventArgs IdChangedArgs = CreateArgs<ClosableViewModel>(x => x.Id); | |
45 private static readonly PropertyChangedEventArgs TitleChangedArgs = CreateArgs<ClosableViewModel>(x => x.Title); | |
46 | |
47 #endregion | |
48 | |
49 #region · Logger · | |
50 | |
51 private static Logger Logger = LogManager.GetCurrentClassLogger(); | |
52 | |
53 #endregion | |
54 | |
55 #region · Fields · | |
56 | |
57 private Guid id; | |
58 private ActionCommand closeCommand; | |
59 private string title; | |
60 private List<IChannelObserver> observers; | |
61 | |
62 #endregion | |
63 | |
64 #region · Commands · | |
65 | |
66 /// <summary> | |
67 /// Gets the Close Command | |
68 /// </summary> | |
69 public ActionCommand CloseCommand | |
70 { | |
71 get | |
72 { | |
73 if (this.closeCommand == null) | |
74 { | |
75 this.closeCommand = new ActionCommand | |
76 ( | |
77 () => Close(), | |
78 () => CanClose() | |
79 ); | |
80 } | |
81 | |
82 return this.closeCommand; | |
83 } | |
84 } | |
85 | |
86 #endregion | |
87 | |
88 #region · Properties · | |
89 | |
90 /// <summary> | |
91 /// Gets or sets the unique identifier. | |
92 /// </summary> | |
93 /// <value>The id.</value> | |
94 public Guid Id | |
95 { | |
96 get { return this.id; } | |
97 set | |
98 { | |
99 if (this.id != value) | |
100 { | |
101 this.id = value; | |
102 this.NotifyPropertyChanged(IdChangedArgs); | |
103 } | |
104 } | |
105 } | |
106 | |
107 /// <summary> | |
108 /// Returns the user-friendly name of this object. | |
109 /// Child classes can set this property to a new value, | |
110 /// or override it to determine the value on-demand. | |
111 /// </summary> | |
112 public virtual string Title | |
113 { | |
114 get { return this.title; } | |
115 set | |
116 { | |
117 if (this.title != value) | |
118 { | |
119 this.title = value; | |
120 this.NotifyPropertyChanged(TitleChangedArgs); | |
121 } | |
122 } | |
123 } | |
124 | |
125 #endregion | |
126 | |
127 #region · Constructors · | |
128 | |
129 /// <summary> | |
130 /// Initializes a new instance of the <see cref="ClosableViewModel"/> class. | |
131 /// </summary> | |
132 protected ClosableViewModel() | |
133 : base() | |
134 { | |
135 this.Id = Guid.NewGuid(); | |
136 this.observers = new List<IChannelObserver>(); | |
137 } | |
138 | |
139 #endregion | |
140 | |
141 #region · Messaging Methods · | |
142 | |
143 protected void Subscribe<T>(Action<T> payloadHandler) | |
144 where T: class | |
145 { | |
146 this.Subscribe<T>(payloadHandler, ThreadOption.BackgroundThread); | |
147 } | |
148 | |
149 protected void Subscribe<T>(Action<T> payloadHandler, ThreadOption threadOption) | |
150 where T: class | |
151 { | |
152 ChannelObserver<T> observer = new ChannelObserver<T>((l) => payloadHandler(l)); | |
153 | |
154 observer.Subscribe(threadOption); | |
155 | |
156 this.observers.Add(observer); | |
157 } | |
158 | |
159 protected void Publish<T>(T message) | |
160 where T: class | |
161 { | |
162 Channel<T>.Publish(message, true); | |
163 } | |
164 | |
165 protected void Publish<T>(T message, bool async) | |
166 where T: class | |
167 { | |
168 Channel<T>.Publish(message, async); | |
169 } | |
170 | |
171 #endregion | |
172 | |
173 #region · Command Actions · | |
174 | |
175 /// <summary> | |
176 /// Determines whether the view related to this view model can be closed. | |
177 /// </summary> | |
178 /// <returns> | |
179 /// <c>true</c> if the related view can be closed; otherwise, <c>false</c>. | |
180 /// </returns> | |
181 public virtual bool CanClose() | |
182 { | |
183 return true; | |
184 } | |
185 | |
186 /// <summary> | |
187 /// Called when the related view is being closed. | |
188 /// </summary> | |
189 public virtual void Close() | |
190 { | |
191 if (this.observers != null) | |
192 { | |
193 this.observers.ForEach(o => o.Unsubscribe()); | |
194 this.observers.Clear(); | |
195 this.observers = null; | |
196 } | |
197 | |
198 this.GetService<IVirtualDesktopManager>().Close(this.Id); | |
199 | |
200 this.id = Guid.Empty; | |
201 this.title = null; | |
202 this.closeCommand = null; | |
203 } | |
204 | |
205 #endregion | |
206 } | |
207 } |