Mercurial > silverbladetech
comparison Chronosv2/source/Presentation/Windows/DesktopSerializer.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.Globalization; | |
28 using System.IO; | |
29 using System.Linq; | |
30 using System.Windows; | |
31 using System.Windows.Controls; | |
32 using System.Windows.Markup; | |
33 using System.Xml; | |
34 using System.Xml.Linq; | |
35 using Chronos.Extensions.Windows; | |
36 using Chronos.Presentation.Core.Services; | |
37 using Chronos.Presentation.Core.Windows; | |
38 using Chronos.Presentation.Windows.Controls; | |
39 using nRoute.ViewServices; | |
40 | |
41 namespace Chronos.Presentation.Windows | |
42 { | |
43 /// <summary> | |
44 /// Desktop serializer for <see cref="E:Chronos.Presentation.Windows.Elements.WidgetElement"/> and | |
45 /// <see cref="E:Chronos.Presentation.Windows.Elements.ShortcutElement"/> contents. | |
46 /// </summary> | |
47 internal static class DesktopSerializer | |
48 { | |
49 #region · Save/Load Methods · | |
50 | |
51 /// <summary> | |
52 /// Loads the contents of the specified filename in the given <see cref="Desktop"/> instance. | |
53 /// </summary> | |
54 /// <param name="desktop">The desktop.</param> | |
55 /// <param name="filename">The filename.</param> | |
56 public static void Load(Desktop desktop, string filename) | |
57 { | |
58 XElement root = LoadFromFile(filename); | |
59 | |
60 if (root == null) | |
61 { | |
62 return; | |
63 } | |
64 | |
65 // Widget Deserialization | |
66 IEnumerable<XElement> widgetsXML = root.Elements("WidgetElements").Elements("WidgetElement"); | |
67 | |
68 foreach (XElement widgetXML in widgetsXML) | |
69 { | |
70 Guid id = new Guid(widgetXML.Element("Id").Value); | |
71 WidgetElement item = DeserializeWidget(widgetXML, id, 0, 0); | |
72 | |
73 desktop.AddElement(item, item.GetPosition()); | |
74 } | |
75 | |
76 // Shortcut Deserialization | |
77 IEnumerable<XElement> shortcutsXML = root.Elements("ShortcutElements").Elements("ShortcutElement"); | |
78 int ti = 0; | |
79 | |
80 foreach (XElement shortcutXML in shortcutsXML) | |
81 { | |
82 Guid id = new Guid(shortcutXML.Element("Id").Value); | |
83 ShortcutElement item = DeserializeShortcut(shortcutXML, id, 0, 0); | |
84 | |
85 item.TabIndex = ++ti; | |
86 | |
87 desktop.AddElement(item, item.GetPosition()); | |
88 } | |
89 } | |
90 | |
91 /// <summary> | |
92 /// Saves the contents of the specified <see cref="Desktop"/> instance in the given file. | |
93 /// </summary> | |
94 /// <param name="desktop">The desktop.</param> | |
95 /// <param name="filename">The filename.</param> | |
96 public static void Save(Desktop desktop, string filename) | |
97 { | |
98 IEnumerable<WidgetElement> widgets = desktop.Children.OfType<WidgetElement>(); | |
99 IEnumerable<ShortcutElement> shortcuts = desktop.Children.OfType<ShortcutElement>(); | |
100 | |
101 XElement widgetsItemsXML = SerializeWidgets(widgets); | |
102 XElement shortcutsItemsXML = SerializeShortcuts(shortcuts); | |
103 | |
104 XElement root = new XElement("Root"); | |
105 | |
106 root.Add(widgetsItemsXML); | |
107 root.Add(shortcutsItemsXML); | |
108 | |
109 SaveFile(filename, root); | |
110 } | |
111 | |
112 #endregion | |
113 | |
114 #region · Serialization Methods · | |
115 | |
116 private static XElement LoadFromFile(string filename) | |
117 { | |
118 try | |
119 { | |
120 if (File.Exists(filename)) | |
121 { | |
122 return XElement.Load(filename); | |
123 } | |
124 } | |
125 catch (Exception) | |
126 { | |
127 IShowMessageViewService showMessageService = ViewServiceLocator.GetViewService<IShowMessageViewService>(); | |
128 | |
129 showMessageService.ButtonSetup = DialogButton.Ok; | |
130 showMessageService.Caption = "Chronos - Error en la carga del escritorio"; | |
131 showMessageService.Text = "No ha sido posible realizar la carga del escritorio."; | |
132 | |
133 showMessageService.ShowMessage(); | |
134 } | |
135 | |
136 return null; | |
137 } | |
138 | |
139 private static void SaveFile(string filename, XElement xElement) | |
140 { | |
141 try | |
142 { | |
143 xElement.Save(filename); | |
144 } | |
145 catch (Exception) | |
146 { | |
147 IShowMessageViewService showMessageService = ViewServiceLocator.GetViewService<IShowMessageViewService>(); | |
148 | |
149 showMessageService.ButtonSetup = DialogButton.Ok; | |
150 showMessageService.Caption = "Chronos - Error al guardar el estado del escritorio"; | |
151 showMessageService.Text = "No ha sido posible al guardar el estado del escritorio."; | |
152 | |
153 showMessageService.ShowMessage(); | |
154 } | |
155 } | |
156 | |
157 private static XElement SerializeWidgets(IEnumerable<WidgetElement> widgets) | |
158 { | |
159 XElement serializedItems = new XElement | |
160 ("WidgetElements", | |
161 from item in widgets | |
162 select new XElement | |
163 ("WidgetElement", | |
164 new XElement("Type", item.GetType().AssemblyQualifiedName), | |
165 new XElement("Left", Canvas.GetLeft(item)), | |
166 new XElement("Top", Canvas.GetTop(item)), | |
167 new XElement("Width", item.Width), | |
168 new XElement("Height", item.RealHeight), | |
169 new XElement("Id", item.Id), | |
170 new XElement("zIndex", Canvas.GetZIndex(item)), | |
171 new XElement("WidgetState", item.WidgetState), | |
172 new XElement("ShowMinimizeButton", item.ShowMinimizeButton) | |
173 ) | |
174 ); | |
175 | |
176 return serializedItems; | |
177 } | |
178 | |
179 private static WidgetElement DeserializeWidget(XElement itemXML, Guid id, double offsetX, double offsetY) | |
180 { | |
181 WidgetElement item = (WidgetElement)Activator.CreateInstance(Type.GetType(itemXML.Element("Type").Value)); | |
182 | |
183 item.ShowMinimizeButton = Boolean.Parse(itemXML.Element("ShowMinimizeButton").Value); | |
184 item.StartupLocation = StartupPosition.Manual; | |
185 | |
186 item.Move(Double.Parse(itemXML.Element("Left").Value, CultureInfo.InvariantCulture) + offsetX, | |
187 Double.Parse(itemXML.Element("Top").Value, CultureInfo.InvariantCulture) + offsetY); | |
188 item.SetZIndex(Int32.Parse(itemXML.Element("zIndex").Value)); | |
189 | |
190 WindowState widgetState = (WindowState)Enum.Parse(typeof(WindowState), itemXML.Element("WidgetState").Value as String); | |
191 | |
192 item.Width = Double.Parse(itemXML.Element("Width").Value, CultureInfo.InvariantCulture); | |
193 item.Height = Double.Parse(itemXML.Element("Height").Value, CultureInfo.InvariantCulture); | |
194 | |
195 if (widgetState == WindowState.Minimized) | |
196 { | |
197 item.WidgetState = widgetState; | |
198 } | |
199 | |
200 return item; | |
201 } | |
202 | |
203 private static XElement SerializeShortcuts(IEnumerable<ShortcutElement> shortcuts) | |
204 { | |
205 XElement serializedItems = new XElement("ShortcutElements", | |
206 from item in shortcuts | |
207 let contentXaml = XamlWriter.Save(((ShortcutElement)item).DataContext) | |
208 select new XElement("ShortcutElement", | |
209 new XElement("Left", Canvas.GetLeft(item)), | |
210 new XElement("Top", Canvas.GetTop(item)), | |
211 new XElement("Width", item.Width), | |
212 new XElement("Height", item.Height), | |
213 new XElement("Id", item.Id), | |
214 new XElement("zIndex", Canvas.GetZIndex(item)), | |
215 new XElement("DataContext", contentXaml) | |
216 )); | |
217 | |
218 return serializedItems; | |
219 } | |
220 | |
221 private static ShortcutElement DeserializeShortcut(XElement itemXML, Guid id, double offsetX, double offsetY) | |
222 { | |
223 ShortcutElement item = new ShortcutElement | |
224 { | |
225 DataContext = XamlReader.Load(XmlReader.Create(new StringReader(itemXML.Element("DataContext").Value))), | |
226 Width = Double.Parse(itemXML.Element("Width").Value, CultureInfo.InvariantCulture), | |
227 Height = Double.Parse(itemXML.Element("Height").Value, CultureInfo.InvariantCulture) | |
228 }; | |
229 | |
230 item.StartupLocation = StartupPosition.Manual; | |
231 item.Move(Double.Parse(itemXML.Element("Left").Value, CultureInfo.InvariantCulture) + offsetX, | |
232 Double.Parse(itemXML.Element("Top").Value, CultureInfo.InvariantCulture) + offsetY); | |
233 item.SetZIndex(Int32.Parse(itemXML.Element("zIndex").Value)); | |
234 | |
235 return item; | |
236 } | |
237 | |
238 #endregion | |
239 } | |
240 } |