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