PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/AODL/Document/Forms/Controls/ODFFormControl.cs

https://bitbucket.org/chrisc/aodl
C# | 389 lines | 248 code | 49 blank | 92 comment | 27 complexity | 897a37af5949850a6ffcb5fa212a2ab5 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;
  23. using System.Xml.Linq;
  24. using AODL.Document.Content;
  25. using AODL.Document.Styles;
  26. namespace AODL.Document.Forms.Controls
  27. {
  28. /// <summary>
  29. /// Summary description for IODFFormControl.
  30. /// </summary>
  31. public abstract class ODFFormControl
  32. {
  33. private ContentCollection _contentCollection;
  34. private ODFControlRef _controlRef;
  35. protected IDocument _document;
  36. private int _refCounter;
  37. protected ODFFormControl(ODFForm parentForm, XElement node)
  38. {
  39. _document = parentForm.Document;
  40. ParentForm = parentForm;
  41. Node = node;
  42. _contentCollection = null;
  43. _controlRef = null;
  44. Properties = new FormPropertyCollection();
  45. Properties.Inserted += PropertyCollection_Inserted;
  46. Properties.Removed += PropertyCollection_Removed;
  47. }
  48. protected ODFFormControl(ODFForm parentForm, ContentCollection contentCollection, string id)
  49. {
  50. _document = parentForm.Document;
  51. ParentForm = parentForm;
  52. Node = parentForm.Node;
  53. _contentCollection = contentCollection;
  54. CreateBasicNode();
  55. Id = id;
  56. ControlImplementation = "ooo:com.sun.star.form.component.TextField";
  57. _controlRef = new ODFControlRef(_document, id);
  58. Properties = new FormPropertyCollection();
  59. Properties.Inserted += PropertyCollection_Inserted;
  60. Properties.Removed += PropertyCollection_Removed;
  61. }
  62. protected ODFFormControl(ODFForm parentForm, ContentCollection contentCollection, string id, string x, string y,
  63. string width, string height)
  64. //: base (document, ContentCollection, id, x, y, width, height)
  65. {
  66. _document = parentForm.Document;
  67. ParentForm = parentForm;
  68. Node = parentForm.Node;
  69. _contentCollection = contentCollection;
  70. CreateBasicNode();
  71. Id = id;
  72. _controlRef = new ODFControlRef(_document, id, x, y, width, height);
  73. Properties = new FormPropertyCollection();
  74. Properties.Inserted += PropertyCollection_Inserted;
  75. Properties.Removed += PropertyCollection_Removed;
  76. }
  77. public ContentCollection ContentCollection
  78. {
  79. get { return _contentCollection; }
  80. set { _contentCollection = value; }
  81. }
  82. public ODFControlRef ControlRef
  83. {
  84. get { return _controlRef; }
  85. set { _controlRef = value; }
  86. }
  87. /// <summary>
  88. /// Collection of generic form properties (form:property in ODF)
  89. /// </summary>
  90. public FormPropertyCollection Properties { get; set; }
  91. /// <summary>
  92. /// Parent form
  93. /// </summary>
  94. public ODFForm ParentForm { get; set; }
  95. /// <summary>
  96. /// XML node that represents the control
  97. /// </summary>
  98. public XElement Node { get; set; }
  99. /// <summary>
  100. /// Control implementation. Don't change it unless required
  101. /// </summary>
  102. public string ControlImplementation
  103. {
  104. get { return (string) Node.Attribute(Ns.Form + "control-implementation"); }
  105. set { Node.SetAttributeValue(Ns.Form + "control-implementation", value); }
  106. }
  107. /// <summary>
  108. /// The name of the control
  109. /// </summary>
  110. public string Name
  111. {
  112. get { return (string) Node.Attribute(Ns.Form + "name"); }
  113. set { Node.SetAttributeValue(Ns.Form + "name", value); }
  114. }
  115. /// <summary>
  116. /// Control ID
  117. /// </summary>
  118. public string Id
  119. {
  120. get { return (string) Node.Attribute(Ns.Form + "id"); }
  121. set
  122. {
  123. Node.SetAttributeValue(Ns.Form + "id", value);
  124. if (_controlRef != null) _controlRef.DrawControl = value;
  125. }
  126. }
  127. /// <summary>
  128. /// Returns the type of the control
  129. /// </summary>
  130. public abstract string Type { get; }
  131. protected virtual void CreateBasicNode()
  132. {
  133. }
  134. public virtual void AddToContentCollection()
  135. {
  136. if (_contentCollection != null)
  137. {
  138. if (_refCounter == 0)
  139. {
  140. _contentCollection.Add(_controlRef);
  141. _refCounter++;
  142. }
  143. else
  144. {
  145. throw new Exception("Cannot add control to form: it already belongs to another form!");
  146. }
  147. }
  148. }
  149. public virtual void RemoveFromContentCollection()
  150. {
  151. if (_contentCollection != null && _refCounter > 0)
  152. {
  153. _contentCollection.Remove(_controlRef);
  154. _refCounter--;
  155. }
  156. }
  157. public void SuppressPropertyEvents()
  158. {
  159. Properties.Inserted -= PropertyCollection_Inserted;
  160. Properties.Removed -= PropertyCollection_Removed;
  161. }
  162. public void RestorePropertyEvents()
  163. {
  164. Properties.Inserted += PropertyCollection_Inserted;
  165. Properties.Removed += PropertyCollection_Removed;
  166. }
  167. private void PropertyCollection_Inserted(int index, object value)
  168. {
  169. XElement formProp = Node.Element(Ns.Form + "properties");
  170. if (formProp == null)
  171. {
  172. formProp = new XElement(Ns.Form + "properties");
  173. Node.Add(formProp);
  174. }
  175. FormProperty prop = (FormProperty) value;
  176. formProp.Add(prop.Node);
  177. }
  178. private void PropertyCollection_Removed(int index, object value)
  179. {
  180. XElement formProp = Node.Element(Ns.Form + "properties");
  181. if (formProp == null)
  182. return;
  183. FormProperty prop = (FormProperty) value;
  184. prop.Node.Remove();
  185. if (index == 0)
  186. {
  187. formProp.Remove();
  188. }
  189. }
  190. /// <summary>
  191. /// Look for a control generic property by its name
  192. /// </summary>
  193. /// <param name="name">Property name</param>
  194. /// <returns></returns>
  195. public FormProperty GetFormProperty(string name)
  196. {
  197. foreach (FormProperty fp in Properties)
  198. {
  199. if (fp.Name == name)
  200. {
  201. return fp;
  202. }
  203. }
  204. return null;
  205. }
  206. public void FixPropertyCollection()
  207. {
  208. Properties.Clear();
  209. SuppressPropertyEvents();
  210. XElement formProp = Node.Element(Ns.Form + "properties");
  211. if (formProp == null) return;
  212. foreach (XElement nodeChild in formProp.Elements())
  213. {
  214. if (nodeChild.Name == Ns.Form + "property" && nodeChild.Parent == formProp)
  215. {
  216. SingleFormProperty sp = new SingleFormProperty(_document, nodeChild);
  217. Properties.Add(sp);
  218. }
  219. if (nodeChild.Name == Ns.Form + "list-property" && nodeChild.Parent == formProp)
  220. {
  221. ListFormProperty lp = new ListFormProperty(_document, nodeChild);
  222. Properties.Add(lp);
  223. }
  224. }
  225. RestorePropertyEvents();
  226. }
  227. #region ODFControlRef properties
  228. /// <summary>
  229. /// X coordinate of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)
  230. /// </summary>
  231. public string X
  232. {
  233. get { return _controlRef.X; }
  234. set { _controlRef.X = value; }
  235. }
  236. /// <summary>
  237. /// Y coordinate of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)
  238. /// </summary>
  239. public string Y
  240. {
  241. get { return _controlRef.Y; }
  242. set { _controlRef.Y = value; }
  243. }
  244. /// <summary>
  245. /// Width of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)
  246. /// </summary>
  247. public string Width
  248. {
  249. get { return _controlRef.Width; }
  250. set { _controlRef.Width = value; }
  251. }
  252. /// <summary>
  253. /// Height of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)
  254. /// </summary>
  255. public string Height
  256. {
  257. get { return _controlRef.Height; }
  258. set { _controlRef.Height = value; }
  259. }
  260. /// <summary>
  261. /// Control's Z index
  262. /// </summary>
  263. public int ZIndex
  264. {
  265. get { return _controlRef.ZIndex; }
  266. set { _controlRef.ZIndex = value; }
  267. }
  268. /// <summary>
  269. /// A Style class which is referenced with the content object.
  270. /// If no style is available this is null.
  271. /// </summary>
  272. public IStyle Style
  273. {
  274. get { return _controlRef.Style; }
  275. set { _controlRef.Style = value; }
  276. }
  277. /// <summary>
  278. /// Style name
  279. /// </summary>
  280. public string StyleName
  281. {
  282. get { return _controlRef.StyleName; }
  283. set { _controlRef.StyleName = value; }
  284. }
  285. /// <summary>
  286. /// Text style name
  287. /// </summary>
  288. public string TextStyleName
  289. {
  290. get { return _controlRef.TextStyleName; }
  291. set { _controlRef.TextStyleName = value; }
  292. }
  293. /// <summary>
  294. /// A text style.
  295. /// If no style is available this is null.
  296. /// </summary>
  297. public IStyle TextStyle
  298. {
  299. get { return _controlRef.TextStyle; }
  300. set { _controlRef.TextStyle = value; }
  301. }
  302. /// <summary>
  303. /// Number of page to use as an anchor
  304. /// </summary>
  305. public int AnchorPageNumber
  306. {
  307. get { return _controlRef.AnchorPageNumber; }
  308. set { _controlRef.AnchorPageNumber = value; }
  309. }
  310. /// <summary>
  311. /// What to use as an anchor
  312. /// </summary>
  313. public AnchorType? AnchorType
  314. {
  315. get { return _controlRef.AnchorType; }
  316. set { _controlRef.AnchorType = value; }
  317. }
  318. /// <summary>
  319. /// Table background
  320. /// </summary>
  321. public bool? TableBackground
  322. {
  323. get { return _controlRef.TableBackground; }
  324. set { _controlRef.TableBackground = value; }
  325. }
  326. /// <summary>
  327. /// Transform. For the details, see ODF v1.0 specification
  328. /// </summary>
  329. public string Transform
  330. {
  331. get { return _controlRef.Transform; }
  332. set { _controlRef.Transform = value; }
  333. }
  334. ~ODFFormControl()
  335. {
  336. RemoveFromContentCollection();
  337. }
  338. #endregion
  339. }
  340. }