PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/AODL/Document/Forms/FormProperties.cs

https://bitbucket.org/chrisc/aodl
C# | 499 lines | 373 code | 49 blank | 77 comment | 14 complexity | 380eadd2f30923da9ba7e85022afb875 MD5 | raw file
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  4. *
  5. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Use is subject to license terms.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
  12. * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. *
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. ************************************************************************/
  22. using System.Xml.Linq;
  23. namespace AODL.Document.Forms
  24. {
  25. #region FormProperty abstract class
  26. public abstract class FormProperty
  27. {
  28. protected IDocument _document;
  29. protected XElement _node;
  30. /// <summary>
  31. /// The XML node representing the property
  32. /// </summary>
  33. public XElement Node
  34. {
  35. get { return _node; }
  36. set { _node = value; }
  37. }
  38. /// <summary>
  39. /// The document that contains the form
  40. /// </summary>
  41. public IDocument Document
  42. {
  43. get { return _document; }
  44. set { _document = value; }
  45. }
  46. /// <summary>
  47. /// Property name
  48. /// </summary>
  49. public abstract string Name { get; set; }
  50. public abstract PropertyValueType? PropertyValueType { get; set; }
  51. }
  52. #endregion
  53. #region SingleFormProperty class
  54. public class SingleFormProperty : FormProperty
  55. {
  56. protected object _value;
  57. /// <summary>
  58. /// Creates the SingleFormProperty
  59. /// </summary>
  60. /// <param name="document">Document containing the form</param>
  61. /// <param name="propValueType">Type of the property value</param>
  62. /// <param name="propName">Property name</param>
  63. /// <param name="propValue">Property value</param>
  64. public SingleFormProperty(IDocument document, PropertyValueType propValueType, string propName, string propValue)
  65. {
  66. Document = document;
  67. Node = new XElement(Ns.Form + "property");
  68. PropertyValueType = propValueType;
  69. Name = propName;
  70. Value = propValue;
  71. }
  72. /// <summary>
  73. /// Creates the SingleFormProperty
  74. /// </summary>
  75. /// <param name="document">Document containing the form</param>
  76. /// <param name="propValueType">Type of the property value</param>
  77. public SingleFormProperty(IDocument document, PropertyValueType propValueType)
  78. {
  79. Document = document;
  80. Node = new XElement(Ns.Form + "property");
  81. PropertyValueType = propValueType;
  82. }
  83. public SingleFormProperty(IDocument document, XElement node)
  84. {
  85. Document = document;
  86. Node = node;
  87. }
  88. public override PropertyValueType? PropertyValueType
  89. {
  90. get
  91. {
  92. string s = (string) Node.Attribute(Ns.Office + "value-type");
  93. if (s == null) return null;
  94. switch (s)
  95. {
  96. case "float":
  97. return Forms.PropertyValueType.Float;
  98. case "percentage":
  99. return Forms.PropertyValueType.Percentage;
  100. case "currency":
  101. return Forms.PropertyValueType.Currency;
  102. case "date":
  103. return Forms.PropertyValueType.Date;
  104. case "time":
  105. return Forms.PropertyValueType.Time;
  106. case "boolean":
  107. return Forms.PropertyValueType.Boolean;
  108. case "string":
  109. return Forms.PropertyValueType.String;
  110. default:
  111. return Forms.PropertyValueType.String;
  112. }
  113. }
  114. set
  115. {
  116. string s = "";
  117. switch (value)
  118. {
  119. case Forms.PropertyValueType.Float:
  120. s = "float";
  121. break;
  122. case Forms.PropertyValueType.Percentage:
  123. s = "percentage";
  124. break;
  125. case Forms.PropertyValueType.Currency:
  126. s = "currency";
  127. break;
  128. case Forms.PropertyValueType.Date:
  129. s = "date";
  130. break;
  131. case Forms.PropertyValueType.Time:
  132. s = "time";
  133. break;
  134. case Forms.PropertyValueType.Boolean:
  135. s = "boolean";
  136. break;
  137. case Forms.PropertyValueType.String:
  138. s = "string";
  139. break;
  140. }
  141. Node.SetAttributeValue(Ns.Office + "value-type", s);
  142. }
  143. }
  144. /// <summary>
  145. /// Property value
  146. /// </summary>
  147. public string Value
  148. {
  149. get
  150. {
  151. string s = "";
  152. switch (PropertyValueType)
  153. {
  154. case Forms.PropertyValueType.Float:
  155. s = "value";
  156. break;
  157. case Forms.PropertyValueType.Percentage:
  158. s = "value";
  159. break;
  160. case Forms.PropertyValueType.Currency:
  161. s = "value";
  162. break;
  163. case Forms.PropertyValueType.Date:
  164. s = "date-value";
  165. break;
  166. case Forms.PropertyValueType.Time:
  167. s = "time-value";
  168. break;
  169. case Forms.PropertyValueType.Boolean:
  170. s = "boolean-value";
  171. break;
  172. case Forms.PropertyValueType.String:
  173. s = "string-value";
  174. break;
  175. }
  176. return (string) Node.Attribute(Ns.Office + s);
  177. }
  178. set
  179. {
  180. string s = "";
  181. switch (PropertyValueType)
  182. {
  183. case Forms.PropertyValueType.Float:
  184. s = "value";
  185. break;
  186. case Forms.PropertyValueType.Percentage:
  187. s = "value";
  188. break;
  189. case Forms.PropertyValueType.Currency:
  190. s = "value";
  191. break;
  192. case Forms.PropertyValueType.Date:
  193. s = "date-value";
  194. break;
  195. case Forms.PropertyValueType.Time:
  196. s = "time-value";
  197. break;
  198. case Forms.PropertyValueType.Boolean:
  199. s = "boolean-value";
  200. break;
  201. case Forms.PropertyValueType.String:
  202. s = "string-value";
  203. break;
  204. }
  205. Node.SetAttributeValue(Ns.Office + s, value);
  206. }
  207. }
  208. /// <summary>
  209. /// Property name
  210. /// </summary>
  211. public override string Name
  212. {
  213. get { return (string) Node.Attribute(Ns.Form + "property-name"); }
  214. set { Node.SetAttributeValue(Ns.Form + "property-name", value); }
  215. }
  216. }
  217. #endregion
  218. #region ListFormPropertyElem class
  219. public class ListFormPropertyElement
  220. {
  221. protected IDocument _document;
  222. protected XElement _node;
  223. protected PropertyValueType? _propertyValueType;
  224. protected object _value;
  225. /// <summary>
  226. /// Creates the ListFormPropertyElement
  227. /// </summary>
  228. /// <param name="property">Property containing this element</param>
  229. /// <param name="propValue">Element value</param>
  230. public ListFormPropertyElement(FormProperty property, string propValue)
  231. {
  232. Document = property.Document;
  233. Node = new XElement(Ns.Form + "list-value");
  234. _propertyValueType = property.PropertyValueType;
  235. Value = propValue;
  236. }
  237. public ListFormPropertyElement(IDocument document, XElement node)
  238. {
  239. Document = document;
  240. Node = node;
  241. }
  242. /// <summary>
  243. /// Creates the ListFormPropertyElement
  244. /// </summary>
  245. /// <param name="property">Property containing this element</param>
  246. public ListFormPropertyElement(FormProperty property)
  247. {
  248. Document = property.Document;
  249. Node = new XElement(Ns.Form + "list-value");
  250. _propertyValueType = property.PropertyValueType;
  251. }
  252. /// <summary>
  253. /// XML node representing the ListFormProperty element
  254. /// </summary>
  255. public XElement Node
  256. {
  257. get { return _node; }
  258. set { _node = value; }
  259. }
  260. /// <summary>
  261. /// Main document
  262. /// </summary>
  263. public IDocument Document
  264. {
  265. get { return _document; }
  266. set { _document = value; }
  267. }
  268. /// <summary>
  269. /// Element value
  270. /// </summary>
  271. public string Value
  272. {
  273. get
  274. {
  275. string s = "";
  276. switch (_propertyValueType)
  277. {
  278. case PropertyValueType.Float:
  279. s = "value";
  280. break;
  281. case PropertyValueType.Percentage:
  282. s = "value";
  283. break;
  284. case PropertyValueType.Currency:
  285. s = "value";
  286. break;
  287. case PropertyValueType.Date:
  288. s = "date-value";
  289. break;
  290. case PropertyValueType.Time:
  291. s = "time-value";
  292. break;
  293. case PropertyValueType.Boolean:
  294. s = "boolean-value";
  295. break;
  296. case PropertyValueType.String:
  297. s = "string-value";
  298. break;
  299. }
  300. return (string) Node.Attribute(Ns.Office + s);
  301. }
  302. set
  303. {
  304. string s = "";
  305. switch (_propertyValueType)
  306. {
  307. case PropertyValueType.Float:
  308. s = "value";
  309. break;
  310. case PropertyValueType.Percentage:
  311. s = "value";
  312. break;
  313. case PropertyValueType.Currency:
  314. s = "value";
  315. break;
  316. case PropertyValueType.Date:
  317. s = "date-value";
  318. break;
  319. case PropertyValueType.Time:
  320. s = "time-value";
  321. break;
  322. case PropertyValueType.Boolean:
  323. s = "boolean-value";
  324. break;
  325. case PropertyValueType.String:
  326. s = "string-value";
  327. break;
  328. }
  329. Node.SetAttributeValue(Ns.Office + s, value);
  330. }
  331. }
  332. }
  333. #endregion
  334. #region ListFormProperty class
  335. public class ListFormProperty : FormProperty
  336. {
  337. protected ListFormPropertyElemCollection _propertyValues;
  338. /// <summary>
  339. /// Creates the ListFormProperty
  340. /// </summary>
  341. /// <param name="document">Main document</param>
  342. /// <param name="propValueType">Property value type</param>
  343. public ListFormProperty(IDocument document, PropertyValueType propValueType)
  344. {
  345. Document = document;
  346. Node = new XElement(Ns.Form + "list-property");
  347. PropertyValueType = propValueType;
  348. _propertyValues = new ListFormPropertyElemCollection();
  349. _propertyValues.Inserted += PropertyValuesCollection_Inserted;
  350. _propertyValues.Removed += PropertyValuesCollection_Removed;
  351. }
  352. public ListFormProperty(IDocument document, XElement node)
  353. {
  354. Document = document;
  355. Node = node;
  356. _propertyValues = new ListFormPropertyElemCollection();
  357. foreach (XElement nodeChild in node.Elements())
  358. {
  359. if (nodeChild.Name == Ns.Form + "list-value")
  360. {
  361. _propertyValues.Add(new ListFormPropertyElement(document, nodeChild));
  362. }
  363. }
  364. _propertyValues.Inserted += PropertyValuesCollection_Inserted;
  365. _propertyValues.Removed += PropertyValuesCollection_Removed;
  366. }
  367. /// <summary>
  368. /// Get the list of property elements
  369. /// </summary>
  370. public ListFormPropertyElemCollection PropertyValues
  371. {
  372. get { return _propertyValues; }
  373. set { _propertyValues = value; }
  374. }
  375. public override PropertyValueType? PropertyValueType
  376. {
  377. get
  378. {
  379. string s = (string) Node.Attribute(Ns.Office + "value-type");
  380. if (s == null) return null;
  381. PropertyValueType vt;
  382. switch (s)
  383. {
  384. case "float":
  385. return Forms.PropertyValueType.Float;
  386. case "percentage":
  387. return Forms.PropertyValueType.Percentage;
  388. case "currency":
  389. return Forms.PropertyValueType.Currency;
  390. case "date":
  391. return Forms.PropertyValueType.Date;
  392. case "time":
  393. return Forms.PropertyValueType.Time;
  394. case "boolean":
  395. return Forms.PropertyValueType.Boolean;
  396. case "string":
  397. return Forms.PropertyValueType.String;
  398. default:
  399. return Forms.PropertyValueType.String;
  400. }
  401. }
  402. set
  403. {
  404. string s = "";
  405. switch (value)
  406. {
  407. case Forms.PropertyValueType.Float:
  408. s = "float";
  409. break;
  410. case Forms.PropertyValueType.Percentage:
  411. s = "percentage";
  412. break;
  413. case Forms.PropertyValueType.Currency:
  414. s = "currency";
  415. break;
  416. case Forms.PropertyValueType.Date:
  417. s = "date";
  418. break;
  419. case Forms.PropertyValueType.Time:
  420. s = "time";
  421. break;
  422. case Forms.PropertyValueType.Boolean:
  423. s = "boolean";
  424. break;
  425. case Forms.PropertyValueType.String:
  426. s = "string";
  427. break;
  428. }
  429. Node.SetAttributeValue(Ns.Office + "value-type", s);
  430. }
  431. }
  432. /// <summary>
  433. /// Propert name
  434. /// </summary>
  435. public override string Name
  436. {
  437. get { return (string) Node.Attribute(Ns.Form + "property-name"); }
  438. set { Node.SetAttributeValue(Ns.Form + "property-name", value); }
  439. }
  440. private void PropertyValuesCollection_Inserted(int index, object value)
  441. {
  442. Node.Add(((ListFormPropertyElement) value).Node);
  443. }
  444. private static void PropertyValuesCollection_Removed(int index, object value)
  445. {
  446. ((ListFormPropertyElement) value).Node.Remove();
  447. }
  448. }
  449. #endregion
  450. }