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