FormWizard.xba 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="FormWizard" script:language="StarBasic">Option Explicit
  21. Public DocumentName as String
  22. Public FormPath as String
  23. Public WizardPath as String
  24. Public WorkPath as String
  25. Public TempPath as String
  26. Public TexturePath as String
  27. Public sQueryName as String
  28. Public oDBConnection as Object
  29. Public bWithBackGraphic as Boolean
  30. Public bNeedFieldRefresh as Boolean
  31. Public oDBForm as Object
  32. Public oColumns() as Object
  33. Public sDatabaseList() as String
  34. Public TableNames() as String
  35. Public QueryNames() as String
  36. Public FieldNames() as String
  37. Public ImgFieldNames() as String
  38. Public oDBContext as Object
  39. Public oUcb as Object
  40. Public oDocInfo as Object
  41. Public WidthList(15,3)
  42. Public ImgWidthList(3,3)
  43. Public sDBName as String
  44. Public Tablename as String
  45. Public Const SBSIZETEXT = &quot;The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.&quot;
  46. Public bDisposeDoc as Boolean
  47. Public bDebug as Boolean
  48. &apos;Public bStartUp as Boolean
  49. Public bConnectionIsovergiven as Boolean
  50. Public FormName As String
  51. Public sFormUrl as String
  52. Public oFormDocuments
  53. &apos; The macro can be called in 4 possible scenarios:
  54. &apos; Scenario 1. No parameters at given
  55. &apos; Scenario 2: Only Datasourcename is given, but no connection and no Content
  56. &apos; Scenario 3: a data source and a connection are given
  57. &apos; Scenario 4: all parameters (data source name, connection, object type and object) are given
  58. Sub Main()
  59. Dim oLocDBContext as Object
  60. Dim oLocConnection as Object
  61. &apos; Scenario 1. No parameters at given
  62. MainWithDefault()
  63. &apos; Scenario 2: Only Datasourcename is given, but no connection and no Content
  64. &apos; MainWithDefault(&quot;Bibliography&quot;)
  65. &apos; Scenario 3: a data source and a connection are given
  66. &apos; oLocDBContext = CreateUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
  67. &apos; oLocConnection = oLocDBContext.GetByName(&quot;Bibliography&quot;).GetConnection(&quot;&quot;,&quot;&quot;)
  68. &apos; MainWithDefault(&quot;Bibliography&quot;, oLocConnection)
  69. &apos; Scenario 4: all parameters (data source name, connection, object type and object) are given
  70. &apos; oLocDBContext = CreateUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
  71. &apos; oLocConnection = oLocDBContext.GetByName(&quot;Bibliography&quot;).GetConnection(&quot;&quot;,&quot;&quot;)
  72. &apos; MainWithDefault(&quot;Bibliography&quot;, oLocConnection, com.sun.star.sdb.CommandType.TABLE, &quot;biblio&quot;)
  73. End Sub
  74. Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String)
  75. Dim i as Integer
  76. Dim SelCount as Integer
  77. Dim RetValue as Integer
  78. Dim SelList(0) as Integer
  79. Dim LocList() as String
  80. SelList(0) = 0
  81. BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  82. bDebug = False
  83. If Not bDebug Then
  84. On Local Error GoTo WIZARDERROR
  85. End If
  86. OpenFormDocument()
  87. CurArrangement = 0
  88. bControlsareCreated = False
  89. bEnableBinaryOptionGroup = False
  90. bDisposeDoc = True
  91. MaxIndex = -1
  92. If Not InitResources(&quot;Formwizard&quot;) Then
  93. Exit Sub
  94. End If
  95. oDBContext = CreateUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
  96. oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
  97. If GetFormWizardPaths() = False Then
  98. Exit Sub
  99. End If
  100. oDocument.GetCurrentController().Frame.ComponentWindow.Enable = False
  101. oProgressBar.Value = 10
  102. LoadLanguage()
  103. oProgressBar.Value = 20
  104. InitializeWidthList()
  105. oProgressBar.Value = 30
  106. Styles() = getListBoxArrays(oUcb, &quot;/stl&quot;)
  107. CurIndex = GetCurIndex(DialogModel, Styles(), 2)
  108. oProgressBar.Value = 40
  109. ConfigurePageStyle()
  110. oProgressBar.Value = 50
  111. InitializeLabelValues()
  112. bNeedFieldRefresh = True
  113. SetDialogLanguage()
  114. &apos; bStartUp = true
  115. With DialogModel
  116. .cmdBack.Enabled = False
  117. .cmdGoOn.Enabled = False
  118. .lblTables.Enabled = False
  119. .lstSelFields.Tag = False
  120. .Step = 1
  121. End With
  122. oProgressBar.Value = 60
  123. bConnectionIsovergiven = Not IsMissing(oConnection)
  124. If Not IsMissing(DataSourceName) Then
  125. sDBName = DataSourceName
  126. If Not IsMissing(oConnection) Then
  127. &apos; Scenario 3: a data source and a connection are given
  128. Set oDBConnection = oConnection
  129. oDataSource = oDBContext.GetByName(DataSourceName)
  130. DialogModel.lstTables.Enabled = True
  131. DialogModel.lblTables.Enabled = True
  132. If GetDBMetaData() Then
  133. LocList() = AddListToList(TableNames(), QueryNames())
  134. iCommandTypes = CreateCommandTypeList()
  135. If Not IsMissing(sContent) Then
  136. &apos; Scenario 4: all parameters (data source name, connection, object type and object) are given
  137. DialogModel.lstTables.StringItemList() = LocList()
  138. iCommandTypes() = CreateCommandTypeList()
  139. SelCount = CountItemsInArray(DialogModel.lstTables.StringItemList(), sContent)
  140. If SelCount = 1 Then
  141. DlgFormDB.GetControl(&quot;lstTables&quot;).SelectItem(sContent, True)
  142. Else
  143. If CommandType = com.sun.star.sdb.CommandType.QUERY Then
  144. SelIndex = IndexInArray(sContent, QueryNames())
  145. DlgFormDB.GetControl(&quot;lstTables&quot;).SelectItemPos(SelIndex, True)
  146. ElseIf CommandType = com.sun.star.sdb.CommandType.TABLE Then
  147. SelIndex = IndexInArray(sContent, TableNames())
  148. DlgFormDB.GetControl(&quot;lstTables&quot;).SelectItemPos(Ubound(QueryNames()+1 + SelIndex, True))
  149. End If
  150. End If
  151. CurCommandType = CommandType
  152. FillUpFieldsListbox(False)
  153. Else
  154. LocList() = AddListToList(Array(sSelectDBTable), LocList())
  155. DialogModel.lstTables.StringItemList() = LocList()
  156. &apos; bSelectContent = True
  157. DialogModel.lstTables.SelectedItems() = Array(0)
  158. End If
  159. End If
  160. Else
  161. &apos; Scenario 2: Only Datasourcename is given, but no connection and no Content
  162. GetSelectedDBMetaData(sDBName)
  163. End If
  164. Else
  165. &apos; Scenario 1: No parameters are given
  166. ToggleListboxControls(DialogModel, False)
  167. End If
  168. oProgressBar.Value = 80
  169. bWithBackGraphic = LoadNewStyles(oDocument, DialogModel, CurIndex, Styles(CurIndex, 8), Styles(), TexturePath)
  170. DlgFormDB.Title = WizardTitle(1)
  171. DialogModel.lstStyles.StringItemList() = ArrayfromMultiArray(Styles, 1)
  172. DialogModel.lstStyles.SelectedItems() = SelList()
  173. ControlCaptionsToStandardLayout()
  174. oDocument.GetCurrentController().Frame.ComponentWindow.Enable = True
  175. oProgressBar.Value = 90
  176. DialogModel.imgTheme.ImageURL = FormPath &amp; &quot;FormWizard_1.png&quot;
  177. DialogModel.imgTheme.BackGroundColor = RGB(0,60,126)
  178. ToggleDatabasePage(True)
  179. oProgressBar.Value = 100
  180. DlgFormDB.GetControl(&quot;lstTables&quot;).SetFocus()
  181. oProgressbar.End
  182. RetValue = DlgFormDB.Execute()
  183. DlgFormDB.Dispose()
  184. If bDisposeDoc Then
  185. Dim aPropertyValues(2) as new com.sun.star.beans.PropertyValue
  186. oFormDocuments = oDataSource.getFormDocuments()
  187. DlgFormDB.Dispose()
  188. oDocument.dispose()
  189. Dim bLinkExists as Boolean
  190. i = 1
  191. Dim FormBaseName as String
  192. FormBaseName = FormName
  193. Do
  194. bLinkExists = oFormDocuments.HasbyHierarchicalName(FormName)
  195. If bLinkExists Then
  196. i = i + 1
  197. FormName = FormBaseName &amp; &quot;_&quot; &amp; i
  198. End If
  199. Loop Until Not bLinkExists
  200. aPropertyValues(0).Name = &quot;Name&quot;
  201. aPropertyValues(0).Value = FormName
  202. aPropertyValues(1).Name = &quot;Parent&quot;
  203. aPropertyValues(1).Value = oFormDocuments()
  204. aPropertyValues(2).Name = &quot;URL&quot;
  205. aPropertyValues(2).Value = sFormUrl
  206. Dim oDBDocument
  207. oDBDocument = oFormDocuments.createInstanceWithArguments(&quot;com.sun.star.sdb.DocumentDefinition&quot;, aPropertyValues())
  208. oFormDocuments.insertbyName(FormName, oDBDocument)
  209. ElseIf RetValue = 0 Then
  210. RemoveNirwanaShapes()
  211. End If
  212. If ((Not IsNull(oDBConnection)) And (Not bConnectionIsovergiven)) Then
  213. oDBConnection.Dispose()
  214. End If
  215. WIZARDERROR:
  216. If Err &lt;&gt; 0 Then
  217. Msgbox(sMsgErrMsg, 16, GetProductName())
  218. Resume LOCERROR
  219. LOCERROR:
  220. End If
  221. End Sub
  222. Sub FormGetFields()
  223. Dim i as Integer
  224. &apos; If bSelectContent Then
  225. &apos; bSelectContent = False
  226. &apos; Exit Sub
  227. &apos; End If
  228. DeleteFirstListBoxEntry(&quot;lstTables&quot;, sSelectDBTable)
  229. ToggleDatabasePage(False)
  230. FillUpFieldsListbox(True)
  231. ToggleDatabasePage(True)
  232. End Sub
  233. Sub FillUpFieldsListbox(bGetCommandType as Boolean)
  234. Dim SelIndex as Integer
  235. Dim QueryIndex as Integer
  236. If Not bDebug Then
  237. On Local Error GoTo NOFIELDS
  238. End If
  239. SelIndex = DlgFormDB.GetControl(&quot;lstTables&quot;).getSelectedItemPos() &apos;.SelectedItems())
  240. If SelIndex &gt; -1 Then
  241. If bGetCommandType Then
  242. CurCommandType = iCommandTypes(SelIndex)
  243. End If
  244. If CurCommandType = com.sun.star.sdb.CommandType.QUERY Then
  245. QueryIndex = SelIndex - Ubound(Tablenames()) - 1
  246. Tablename = QueryNames(QueryIndex)
  247. oColumns = oDBConnection.Queries.GetByName(TableName).Columns
  248. Else
  249. Tablename = Tablenames(SelIndex)
  250. oColumns = oDBConnection.Tables.GetByName(Tablename).Columns
  251. End If
  252. If GetSpecificFieldNames() &lt;&gt; -1 Then
  253. ToggleListboxControls(DialogModel, True)
  254. Exit Sub
  255. End If
  256. End If
  257. EmptyFieldsListboxes()
  258. NOFIELDS:
  259. If Err &lt;&gt; 0 Then
  260. MsgBox sMsgErrCouldNotOpenObject, 16, sMsgWizardName
  261. End If
  262. End Sub
  263. Sub PreviousStep()
  264. If Not bDebug Then
  265. On Local Error GoTo WIZARDERROR
  266. End If
  267. With DialogModel
  268. .Step = 1
  269. .cmdBack.Enabled = False
  270. .cmdGoOn.Enabled = True
  271. .lstSelFields.Tag = Not bControlsareCreated
  272. .cmdGoOn.Label = sGoOn
  273. .imgTheme.ImageUrl = FormPath &amp; &quot;FormWizard_1.png&quot;
  274. End With
  275. FormSetMoveRights()
  276. WIZARDERROR:
  277. If Err &lt;&gt; 0 Then
  278. Msgbox(sMsgErrMsg, 16, GetProductName())
  279. Resume LOCERROR
  280. LOCERROR:
  281. End If
  282. End Sub
  283. Sub NextStep()
  284. If Not bDebug Then
  285. On Local Error GoTo WIZARDERROR
  286. End If
  287. Select Case DialogModel.Step
  288. Case 1
  289. bControlsAreCreated = Not (cBool(DialogModel.lstSelFields.Tag))
  290. If Not bControlsAreCreated Then
  291. GetTableMetaData()
  292. CreateDBForm()
  293. RemoveShapes()
  294. InitializeLayoutSettings()
  295. oDBForm.Load
  296. End If
  297. DialogModel.cmdGoOn.Label = sReady
  298. DialogModel.cmdBack.Enabled = True
  299. DialogModel.Step = 2
  300. bDisposeDoc = False
  301. Case 2
  302. StoreForm()
  303. DlgFormDB.EndExecute()
  304. exit Sub
  305. End Select
  306. DialogModel.imgTheme.ImageUrl = FormPath &amp; &quot;FormWizard_&quot; &amp; DialogModel.Step &amp; &quot;.png&quot;
  307. DlgFormDB.Title = WizardTitle(DialogModel.Step)
  308. WIZARDERROR:
  309. If Err &lt;&gt; 0 Then
  310. Msgbox(sMsgErrMsg, 16, GetProductName())
  311. Resume LOCERROR
  312. LOCERROR:
  313. End If
  314. End Sub
  315. Sub InitializeLayoutSettings()
  316. SwitchArrangementButtons(cTabled)
  317. SwitchAlignMode(SBALIGNLEFT)
  318. SwitchBorderMode(SB3DBORDER)
  319. ToggleBorderGroup(bControlsAreCreated)
  320. ToggleAlignGroup(bControlsAreCreated)
  321. ArrangeControls()
  322. If OldAlignMode &lt;&gt; 0 Then
  323. DlgFormDB.GetControl(&quot;optAlign2&quot;).Model.State = 0
  324. End If
  325. End Sub
  326. Sub ToggleDatabasePage(bDoEnable as Boolean)
  327. With DialogModel
  328. .cmdBack.Enabled = False
  329. .cmdHelp.Enabled = bDoEnable
  330. .cmdGoOn.Enabled = Ubound(DialogModel.lstSelFields.StringItemList()) &lt;&gt; -1
  331. .hlnBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  332. .optIgnoreBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  333. .optBinariesasGraphics.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  334. End With
  335. End Sub
  336. &apos; This Sub is called from the Procedure &quot;StoreDocument&quot; in the &quot;Tools&quot; Library
  337. Sub CommitLastDocumentChanges(sTargetPath as String)
  338. Dim i as Integer
  339. Dim sBookmarkName as String
  340. Dim oDBBookmarks as Object
  341. Dim bLinkExists as Boolean
  342. Dim sBaseBookmarkName as String
  343. sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath))
  344. sBaseBookmarkName = sBookmarkName
  345. oDBBookmarks = oDataSource.GetBookmarks()
  346. i = 1
  347. Do
  348. bLinkExists = oDBBookmarks.HasbyName(sBookmarkName)
  349. If bLinkExists Then
  350. i = i + 1
  351. sBookmarkName = sBaseBookmarkName &amp; &quot;_&quot; &amp; i
  352. Else
  353. oDBBookmarks.insertByName(sBookmarkName, sTargetPath)
  354. End If
  355. Loop Until Not bLinkExists
  356. bDisposeDoc = False
  357. GroupShapesTogether()
  358. ToggleDesignMode(oDocument)
  359. oDBForm.Reload()
  360. End Sub
  361. Sub StoreFormInDatabase()
  362. Dim NoArgs() as new com.sun.star.beans.PropertyValue
  363. FormName = &quot;Form_&quot; &amp; sDBName &amp; &quot;_&quot; &amp; TableName &amp; &quot;.sxw&quot;
  364. sFormUrl = TempPath &amp; &quot;/&quot; &amp; FormName
  365. oDocument.StoreAsUrl(sFormUrl, NoArgs())
  366. bdisposeDoc = true
  367. DlgFormDB.Endexecute()
  368. End Sub
  369. Sub StoreForm()
  370. Dim sTargetPath as String
  371. Dim TypeNames(0,2) as String
  372. Dim oMasterKey as Object
  373. Dim oTypes() as Object
  374. oMasterKey = GetRegistryKeyContent(&quot;org.openoffice.TypeDetection.Types/&quot;)
  375. oTypes() = oMasterKey.Types
  376. TypeNames(0,0) = GetFilterName(&quot;StarOffice XML (Writer)&quot;)
  377. TypeNames(0,1) = &quot;*.sxw&quot;
  378. TypeNames(0,2) = &quot;&quot;
  379. StoreFormInDatabase()
  380. &apos; sTargetPath = StoreDocument(oDocument, TypeNames(), &quot;Form_&quot; &amp; sDBName &amp; &quot;_&quot; &amp; TableName &amp; &quot;.sxw&quot;, WorkPath, 1)
  381. End Sub
  382. Sub EmptyFieldsListboxes()
  383. Dim NullList() as String
  384. ToggleListboxControls(DialogModel, False)
  385. DialogModel.lstFields.StringItemList() = NullList()
  386. DialogModel.lstSelFields.StringItemList() = NullList()
  387. bEnableBinaryOptionGroup = False
  388. End Sub
  389. Sub DeleteFirstTableListBoxEntry()
  390. DeleteFirstListBoxEntry(&quot;lstTables&quot;, sSelectDBTable)
  391. End Sub
  392. Sub DeleteFirstListboxEntry(ListBoxName as String, DelEntryName as String)
  393. Dim oListbox as Object
  394. Dim sFirstItem as String
  395. dim iSelPos as Integer
  396. oListBox = DlgFormDB.getControl(ListBoxName)
  397. sFirstItem = oListBox.getItem(0)
  398. If sFirstItem = DelEntryName Then
  399. iSelPos = oListBox.getSelectedItemPos()
  400. oListBox.removeItems(0, 1)
  401. If iSelPos &gt; 0 Then
  402. oListBox.selectItemPos(iSelPos-1, True)
  403. End If
  404. End If
  405. End Sub
  406. </script:module>