summaryrefslogtreecommitdiff
path: root/wizards/source/formwizard/FormWizard.xba
blob: ecd888a770b3e358dcfb7a9b082cac4dd292dbe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="FormWizard" script:language="StarBasic">Option Explicit

Public DocumentName as String
Public FormPath as String
Public WizardPath as String
Public WebWizardPath as String
Public WorkPath as String
Public TexturePath as String
Public sQueryName as String
Public oDBConnection as Object
Public bWithBackGraphic as Boolean
Public bNeedFieldRefresh as Boolean
Public oDBForm as Object
Public oColumns() as Object
Public sDatabaseList()
Public TableNames() as String
Public QueryNames() as String
Public FieldNames() as String
Public ImgFieldNames() as String
Public oDBContext as Object
Public oUcb as Object
Public oDocInfo as Object
Public WidthList(15,3)
Public ImgWidthList(3,3)
Public sDBName as String
Public Tablename as String
Public Const SBSIZETEXT = &quot;The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.&quot;
Public bDisposeDoc as Boolean
Public bDebug as Boolean

&apos; The macro can be called in 4 possible scenarios:
&apos; Scenario 1. No parameters at given
&apos; Scenario 2: Only Datasourcename is given, but no connection and no Content
&apos; Scenario 3: a data source and a connection are given
&apos; Scenario 4: all parameters (data source name, connection, object type and object) are given

Sub Main()
Dim oLocDBContext as Object
Dim oLocConnection as Object

&apos; Scenario 1. No parameters at given
	MainWithDefault()

&apos; Scenario 2: Only Datasourcename is given, but no connection and no Content
&apos;	MainWithDefault(&quot;Bibliography&quot;)

&apos; Scenario 3: a data source and a connection are given
&apos;	oLocDBContext = CreateUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
&apos;	oLocConnection = oLocDBContext.GetByName(&quot;Bibliography&quot;).GetConnection(&quot;&quot;,&quot;&quot;)
&apos;	MainWithDefault(&quot;Bibliography&quot;, oLocConnection)

&apos; Scenario 4: all parameters (data source name, connection, object type and object) are given
&apos;	oLocDBContext = CreateUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
&apos;	oLocConnection = oLocDBContext.GetByName(&quot;Bibliography&quot;).GetConnection(&quot;&quot;,&quot;&quot;)
&apos;	MainWithDefault(&quot;Bibliography&quot;, oLocConnection, com.sun.star.sdb.CommandType.TABLE, &quot;biblio&quot;)
End Sub


Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String)
Dim i as Integer
Dim SelCount as Integer
Dim RetValue as Integer
Dim SelList(0) as Integer
	SelList(0) = 0
	BasicLibraries.LoadLibrary(&quot;Tools&quot;)
	BasicLibraries.LoadLibrary(&quot;WebWizard&quot;)
	bDebug = False
	If Not bDebug Then
		On Local Error GoTo WIZARDERROR
	End If
	OpenFormDocument()
	CurArrangement = 0
	bControlsareCreated = False
	bEnableBinaryOptionGroup = False
	bDisposeDoc = True
	MaxIndex = -1
	If Not InitResources(&quot;Formwizard&quot;,&quot;dbw&quot;) Then
		Exit Sub
	End If
	oDBContext = CreateUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
	oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
	FormPath = GetOfficeSubPath(&quot;Template&quot;,&quot;wizard/bitmap&quot;)
	WebWizardPath = GetOfficeSubPath(&quot;Template&quot;,&quot;wizard/web&quot;)
	WizardPath = GetOfficeSubPath(&quot;Template&quot;,&quot;wizard/&quot;)
	TexturePath = GetOfficeSubPath(&quot;Gallery&quot;, &quot;www-back/&quot;)
	WorkPath = GetPathSettings(&quot;Work&quot;)
	oDocument.GetCurrentController().Frame.ComponentWindow.Enable = False
	oProgressBar.Value = 5
	GetDatabaseNames()
	oProgressBar.Value = 10
	InitializeWidthList()
	oProgressBar.Value = 20
	LoadLanguage()
	oProgressBar.Value = 30
	Styles() = getListBoxArrays(oUcb, &quot;/stl&quot;)
	CurIndex = GetCurIndex(DialogModel, Styles(), 2)
	oProgressBar.Value = 40
	ConfigurePageStyle()
	oProgressBar.Value = 50
	InitializeLabelValues()
	bNeedFieldRefresh = True
	SetDialogLanguage()
	With DialogModel
		.cmdBack.Enabled = False
		.cmdGoOn.Enabled = False
		.lblTables.Enabled = False
		.lstSelFields.Tag = False
		.Step = 1
		.lstDatabases.StringItemList()= sDatabaseList()&apos; = AddItem(sDatabaseList(i)
	End With
	oProgressBar.Value = 60
	If Not IsMissing(DataSourceName) Then
		sDBName = DataSourceName
		DlgFormDB.GetControl(&quot;lstDatabases&quot;).SelectItem(DataSourceName, True)
		If Not IsMissing(oConnection) Then
			&apos; Scenario 3: a data source and a connection are given
			Set oDBConnection = oConnection
			oDataSource = oDBContext.GetByName(DataSourceName)
			DialogModel.lstTables.Enabled = True
			DialogModel.lblTables.Enabled = True
			If GetDBMetaData() Then
				DialogModel.lstTables.StringItemList() = AddListToList(TableNames(), QueryNames())
				iCommandTypes = CreateCommandTypeList()
				If Not IsMissing(sContent) Then
					&apos; Scenario 4: all parameters (data source name, connection, object type and object) are given
					iCommandTypes() = CreateCommandTypeList()
					SelCount = CountItemsInArray(DialogModel.lstTables.StringItemList(), sContent)
					If SelCount = 1 Then
						DlgFormDB.GetControl(&quot;lstTables&quot;).SelectItem(sContent, True)
					Else
						If CommandType = com.sun.star.sdb.CommandType.QUERY Then
							SelIndex = IndexInArray(sContent, QueryNames()
							DlgFormDB.GetControl(&quot;lstTables&quot;).SelectItemPos(SelIndex, True)
						ElseIf CommandType = com.sun.star.sdb.CommandType.TABLE Then
							SelIndex = IndexInArray(sContent, TableNames()
							DlgFormDB.GetControl(&quot;lstTables&quot;).SelectItemPos(Ubound(QueryNames()+1 + SelIndex, True)		
						End If
					End If
					CurCommandType = CommandType
					FillUpFieldsListbox(False)
				End If
			End If
		Else
			&apos; Scenario 2: Only Datasourcename is given, but no connection and no Content
			GetSelectedDBMetaData()
		End If
	Else
		&apos; Scenario 1: No parameters are given
		ToggleListboxControls(DialogModel, False)
	End If
	oProgressBar.Value = 80
	bWithBackGraphic = LoadNewStyles(oDocument, DialogModel, CurIndex, Styles(CurIndex, 8), Styles(), TexturePath)
	DlgFormDB.Title = WizardTitle(1)
	DialogModel.lstStyles.StringItemList() = ArrayfromMultiArray(Styles, 1)
	DialogModel.lstStyles.SelectedItems() = SelList()
	ControlCaptionsToStandardLayout()
	
	oDocument.GetCurrentController().Frame.ComponentWindow.Enable = True
	oProgressBar.Value = 90
	DialogModel.imgTheme.ImageURL = FormPath &amp; &quot;FormWizard_1.bmp&quot;
	ToggleDatabasePage(True)
	oProgressBar.Value = 100
	DlgFormDB.GetControl(&quot;lstDatabases&quot;).SetFocus()
	oProgressbar.End
	RetValue = DlgFormDB.Execute()
	DlgFormDB.Dispose()
	If Not IsNull(oDBConnection) Then
		oDBConnection.Dispose()
	End If
	If bDisposeDoc Then
		oDocument.Dispose()
	ElseIf RetValue = 0 Then
		RemoveNirwanaShapes()
	End If
WIZARDERROR:
	If Err &lt;&gt; 0 Then	
		Msgbox(sMsgErrMsg, 16, GetProductName())
		Resume LOCERROR
		LOCERROR:		
	End If
End Sub


Sub FormGetFields()
Dim i as Integer
	ToggleDatabasePage(False)
	FillUpFieldsListbox(True)
	ToggleDatabasePage(True)
End Sub


Sub FillUpFieldsListbox(bGetCommandType as Boolean)
Dim n as Integer
Dim SelIndex as Integer
Dim QueryIndex as Integer
	If Not bDebug Then
		On Local Error GoTo NOFIELDS
	End If
	n = Ubound(DialogModel.lstTables.SelectedItems())
	If n &gt; -1 Then
		SelIndex = DialogModel.lstTables.SelectedItems(0)
		If bGetCommandType Then
			CurCommandType = iCommandTypes(SelIndex)
		End If
		If CurCommandType = com.sun.star.sdb.CommandType.QUERY Then
			QueryIndex = SelIndex - Ubound(Tablenames()) - 1
			Tablename = QueryNames(QueryIndex)
			oColumns = oDBConnection.Queries.GetByName(TableName).Columns
		Else
			Tablename = Tablenames(SelIndex)
			oColumns = oDBConnection.Tables.GetByName(Tablename).Columns
		End If
		If GetSpecificFieldNames() &lt;&gt; -1 Then
			ToggleListboxControls(DialogModel, True)			
		Else
			EmptyFieldsListboxes()
		End If
	Else
		EmptyFieldsListboxes()
	End If
NOFIELDS:
	If Err &lt;&gt; 0 Then
		MsgBox sMsgErrCouldNotOpenObject, 16, sMsgWizardName
	End If
End Sub


Sub PreviousStep()
	If Not bDebug Then
		On Local Error GoTo WIZARDERROR
	End If
	With DialogModel
		.Step = 1
		.cmdBack.Enabled = False
		.cmdGoOn.Enabled = True
		.lstSelFields.Tag = Not bControlsareCreated
		.cmdGoOn.Label = sGoOn
		.imgTheme.ImageUrl = FormPath &amp; &quot;FormWizard_1.bmp&quot;
	End With
	FormSetMoveRights()
WIZARDERROR:
	If Err &lt;&gt; 0 Then	
		Msgbox(sMsgErrMsg, 16, GetProductName())
		Resume LOCERROR
		LOCERROR:		
	End If
End Sub


Sub NextStep()
	If Not bDebug Then
		On Local Error GoTo WIZARDERROR
	End If
	Select Case DialogModel.Step
		Case 1
			bControlsAreCreated = Not (cBool(DialogModel.lstSelFields.Tag))
			If Not bControlsAreCreated Then
				GetTableMetaData()
				CreateDBForm()
				RemoveShapes()
				InitializeLayoutSettings()
				oDBForm.Load
			End If
			DialogModel.cmdGoOn.Label = sReady
			DialogModel.cmdBack.Enabled = True
			DialogModel.Step = 2
			bDisposeDoc = False
		Case 2
			StoreForm()
	End Select
	DialogModel.imgTheme.ImageUrl = FormPath &amp; &quot;FormWizard_&quot; &amp; DialogModel.Step &amp; &quot;.bmp&quot;
	DlgFormDB.Title = WizardTitle(DialogModel.Step)
WIZARDERROR:
	If Err &lt;&gt; 0 Then
		Msgbox(sMsgErrMsg, 16, GetProductName())
		Resume LOCERROR
		LOCERROR:		
	End If
End Sub


Sub InitializeLayoutSettings()
	SwitchArrangementButtons(cTabled)
	SwitchAlignMode(SBALIGNLEFT)
	SwitchBorderMode(SB3DBORDER)
	ToggleBorderGroup(bControlsAreCreated)
	ToggleAlignGroup(bControlsAreCreated)
	ArrangeControls()
	If OldAlignMode &lt;&gt; 0 Then
		DlgFormDB.GetControl(&quot;optAlign2&quot;).Model.State = 0
	End If
End Sub


Sub ToggleDatabasePage(bDoEnable as Boolean)
	With DialogModel
		.cmdBack.Enabled = False
		.cmdHelp.Enabled = bDoEnable
		.cmdGoOn.Enabled = Ubound(DialogModel.lstSelFields.StringItemList()) &lt;&gt; -1
		.hlnBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
		.optIgnoreBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
		.optBinariesasGraphics.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
	End With
End Sub


&apos; This Sub is called from the Procedure &quot;StoreDocument&quot; in the &quot;Tools&quot; Library
Sub CommitLastDocumentChanges(sTargetPath as String)
Dim i as Integer	
Dim sBookmarkName as String
Dim oDBBookmarks as Object		
Dim bLinkExists as Boolean
Dim sBaseBookmarkName as String
	sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath))
	sBaseBookmarkName = sBookmarkName
	oDBBookmarks = oDataSource.GetBookmarks()
	i = 1
	Do
		bLinkExists = oDBBookmarks.HasbyName(sBookmarkName)
		If bLinkExists Then
			i = i + 1
			sBookmarkName = sBaseBookmarkName &amp; &quot;_&quot; &amp; i
		Else
			oDBBookmarks.insertByName(sBookmarkName, sTargetPath)
		End If
	Loop Until Not bLinkExists
	bDisposeDoc = False
	GroupShapesTogether()
	ToggleDesignMode(oDocument)
	oDBForm.Reload()
End Sub


Sub StoreForm()
Dim sTargetPath as String
Dim TypeNames(0,2) as String
Dim oMasterKey as Object
Dim oTypes() as Object
	oMasterKey = GetRegistryKeyContent(&quot;org.openoffice.Office.TypeDetection/&quot;)
	oTypes() = oMasterKey.Types
	TypeNames(0,0) = oTypes.GetByName(&quot;writer_StarOffice_XML_Writer&quot;).UIName
	TypeNames(0,1) = &quot;*.sxw&quot;
	TypeNames(0,2) = &quot;&quot;
	sTargetPath = StoreDocument(oDocument, TypeNames(), &quot;Form_&quot; &amp; sDBName &amp; &quot;_&quot; &amp; TableName &amp; &quot;.sxw&quot;, WorkPath, 1)
	If sTargetPath &lt;&gt; &quot;&quot; Then
		DlgFormDB.EndExecute()
	End If
End Sub


Sub EmptyFieldsListboxes()
Dim NullList() as String
	ToggleListboxControls(DialogModel, False)
	DialogModel.lstFields.StringItemList() = NullList()
	DialogModel.lstSelFields.StringItemList() = NullList()
	bEnableBinaryOptionGroup = False
End Sub
</script:module>