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