Mercurial > silverbladetech
comparison Chronosv2/source/WidgetLibrary/WidgetLibraryViewModel.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 | 741981715d94 |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Collections.ObjectModel; | |
4 using System.ComponentModel; | |
5 using System.Linq; | |
6 using System.Windows.Input; | |
7 using Chronos.Presentation.Core.Configuration; | |
8 using Chronos.Presentation.Core.VirtualDesktops; | |
9 using Chronos.Presentation.Core.Widgets; | |
10 using Chronos.Presentation.Core.Windows; | |
11 using Chronos.Presentation.ViewModel; | |
12 using NLog; | |
13 using nRoute.Components; | |
14 | |
15 namespace Chronos.WidgetLibrary | |
16 { | |
17 /// <summary> | |
18 /// Widget library ViewModel | |
19 /// </summary> | |
20 public sealed class WidgetLibraryViewModel | |
21 : WidgetViewModel | |
22 { | |
23 #region · Constants · | |
24 | |
25 private static readonly string AllWidgetsKey = "All"; | |
26 | |
27 #endregion | |
28 | |
29 #region · PropertyChangedEventArgs Cached Instances · | |
30 | |
31 private static readonly PropertyChangedEventArgs FilterChangedArgs = CreateArgs<WidgetLibraryViewModel>(x => x.Filter); | |
32 private static readonly PropertyChangedEventArgs WidgetsChangedArgs = CreateArgs<WidgetLibraryViewModel>(x => x.Widgets); | |
33 private static readonly PropertyChangedEventArgs SelectedWidgetChangedArgs = CreateArgs<WidgetLibraryViewModel>(x => x.SelectedWidget); | |
34 | |
35 #endregion | |
36 | |
37 #region · Fields · | |
38 | |
39 private string filter; | |
40 private List<string> groups; | |
41 private List<WidgetItemViewModel> widgets; | |
42 private IWidget selectedWidget; | |
43 private ActionCommand createWidgetCommand; | |
44 | |
45 #endregion | |
46 | |
47 #region · Commands · | |
48 | |
49 /// <summary> | |
50 /// Gets the create widget command. | |
51 /// </summary> | |
52 /// <value>The create widget command.</value> | |
53 public ActionCommand CreateWidgetCommand | |
54 { | |
55 get | |
56 { | |
57 if (this.createWidgetCommand == null) | |
58 { | |
59 this.createWidgetCommand = new ActionCommand | |
60 ( | |
61 () => OnCreateWidget(), | |
62 () => CanCreateWidget() | |
63 ); | |
64 } | |
65 | |
66 return this.createWidgetCommand; | |
67 } | |
68 } | |
69 | |
70 #endregion | |
71 | |
72 #region · Properties · | |
73 | |
74 /// <summary> | |
75 /// Gets/sets a fragment of the name to filter for. | |
76 /// </summary> | |
77 public string Filter | |
78 { | |
79 get { return this.filter; } | |
80 set | |
81 { | |
82 if (value != filter) | |
83 { | |
84 this.filter = value; | |
85 this.NotifyPropertyChanged(FilterChangedArgs); | |
86 this.NotifyPropertyChanged(WidgetsChangedArgs); | |
87 | |
88 this.SelectedWidget = null; | |
89 } | |
90 } | |
91 } | |
92 | |
93 /// <summary> | |
94 /// Gets the list of widget groups. | |
95 /// </summary> | |
96 /// <value>The groups.</value> | |
97 public IEnumerable<string> Groups | |
98 { | |
99 get { return this.groups; } | |
100 } | |
101 | |
102 /// <summary> | |
103 /// Gets the list of widgets | |
104 /// </summary> | |
105 public IList<WidgetItemViewModel> Widgets | |
106 { | |
107 get | |
108 { | |
109 if (String.IsNullOrEmpty(this.filter) | |
110 || this.filter == AllWidgetsKey) | |
111 { | |
112 return this.widgets; | |
113 } | |
114 else | |
115 { | |
116 return new ReadOnlyCollection<WidgetItemViewModel>(this.widgets.Where(x => x.Group == this.filter).ToList()); | |
117 } | |
118 } | |
119 } | |
120 | |
121 /// <summary> | |
122 /// Gets or sets the selected widget. | |
123 /// </summary> | |
124 /// <value>The selected widget.</value> | |
125 public IWidget SelectedWidget | |
126 { | |
127 get { return this.selectedWidget; } | |
128 set | |
129 { | |
130 if (this.selectedWidget != value) | |
131 { | |
132 this.selectedWidget = value; | |
133 this.NotifyPropertyChanged(SelectedWidgetChangedArgs); | |
134 this.CreateWidgetCommand.RequeryCanExecute(); | |
135 } | |
136 } | |
137 } | |
138 | |
139 #endregion | |
140 | |
141 #region · Constructors · | |
142 | |
143 /// <summary> | |
144 /// Initializes a new instance of the <see cref="WidgetLibrary"/> class. | |
145 /// </summary> | |
146 public WidgetLibraryViewModel() | |
147 : base() | |
148 { | |
149 this.widgets = this.DiscoverWidgets(); | |
150 this.groups = this.DiscoverWidgetGroups(); | |
151 } | |
152 | |
153 #endregion | |
154 | |
155 #region · Overriden Methods · | |
156 | |
157 /// <summary> | |
158 /// Called when the related view is being closed. | |
159 /// </summary> | |
160 public override void Close() | |
161 { | |
162 base.Close(); | |
163 | |
164 this.selectedWidget = null; | |
165 this.createWidgetCommand = null; | |
166 | |
167 if (this.groups != null) | |
168 { | |
169 this.groups.Clear(); | |
170 this.groups = null; | |
171 } | |
172 | |
173 if (this.widgets != null) | |
174 { | |
175 this.widgets.Clear(); | |
176 this.widgets = null; | |
177 } | |
178 } | |
179 | |
180 #endregion | |
181 | |
182 #region · Command Actions · | |
183 | |
184 private bool CanCreateWidget() | |
185 { | |
186 return (this.selectedWidget != null); | |
187 } | |
188 | |
189 /// <summary> | |
190 /// Handles the create widget command. | |
191 /// </summary> | |
192 private void OnCreateWidget() | |
193 { | |
194 this.GetService<IVirtualDesktopManager>() | |
195 .Show(this.selectedWidget.CreateView() as IDesktopElement); | |
196 } | |
197 | |
198 #endregion | |
199 | |
200 #region · Private Methods · | |
201 | |
202 private List<string> DiscoverWidgetGroups() | |
203 { | |
204 List<string> groups = new List<string>(); | |
205 var q = from widgetdef in this.widgets | |
206 select widgetdef.Group; | |
207 | |
208 groups.Add(AllWidgetsKey); | |
209 groups.AddRange(q.Distinct().ToList()); | |
210 | |
211 return groups; | |
212 } | |
213 | |
214 private List<WidgetItemViewModel> DiscoverWidgets() | |
215 { | |
216 IEnumerable<IWidget> widgetDefs = this.GetService<IWidgetConfigurationService>() | |
217 .GetWidgets(); | |
218 | |
219 if (widgetDefs != null) | |
220 { | |
221 var query = from widgetdef in widgetDefs | |
222 select new WidgetItemViewModel(widgetdef); | |
223 | |
224 return query.ToList(); | |
225 } | |
226 | |
227 return null; | |
228 } | |
229 | |
230 #endregion | |
231 } | |
232 } |