PageRenderTime 71ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/AODL/Document/Forms/Controls/ODFListBox.cs

https://bitbucket.org/chrisc/aodl
C# | 347 lines | 220 code | 30 blank | 97 comment | 16 complexity | 6b277501617343526e14b592250289e2 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. using AODL.Document.Content;
  24. namespace AODL.Document.Forms.Controls
  25. {
  26. public class ODFListBox : ODFFormControl
  27. {
  28. private ODFOptionCollection _options;
  29. /// <summary>
  30. /// Creates an ODFListBox
  31. /// </summary>
  32. /// <param name="parentForm">Parent form that the control belongs to</param>
  33. /// <param name="contentCollection">Collection of content where the control will be referenced</param>
  34. /// <param name="id">Control ID. Please specify a unique ID!</param>
  35. public ODFListBox(ODFForm parentForm, ContentCollection contentCollection, string id)
  36. : base(parentForm, contentCollection, id)
  37. {
  38. _options = new ODFOptionCollection();
  39. RestoreOptionEvents();
  40. }
  41. /// <summary>
  42. /// Creates an ODFListBox
  43. /// </summary>
  44. /// <param name="parentForm">Parent form that the control belongs to</param>
  45. /// <param name="contentCollection">Collection of content where the control will be referenced</param>
  46. /// <param name="id">Control ID. Please specify a unique ID!</param>
  47. /// <param name="x">X coordinate of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
  48. /// <param name="y">Y coordinate of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
  49. /// <param name="width">Width of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
  50. /// <param name="height">Height of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
  51. public ODFListBox(ODFForm parentForm, ContentCollection contentCollection, string id, string x, string y,
  52. string width, string height) : base(parentForm, contentCollection, id, x, y, width, height)
  53. {
  54. _options = new ODFOptionCollection();
  55. RestoreOptionEvents();
  56. }
  57. public ODFListBox(ODFForm parentForm, XElement node) : base(parentForm, node)
  58. {
  59. _options = new ODFOptionCollection();
  60. RestoreOptionEvents();
  61. }
  62. /// <summary>
  63. /// The collection of the ODFOptions (each option stand for a list box element)
  64. /// </summary>
  65. public ODFOptionCollection Options
  66. {
  67. get { return _options; }
  68. set { _options = value; }
  69. }
  70. public override string Type
  71. {
  72. get { return "listbox"; }
  73. }
  74. /// <summary>
  75. /// Specifies whether or not a control can accept user input
  76. /// </summary>
  77. public bool? Disabled
  78. {
  79. get { return (bool?) Node.Attribute(Ns.Form + "disabled"); }
  80. set { Node.SetAttributeValue(Ns.Form + "disabled", value); }
  81. }
  82. /// <summary>
  83. /// Contains additional information about a control.
  84. /// </summary>
  85. public string Title
  86. {
  87. get { return (string) Node.Attribute(Ns.Form + "title"); }
  88. set { Node.SetAttributeValue(Ns.Form + "title", value); }
  89. }
  90. /// <summary>
  91. /// Specifies the source used to populate the list in a list box or
  92. /// combo box. The first column of the list source result set populates the list.
  93. /// </summary>
  94. public string ListSource
  95. {
  96. get { return (string) Node.Attribute(Ns.Form + "list-source"); }
  97. set { Node.SetAttributeValue(Ns.Form + "list-source", value); }
  98. }
  99. /// <summary>
  100. /// Specifies the column values of the list source result set that
  101. /// are used to fill the data field values
  102. /// </summary>
  103. public string BoundColumn
  104. {
  105. get { return (string) Node.Attribute(Ns.Form + "bound-column"); }
  106. set { Node.SetAttributeValue(Ns.Form + "bound-column", value); }
  107. }
  108. /// <summary>
  109. /// Specifies the name of a result set column. The result set is
  110. /// determined by the form which the control belongs to
  111. /// </summary>
  112. public string DataField
  113. {
  114. get { return (string) Node.Attribute(Ns.Form + "data-field"); }
  115. set { Node.SetAttributeValue(Ns.Form + "data-field", value); }
  116. }
  117. /// <summary>
  118. /// Specifies the type of data source that is used to
  119. /// populate the list data in a list box or combo box
  120. /// </summary>
  121. public ListSourceType? ListSourceType
  122. {
  123. get
  124. {
  125. string s = (string) Node.Attribute(Ns.Form + "list-source-type");
  126. if (s == null) return null;
  127. switch (s)
  128. {
  129. case "table":
  130. return Forms.ListSourceType.Table;
  131. case "query":
  132. return Forms.ListSourceType.Query;
  133. case "sql":
  134. return Forms.ListSourceType.Sql;
  135. case "sql-pass-through":
  136. return Forms.ListSourceType.SqlPassThrough;
  137. case "value-list":
  138. return Forms.ListSourceType.ValueList;
  139. case "table-fields":
  140. return Forms.ListSourceType.TableFields;
  141. default:
  142. return null;
  143. }
  144. }
  145. set
  146. {
  147. string s = null;
  148. if (value.HasValue)
  149. {
  150. switch (value.Value)
  151. {
  152. case Forms.ListSourceType.Table:
  153. s = "table";
  154. break;
  155. case Forms.ListSourceType.Query:
  156. s = "query";
  157. break;
  158. case Forms.ListSourceType.Sql:
  159. s = "sql";
  160. break;
  161. case Forms.ListSourceType.SqlPassThrough:
  162. s = "sql-pass-through";
  163. break;
  164. case Forms.ListSourceType.ValueList:
  165. s = "value-list";
  166. break;
  167. case Forms.ListSourceType.TableFields:
  168. s = "table-fields";
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. Node.SetAttributeValue(Ns.Form + "list-source-type", s);
  175. }
  176. }
  177. /// <summary>
  178. /// Specifies the tabbing navigation order of a control within a form
  179. /// </summary>
  180. public int TabIndex
  181. {
  182. get { return (int) Node.Attribute(Ns.Form + "tab-index"); }
  183. set { Node.SetAttributeValue(Ns.Form + "tab-index", value); }
  184. }
  185. /// <summary>
  186. /// Specifies whether or not a control is included in the tabbing
  187. /// navigation order
  188. /// </summary>
  189. public bool? TabStop
  190. {
  191. get { return (bool?) Node.Attribute(Ns.Form + "tab-stop"); }
  192. set { Node.SetAttributeValue(Ns.Form + "tab-stop", value); }
  193. }
  194. /// <summary>
  195. /// Specifies whether or not a control is printed when a user prints
  196. /// the document in which the control is contained
  197. /// </summary>
  198. public bool? Printable
  199. {
  200. get { return (bool?) Node.Attribute(Ns.Form + "printable"); }
  201. set { Node.SetAttributeValue(Ns.Form + "printable", value); }
  202. }
  203. /// <summary>
  204. /// Specifies whether or not a user can modify the value of a control
  205. /// </summary>
  206. public bool? ReadOnly
  207. {
  208. get { return (bool?) Node.Attribute(Ns.Form + "readonly"); }
  209. set { Node.SetAttributeValue(Ns.Form + "readonly", value); }
  210. }
  211. /// <summary>
  212. /// Specifies whether the list in a combo box or list box is always
  213. /// visible or is only visible when the user clicks the drop-down button
  214. /// </summary>
  215. public bool? DropDown
  216. {
  217. get { return (bool?) Node.Attribute(Ns.Form + "dropdown"); }
  218. set { Node.SetAttributeValue(Ns.Form + "dropdown", value); }
  219. }
  220. /// <summary>
  221. /// specifies the number of rows that are visible at a time in a combo box
  222. /// list or a list box list
  223. /// </summary>
  224. public int Size
  225. {
  226. get { return (int?) Node.Attribute(Ns.Form + "size") ?? -1; }
  227. set
  228. {
  229. if (value <= 0)
  230. return;
  231. Node.SetAttributeValue(Ns.Form + "size", value);
  232. }
  233. }
  234. /// <summary>
  235. /// specifies whether or not empty current values are regarded as NULL
  236. /// </summary>
  237. public bool? ConvertEmptyToNull
  238. {
  239. get { return (bool?) Node.Attribute(Ns.Form + "convert-empty-to-null"); }
  240. set { Node.SetAttributeValue(Ns.Form + "convert-empty-to-null", value); }
  241. }
  242. public void SuppressOptionEvents()
  243. {
  244. _options.Inserted -= OptionCollection_Inserted;
  245. _options.Removed -= OptionCollection_Removed;
  246. }
  247. public void RestoreOptionEvents()
  248. {
  249. _options.Inserted += OptionCollection_Inserted;
  250. _options.Removed += OptionCollection_Removed;
  251. }
  252. private void OptionCollection_Inserted(int index, object value)
  253. {
  254. ODFOption opt = (ODFOption) value;
  255. Node.Add(opt.Node);
  256. }
  257. private static void OptionCollection_Removed(int index, object value)
  258. {
  259. ODFOption opt = value as ODFOption;
  260. if (opt != null)
  261. opt.Node.Remove();
  262. }
  263. /// <summary>
  264. /// Looks for a specified option by its value
  265. /// </summary>
  266. /// <param name="val">Option value</param>
  267. /// <returns></returns>
  268. public ODFOption GetOptionByValue(string val)
  269. {
  270. foreach (ODFOption opt in _options)
  271. {
  272. if (opt.Value == val)
  273. {
  274. return opt;
  275. }
  276. }
  277. return null;
  278. }
  279. /// <summary>
  280. /// Looks for a specified option by its label
  281. /// </summary>
  282. /// <param name="lbl">Option label</param>
  283. /// <returns></returns>
  284. public ODFOption GetOptionByLabel(string lbl)
  285. {
  286. foreach (ODFOption opt in _options)
  287. {
  288. if (opt.Label == lbl)
  289. {
  290. return opt;
  291. }
  292. }
  293. return null;
  294. }
  295. public void FixOptionCollection()
  296. {
  297. _options.Clear();
  298. SuppressOptionEvents();
  299. foreach (XElement nodeChild in Node.Elements())
  300. {
  301. if (nodeChild.Name == Ns.Form + "option" && nodeChild.Parent == Node)
  302. {
  303. ODFOption sp = new ODFOption(_document, nodeChild);
  304. _options.Add(sp);
  305. }
  306. }
  307. RestoreOptionEvents();
  308. }
  309. protected override void CreateBasicNode()
  310. {
  311. XElement xe = new XElement(Ns.Form + "listbox");
  312. Node.Add(xe);
  313. Node = xe;
  314. ControlImplementation = "ooo:com.sun.star.form.component.ListBox";
  315. }
  316. }
  317. }