PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/AODL/Document/Forms/Controls/ODFComboBox.cs

https://bitbucket.org/chrisc/aodl
C# | 362 lines | 227 code | 32 blank | 103 comment | 14 complexity | 9023909195f57a8e6f6fee2ddfd5d2dc 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 ODFComboBox : ODFFormControl
  27. {
  28. private ODFItemCollection _items;
  29. /// <summary>
  30. /// Creates an ODFComboBox
  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 ODFComboBox(ODFForm parentForm, ContentCollection contentCollection, string id)
  36. : base(parentForm, contentCollection, id)
  37. {
  38. _items = new ODFItemCollection();
  39. RestoreItemEvents();
  40. }
  41. /// <summary>
  42. /// Creates an ODFComboBox
  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 ODFComboBox(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. _items = new ODFItemCollection();
  55. RestoreItemEvents();
  56. }
  57. public ODFComboBox(ODFForm parentForm, XElement node) : base(parentForm, node)
  58. {
  59. _items = new ODFItemCollection();
  60. RestoreItemEvents();
  61. }
  62. /// <summary>
  63. /// Collection of combo box items
  64. /// </summary>
  65. public ODFItemCollection Items
  66. {
  67. get { return _items; }
  68. set { _items = value; }
  69. }
  70. public override string Type
  71. {
  72. get { return "combobox"; }
  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. /// Specifies whether, when the user enters text in the
  84. /// combobox that matches one of the list items in the combobox, the application automatically
  85. /// completes the text for the user.
  86. /// </summary>
  87. public bool? AutoComplete
  88. {
  89. get { return (bool?) Node.Attribute(Ns.Form + "auto-complete"); }
  90. set { Node.SetAttributeValue(Ns.Form + "auto-complete", value); }
  91. }
  92. /// <summary>
  93. /// Contains additional information about a control.
  94. /// </summary>
  95. public string Title
  96. {
  97. get { return (string) Node.Attribute(Ns.Form + "title"); }
  98. set { Node.SetAttributeValue(Ns.Form + "title", value); }
  99. }
  100. /// <summary>
  101. /// Specifies the source used to populate the list in a list box or
  102. /// combo box. The first column of the list source result set populates the list.
  103. /// </summary>
  104. public string ListSource
  105. {
  106. get { return (string) Node.Attribute(Ns.Form + "list-source"); }
  107. set { Node.SetAttributeValue(Ns.Form + "list-source", value); }
  108. }
  109. /// <summary>
  110. /// Specifies the name of a result set column. The result set is
  111. /// determined by the form which the control belongs to
  112. /// </summary>
  113. public string DataField
  114. {
  115. get { return (string) Node.Attribute(Ns.Form + "data-field"); }
  116. set { Node.SetAttributeValue(Ns.Form + "data-field", value); }
  117. }
  118. /// <summary>
  119. /// Specifies the type of data source that is used to
  120. /// populate the list data in a list box or combo box
  121. /// </summary>
  122. public ListSourceType? ListSourceType
  123. {
  124. get
  125. {
  126. string s = (string) Node.Attribute(Ns.Form + "list-source-type");
  127. if (s == null) return null;
  128. switch (s)
  129. {
  130. case "table":
  131. return Forms.ListSourceType.Table;
  132. case "query":
  133. return Forms.ListSourceType.Query;
  134. case "sql":
  135. return Forms.ListSourceType.Sql;
  136. case "sql-pass-through":
  137. return Forms.ListSourceType.SqlPassThrough;
  138. case "value-list":
  139. return Forms.ListSourceType.ValueList;
  140. case "table-fields":
  141. return Forms.ListSourceType.TableFields;
  142. default:
  143. return null;
  144. }
  145. }
  146. set
  147. {
  148. string s;
  149. switch (value)
  150. {
  151. case Forms.ListSourceType.Table:
  152. s = "table";
  153. break;
  154. case Forms.ListSourceType.Query:
  155. s = "query";
  156. break;
  157. case Forms.ListSourceType.Sql:
  158. s = "sql";
  159. break;
  160. case Forms.ListSourceType.SqlPassThrough:
  161. s = "sql-pass-through";
  162. break;
  163. case Forms.ListSourceType.ValueList:
  164. s = "value-list";
  165. break;
  166. case Forms.ListSourceType.TableFields:
  167. s = "table-fields";
  168. break;
  169. default:
  170. s = null;
  171. break;
  172. }
  173. Node.SetAttributeValue(Ns.Form + "list-source-type", s);
  174. }
  175. }
  176. /// <summary>
  177. /// Specifies the tabbing navigation order of a control within a form
  178. /// </summary>
  179. public int TabIndex
  180. {
  181. get { return (int) Node.Attribute(Ns.Form + "tab-index"); }
  182. set { Node.SetAttributeValue(Ns.Form + "tab-index", value); }
  183. }
  184. /// <summary>
  185. /// Specifies whether or not a control is included in the tabbing
  186. /// navigation order
  187. /// </summary>
  188. public bool? TabStop
  189. {
  190. get { return (bool?) Node.Attribute(Ns.Form + "tab-stop"); }
  191. set { Node.SetAttributeValue(Ns.Form + "tab-stop", value); }
  192. }
  193. /// <summary>
  194. /// Specifies whether or not a control is printed when a user prints
  195. /// the document in which the control is contained
  196. /// </summary>
  197. public bool? Printable
  198. {
  199. get { return (bool?) Node.Attribute(Ns.Form + "printable"); }
  200. set { Node.SetAttributeValue(Ns.Form + "printable", value); }
  201. }
  202. /// <summary>
  203. /// Specifies whether or not a user can modify the value of a control
  204. /// </summary>
  205. public bool? ReadOnly
  206. {
  207. get { return (bool?) Node.Attribute(Ns.Form + "readonly"); }
  208. set { Node.SetAttributeValue(Ns.Form + "readonly", value); }
  209. }
  210. /// <summary>
  211. /// Specifies whether the list in a combo box or list box is always
  212. /// visible or is only visible when the user clicks the drop-down button
  213. /// </summary>
  214. public bool? DropDown
  215. {
  216. get { return (bool?) Node.Attribute(Ns.Form + "dropdown"); }
  217. set { Node.SetAttributeValue(Ns.Form + "dropdown", value); }
  218. }
  219. /// <summary>
  220. /// specifies the number of rows that are visible at a time in a combo box
  221. /// list or a list box list
  222. /// </summary>
  223. public int Size
  224. {
  225. get { return (int?) Node.Attribute(Ns.Form + "size") ?? -1; }
  226. set
  227. {
  228. if (value <= 0)
  229. return;
  230. Node.SetAttributeValue(Ns.Form + "size", value);
  231. }
  232. }
  233. /// <summary>
  234. /// specifies whether or not empty current values are regarded as NULL
  235. /// </summary>
  236. public bool? ConvertEmptyToNull
  237. {
  238. get { return (bool?) Node.Attribute(Ns.Form + "convert-empty-to-null"); }
  239. set { Node.SetAttributeValue(Ns.Form + "convert-empty-to-null", value); }
  240. }
  241. /// <summary>
  242. /// Specifies the current status of an input control
  243. /// </summary>
  244. public string CurrentValue
  245. {
  246. get { return (string) Node.Attribute(Ns.Form + "current-value"); }
  247. set { Node.SetAttributeValue(Ns.Form + "current-value", value); }
  248. }
  249. /// <summary>
  250. /// Specifies the maximum number of characters that a user can
  251. /// enter in an input control
  252. /// </summary>
  253. public int MaxLength
  254. {
  255. get { return (int?) Node.Attribute(Ns.Form + "max-length") ?? -1; }
  256. set
  257. {
  258. if (value <= 0)
  259. return;
  260. Node.SetAttributeValue(Ns.Form + "max-length", value);
  261. }
  262. }
  263. /// <summary>
  264. /// Specifies the default value of the control
  265. /// </summary>
  266. public string Value
  267. {
  268. get { return (string) Node.Attribute(Ns.Form + "value"); }
  269. set { Node.SetAttributeValue(Ns.Form + "value", value); }
  270. }
  271. public void SuppressItemEvents()
  272. {
  273. _items.Inserted -= ItemCollection_Inserted;
  274. _items.Removed -= ItemCollection_Removed;
  275. }
  276. public void RestoreItemEvents()
  277. {
  278. _items.Inserted += ItemCollection_Inserted;
  279. _items.Removed += ItemCollection_Removed;
  280. }
  281. private void ItemCollection_Inserted(int index, object value)
  282. {
  283. ODFItem opt = (ODFItem) value;
  284. Node.Add(opt.Node);
  285. }
  286. private static void ItemCollection_Removed(int index, object value)
  287. {
  288. ODFItem opt = value as ODFItem;
  289. if (opt != null)
  290. opt.Node.Remove();
  291. }
  292. public void FixItemCollection()
  293. {
  294. _items.Clear();
  295. SuppressItemEvents();
  296. foreach (XElement nodeChild in Node.Elements())
  297. {
  298. if (nodeChild.Name == Ns.Form + "item" && nodeChild.Parent == Node)
  299. {
  300. ODFItem sp = new ODFItem(_document, nodeChild);
  301. _items.Add(sp);
  302. }
  303. }
  304. RestoreItemEvents();
  305. }
  306. /// <summary>
  307. /// Looks for a specified item by its label
  308. /// </summary>
  309. /// <param name="label">Option value</param>
  310. /// <returns></returns>
  311. public ODFItem GetItemByLabel(string label)
  312. {
  313. foreach (ODFItem it in _items)
  314. {
  315. if (it.Label == label)
  316. {
  317. return it;
  318. }
  319. }
  320. return null;
  321. }
  322. protected override void CreateBasicNode()
  323. {
  324. XElement xe = new XElement(Ns.Form + "combobox");
  325. Node.Add(xe);
  326. Node = xe;
  327. ControlImplementation = "ooo:com.sun.star.form.component.ComboBox";
  328. }
  329. }
  330. }