PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/AODL/Document/Forms/ODFForm.cs

https://bitbucket.org/chrisc/aodl
C# | 693 lines | 494 code | 68 blank | 131 comment | 61 complexity | f24455d685737b60afbce7f7d644aa32 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.Forms.Controls;
  24. namespace AODL.Document.Forms
  25. {
  26. /// <summary>
  27. /// Summary description for ODFForm.
  28. /// </summary>
  29. public class ODFForm
  30. {
  31. private ODFFormCollection _formCollection;
  32. /// <summary>
  33. /// Creates an ODFForm
  34. /// </summary>
  35. /// <param name="document">Parent document</param>
  36. /// <param name="name">Form name</param>
  37. public ODFForm(IDocument document, string name)
  38. {
  39. Document = document;
  40. CreateBasicNode();
  41. ControlImplementation = "ooo:com.sun.star.form.component.Form";
  42. ApplyFilter = true;
  43. CommandType = Forms.CommandType.Table;
  44. Name = name;
  45. Controls = new ODFControlsCollection();
  46. Controls.Inserted += ControlsCollection_Inserted;
  47. Controls.Removed += ControlsCollection_Removed;
  48. Controls.Clearing += ControlsCollection_Clearing;
  49. Properties = new FormPropertyCollection();
  50. Properties.Inserted += PropertyCollection_Inserted;
  51. Properties.Removed += PropertyCollection_Removed;
  52. _formCollection = new ODFFormCollection();
  53. _formCollection.Inserted += FormCollection_Inserted;
  54. _formCollection.Removed += FormCollection_Removed;
  55. }
  56. public ODFForm(XElement node, IDocument document)
  57. {
  58. Document = document;
  59. Node = node;
  60. Controls = new ODFControlsCollection();
  61. Controls.Inserted += ControlsCollection_Inserted;
  62. Controls.Removed += ControlsCollection_Removed;
  63. Controls.Clearing += ControlsCollection_Clearing;
  64. Properties = new FormPropertyCollection();
  65. Properties.Inserted += PropertyCollection_Inserted;
  66. Properties.Removed += PropertyCollection_Removed;
  67. _formCollection = new ODFFormCollection();
  68. _formCollection.Inserted += FormCollection_Inserted;
  69. _formCollection.Removed += FormCollection_Removed;
  70. }
  71. /// <summary>
  72. /// Represents the IRI of the processing agent for the form
  73. /// </summary>
  74. public string Href
  75. {
  76. get { return (string) Node.Attribute(Ns.XLink + "href"); }
  77. set { Node.SetAttributeValue(Ns.XLink + "href", value); }
  78. }
  79. /// <summary>
  80. /// Do not change it unless it is necessary
  81. /// </summary>
  82. public string ControlImplementation
  83. {
  84. get { return (string) Node.Attribute(Ns.Form + "control-implementation"); }
  85. set { Node.SetAttributeValue(Ns.Form + "control-implementation", value); }
  86. }
  87. /// <summary>
  88. /// Form name
  89. /// </summary>
  90. public string Name
  91. {
  92. get { return (string) Node.Attribute(Ns.Form + "name"); }
  93. set { Node.SetAttributeValue(Ns.Form + "name", value); }
  94. }
  95. /// <summary>
  96. /// Specifies the target frame of the form
  97. /// </summary>
  98. public TargetFrame? TargetFrame
  99. {
  100. get
  101. {
  102. string s = (string) Node.Attribute(Ns.Office + "target-frame");
  103. if (s == null) return null;
  104. switch (s)
  105. {
  106. case "_self":
  107. return Forms.TargetFrame.Self;
  108. case "_blank":
  109. return Forms.TargetFrame.Blank;
  110. case "_parent":
  111. return Forms.TargetFrame.Parent;
  112. case "_top":
  113. return Forms.TargetFrame.Top;
  114. default:
  115. return null;
  116. }
  117. }
  118. set
  119. {
  120. string s;
  121. switch (value)
  122. {
  123. case Forms.TargetFrame.Self:
  124. s = "_self";
  125. break;
  126. case Forms.TargetFrame.Blank:
  127. s = "_blank";
  128. break;
  129. case Forms.TargetFrame.Parent:
  130. s = "_parent";
  131. break;
  132. case Forms.TargetFrame.Top:
  133. s = "_top";
  134. break;
  135. default:
  136. s = null;
  137. break;
  138. }
  139. Node.SetAttributeValue(Ns.Office + "target-frame", s);
  140. }
  141. }
  142. /// <summary>
  143. /// Specifies the HTTP method to use to submit the data in the form to
  144. /// the server
  145. /// </summary>
  146. public Method? Method
  147. {
  148. get
  149. {
  150. string s = (string) Node.Attribute(Ns.Form + "method");
  151. if (s == null) return null;
  152. switch (s)
  153. {
  154. case "get":
  155. return Forms.Method.Get;
  156. case "post":
  157. return Forms.Method.Post;
  158. default:
  159. return null;
  160. }
  161. }
  162. set
  163. {
  164. string s;
  165. switch (value)
  166. {
  167. case Forms.Method.Get:
  168. s = "get";
  169. break;
  170. case Forms.Method.Post:
  171. s = "post";
  172. break;
  173. default:
  174. s = null;
  175. break;
  176. }
  177. Node.SetAttributeValue(Ns.Form + "method", s);
  178. }
  179. }
  180. /// <summary>
  181. /// Specifies the content type used to submit the form to the server
  182. /// </summary>
  183. public string Enctype
  184. {
  185. get { return (string) Node.Attribute(Ns.Form + "enctype"); }
  186. set { Node.SetAttributeValue(Ns.Form + "enctype", value); }
  187. }
  188. /// <summary>
  189. /// Specifies whether or not data records can be deleted
  190. /// </summary>
  191. public bool? AllowDeletes
  192. {
  193. get { return (bool?) Node.Attribute(Ns.Form + "allow-deletes"); }
  194. set { Node.SetAttributeValue(Ns.Form + "allow-deletes", value); }
  195. }
  196. /// <summary>
  197. /// Specifies whether or not new data records can be inserted
  198. /// </summary>
  199. public bool? AllowInserts
  200. {
  201. get { return (bool?) Node.Attribute(Ns.Form + "allow-inserts"); }
  202. set { Node.SetAttributeValue(Ns.Form + "allow-inserts", value); }
  203. }
  204. /// <summary>
  205. /// Specifies whether or not data records can be updated
  206. /// </summary>
  207. public bool? AllowUpdates
  208. {
  209. get { return (bool?) Node.Attribute(Ns.Form + "allow-updates"); }
  210. set { Node.SetAttributeValue(Ns.Form + "allow-updates", value); }
  211. }
  212. /// <summary>
  213. /// Specifies whether or not filters should be applied to the form
  214. /// </summary>
  215. public bool? ApplyFilter
  216. {
  217. get { return (bool?) Node.Attribute(Ns.Form + "apply-filter"); }
  218. set { Node.SetAttributeValue(Ns.Form + "apply-filter", value); }
  219. }
  220. /// <summary>
  221. /// Specifies the type of command to execute on the data source
  222. /// </summary>
  223. public CommandType? CommandType
  224. {
  225. get
  226. {
  227. string s = (string) Node.Attribute(Ns.Form + "command-type");
  228. if (s == null) return null;
  229. switch (s)
  230. {
  231. case "table":
  232. return Forms.CommandType.Table;
  233. case "query":
  234. return Forms.CommandType.Query;
  235. case "command":
  236. return Forms.CommandType.Command;
  237. default:
  238. return null;
  239. }
  240. }
  241. set
  242. {
  243. string s;
  244. switch (value)
  245. {
  246. case Forms.CommandType.Table:
  247. s = "table";
  248. break;
  249. case Forms.CommandType.Query:
  250. s = "query";
  251. break;
  252. case Forms.CommandType.Command:
  253. s = "command";
  254. break;
  255. default:
  256. s = null;
  257. break;
  258. }
  259. Node.SetAttributeValue(Ns.Form + "command-type", s);
  260. }
  261. }
  262. /// <summary>
  263. /// Specifies the command to execute on the data source
  264. /// </summary>
  265. public string Command
  266. {
  267. get { return (string) Node.Attribute(Ns.Form + "command"); }
  268. set { Node.SetAttributeValue(Ns.Form + "command", value); }
  269. }
  270. /// <summary>
  271. /// Specifies the name of a data source to use for the form
  272. /// </summary>
  273. public string DataSource
  274. {
  275. get { return (string) Node.Attribute(Ns.Form + "datasource"); }
  276. set { Node.SetAttributeValue(Ns.Form + "datasource", value); }
  277. }
  278. /// <summary>
  279. /// Specifies the names of the columns in the result set represented by the parent form
  280. /// </summary>
  281. public string MasterFields
  282. {
  283. get { return (string) Node.Attribute(Ns.Form + "master-fields"); }
  284. set { Node.SetAttributeValue(Ns.Form + "master-fields", value); }
  285. }
  286. /// <summary>
  287. /// Specifies the names of the columns in detail forms that are related to columns in the parent form
  288. /// </summary>
  289. public string DetailFields
  290. {
  291. get { return (string) Node.Attribute(Ns.Form + "detail-fields"); }
  292. set { Node.SetAttributeValue(Ns.Form + "detail-fields", value); }
  293. }
  294. /// <summary>
  295. /// Specifies whether or not the application processes the command before passing it to the
  296. /// database driver
  297. /// </summary>
  298. public bool? EscapeProcessing
  299. {
  300. get { return (bool?) Node.Attribute(Ns.Form + "escape-processing"); }
  301. set { Node.SetAttributeValue(Ns.Form + "escape-processing", value); }
  302. }
  303. /// <summary>
  304. /// Specifies a filter for the command to base the form on
  305. /// </summary>
  306. public string Filter
  307. {
  308. get { return (string) Node.Attribute(Ns.Form + "filter"); }
  309. set { Node.SetAttributeValue(Ns.Form + "filter", value); }
  310. }
  311. /// <summary>
  312. /// Specifies how the records in a database form are navigated
  313. /// </summary>
  314. public NavigationMode? NavigationMode
  315. {
  316. get
  317. {
  318. string s = (string) Node.Attribute(Ns.Form + "navigation-mode");
  319. if (s == null) return null;
  320. switch (s)
  321. {
  322. case "current":
  323. return Forms.NavigationMode.Current;
  324. case "parent":
  325. return Forms.NavigationMode.Parent;
  326. case "none":
  327. return Forms.NavigationMode.None;
  328. default:
  329. return null;
  330. }
  331. }
  332. set
  333. {
  334. string s;
  335. switch (value)
  336. {
  337. case Forms.NavigationMode.Current:
  338. s = "current";
  339. break;
  340. case Forms.NavigationMode.Parent:
  341. s = "parent";
  342. break;
  343. case Forms.NavigationMode.None:
  344. s = "none";
  345. break;
  346. default:
  347. s = null;
  348. break;
  349. }
  350. Node.SetAttributeValue(Ns.Form + "navigation-mode", s);
  351. }
  352. }
  353. /// <summary>
  354. /// Specifies whether or not to discard all results that are
  355. /// retrieved from the underlying data source
  356. /// </summary>
  357. public bool? IgnoreResult
  358. {
  359. get { return (bool?) Node.Attribute(Ns.Form + "ignore-result"); }
  360. set { Node.SetAttributeValue(Ns.Form + "ignore-result", value); }
  361. }
  362. /// <summary>
  363. /// Specifies a sort criteria for the command.
  364. /// </summary>
  365. public string Order
  366. {
  367. get { return (string) Node.Attribute(Ns.Form + "order"); }
  368. set { Node.SetAttributeValue(Ns.Form + "order", value); }
  369. }
  370. /// <summary>
  371. /// Specifies how the application responds when the user presses
  372. /// the TAB key in the controls in a for
  373. /// </summary>
  374. public TabCycle? TabCycle
  375. {
  376. get
  377. {
  378. string s = (string) Node.Attribute(Ns.Form + "tab-cycle");
  379. if (s == null) return null;
  380. switch (s)
  381. {
  382. case "current":
  383. return Forms.TabCycle.Current;
  384. case "records":
  385. return Forms.TabCycle.Records;
  386. case "page":
  387. return Forms.TabCycle.Page;
  388. default:
  389. return null;
  390. }
  391. }
  392. set
  393. {
  394. string s;
  395. switch (value)
  396. {
  397. case Forms.TabCycle.Current:
  398. s = "current";
  399. break;
  400. case Forms.TabCycle.Records:
  401. s = "records";
  402. break;
  403. case Forms.TabCycle.Page:
  404. s = "page";
  405. break;
  406. default:
  407. s = null;
  408. break;
  409. }
  410. Node.SetAttributeValue(Ns.Form + "tab-cycle", s);
  411. }
  412. }
  413. /// <summary>
  414. /// Specifies the source database by an [XLink].
  415. /// </summary>
  416. public string ConnectionResource
  417. {
  418. get { return (string) Node.Attribute(Ns.Form + "connection-resource"); }
  419. set { Node.SetAttributeValue(Ns.Form + "connection-resource", value); }
  420. }
  421. /// <summary>
  422. /// The XML node that represents the form and its content
  423. /// </summary>
  424. public XElement Node { get; set; }
  425. /// <summary>
  426. /// Parent document
  427. /// </summary>
  428. public IDocument Document { get; set; }
  429. /// <summary>
  430. /// List of the child controls
  431. /// </summary>
  432. public ODFControlsCollection Controls { get; set; }
  433. /// <summary>
  434. /// Generic form:property collection
  435. /// </summary>
  436. public FormPropertyCollection Properties { get; set; }
  437. /// <summary>
  438. /// Child forms collection
  439. /// </summary>
  440. public ODFFormCollection ChildForms
  441. {
  442. get { return _formCollection; }
  443. set { _formCollection = value; }
  444. }
  445. private void CreateBasicNode()
  446. {
  447. Node = new XElement(Ns.Form + "form");
  448. }
  449. public void SuppressControlEvents()
  450. {
  451. Controls.Inserted -= ControlsCollection_Inserted;
  452. Controls.Removed -= ControlsCollection_Removed;
  453. Controls.Clearing -= ControlsCollection_Clearing;
  454. }
  455. public void RestoreControlEvents()
  456. {
  457. Controls.Inserted += ControlsCollection_Inserted;
  458. Controls.Removed += ControlsCollection_Removed;
  459. Controls.Clearing += ControlsCollection_Clearing;
  460. }
  461. public void SuppressPropertyEvents()
  462. {
  463. Properties.Inserted -= PropertyCollection_Inserted;
  464. Properties.Removed -= PropertyCollection_Removed;
  465. }
  466. private void RestorePropertyEvents()
  467. {
  468. Properties.Inserted += PropertyCollection_Inserted;
  469. Properties.Removed += PropertyCollection_Removed;
  470. }
  471. private void ControlsCollection_Inserted(int index, object value)
  472. {
  473. ODFFormControl ctrl = (ODFFormControl) value;
  474. Node.Add(ctrl.Node);
  475. ctrl.AddToContentCollection();
  476. }
  477. private static void ControlsCollection_Removed(int index, object value)
  478. {
  479. ODFFormControl ctrl = (ODFFormControl) value;
  480. ctrl.Node.Remove();
  481. ctrl.RemoveFromContentCollection();
  482. }
  483. private void ControlsCollection_Clearing()
  484. {
  485. for (int i = 0; i < Controls.Count; i++)
  486. {
  487. ODFFormControl ctrl = Controls[i];
  488. if (ctrl != null)
  489. {
  490. ctrl.Node.Remove();
  491. ctrl.RemoveFromContentCollection();
  492. }
  493. }
  494. }
  495. private void PropertyCollection_Inserted(int index, object value)
  496. {
  497. XElement formProp = Node.Element(Ns.Form + "properties");
  498. if (formProp == null)
  499. {
  500. formProp = new XElement(Ns.Form + "properties");
  501. Node.Add(formProp);
  502. }
  503. FormProperty prop = (FormProperty) value;
  504. formProp.Add(prop.Node);
  505. }
  506. private void PropertyCollection_Removed(int index, object value)
  507. {
  508. XElement formProp = Node.Element(Ns.Form + "properties");
  509. if (formProp != null)
  510. {
  511. FormProperty prop = (FormProperty) value;
  512. prop.Node.Remove();
  513. if (index == 0)
  514. {
  515. formProp.Remove();
  516. }
  517. }
  518. }
  519. private void FormCollection_Inserted(int index, object value)
  520. {
  521. ODFForm child = (value as ODFForm);
  522. if (child != null)
  523. Node.Add(child.Node);
  524. }
  525. private static void FormCollection_Removed(int index, object value)
  526. {
  527. ODFForm child = (value as ODFForm);
  528. if (child != null)
  529. {
  530. child.Controls.Clear();
  531. if (child.Node != null)
  532. child.Node.Remove();
  533. }
  534. }
  535. /// <summary>
  536. /// Looks up a control by its ID
  537. /// </summary>
  538. /// <param name="id">Control ID</param>
  539. /// <param name="searchInSubforms">Specifies whether to look in the subforms</param>
  540. /// <returns></returns>
  541. public ODFFormControl FindControlById(string id, bool searchInSubforms)
  542. {
  543. if (searchInSubforms)
  544. {
  545. foreach (ODFForm f in ChildForms)
  546. {
  547. ODFFormControl ctrl = f.FindControlById(id, true);
  548. if (ctrl != null) return ctrl;
  549. }
  550. }
  551. foreach (ODFFormControl c in Controls)
  552. {
  553. if (c != null)
  554. {
  555. if (c.Id == id)
  556. return c;
  557. }
  558. }
  559. return null;
  560. }
  561. /// <summary>
  562. /// Looks up a control by its name
  563. /// </summary>
  564. /// <param name="name">Control name</param>
  565. /// <param name="searchInSubforms">Specifies whether to look in the subforms</param>
  566. /// <returns></returns>
  567. public ODFFormControl FindControlByName(string name, bool searchInSubforms)
  568. {
  569. if (searchInSubforms)
  570. {
  571. foreach (ODFForm f in ChildForms)
  572. {
  573. ODFFormControl ctrl = f.FindControlByName(name, true);
  574. if (ctrl != null) return ctrl;
  575. }
  576. }
  577. foreach (ODFFormControl c in Controls)
  578. {
  579. if (c != null)
  580. {
  581. if (c.Name == name)
  582. return c;
  583. }
  584. }
  585. return null;
  586. }
  587. /// <summary>
  588. /// Gets the generic form property by its name
  589. /// </summary>
  590. /// <param name="name">Generic form property</param>
  591. /// <returns></returns>
  592. public FormProperty GetFormProperty(string name)
  593. {
  594. foreach (FormProperty fp in Properties)
  595. {
  596. if (fp.Name == name)
  597. {
  598. return fp;
  599. }
  600. }
  601. return null;
  602. }
  603. public void FixPropertyCollection()
  604. {
  605. Properties.Clear();
  606. SuppressPropertyEvents();
  607. XElement formProp = Node.Element(Ns.Form + "properties");
  608. if (formProp == null) return;
  609. foreach (XElement nodeChild in formProp.Elements())
  610. {
  611. if (nodeChild.Name == Ns.Form + "property" && nodeChild.Parent == formProp)
  612. {
  613. SingleFormProperty sp = new SingleFormProperty(Document, nodeChild);
  614. Properties.Add(sp);
  615. }
  616. if (nodeChild.Name == Ns.Form + "list-property" && nodeChild.Parent == formProp)
  617. {
  618. ListFormProperty lp = new ListFormProperty(Document, nodeChild);
  619. Properties.Add(lp);
  620. }
  621. }
  622. RestorePropertyEvents();
  623. }
  624. }
  625. }