update dev300-m58
[ooovba.git] / wizards / source / formwizard / FormWizard.xba
blob99103d9335bc0de7fca693016abaddd28323e7f2
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
5 Public DocumentName as String
6 Public FormPath as String
7 Public WizardPath as String
8 Public WebWizardPath as String
9 Public WorkPath as String
10 Public TempPath as String
11 Public TexturePath as String
12 Public sQueryName as String
13 Public oDBConnection as Object
14 Public bWithBackGraphic as Boolean
15 Public bNeedFieldRefresh as Boolean
16 Public oDBForm as Object
17 Public oColumns() as Object
18 Public sDatabaseList() as String
19 Public TableNames() as String
20 Public QueryNames() as String
21 Public FieldNames() as String
22 Public ImgFieldNames() as String
23 Public oDBContext as Object
24 Public oUcb as Object
25 Public oDocInfo as Object
26 Public WidthList(15,3)
27 Public ImgWidthList(3,3)
28 Public sDBName as String
29 Public Tablename as String
30 Public Const SBSIZETEXT = &quot;The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.&quot;
31 Public bDisposeDoc as Boolean
32 Public bDebug as Boolean
33 &apos;Public bStartUp as Boolean
34 Public bConnectionIsovergiven as Boolean
35 Public FormName As String
36 Public sFormUrl as String
37 Public oFormDocuments
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
46 Sub Main()
47 Dim oLocDBContext as Object
48 Dim oLocConnection as Object
50 &apos; Scenario 1. No parameters at given
51 MainWithDefault()
53 &apos; Scenario 2: Only Datasourcename is given, but no connection and no Content
54 &apos; MainWithDefault(&quot;Bibliography&quot;)
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)
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;)
65 End Sub
68 Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String)
69 Dim i as Integer
70 Dim SelCount as Integer
71 Dim RetValue as Integer
72 Dim SelList(0) as Integer
73 Dim 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)
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
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
211 WIZARDERROR:
212 If Err &lt;&gt; 0 Then
213 Msgbox(sMsgErrMsg, 16, GetProductName())
214 Resume LOCERROR
215 LOCERROR:
216 End If
217 End Sub
220 Sub FormGetFields()
221 Dim 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)
230 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
265 Sub 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()
278 WIZARDERROR:
279 If Err &lt;&gt; 0 Then
280 Msgbox(sMsgErrMsg, 16, GetProductName())
281 Resume LOCERROR
282 LOCERROR:
283 End If
284 End Sub
287 Sub 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)
312 WIZARDERROR:
313 If Err &lt;&gt; 0 Then
314 Msgbox(sMsgErrMsg, 16, GetProductName())
315 Resume LOCERROR
316 LOCERROR:
317 End If
318 End Sub
321 Sub 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
331 End Sub
334 Sub 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
343 End Sub
346 &apos; This Sub is called from the Procedure &quot;StoreDocument&quot; in the &quot;Tools&quot; Library
347 Sub CommitLastDocumentChanges(sTargetPath as String)
348 Dim i as Integer
349 Dim sBookmarkName as String
350 Dim oDBBookmarks as Object
351 Dim bLinkExists as Boolean
352 Dim sBaseBookmarkName as String
353 sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath))
354 sBaseBookmarkName = sBookmarkName
355 oDBBookmarks = oDataSource.GetBookmarks()
356 i = 1
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()
370 End Sub
373 Sub 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()
380 End Sub
384 Sub StoreForm()
385 Dim sTargetPath as String
386 Dim TypeNames(0,2) as String
387 Dim oMasterKey as Object
388 Dim 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)
396 End Sub
400 Sub EmptyFieldsListboxes()
401 Dim NullList() as String
402 ToggleListboxControls(DialogModel, False)
403 DialogModel.lstFields.StringItemList() = NullList()
404 DialogModel.lstSelFields.StringItemList() = NullList()
405 bEnableBinaryOptionGroup = False
406 End Sub
409 Sub DeleteFirstTableListBoxEntry()
410 DeleteFirstListBoxEntry(&quot;lstTables&quot;, sSelectDBTable)
411 End Sub
413 Sub DeleteFirstListboxEntry(ListBoxName as String, DelEntryName as String)
414 Dim oListbox as Object
415 Dim sFirstItem as String
416 dim 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
426 End Sub
427 </script:module>