DataWizardForm.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using FastReport.Utils;
  6. using FastReport.Data;
  7. using FastReport.Design;
  8. using FastReport.Controls;
  9. namespace FastReport.Forms
  10. {
  11. internal partial class DataWizardForm : BaseWizardForm
  12. {
  13. private Report report;
  14. private LastConnections connections;
  15. private DataConnectionBase connection;
  16. private bool updating;
  17. private bool editMode;
  18. #region Properties
  19. public DataConnectionBase Connection
  20. {
  21. get { return connection; }
  22. set
  23. {
  24. if (value != connection)
  25. {
  26. if (connection != null)
  27. {
  28. report.Dictionary.Connections.Remove(connection);
  29. connection.Clear();
  30. }
  31. connection = value;
  32. if (value != null)
  33. {
  34. connections.Add(connection);
  35. report.Dictionary.Connections.Add(connection);
  36. UpdateConnectionsCombo();
  37. }
  38. }
  39. if (value != null)
  40. tbConnString.Text = value.ConnectionString;
  41. UpdateButtons();
  42. }
  43. }
  44. public bool EditMode
  45. {
  46. get { return editMode; }
  47. set
  48. {
  49. editMode = value;
  50. if (value)
  51. {
  52. cbxConnections.Enabled = false;
  53. btnNewConnection.Enabled = false;
  54. tbConnName.Text = Connection.Name;
  55. }
  56. }
  57. }
  58. public override int VisiblePanelIndex
  59. {
  60. get { return base.VisiblePanelIndex; }
  61. set
  62. {
  63. base.VisiblePanelIndex = value;
  64. if (value == 1)
  65. UpdateTree();
  66. }
  67. }
  68. private bool ApplicationConnectionMode
  69. {
  70. get { return Config.DesignerSettings.ApplicationConnection != null; }
  71. }
  72. #endregion
  73. #region First page
  74. private void cbxConnections_DrawItem(object sender, DrawItemEventArgs e)
  75. {
  76. e.DrawBackground();
  77. if (e.Index >= 0)
  78. {
  79. try
  80. {
  81. DataConnectionBase connection = cbxConnections.Items[e.Index] as DataConnectionBase;
  82. this.DrawImageAndText(e, null, connection.GetConnectionId());
  83. }
  84. catch
  85. {
  86. }
  87. }
  88. }
  89. private void btnConnString_Click(object sender, EventArgs e)
  90. {
  91. if (!tbConnString.Visible)
  92. {
  93. btnConnString.Image = GetImage(184);
  94. tbConnString.Visible = true;
  95. }
  96. else
  97. {
  98. btnConnString.Image = GetImage(183);
  99. tbConnString.Visible = false;
  100. }
  101. }
  102. private void UpdateConnectionsCombo()
  103. {
  104. updating = true;
  105. cbxConnections.Items.Clear();
  106. cbxConnections.Items.AddRange(connections.ToArray());
  107. if (cbxConnections.Items.Count > 0)
  108. cbxConnections.SelectedIndex = 0;
  109. updating = false;
  110. }
  111. private void cbxConnections_SelectedIndexChanged(object sender, EventArgs e)
  112. {
  113. if (updating)
  114. return;
  115. updating = true;
  116. Connection = cbxConnections.SelectedItem as DataConnectionBase;
  117. updating = false;
  118. }
  119. private void btnNewConnection_Click(object sender, EventArgs e)
  120. {
  121. using (ConnectionForm form = new ConnectionForm())
  122. {
  123. if (form.ShowDialog() == DialogResult.OK)
  124. Connection = form.Connection;
  125. }
  126. }
  127. private void btnEditConnection_Click(object sender, EventArgs e)
  128. {
  129. using (ConnectionForm form = new ConnectionForm())
  130. {
  131. form.Connection = Connection;
  132. form.EditMode = true;
  133. if (form.ShowDialog() == DialogResult.OK)
  134. Connection = form.Connection;
  135. }
  136. }
  137. #endregion
  138. #region Second page
  139. private void CheckTableSchema(TreeNode node)
  140. {
  141. TableDataSource data = node.Tag as TableDataSource;
  142. if (data != null && data.Columns.Count == 0)
  143. {
  144. try
  145. {
  146. data.InitSchema();
  147. }
  148. catch (Exception ex)
  149. {
  150. FRMessageBox.Error(ex.Message);
  151. }
  152. finally
  153. {
  154. node.Nodes.Clear();
  155. DataTreeHelper.AddColumns(node.Nodes, data.Columns, false, true);
  156. }
  157. }
  158. }
  159. private void tvTables_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  160. {
  161. CheckTableSchema(e.Node);
  162. }
  163. private void tvTables_AfterCheck(object sender, TreeViewEventArgs e)
  164. {
  165. if (updating)
  166. return;
  167. CheckTableSchema(e.Node);
  168. }
  169. private void btnAddTable_Click(object sender, EventArgs e)
  170. {
  171. TableDataSource table = new TableDataSource();
  172. table.Name = report.Dictionary.CreateUniqueName("Table");
  173. table.Alias = report.Dictionary.CreateUniqueAlias(table.Alias);
  174. table.Connection = Connection;
  175. using (QueryWizardForm form = new QueryWizardForm(table))
  176. {
  177. if (form.ShowDialog() != DialogResult.OK)
  178. table.Dispose();
  179. else
  180. UpdateTree();
  181. }
  182. }
  183. private void UpdateTree()
  184. {
  185. updating = true;
  186. tvTables.BeginUpdate();
  187. tvTables.Nodes.Clear();
  188. tvTables.Sorted = false;
  189. lblWait.Location = new Point(tvTables.Left + 1, tvTables.Top + tvTables.Height / 2 - 32);
  190. lblWait.Size = new Size(tvTables.Width - 2, 32);
  191. lblWait.Visible = true;
  192. lblWait.Refresh();
  193. try
  194. {
  195. connection.CreateAllTables(false);
  196. }
  197. catch
  198. {
  199. }
  200. lblWait.Visible = false;
  201. btnAddQuery.Enabled = Connection.IsSqlBased;
  202. DataTreeHelper.CreateDataTree(report.Dictionary, Connection, tvTables.Nodes);
  203. tvTables.EndUpdate();
  204. updating = false;
  205. }
  206. private void cbxCheckAll_CheckedChanged(object sender, EventArgs e)
  207. {
  208. foreach (TreeNode node in tvTables.Nodes)
  209. {
  210. node.Checked = cbxCheckAll.Checked;
  211. }
  212. }
  213. #endregion
  214. private void DataWizardForm_FormClosed(object sender, FormClosedEventArgs e)
  215. {
  216. Done();
  217. Config.SaveFormState(this);
  218. }
  219. private void UpdateButtons()
  220. {
  221. bool enabled = Connection != null;
  222. btnEditConnection.Enabled = enabled;
  223. btnNext.Enabled = enabled;
  224. }
  225. private void ReadConnections()
  226. {
  227. if (Config.DesignerSettings.CustomConnections.Count > 0)
  228. {
  229. connections.Deserialize(Config.DesignerSettings.CustomConnections);
  230. }
  231. else
  232. {
  233. XmlItem root = Config.Root.FindItem("Designer").FindItem("LastConnections");
  234. // read last connections
  235. using (FRReader reader = new FRReader(null, root))
  236. {
  237. reader.Read(connections);
  238. }
  239. // write successfully readed connections back
  240. using (FRWriter writer = new FRWriter(root))
  241. {
  242. root.Clear();
  243. writer.Write(connections);
  244. }
  245. }
  246. if (connections.Count > 0)
  247. Connection = connections[0];
  248. }
  249. private void Init()
  250. {
  251. if (ApplicationConnectionMode)
  252. {
  253. if (report.Dictionary.Connections.Count == 0)
  254. {
  255. Connection = Activator.CreateInstance(Config.DesignerSettings.ApplicationConnectionType) as DataConnectionBase;
  256. Connection.Name = report.Dictionary.CreateUniqueName("Connection");
  257. }
  258. else
  259. Connection = report.Dictionary.Connections[0];
  260. Connection.ConnectionString = Config.DesignerSettings.ApplicationConnection.ConnectionString;
  261. EditMode = true;
  262. VisiblePanelIndex = 1;
  263. btnPrevious.Visible = false;
  264. btnNext.Visible = false;
  265. }
  266. else
  267. {
  268. VisiblePanelIndex = 0;
  269. tbConnName.Text = report.Dictionary.CreateUniqueName("Connection");
  270. ReadConnections();
  271. }
  272. UpdateButtons();
  273. }
  274. private void Done()
  275. {
  276. if (DialogResult == DialogResult.OK)
  277. {
  278. Connection.Name = tbConnName.Text;
  279. // enable items that we have checked in the table tree
  280. foreach (TreeNode tableNode in tvTables.Nodes)
  281. {
  282. TableDataSource table = tableNode.Tag as TableDataSource;
  283. if (table != null)
  284. {
  285. table.Enabled = tableNode.Checked;
  286. foreach (TreeNode colNode in tableNode.Nodes)
  287. {
  288. Column column = colNode.Tag as Column;
  289. if (column != null)
  290. column.Enabled = colNode.Checked;
  291. }
  292. }
  293. }
  294. // create relations if any
  295. Connection.CreateRelations();
  296. XmlItem root = Config.Root.FindItem("Designer").FindItem("LastConnections");
  297. using (FRWriter writer = new FRWriter(root))
  298. {
  299. root.Clear();
  300. writer.Write(connections);
  301. }
  302. }
  303. if (ApplicationConnectionMode)
  304. {
  305. // in this mode, we don't have to store connection string in the report
  306. Connection.ConnectionString = "";
  307. }
  308. if (DialogResult == DialogResult.OK || EditMode)
  309. connections.Remove(Connection);
  310. connections.Dispose();
  311. }
  312. public override void Localize()
  313. {
  314. base.Localize();
  315. MyRes res = new MyRes("Forms,DataWizard");
  316. Text = res.Get("");
  317. pnDatabase.Text = res.Get("Page1");
  318. pnTables.Text = res.Get("Page2");
  319. lblWhichData.Text = res.Get("WhichData");
  320. lblHint.Text = res.Get("Hint");
  321. btnNewConnection.Text = res.Get("NewConnection");
  322. btnEditConnection.Text = res.Get("EditConnection");
  323. lblConnName.Text = res.Get("ConnectionName");
  324. lblConnString.Text = res.Get("ConnectionString");
  325. lblWhichTables.Text = res.Get("WhichTables");
  326. lblWait.Text = res.Get("Wait");
  327. btnAddQuery.Text = res.Get("AddQuery");
  328. cbxCheckAll.Text = res.Get("CheckAll");
  329. }
  330. public override void UpdateDpiDependencies()
  331. {
  332. base.UpdateDpiDependencies();
  333. lblWhichData.Font = this.LogicalToDevice(new Font(DrawUtils.DefaultFont, FontStyle.Bold), true);
  334. tbConnString.Height = pnDatabase.Height - tbConnString.Top - 12;
  335. picIcon.Image = GetImage("Images.DataWizard.png");
  336. btnConnString.Image = GetImage(183);
  337. tvTables.ImageList = GetImages();
  338. }
  339. public DataWizardForm(Report report)
  340. {
  341. this.report = report;
  342. connections = new LastConnections();
  343. InitializeComponent();
  344. Localize();
  345. Init();
  346. Config.RestoreFormState(this);
  347. lblWhichData.Font = new Font(DrawUtils.DefaultFont, FontStyle.Bold);
  348. lblWhichTables.Font = lblWhichData.Font;
  349. UIUtils.CheckRTL(this);
  350. UpdateDpiDependencies();
  351. }
  352. private class LastConnections : IDisposable, IFRSerializable
  353. {
  354. private List<DataConnectionBase> connections;
  355. public DataConnectionBase this[int index]
  356. {
  357. get { return connections[index]; }
  358. }
  359. public int Count
  360. {
  361. get { return connections.Count; }
  362. }
  363. public void Serialize(FRWriter writer)
  364. {
  365. List<string> savedConnections = new List<string>();
  366. writer.ItemName = "LastConnections";
  367. foreach (DataConnectionBase connection in connections)
  368. {
  369. if (savedConnections.IndexOf(connection.GetConnectionId()) == -1)
  370. {
  371. writer.SaveChildren = false;
  372. writer.Write(connection);
  373. savedConnections.Add(connection.GetConnectionId());
  374. }
  375. }
  376. }
  377. public void Deserialize(FRReader reader)
  378. {
  379. while (reader.NextItem())
  380. {
  381. try
  382. {
  383. DataConnectionBase connection = reader.Read() as DataConnectionBase;
  384. if (connection != null)
  385. connections.Add(connection);
  386. }
  387. catch
  388. {
  389. }
  390. }
  391. }
  392. public void Deserialize(List<ConnectionEntry> connections)
  393. {
  394. foreach (ConnectionEntry ce in connections)
  395. {
  396. try
  397. {
  398. DataConnectionBase connection =
  399. Activator.CreateInstance(ce.type) as DataConnectionBase;
  400. connection.ConnectionString = ce.connectionString;
  401. this.connections.Add(connection);
  402. }
  403. catch
  404. {
  405. }
  406. }
  407. }
  408. public void Add(DataConnectionBase connection)
  409. {
  410. for (int i = 0; i < connections.Count; i++)
  411. {
  412. try
  413. {
  414. if (connections[i].GetConnectionId() == connection.GetConnectionId())
  415. {
  416. connections.RemoveAt(i);
  417. break;
  418. }
  419. }
  420. catch
  421. {
  422. }
  423. }
  424. connections.Insert(0, connection);
  425. }
  426. public void Remove(DataConnectionBase connection)
  427. {
  428. if (connections.Contains(connection))
  429. connections.Remove(connection);
  430. }
  431. public DataConnectionBase[] ToArray()
  432. {
  433. return connections.ToArray();
  434. }
  435. public void Dispose()
  436. {
  437. foreach (DataConnectionBase connection in connections)
  438. {
  439. connection.Dispose();
  440. }
  441. connections.Clear();
  442. }
  443. public LastConnections()
  444. {
  445. connections = new List<DataConnectionBase>();
  446. }
  447. }
  448. }
  449. }